-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: master
Are you sure you want to change the base?
Conversation
So, I managed to find a good solution to my issue (#3220) . First I tried a different approach, with a second var: Hope you'll like the changes. 👍 Best regards, |
There was a problem hiding this 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.
thonny/editors.py
Outdated
ext = self._filename.split(".")[-get_dots].lower() | ||
|
||
# Extract the 1st line to get the env | ||
with open(self._filename, 'r') as sheba 8000 ng: |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)?
thonny/editors.py
Outdated
|
||
# Extract the 1st line to get the env | ||
with open(self._filename, 'r') as shebang: | ||
is_python_file = bool('python' in shebang.__next__().strip()) |
There was a problem hiding this comment.
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" ;)
There was a problem hiding this comment.
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?
thonny/editors.py
Outdated
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() |
There was a problem hiding this comment.
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.
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 · Eric |
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
So, I managed to find a way to get the shebang. Tried to think of the naming, so the Also added an xtra commit, for some redundant code I saw (ie. 2 same if's in a row). |
The
file_type
is just based on file extension, and maybe not byits 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 inext
(ie.foobar.py.orig
will also pick uppy
)is_python_file
: returns True ifpython
is foundCloses #3220