diff --git a/FileHeader.py b/FileHeader.py index e4e5d25..388594f 100644 --- a/FileHeader.py +++ b/FileHeader.py @@ -3,8 +3,7 @@ # @Author: lime # @Date: 2013-10-28 13:39:48 # @Last Modified by: lime -# @Last Modified time: 2013-11-09 08:00:00 - +# @Last Modified time: 2013-11-11 10:35:19 import os import sys @@ -16,7 +15,7 @@ import zipfile import getpass import shutil - +import time from datetime import datetime @@ -70,7 +69,7 @@ def plugin_loaded(): def Window(): - '''Get current act``ive window''' + '''Get current active window''' return sublime.active_window() @@ -133,17 +132,30 @@ def get_user(): return user -def get_args(syntax_type): - '''Get the args rendered''' +def get_args(syntax_type, path=None): + '''Get the args rendered. + + :Para: + - `syntax_type`: Language type + - `which`: candidates are 'new' and 'add' + ''' args = Settings().get('Default') args.update(Settings().get(syntax_type, {})) - format = get_strftime() - time = datetime.now().strftime(format) + c_time = m_time = datetime.now() + if path is not None: + try: + stat = os.stat(path) + except: + pass + else: + c_time = datetime(*time.localtime(stat.st_ctime)[:6]) + m_time = datetime(*time.localtime(stat.st_mtime)[:6]) - args.update({'create_time': time}) - args.update({'last_modified_time': time}) + format = get_strftime() + args.update({'create_time': c_time.strftime(format)}) + args.update({'last_modified_time': m_time.strftime(format)}) user = get_user() @@ -156,7 +168,7 @@ def get_args(syntax_type): return args -def render_template(syntax_type, part=None): +def render_template(syntax_type, part=None, path=None): '''Render the template correspond `syntax_type`''' from jinja2 import Template @@ -166,7 +178,7 @@ def render_template(syntax_type, part=None): else: template = Template(get_template(syntax_type)) - render_string = template.render(get_args(syntax_type)) + render_string = template.render(get_args(syntax_type, path)) except Exception as e: sublime.error_message(str(e)) render_string = '' @@ -267,7 +279,6 @@ def get_path(self, paths): return path - def on_done(self, path, name): if not name: return @@ -280,7 +291,6 @@ def on_done(self, path, name): path = os.path.join(path, name) self.new_file(path, syntax_type) - def run(self, paths=[]): path = self.get_path(paths) @@ -302,7 +312,7 @@ def __init__(self, path): def run(self): syntax_type = get_syntax_type(self.path) - header = render_template(syntax_type, 'header') + header = render_template(syntax_type, 'header', self.path) try: with open(self.path, 'r') as f: @@ -321,7 +331,7 @@ class AddFileHeaderCommand(sublime_plugin.TextCommand): def run(self, edit, path, part=None): syntax_type = get_syntax_type(path) - header = render_template(syntax_type, part) + header = render_template(syntax_type, part, path) self.view.insert(edit, 0, header) @@ -374,7 +384,6 @@ def can_add_to_dir(path): return False - def add(self, path): '''Add to a file''' @@ -390,7 +399,6 @@ def add(self, path): thread = BackgroundAddHeaderThread(path) thread.start() - def walk(self, path): '''Add files in the path''' @@ -400,7 +408,6 @@ def walk(self, path): if self.can_add(file_name): self.add(file_name) - def on_done(self, path): if not path: return @@ -416,7 +423,6 @@ def on_done(self, path): elif os.path.isdir(path) and self.can_add(path): self.walk(path) - def run(self, paths=[]): initial_text = '' if paths: @@ -503,7 +509,7 @@ def update_last_modified(self, view, what): spaces = (index - space_start) * ' ' if what == 'BY': args = get_args(syntax_type) - strings = (spaces+ args['last_modified_by']) + strings = (spaces + args['last_modified_by']) else: strftime = get_strftime() time = datetime.now().strftime(strftime) diff --git a/README.rst b/README.rst index 343145a..f37f7cb 100644 --- a/README.rst +++ b/README.rst @@ -54,7 +54,7 @@ Usage Create a new file ----------------- -- Sidebar menu +- Sidebar Menu .. image:: https://raw.github.com/shiyanhui/shiyanhui.github.io/master/images/FileHeader/new-file.gif @@ -62,14 +62,14 @@ Create a new file The default shortcuts is **super+alt+n** on OS X, **ctrl+alt+n** on Windows and Linux. -- Context menu +- Context Menu - Similar to **Sidebar menu**. + Similar to **Sidebar Menu**. Add header to an existed file ----------------------------- -- Sidebar menu +- Sidebar Menu .. image:: https://raw.github.com/shiyanhui/shiyanhui.github.io/master/images/FileHeader/add-header.gif @@ -77,9 +77,9 @@ Add header to an existed file The default shortcuts is **super+alt+a** on OS X, **ctrl+alt+a** on Windows and Linux. -- Context menu +- Context Menu - Similar to **Sidebar menu**. + Similar to **Sidebar Menu**. Add header to files in the specified directory ----------------------------------------------