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

PR: Change the way %(date)s and %(username)s are dealt with in the template.py #6380

Closed

Conversation

chengtian5huang
Copy link

@chengtian5huang chengtian5huang commented Feb 4, 2018

Fixes #6379

Take the part between tripe-double-quotes separately and deal with it separately we can replace %(date)s properly no matter what a man add in template.py

take the part between tripe-double-quotes seperately and deal with it seperately we can replace %(date)s properly no matter what a man add in template.py
@pep8speaks
Copy link

pep8speaks commented Feb 4, 2018

Hello @chengtian5huang! Thanks for updating the PR.

Line 1863:1: W293 blank line contains whitespace
Line 1869:13: E722 do not use bare except'
Line 1903:29: E231 missing whitespace after ','

Line 6:1: E302 expected 2 blank lines, found 0
Line 43:80: E501 line too long (87 > 79 characters)
Line 64:80: E501 line too long (87 > 79 characters)
Line 158:80: E501 line too long (87 > 79 characters)
Line 174:80: E501 line too long (86 > 79 characters)
Line 185:80: E501 line too long (87 > 79 characters)
Line 199:1: E305 expected 2 blank lines after class or function definition, found 0

Comment last updated on February 10, 2018 at 03:19 Hours UTC

@jnsebgosselin jnsebgosselin changed the title see issue #6379 Change the way %(date)s and %(username)s are dealt with in the template.py Feb 4, 2018
@ccordoba12
Copy link
Member

Thanks for your contribution. There can be several triple quote sections in a template. How does your code deal with that?

@chengtian5huang
Copy link
Author

improved my code now, it can handle all cases i hope.

to anwser ccordoba12's question:

if there are several triple quote sections in a template, it will check and check only(leave along other part of template) every section to replace vaild placeholder in the pre-set VARS.

and i have to change the name of these placeholders(eg change "date" to "date-spyder") in order to avoide over-replacing.

i describe in detail how it work under all cases as following:(skip this if u r not interested)

following "invaild"s means spyder DONT know how to replace it.to be specific:it is NOT in the VARS dict.
following "vaild"s means spyder know how to replace it.to be specific:it is in the VARS dict.
default

this problem can be divided into following cases

  • there is no invaild placeholder in the template.

replace them all, of course.

  • there are invaild placeholder in the template but all of them are in the outside of triple quote sections(no matter how many)

ignore these placeholders, they will remain the same

  • there are invaild placeholder in the template but all of them are in triple quote sections(no matter how many)

the triple quote sections which contain and contain only vaild placeholder will be dealt properly. all placeholders in them will be replaced.
the triple quote sections which contain invaild placeholder will be ignore. any of these placeholders wont be replaced.

  • there are invaild placeholder in the template but some of them are in triple quote sections(no matter how many), some of them are in the outside of triple quote sections(no matter how many).

only triple quote sections which contain and contain only vaild placeholders will be replaced.
the placeholder in the outside of triple quote sections wont be replaced.
all triple quote sections contain any invaild placeholders will be remain the same.

Copy link
Member

@CAM-Gerlach CAM-Gerlach left a comment

Choose a reason for hiding this comment

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

Aside from the minor notes to consider, any way you can add some simple tests for _replace_triple_quote, and maybe new(), to cover the scenarios you've laid out?

@@ -1860,21 +1860,30 @@ def _clone_file_everywhere(self, finfo):
for editorstack in self.editorstacks[1:]:
editor = editorstack.clone_editor_from(finfo, set_current=False)
self.register_widget_shortcuts(editor)

Copy link
Member

Choose a reason for hiding this comment

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

Delete extra blank spaces

@@ -1860,21 +1860,30 @@ def _clone_file_everywhere(self, finfo):
for editorstack in self.editorstacks[1:]:
editor = editorstack.clone_editor_from(finfo, set_current=False)
self.register_widget_shortcuts(editor)

def _replace_tripe_quote(self, m, v=None):
Copy link
Member

Choose a reason for hiding this comment

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

Maybe use slightly more descriptive parameter names than just single characters?

