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

Created on 4 Feb 2018  ·  4Comments  ·  Source: spyder-ide/spyder

maybe we should only replace placeholder between the tripe-double-quotes(“”“...”“”)

if u use this template.py

# -*- coding: utf-8 -*-
"""
Created on %(date)s

Creator: yourname
"""
from logging import debug as LD
from logging import info as LI
import logging as log
log.basicConfig(level=log.DEBUG,
               format='%(levelname)s|%(funcName)s|line:%(lineno)d - %(message)s')
log.disable(log.DEBUG)
#log.disable(log.CRITICAL)

u will get this

# -*- coding: utf-8 -*-
"""
Created on %(date)s

Creator: yourname
"""
from logging import debug as LD
from logging import info as LI
import logging as log
log.basicConfig(level=log.DEBUG,
               format='%(levelname)s|%(funcName)s|line:%(lineno)d - %(message)s')
log.disable(log.DEBUG)
#log.disable(log.CRITICAL)

but of course we r expecting this

# -*- coding: utf-8 -*-
"""
Created on 2018-2-4

Creator: yourname
"""
from logging import debug as LD
from logging import info as LI
import logging as log
log.basicConfig(level=log.DEBUG,
               format='%(levelname)s|%(funcName)s|line:%(lineno)d - %(message)s')
log.disable(log.DEBUG)
#log.disable(log.CRITICAL)

the %(date)s has not been replaced properly because there is more placeholder

format='%(levelname)s|%(funcName)s|line:%(lineno)d - %(message)s')

in this template.py

in the code dealing with template.py

spyder

if we 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

i edit the edior.py as following,and it worked.

        if text is None:
            default_content = True
            text, enc = encoding.read(self.TEMPLATE_PATH)
            enc_match = re.search('-*- coding: ?([a-z0-9A-Z\-]*) -*-', text)
            annotation_match = re.search('"""(?:(?!""").|[\n\r])*"""', text)
            if enc_match:
                enc = enc_match.group(1)
            # Initialize template variables
            # Windows
            username = encoding.to_unicode_from_fs(os.environ.get('USERNAME',
                                                                  ''))
            # Linux, Mac OS X
            if not username:
                username = encoding.to_unicode_from_fs(os.environ.get('USER',
                                                                      '-'))
            VARS = {
                'date': time.strftime("%Y-%m-%d %H:%M"),
                'username': username,
            }
            try:
                #text = text % VARS
                text = re.sub('"""(?:(?!""").|[\n\r])*"""',
                              annotation_match[0] % VARS,
                              text,
                              count=1)
            except:
                pass
Editor Enhancement

Most helpful comment

is this going to be added to the next version of spyder?

All 4 comments

does anyone know how to change the date format in template.py to ,say, 2018-07-11 22:22:22?

@Hailprob It looks like it's hard coded in editor.py so you would have to change spyder code:

https://github.com/spyder-ide/spyder/blob/b7bc7d16ed81651e4ea5cfee827c7585dfa7e7b7/spyder/plugins/editor.py#L1901

is this going to be added to the next version of spyder?

No.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cchu08 picture cchu08  ·  3Comments

ok97465 picture ok97465  ·  3Comments

yogu220172 picture yogu220172  ·  3Comments

spyder-bot picture spyder-bot  ·  3Comments

impact27 picture impact27  ·  3Comments