spyder icon indicating copy to clipboard operation
spyder copied to clipboard

Use `string.Template` when creating a new file

Open sphh opened this issue 1 year ago • 4 comments

Description of Changes

  • [ ] Wrote at least one-line docstrings (for any new functions)
  • [ ] Added unit test(s) covering the changes (if testable)
  • [ ] Included a screenshot or animation (if affecting the UI, see Licecap)

Spyder uses a file template when opening a new file. In this template known tokens are replaced by values. Currently only two tokens are recognized: %date and %username. The syntax %<name> also interferes with more elaborated templates (e.g. #21058).

This PR now changes the simple text % VARS replacement with the string.Template functionality built into Python. This now uses $date (or ${date}) instead and the dollar signs can be escaped. This should help in writing more elaborated customary templates.

Also more template fields are made available with this PR, mainly related to the current time. But also the full name of the user is made available under $fullname. These file related variables are also available: $file, $filename, $filepath and also these project related variables: $projectname, $projectpath.

Finally $|$ can be used to mark the position of the cursor in the new file!

It also adds a Copyright ${year} ${fullname} line to the default template (and the @file and @project key-value pairs) and uses the datetime.datetime module instead of time (because that is imported anyway for newer versions of this file).

Caveats

It breaks the template syntax, because it uses $ instead of % to mark variables in the template. On the other hand it resolves the problem due to using %.

The update is easy: Replace all %<name> with $<name> in your template.

Note: When backporting this PR to Spyder 5, please add the following import statement from datetime import datetime and remove import time from the file.

Issue(s) Resolved

Fixes #21058 Fixes #11426 Fixes #6379 Fixes #2884 (That issue consists actually of two issues: One regarding the template itself – that is fixed – and one regarding its documentation and move the edition to the settings dialog – that is not fixed)

Closes #14579

Question

The current default template includes a # -*- coding: utf-8 -*- line. Is this still necessary? Python already uses utf-8 throughout …

Affirmation

By submitting this Pull Request or typing my (user)name below, I affirm the Developer Certificate of Origin with respect to all commits and content included in this PR, and understand I am releasing the same under Spyder's MIT (Expat) license.

I certify the above statement is true and correct: sphh

sphh avatar Jan 14 '24 16:01 sphh

I have update my original description with the new features of my last commits. I hope that's ok.

@ccordoba12 @CAM-Gerlach:

  • Please feel free to modify the default template to your liking (or tell me, what you prefer, so that I can include that in a commit)!
  • Please also tell me, if you would prefer different names for the template variables.
  • For some variables ($fullname, $projectname and $projectpath) I use "n/a", if those variable are empty. That's debatable if this is a good choice. Should it be translated? Please advise and I am happy to modify it.
  • Finally, if you like more variables to be included, please feel free to tell me and I will look what I can do.

sphh avatar Jan 14 '24 21:01 sphh

The failing tests puzzle me. If I understand the failed test test_go_to_prev_next_cursor_position correctly, the movement of the cursor in the third file should not result in this file being appended to the history.

My guess is, that test testing the last entry of the history stack ([-1]) fails. This test should test that the third file file3.py is not added to the history (and the newly created untitled5.py is still the last entry in the history).

Is this correct?

If yes, what could I do to pass the tests, because I believe the error is outside of my PR? If not, what have I missed?

Thank you so much for your help!

sphh avatar Jan 15 '24 14:01 sphh

@CAM-Gerlach I marked a couple of proposed changes with Add to batch and then committed those. Strangely I got an error message. I cannot find the commit and I cannot add those changes again to the batch. Could you please check, if those changes were actually committed? Thanks.

sphh avatar Mar 11 '24 21:03 sphh