from functools import partial
replace = partial(self._replace_tripe_quote, v=VARS)
pat = '"""(?:(?!""").|[\n\r])*"""'
text = re.sub(pat,
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps a matter of taste, but any reason this can't all go in one line? It would only be 49 chars long...

@chengtian5huang
Copy link
Author

chengtian5huang commented Feb 9, 2018

@CAM-Gerlach
i put whole thing into new().
i dont know what do u mean by "simple tests". i cant use simple tests to deal with these.
only when

text = text % VARS

raise an exception i am able to know there is user-specified(which is invaild to spyder) placeholder.
the simpler way i can see is go down templete.py line by line.

    try:
        text = text % VARS
    except (KeyError,TypeError) as err:
        text = text.split('\n') # if Macintosh, use \r
        for i,line in enumerate(text):
            try:
                text[i] = line % VARS
            except:
                pass
        text = '\n'.join(text)

Copy link
Member

@CAM-Gerlach CAM-Gerlach left a comment

Choose a reason for hiding this comment

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

Sorry, but please revert your last commit, as it seems to have little to nothing to do with what I suggested while introducing two major regressions. It removes all whitespace in the entire file, (rather than just the one line you introduced), and nowhere did I say anything about moving your helper function to new(), its better design to keep it separate. Honestly, I'd just git reset --hard HEAD~ then git push -f and start again from there.

@CAM-Gerlach
Copy link
Member

Sorry, but I think we've had a major misunderstanding here. Please see my review comment for more on that specifically.

i put whole thing into new().

Umm, what? Where exactly did I ever suggest you do this? It is better design to keep it separate as a helper function as you had it originally, so I have no idea what you thought it should be changed.

i dont know what do u mean by "simple tests". i cant use simple tests to deal with these.

I mean unit tests for your code, not anything to do with the program logic you've already written. As you seemed to have an understanding of various cases and how it should handle them, I figured it wouldn't be too difficult to write a few of them into proper tests. At the very least, testing your helper function should be easy to do. If you don't know what unit tests are, I suggest you at least make yourself familiar with them as they are a huge part of programming nowadays; pytest makes them very easy to write without a lot of extra baggage that other frameworks have.

only when text = text % VARS raise an exception i am able to know there is user-specified(which is invaild to spyder) placeholder. the simpler way i can see is go down templete.py line by line.

Not 100% sure what you're proposing nor why, but it seems clear it has nothing to do with what I requested, which were only minor changes and adding tests that had nothing to do with the core logic.

Copy link
Member

@CAM-Gerlach CAM-Gerlach left a comment

Choose a reason for hiding this comment

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

Thanks, although it would probably just make more sense to just git reset to before you made that first bad commit and force push that since the last two commits (that commit and the revert) are both consecutive and rather pointless, cluttering the commit history.

@CAM-Gerlach CAM-Gerlach dismissed their stale review February 9, 2018 05:40

Changes reverted, though as mentioned it would really be better IMO to just reset to before the two pointless commits as they are essentially just clutter.

Copy link
Member

@CAM-Gerlach CAM-Gerlach left a comment

Choose a reason for hiding this comment

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

I honestly have no idea what's going on right now. Now you've reverted your actual good changes too plus added even more pointless revert commits? I don't mean to sound rude, but quite frankly that's exactly the opposite of what I just asked you to do.

Please, just type the following two commands into your git console:

git reset --hard 3206df3
git push -f

which will mercifully undo this mess.

@chengtian5huang
Copy link
Author

truly sorry for inconvenience.
i will commit one with unit tests added later.

@CAM-Gerlach CAM-Gerlach dismissed their stale review February 9, 2018 06:32

reset as requested

@CAM-Gerlach
Copy link
Member

i will commit one with unit tests added later.

Thanks

if there are invaild placeholders in template.py, check line by line to replace vaild placeholders and ignore the invailds.
@CAM-Gerlach CAM-Gerlach changed the title Change the way %(date)s and %(username)s are dealt with in the template.py PR: Change the way %(date)s and %(username)s are dealt with in the template.py Feb 10, 2018
@CAM-Gerlach CAM-Gerlach added this to the v3.2.x milestone Mar 11, 2018
Copy link
Member

@CAM-Gerlach CAM-Gerlach left a comment

Choose a reason for hiding this comment

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

You have some PEP8 issues you still need to fix; please see the PEP8Speaks comment. Also, you need to rebase it to incorporate recent changes.

@CAM-Gerlach
Copy link
Member

@chengtian5huang Hey, any updates?

@ccordoba12 ccordoba12 removed this from the v3.x milestone Apr 23, 2018
@ccordoba12
Copy link
Member

I'm closing this one. I don't understand very well what it tries to fix or improve, sorry.

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

Successfully merging this pull request may close these issues.

4 participants