Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX for codeblock break out and filename #107

Open
Wade-BuildOtto opened this issue Jun 19, 2023 · 1 comment
Open

FIX for codeblock break out and filename #107

Wade-BuildOtto opened this issue Jun 19, 2023 · 1 comment

Comments

@Wade-BuildOtto
Copy link

Wade-BuildOtto commented Jun 19, 2023

The codeblock is not broken out correctly this is the fix I added

`
import re
def write_file(filename, filecode, directory):
# Output the filename in blue color
print("\033[94m" + filename + "\033[0m")
print(filecode)

regex = r"```(.*?)```"
matches = re.finditer(regex, filecode, re.DOTALL)
code = filecode

if matches:
    for match in matches:
        # Get the code
        code = match.group(1).split("\n")[1:]
        code = "\n".join(code)
        break

file_path = os.path.join(directory, filename)

# Check if the filename is actually a directory
if os.path.isdir(file_path):
    print(f"Error: {filename} is a directory, not a file.")
    return

# Open the file in write mode
with open(file_path, "w") as file:
    # Write content to the file
    file.write(code)

print("File written successfully.")

`

also lines 127 & 128
by adding that the list will be evaluated by ast it knows better how to deliver the list, "found on another issue"
otherwise the list starts with "- " dash space
only list the filepaths you would write, and return them as a python list of strings to be evaluated by the ast.literal_eval function. This is important: do not add any other explanation or notes, only return a python list of strings.

adding these 2 fixed make the code fox

@Wade-BuildOtto
Copy link
Author

given that some projects need to create a folder structure

def write_file(filename, filecode, directory):
filepath = os.path.dirname(filename)
filename = os.path.basename(filename)
directory = os.path.join(directory, filepath)
# Output the filename in blue color
print("\033[94m" + filename + "\033[0m")
print("\033[94m" + directory + "\033[0m")
print(filecode)

regex = r"```(.*?)```"
matches = re.finditer(regex, filecode, re.DOTALL)
code = filecode

if matches:
    for match in matches:
        # Get the code
        code = match.group(1).split("\n")[1:]
        code = "\n".join(code)
        break

file_path = os.path.join(directory, filename)

# Create directory if it does not exist
if not os.path.exists(directory):
    os.makedirs(directory)

# Check if the filename is actually a directory
if os.path.isdir(file_path):
    print(f"Error: {filename} is a directory, not a file.")
    return

# Open the file in write mode
with open(file_path, "w") as file:
    # Write content to the file
    file.write(code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant