8000 Make file_type to recognize more python files by iefdev · Pull Request #3221 · thonny/thonny · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Make file_type to recognize more python files #3221

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

iefdev
Copy link
@iefdev iefdev commented Jun 16, 2024

The file_type is just based on file extension, and maybe not by
its actual file type. This will also allow backup files, as in:
*.py.bak, *.py.orig, etc. It will also check the first line for the env in the “shebang”.

  • get_dots: get the number of dots to use in ext (ie. foobar.py.orig will also pick up py)
  • is_python_file: returns True if python is found

Closes #3220

@iefdev
Copy link
Author
iefdev commented Jun 16, 2024

So, I managed to find a good solution to my issue (#3220) . First I tried a different approach, with a second var: PYTHON_FILES_PATTERN = set() and matching a few expressions. But, I think this is better and a more generic solution.

Hope you'll like the changes. 👍

Best regards,
· Eric

Copy link
Member
@aivarannamaa aivarannamaa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

Please check my comments.

ext = self._filename.split(".")[-get_dots].lower()

# Extract the 1st line to get the env
with open(self._filename, 'r') as sheba 8000 ng:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should consult the editor content, not the file, as the file may be located in another machine.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed all the other things, but this one… Well, there's been a lot of trial'n'error. :) I don't now, but I think I'm on to it. Just can't get the output/content. If you could provide some help/tips, I would really appreciate that.

So, after trying a lot of different things - printing out a lot to terminal. With self.notebook.get_editor(self.get_filename(), True) I can get the editor, and I can get it to print out the filename, when I open a file.

e = self.notebook.get_editor(self.get_filename(), True)

# e have: _name, _filename and _code_view
e._name                 # !editor2  
e._filename             # /path/2/my/file.py
e._code_view.text       # .!codeview2.!editorcodeviewtext

e._code_view.text have get, so I thought just grabbing the first line would be great, but it's empty.

e._code_view.text.get('1.0', '1.end')    # empty
e._code_view.get_content()               # empty

Ideally, one would just want to use something like: .text.get('1.0', '1.end').find('python'). But, I'm not too familiar with tk, so I feel like I'm a bit in the dark now - trying and guessing.

Any ideas? Or maybe I'm looking at the wrong place/function(s)?


# Extract the 1st line to get the env
with open(self._filename, 'r') as shebang:
is_python_file = bool('python' in shebang.__next__().strip())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bool is not required, as in operator already returns a bool.

I suggest has_python_shebang as the variable name, because the ext in PYTHON_EXTENSIONS or is_python_file part sounds like "an animal is zebra if it has black and white stripes or it is zebra" ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also consider this file named "my-favorite-programs":

python
cat
less
ssh

Would you want to color it as Python code?

if ext in PYTHON_EXTENSIONS:
# Include backup files: *.py.bak, *.py.orig, etc
get_dots = len(re.findall(r'\.', self._filename))
ext = self._filename.split(".")[-get_dots].lower()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the filename is "blah.blah.py", the extension would be "blah"? This doesn't look right.

@iefdev
Cop 8000 y link
Author
iefdev commented Jun 17, 2024

Thanks for the comments/review. 👍 All good valid points. I'll work through these changes in the coming days. I might have to get back to you on the “editor content”, if I don't fig that out. :) I used self._filename since it was what in use within the class.

· Eric

iefdev and others added 2 commits June 19, 2024 14:13
The `file_type` is just based on file extension, and maybe not by its
actual file type. This will also allow backup files, as in: `*.py.bak,
*.py.orig, etc`, by checking the last 2 ext's.

It will also look for python in the “shebang”. This is done when loading
a file, and when saving a file - and will update accordingly.

- `get_dots`: get the number of dots to use in `ext`
  (ie. foobar.py.orig will also pick up 'py')
- `_has_env_in_shebang()`, returns a bool if env is found
- `has_python_file`:  returns True if `python` is found

Closes thonny#3220
@iefdev
Copy link
Author
iefdev commented Jun 19, 2024

So, I managed to find a way to get the shebang. Tried to think of the naming, so the _has_env_in_shebang() is kindof generic, incase you decides to add more syntax later on, it would be like: _has_env_in_shebang('bash').

Also added an xtra commit, for some redundant code I saw (ie. 2 same if's in a row).

@aivarannamaa aivarannamaa added this to the 5.0.0 milestone Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Highlightning not only on file extension
2 participants
0