-
Notifications
You must be signed in to change notification settings - Fork 0
/
regenzshrc.py
executable file
·109 lines (94 loc) · 2.85 KB
/
regenzshrc.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
#!/bin/env python
import os
import sys
import datetime
def open_file(path, mode='r'):
if sys.version_info[0] == 3:
return open(path, mode=mode, encoding='utf-8')
else:
return open(path, mode=mode)
class ZshRc(object):
directory_path = os.path.expanduser('~/.zshrc.d')
config_path = os.path.expanduser('~/.zshrc.d/zshrc.conf')
zshrc_path = os.path.expanduser('~/.zshrc')
@staticmethod
def is_module_name(name):
name = name.strip()
if name.startswith('#'):
return False
elif len(name) == 0:
return False
else:
return True
def __init__(self, debug_mode=False):
module_names = [
name.strip()
for name in open_file(self.config_path).readlines()
if self.is_module_name(name)
]
self.module_names = module_names
self.debug_mode = debug_mode
def generate_module_data(self, name):
path = os.path.join(self.directory_path, name)
_data = open_file(path).read().strip()
data_list = [
'##################################',
'# {}'.format(name),
'##################################',
]
if self.debug_mode:
data_list.append("echo 'DEBUG: {}'".format(name))
if self.debug_mode:
data_list += [
'() {',
'_start=`date +%s%N`',
'{}'.format(_data),
'_end=`date +%s%N`',
'echo {} $((_end - _start))'.format(name),
'}',
]
else:
data_list += [
'() {',
'{}'.format(_data),
'}',
]
data = '\n'.join(data_list)
return data
def generate(self):
data = '\n'.join([
'',
'',
'',
'########################################',
'# THIS FILE IS AUTOMATICALLY GENERATED #',
'########################################',
'',
'# generated at: {}'.format(datetime.datetime.now()),
'',
'',
'',
'',
'',
])
module_data = []
for name in self.module_names:
try:
module_data.append(self.generate_module_data(name))
except Exception as exp:
print("Exception catch module name:{}".format(name), exp)
data += '\n\n\n\n'.join(module_data)
return data
def update(self):
with open_file(self.zshrc_path, 'w') as fp:
fp.write(self.generate())
def main():
print('regenzshrc')
debug_mode = False
if len(sys.argv) >= 2:
print('debug mode')
debug_mode = True
zshrc = ZshRc(debug_mode=debug_mode)
zshrc.update()
if __name__ == '__main__':
main()