Skip to content
New issue

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

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

Already on GitHub? # to your account

Support UTF-8 encoding #1

Closed
Nhajela opened this issue May 14, 2024 · 1 comment
Closed

Support UTF-8 encoding #1

Nhajela opened this issue May 14, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@Nhajela
Copy link

Nhajela commented May 14, 2024

Hi - I was getting some unicode decode error trying to run this, and asked gpt for help. it fixed the code and gave me this to share with you here.

Here's the description for the GitHub issue:


Issue: UnicodeEncodeError When Writing Files

Description

When attempting to write files using the write_file function, a UnicodeEncodeError occurs if the text contains characters not supported by the default encoding (cp1252). This error is specifically encountered with the character \u2028.

Error Message

Traceback (most recent call last):
  File "C:\Users\naman\SandBox\1. Projects\3. Software\external-tools\tkforge\tkforge.py", line 63, in <module>
    main()
  File "C:\Users\naman\SandBox\1. Projects\3. Software\external-tools\tkforge\tkforge.py", line 55, in main
    code = tk_code(extract_figma_id(file_id), token, output)
  File "C:\Users\naman\SandBox\1. Projects\3. Software\external-tools\tkforge\tk.py", line 271, in tk_code
    write_file(template, out)
  File "C:\Users\naman\SandBox\1. Projects\3. Software\external-tools\tkforge\core.py", line 19, in write_file
    file.write(text)
  File "C:\Users\naman\AppData\Local\Programs\Python\Python310\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2028' in position 884: character maps to <undefined>

Steps to Reproduce

  1. Use the write_file function to write text that includes characters outside the cp1252 encoding range.
  2. Run the script and observe the error.

Proposed Solution

Modify the write_file function to open the file with UTF-8 encoding, which supports a wider range of characters. This can be achieved by specifying the encoding parameter when opening the file.

Code Changes

In the write_file function, update the open call as follows:

def write_file(text, out=None, frame=None):
    if out is None:
        folder_path = 'TkForge'
    else:
        folder_path = os.path.join(out, 'TkForge')

    if not os.path.exists(folder_path):
        os.makedirs(folder_path)
    
    if frame is not None:
        file_name = f'frame_{frame}.py'
    else:
        file_name = 'main.py'

    with open(os.path.join(folder_path, file_name), 'w', encoding='utf-8') as file:  # Added encoding='utf-8'
        file.write(text)

This change ensures that the file is opened with UTF-8 encoding, preventing UnicodeEncodeError for characters not supported by cp1252.

References


You can post this description as an issue on the relevant GitHub repository to help the maintainer address the problem.

@Axorax
Copy link
Owner

Axorax commented May 17, 2024

Hey, thanks for bringing this up. I will make sure to fix this. Due to exams, I couldn't see this issue earlier. I will fix it as soon as I can.

@Axorax Axorax self-assigned this May 17, 2024
@Axorax Axorax added the enhancement New feature or request label May 17, 2024
@Axorax Axorax closed this as completed May 17, 2024
@Axorax Axorax changed the title UTF-8 on file.write Support UTF-8 encoding May 17, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants