@@ -20,86 +20,86 @@ def get_local_ip():
20
20
21
21
class CustominsertCommand (sublime_plugin .TextCommand ):
22
22
23
- def get_settings_param (self ,chains = [],default = '' ):
23
+ def get_settings_param (self ,chains = [],default = '' ):
24
24
'''get params in dict chains'''
25
- obj = self .settings
25
+ obj = self .settings
26
26
for e in chains :
27
- obj = obj .get (e )
27
+ obj = obj .get (e )
28
28
if not obj :
29
29
return default
30
30
return obj
31
31
32
32
def get_predefined_param (self ,match ):
33
33
'''{%%}'''
34
- key = match .group (1 )
35
- if key == 'filename' :
34
+ key = match .group (1 )
35
+ if key == 'filename' :
36
36
return os .path .basename (self .view .file_name () or '' )
37
- elif key == 'filepath' :
37
+ elif key == 'filepath' :
38
38
return self .view .file_name () or ''
39
- elif key == 'dirname' :
39
+ elif key == 'dirname' :
40
40
return os .path .dirname (self .view .file_name () or '' )
41
- elif key == 'platform' :
41
+ elif key == 'platform' :
42
42
return sublime .platform ()
43
- elif key == 'arch' :
43
+ elif key == 'arch' :
44
44
return sublime .arch ()
45
- elif key == 'encoding' :
45
+ elif key == 'encoding' :
46
46
encoding = self .view .encoding ()
47
47
return encoding if 'Undefined' != encoding else self .settings .get ('default_encoding' )
48
- elif key == 'ip' :
48
+ elif key == 'ip' :
49
49
return get_local_ip ()
50
- ipaddrlist = socket .gethostbyname_ex (socket .gethostname ())
51
- ips = ''
50
+ ipaddrlist = socket .gethostbyname_ex (socket .gethostname ())
51
+ ips = ''
52
52
for e in ipaddrlist :
53
53
if isinstance (e ,list ):
54
54
for k in e :
55
- ips += " " + k
55
+ ips += " " + k
56
56
else :
57
- ips += e
57
+ ips += e
58
58
return ips
59
- elif key == 'user' :
60
- user = os .getlogin () if 'windows' != sublime .platform () else ''
59
+ elif key == 'user' :
60
+ user = os .getlogin () if 'windows' != sublime .platform () else ''
61
61
if user :
62
62
return user
63
63
#windows?
64
- user = os .popen ('whoami' ).read ()
65
- p = re .compile ('[\r \n ]' ,re .M )
64
+ user = os .popen ('whoami' ).read ()
65
+ p = re .compile ('[\r \n ]' ,re .M )
66
66
return re .sub (p ,'' ,user )
67
- elif key == 'ext' :
68
- ext = os .path .splitext (self .view .file_name () or '' )[1 ]
67
+ elif key == 'ext' :
68
+ ext = os .path .splitext (self .view .file_name () or '' )[1 ]
69
69
#remove . at start position
70
- p = re .compile ('^\.' )
70
+ p = re .compile ('^\.' )
71
71
return re .sub (p ,'' ,ext )
72
- elif key == 'datetime' :
73
- t = datetime .datetime .today ()
72
+ elif key == 'datetime' :
73
+ t = datetime .datetime .today ()
74
74
return t .strftime (self .get_action_param ('datetime_format' ,'%Y-%m-%d %H:%M:%S' ))
75
75
return match .group (1 )
76
76
77
77
def get_defined_param (self ,match ):
78
78
'''{{}} '''
79
- key = match .group (1 )
79
+ key = match .group (1 )
80
80
return self .get_settings_param (['actions' ,self .action ,'data' ,key ],self .get_settings_param (['data' ,key ]))
81
81
82
- def get_action_param (self ,key ,default = '' ):
82
+ def get_action_param (self ,key ,default = '' ):
83
83
'''get params of actions'''
84
84
return self .get_settings_param (['actions' ,self .action ,key ],self .get_settings_param ([key ]))
85
85
86
- def run (self , edit ,action = '' ):
87
- v = self .view
86
+ def run (self , edit ,action = '' ):
87
+ v = self .view
88
88
settings_name = 'Custominsert'
89
89
settings = sublime .load_settings (settings_name + '.sublime-settings' ) or {}
90
90
#save settings
91
- self .settings = settings
91
+ self .settings = settings
92
92
#save action
93
- self .action = action ;
94
- content = self .get_action_param ('content' )
93
+ self .action = action ;
94
+ content = self .get_action_param ('content' )
95
95
#replace {%%}
96
- p = re .compile ('\{%\s*?([\w]+)\s*?%\}' ,re .LOCALE | re .MULTILINE )
97
- content = re .sub (p ,self .get_predefined_param ,content )
96
+ p = re .compile ('\{%\s*?([\w]+)\s*?%\}' ,re .LOCALE | re .MULTILINE )
97
+ content = re .sub (p ,self .get_predefined_param ,content )
98
98
#replace {{}}
99
- p = re .compile ('\{\{\s*?([\w]+)\s*?\}\}' ,re .LOCALE | re .MULTILINE )
100
- content = re .sub (p ,self .get_defined_param ,content )
99
+ p = re .compile ('\{\{\s*?([\w]+)\s*?\}\}' ,re .LOCALE | re .MULTILINE )
100
+ content = re .sub (p ,self .get_defined_param ,content )
101
101
#insert position
102
- if 'start' == self .get_action_param ('position' ):
102
+ if 'start' == self .get_action_param ('position' ):
103
103
v .insert (edit , 0 , content )
104
104
else :
105
105
for sel in v .sel ():
0 commit comments