Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyanhui committed Nov 11, 2013
1 parent 4f5ba20 commit 0d2e7f6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 27 deletions.
48 changes: 27 additions & 21 deletions FileHeader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,7 +15,7 @@
import zipfile
import getpass
import shutil

import time

from datetime import datetime

Expand Down Expand Up @@ -70,7 +69,7 @@ def plugin_loaded():


def Window():
'''Get current act``ive window'''
'''Get current active window'''

return sublime.active_window()

Expand Down Expand Up @@ -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()

Expand All @@ -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
Expand All @@ -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 = ''
Expand Down Expand Up @@ -267,7 +279,6 @@ def get_path(self, paths):

return path


def on_done(self, path, name):
if not name:
return
Expand All @@ -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)

Expand All @@ -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:
Expand All @@ -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)


Expand Down Expand Up @@ -374,7 +384,6 @@ def can_add_to_dir(path):

return False


def add(self, path):
'''Add to a file'''

Expand All @@ -390,7 +399,6 @@ def add(self, path):
thread = BackgroundAddHeaderThread(path)
thread.start()


def walk(self, path):
'''Add files in the path'''

Expand All @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,32 @@ Usage
Create a new file
-----------------

- Sidebar menu
- Sidebar Menu

.. image:: https://raw.github.com/shiyanhui/shiyanhui.github.io/master/images/FileHeader/new-file.gif

- Shortcuts

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

- Shortcuts

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
----------------------------------------------
Expand Down

0 comments on commit 0d2e7f6

Please sign in to comment.