Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shiyanhui committed Nov 1, 2013
1 parent fbcf00e commit 715e9c5
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 15 deletions.
71 changes: 56 additions & 15 deletions FileHeader.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# @Date: 2013-10-28 13:39:48
# @Email: [email protected]
# @Last modified by: lime
# @Last Modified time: 2013-11-01 12:17:11
# @Last Modified time: 2013-11-01 15:13:16

import os
import sys
Expand All @@ -18,6 +18,11 @@

from datetime import datetime

if sys.version < '3':
import commands as process
else:
import subprocess as process

PLUGIN_NAME = 'FileHeader'
PACKAGES_PATH = sublime.packages_path()
PLUGIN_PATH = os.path.join(PACKAGES_PATH, PLUGIN_NAME)
Expand Down Expand Up @@ -61,8 +66,6 @@ def Settings():
def get_template(syntax_type):
'''Get template correspond `syntax_type`'''

print(TEMPLATE_PATH)

tmpl_name = '%s.tmpl' % syntax_type
tmpl_file = os.path.join(TEMPLATE_PATH, tmpl_name)

Expand Down Expand Up @@ -96,11 +99,6 @@ def get_strftime():
def get_user():
'''Get user'''

if sys.version < '3':
import commands as process
else:
import subprocess as process

user = getpass.getuser()
status, _ = process.getstatusoutput('git status')
if status == 0:
Expand Down Expand Up @@ -187,7 +185,6 @@ def _block():

_block()


class FileHeaderNewFileCommand(sublime_plugin.WindowCommand):
'''Create a new file with header'''

Expand Down Expand Up @@ -258,7 +255,6 @@ def run(self, paths=[]):
Window().show_input_panel(caption, '', functools.partial(
self.on_done, path), None, None)


class BackgroundAddHeaderThread(threading.Thread):
'''Add header in background.'''

Expand All @@ -267,7 +263,6 @@ def __init__(self, path):
super(BackgroundAddHeaderThread, self).__init__()

def run(self):

syntax_type = get_syntax_type(self.path)
header = render_template(syntax_type)

Expand All @@ -294,6 +289,51 @@ def run(self, edit, path):
class FileHeaderAddHeaderCommand(sublime_plugin.WindowCommand):
'''Conmmand: add `header` in a file or directory'''

def is_hidden(self, path):
'''Whether the file or dir is hidden'''

hidden = False
platform = sublime.platform()
if platform == 'windows':
status, output = process.getstatusoutput('attrib %s' % path)
if status == 0:
try:
if output[4].upper() == 'H':
hidden = True
except:
pass
else:
basename = os.path.basename(path)
if basename.startswith('.'):
hidden = True
return hidden

def can_add(self, path):
'''Whether can add header to path'''

def can_add_to_dir(path):
return enable_add_to_hidden_dir or (not enable_add_to_hidden_dir and
not self.is_hidden(path))

if not os.path.exists(path):
return False

options = Settings().get('options')
file_suffix_mapping = options['file_suffix_mapping']
enable_add_to_hidden_dir = options['enable_add_header_to_hidden_dir']
enable_add_to_hidden_file = options['enable_add_header_to_hidden_file']

if os.path.isfile(path):
if can_add_to_dir(os.path.dirname(path)):
if enable_add_to_hidden_file or (not enable_add_to_hidden_file
and not self.is_hidden(path)):
return True

elif os.path.isdir(path):
return can_add_to_dir(path)

return False

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

Expand All @@ -315,7 +355,8 @@ def walk(self, path):
for root, subdirs, files in os.walk(path):
for f in files:
file_name = os.path.join(root, f)
self.add(file_name)
if self.can_add(file_name):
self.add(file_name)

def on_done(self, path):
if not path:
Expand All @@ -326,10 +367,10 @@ def on_done(self, path):
return

path = os.path.abspath(path)

if os.path.isfile(path):
if os.path.isfile(path) and self.can_add(path):
self.add(path)
elif os.path.isdir(path):

elif os.path.isdir(path) and self.can_add(path):
self.walk(path)

def run(self, paths=[]):
Expand Down
10 changes: 10 additions & 0 deletions FileHeader.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
*/
"open_file_when_add_header_to_directory": true,
/*
Whether enable add header to hidden directory. If false, FileHeader
won't add header to files under it.
*/
"enable_add_header_to_hidden_dir": false,
/*
Whether enable add header to hidden file. If false, FileHeader
won't add header to it.
*/
"enable_add_header_to_hidden_file": false,
/*
FileHeader judges programming language according file suffix.
Default programming language if FileHeader judges failed when you
Expand Down

0 comments on commit 715e9c5

Please sign in to comment.