1- #!/usr/bin/env python
21# -*- coding: utf-8 -*-
3- # @Author: lime
2+ # @Author: Lime
43# @Date: 2013-10-28 13:39:48
54# @Last Modified by: Lime
6- # @Last Modified time: 2015-09-10 10 :47:07
5+ # @Last Modified time: 2016-02-23 14 :47:02
76
87import os
98import sys
109import re
11- import sublime
12- import sublime_plugin
1310import functools
1411import threading
1512import zipfile
1613import getpass
1714import shutil
18- import time
1915import pickle
2016import filecmp
21-
17+ import subprocess
2218from datetime import datetime
2319
24- if sys .version < '3' :
25- import commands as process
26- else :
27- import subprocess as process
20+ import sublime
21+ import sublime_plugin
22+
2823
2924PLUGIN_NAME = 'FileHeader'
3025INSTALLED_PLUGIN_NAME = '%s.sublime-package' % PLUGIN_NAME
@@ -85,6 +80,12 @@ def plugin_loaded():
8580 shutil .copyfile (INSTALLED_PLGIN_PATH , _ )
8681
8782
83+ def getOutputError (cmd ):
84+ return map (str .strip , subprocess .Popen (
85+ cmd , shell = True , universal_newlines = True ,
86+ stdout = subprocess .PIPE , stderr = subprocess .PIPE ).communicate ())
87+
88+
8889def Window ():
8990 '''Get current active window'''
9091
@@ -100,29 +101,31 @@ def Settings():
100101def get_template_part (syntax_type , part ):
101102 '''Get template header or body'''
102103
103- tmpl_name = '%s.tmpl' % syntax_type
104- path = HEADER_PATH if part == 'header' else BODY_PATH
105- tmpl_file = os . path . join ( path , tmpl_name )
104+ template_name = '%s.tmpl' % syntax_type
105+ tmplate_path = os . path . join (
106+ HEADER_PATH if part == 'header' else BODY_PATH , template_name )
106107
107108 custom_template_path = Settings ().get ('custom_template_%s_path' % part )
108109 if custom_template_path :
109- _ = os .path .join (custom_template_path , tmpl_name )
110- if os .path .exists (_ ) and os .path .isfile (_ ):
111- tmpl_file = _
110+ path = os .path .abspath (os .path .expanduser (os .path .expandvars (
111+ os .path .join (custom_template_path , template_name ))))
112+
113+ if os .path .exists (path ) and os .path .isfile (path ):
114+ tmplate_path = path
112115
113116 try :
114- template_file = open (tmpl_file , 'r' )
115- contents = template_file .read ()
116- template_file .close ()
117+ with open (tmplate_path , 'r' ) as f :
118+ contents = f .read ()
117119 except :
118120 contents = ''
119121 return contents
120122
121123
122124def get_template (syntax_type ):
123- return '' .join (
124- [get_template_part (syntax_type , part )
125- for part in ['header' , 'body' ]])
125+ return '' .join ([
126+ get_template_part (syntax_type , part )
127+ for part in ['header' , 'body' ]
128+ ])
126129
127130
128131def get_strftime ():
@@ -145,12 +148,12 @@ def get_user():
145148 '''Get user'''
146149
147150 user = getpass .getuser ()
148- status , _ = process .getstatusoutput ('git status' )
149- if status == 0 :
150- status , output = process .getstatusoutput ('git config --get user.name' )
151- if status == 0 and output :
152- user = output
153151
152+ output , error = getOutputError ('git status' )
153+ if not error :
154+ output , error = getOutputError ('git config --get user.name' )
155+ if not error and output :
156+ user = output
154157 return user
155158
156159
@@ -189,8 +192,8 @@ def get_time(path):
189192 except :
190193 pass
191194 else :
192- c_time = datetime ( * time . localtime ( stat . st_ctime )[: 6 ])
193- m_time = datetime ( * time . localtime (stat .st_mtime )[: 6 ] )
195+ c_time , m_time = map (
196+ datetime . fromtimestamp , (stat .st_ctime , stat . st_mtime ))
194197
195198 return c_time , m_time
196199
@@ -258,11 +261,9 @@ def render_template(syntax_type, part=None, options={}):
258261
259262 from jinja2 import Template
260263 try :
261- if part is not None :
262- template = Template (get_template_part (syntax_type , part ))
263- else :
264- template = Template (get_template (syntax_type ))
265-
264+ template = Template (
265+ get_template_part (syntax_type , part )
266+ if part else get_template (syntax_type ))
266267 render_string = template .render (get_args (syntax_type , options ))
267268 except :
268269 render_string = ''
@@ -400,8 +401,9 @@ def run(self, paths=[]):
400401 path = self .get_path (paths )
401402
402403 Window ().run_command ('hide_panel' )
403- Window ().show_input_panel ('File Name:' , '' , functools .partial (
404- self .on_done , path ), None , None )
404+ Window ().show_input_panel (
405+ 'File Name:' , '' ,
406+ functools .partial (self .on_done , path ), None , None )
405407
406408
407409class BackgroundAddHeaderThread (threading .Thread ):
@@ -449,42 +451,46 @@ class FileHeaderAddHeaderCommand(sublime_plugin.WindowCommand):
449451 def is_hidden (self , path ):
450452 '''Whether the file or dir is hidden'''
451453
452- hidden = False
453- platform = sublime .platform ()
454+ hidden , platform = False , sublime .platform ()
454455 if platform == 'windows' :
455- status , output = process . getstatusoutput ('attrib %s' % path )
456- if status == 0 :
456+ output , error = getOutputError ('attrib %s' % path )
457+ if not error :
457458 try :
458459 if output [4 ].upper () == 'H' :
459460 hidden = True
460461 except :
461462 pass
462- else :
463- basename = os .path .basename (path )
464- if basename .startswith ('.' ):
465- hidden = True
463+ elif os .path .basename (path ).startswith ('.' ):
464+ hidden = True
466465 return hidden
467466
468467 def can_add (self , path ):
469468 '''Whether can add header to path'''
470469
471470 def can_add_to_dir (path ):
472- return enable_add_to_hidden_dir or (not enable_add_to_hidden_dir
473- and not self .is_hidden (path ))
471+ return enable_add_to_hidden_dir or (
472+ not enable_add_to_hidden_dir and
473+ not self .is_hidden (path ))
474+
475+ def can_add_to_file (path ):
476+ return enable_add_to_hidden_file or (
477+ not enable_add_to_hidden_file and
478+ not self .is_hidden (path ))
474479
475480 if not os .path .exists (path ):
476481 return False
477482
478- enable_add_to_hidden_dir = Settings ().get (
479- 'enable_add_header_to_hidden_dir' )
480- enable_add_to_hidden_file = Settings ().get (
481- 'enable_add_header_to_hidden_file' )
483+ enable_add_to_hidden_dir , enable_add_to_hidden_file = map (
484+ Settings ().get , (
485+ 'enable_add_header_to_hidden_dir' ,
486+ 'enable_add_header_to_hidden_file'
487+ )
488+ )
482489
483- if os .path .isfile (path ):
484- if can_add_to_dir (os .path .dirname (path )):
485- if enable_add_to_hidden_file or (not enable_add_to_hidden_file
486- and not self .is_hidden (path )):
487- return True
490+ if (os .path .isfile (path ) and
491+ can_add_to_dir (os .path .dirname (path )) and
492+ can_add_to_file (path )):
493+ return True
488494
489495 elif os .path .isdir (path ):
490496 return can_add_to_dir (path )
@@ -503,8 +509,7 @@ def add(self, path):
503509 'add_file_header' , {'path' : path , 'part' : 'header' })
504510 block (modified_file , modified_file .show , 0 )
505511 else :
506- thread = BackgroundAddHeaderThread (path )
507- thread .start ()
512+ BackgroundAddHeaderThread (path ).start ()
508513
509514 def walk (self , path ):
510515 '''Add files in the path'''
@@ -611,18 +616,16 @@ def update_automatically(self, view, what):
611616 break
612617
613618 line_header = re .escape (line_header )
614- if what == LAST_MODIFIED_BY or what == FILE_NAME or \
615- what == FILE_NAME_WITHOUT_EXTENSION or \
616- what == FILE_PATH :
619+ if what in set ([
620+ LAST_MODIFIED_BY , FILE_NAME ,
621+ FILE_NAME_WITHOUT_EXTENSION , FILE_PATH ]) :
617622 line_pattern = '%s.*\n ' % line_header
618623
619624 elif what == LAST_MODIFIED_TIME :
620625 line_pattern = '%s\s*%s.*\n ' % (
621626 line_header , self .time_pattern ())
622-
623627 else :
624628 raise KeyError ()
625-
626629 break
627630
628631 if line_pattern is not None :
@@ -652,12 +655,14 @@ def update_automatically(self, view, what):
652655
653656 region = sublime .Region (int (a ), int (b ))
654657 if view .substr (region ) != strings :
655- view .run_command ('file_header_replace' ,
656- {'a' : a , 'b' : b , 'strings' : strings })
658+ view .run_command (
659+ 'file_header_replace' ,
660+ {'a' : a , 'b' : b , 'strings' : strings })
657661
658662 def insert_template (self , view , exists ):
659663 enable_add_template_to_empty_file = Settings ().get (
660- 'enable_add_template_to_empty_file' )
664+ 'enable_add_template_to_empty_file' ) and view .settings ().get (
665+ 'enable_add_template_to_empty_file' , True )
661666
662667 path = view .file_name ()
663668 condition = (path and enable_add_template_to_empty_file
0 commit comments