forked from josa42/ZyFileheader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzy_file_header.py
183 lines (145 loc) · 6.42 KB
/
zy_file_header.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# *********************************************************#
# @@ScriptName: zy_file_header_bak.py
# @@Author: zhenyu<[email protected]>
# @@Create Date: 2012-11-26 20:27:03
# @@Modify Date: 2013-09-06 07:49:03
# @@Function: It help to add file header automatically.
# *********************************************************#
import sublime_plugin
import os
import datetime
import re
import sublime
#from zy_config import ZyConfig
class ZyConfig:
config = None
@classmethod
def get_singleton(self):
self.load_settings()
return self.config
@classmethod
def load_settings(self):
s = sublime.load_settings('Preferences.sublime-settings')
self.config = s.get('zy_file_header')
if not self.config:
raise Exception("zy_file_header is not configured.")
"""set default time_format"""
if not self.config.get('time_format'):
self.config['time_format'] = '%Y-%m-%d %H:%M:%S'
class ZyAddHeaderOnCreatedCommand(sublime_plugin.TextCommand):
def run(self, edit):
zy_config = ZyConfig.get_singleton()
if zy_config.get('add_on_created') == False:
return
else:
self.view.run_command('zy_file_new_header')
class ZyFileNewHeaderCommand(sublime_plugin.TextCommand):
def run(self, edit):
zy_config = ZyConfig.get_singleton()
if not self.view.file_name():
file_header_format = zy_config.get('file_header_format')
else: # for existed files
file_name = self.view.file_name()
prefix, extensions = os.path.splitext(file_name)
file_header_format = zy_config.get(
'file_header_format' + extensions)
if not file_header_format:
file_header_format = zy_config.get('file_header_format')
"""replace @@author and @@email with the user definied value"""
author = zy_config.get('author')
email = zy_config.get('email')
file_header_format = file_header_format.replace('@@author', author)
file_header_format = file_header_format.replace('@@email', email)
"""
when file exists already, we need to use original create time
using os.stat, otherwise using current time instead
"""
if not self.view.file_name():
create_time = datetime.datetime.now().strftime(
zy_config.get('time_format'))
else:
file_stat = os.stat(self.view.file_name())
st_ctime = file_stat[9]
create_time = datetime.datetime.fromtimestamp(
st_ctime).strftime(zy_config.get('time_format'))
if file_header_format.find('@@Create Date') >= 0:
file_header_format = file_header_format.replace(
'@@Create Date:', '@@Create Date: ' + create_time)
self.view.insert(edit, 0, file_header_format)
class ZyAddCmdHeaderCommand(sublime_plugin.TextCommand):
def run(self, edit):
zy_config = ZyConfig.get_singleton()
if zy_config.get('add_on_created') == False:
return
file_name = self.view.file_name()
if file_name.endswith('.py'):
cmd_header = zy_config.get('python')
elif file_name.endswith('.sh'):
cmd_header = zy_config.get('shell')
"""get config by file extensions"""
prefix, extensions = os.path.splitext(file_name)
cmd_header = zy_config.get(extensions)
cmd_headers = cmd_header.split('\n')
exists = False
for line_no in range(0, 5):
line = self.view.substr(self.view.line(line_no))
for cmd_line in cmd_headers:
if line.find(cmd_line) >= 0:
exists = True
break
if not exists:
self.view.insert(edit, 0, cmd_header + '\n')
class ZyAddFileFooterCommand(sublime_plugin.TextCommand):
def run(self, edit):
default_footer = os.linesep
"""add a line to the end of the line"""
last_line = self.view.substr(self.view.line(self.view.size()))
if len(last_line) > 0:
self.view.insert(edit, self.view.size(), default_footer)
class ZyFileModifiedCommand(sublime_plugin.TextCommand):
def run(self, edit):
modified_date_region = self.view.find('@@Modify Date', 0)
if modified_date_region:
line = self.view.line(modified_date_region)
now = datetime.datetime.now().strftime(
ZyConfig.get_singleton().get('time_format'))
string_line = self.view.substr(line)
before_pos = string_line.find('@@Modify Date')
before_string = ''
if before_pos >= 0:
before_string = string_line[0:before_pos]
self.view.replace(edit,
line,
before_string + '@@Modify Date: ' + now)
file_name_region = self.view.find('@@ScriptName', 0)
if file_name_region:
line = self.view.line(file_name_region)
string_line = self.view.substr(line)
before_pos = string_line.find('@@ScriptName')
before_string = ''
if before_pos >= 0:
before_string = string_line[0:before_pos]
self.view.replace(edit,
line,
before_string + '@@ScriptName: ' + os.path.basename(self.view.file_name()))
class ZyAddFileHeaderManually(sublime_plugin.TextCommand):
def run(self, edit):
self.view.run_command('zy_file_new_header')
self.view.run_command('zy_file_modified')
self.view.run_command('zy_add_cmd_header')
class ZyAddFileAndCmdHeader(sublime_plugin.EventListener):
def on_new(self, view):
view.run_command('zy_add_header_on_created')
def on_pre_save(self, view):
zy_config = ZyConfig.get_singleton()
ignore_files = zy_config.get('ignore_files')
current_file = os.path.basename(view.file_name())
for f in ignore_files:
pattern = re.compile(f)
if pattern.match(current_file):
return
view.run_command('zy_file_modified')
view.run_command('zy_add_cmd_header')
view.run_command('zy_add_file_footer')