3
3
from os .path import isfile
4
4
from os .path import basename
5
5
from os .path import expanduser
6
+ from ConfigObject import ConfigObject
6
7
from urllib import urlopen
7
8
from subprocess import Popen
8
9
from subprocess import PIPE
26
27
27
28
class Manager (object ):
28
29
29
- runtime = expanduser ('~/.vim/bundle' )
30
- autoload = expanduser ('~/.vim/autoload' )
31
- ohmyvim = expanduser ('~/.vim/ohmyvim' )
32
-
33
30
dependencies = {
34
31
'vim-pathogen' : 'https://github.com/tpope/vim-pathogen.git' ,
35
32
'oh-my-vim' : 'https://github.com/gawel/oh-my-vim.git' ,
36
33
}
37
34
38
35
def __init__ (self ):
36
+ self .output = []
37
+
38
+ self .runtime = expanduser ('~/.vim/bundle' )
39
+ self .autoload = expanduser ('~/.vim/autoload' )
40
+ self .ohmyvim = expanduser ('~/.vim/ohmyvim' )
41
+
39
42
for dirname in (self .runtime , self .autoload ,
40
43
self .ohmyvim , expanduser ('~/.vim/swp' )):
41
44
if not isdir (dirname ):
42
45
os .makedirs (dirname )
46
+
43
47
for name , url in self .dependencies .items ():
44
48
if not isdir (join (self .runtime , name )):
45
49
Popen (['git' , 'clone' , '-q' , url ,
46
50
join (self .runtime , name )]).wait ()
51
+
47
52
if not isfile (join (self .ohmyvim , 'theme.vim' )):
48
53
with open (join (self .ohmyvim , 'theme.vim' ), 'w' ) as fd :
49
54
fd .write ('' )
55
+
50
56
if not isfile (join (self .ohmyvim , 'ohmyvim.vim' )):
51
57
with open (join (self .ohmyvim , 'ohmyvim.vim' ), 'w' ) as fd :
52
58
fd .write ('source %s\n ' % join (self .runtime , 'vim-pathogen' ,
53
59
'autoload' , 'pathogen.vim' ))
54
60
fd .write ('call pathogen#runtime_append_all_bundles()\n ' )
55
61
fd .write ('source %s\n ' % join (self .ohmyvim , 'theme.vim' ))
56
- binary = os .path .abspath (sys .argv [0 ])
62
+
63
+ kw = dict (ohmyvim = join (self .ohmyvim , 'ohmyvim.vim' ),
64
+ binary = os .path .abspath (sys .argv [0 ]))
57
65
if not isfile (expanduser ('~/.vimrc' )):
58
66
with open (expanduser ('~/.vimrc' ), 'w' ) as fd :
59
- fd .write (VIMRC % locals () )
67
+ fd .write (VIMRC % kw )
60
68
else :
61
69
with open (expanduser ('~/.vimrc' )) as fd :
62
- if binary not in fd .read ():
70
+ if kw [ ' binary' ] not in fd .read ():
63
71
with open (expanduser ('~/.vimrc' ), 'a' ) as fd :
64
- fd .write (VIMRC % locals ())
72
+ fd .write (VIMRC % kw )
73
+
74
+ def log (self , value , * args ):
75
+ if args :
76
+ value = value % args
77
+ self .output .append (value )
78
+ print (value )
65
79
66
80
def get_plugins (self ):
67
81
plugins = []
@@ -82,14 +96,18 @@ def search(self, args):
82
96
if not terms :
83
97
terms = ['language%3AVimL' ]
84
98
terms = '%20' .join (terms )
85
- webbrowser .open_new (("https://github.com/search?"
86
- "langOverride=&repo=&start_value=1&"
87
- "type=Repositories&language=VimL&q=" ) + terms )
99
+ url = ("https://github.com/search?"
100
+ "langOverride=&repo=&start_value=1&"
101
+ "type=Repositories&language=VimL&q=" ) + terms
102
+ if '__test__' not in os .environ :
103
+ webbrowser .open_new (url )
104
+ else :
105
+ self .log (url )
88
106
89
107
def list (self , args ):
90
108
for plugin , dirname , themes in self .get_plugins ():
91
- if args .raw :
92
- print plugin
109
+ if args .complete :
110
+ self . log ( plugin )
93
111
else :
94
112
os .chdir (dirname )
95
113
p = Popen (['git' , 'remote' , '-v' ], stdout = PIPE )
@@ -98,9 +116,9 @@ def list(self, args):
98
116
remote = remote .split ('\t ' )[1 ].split (' ' )[0 ]
99
117
if args .urls :
100
118
if plugin not in self .dependencies :
101
- print remote
119
+ self . log ( remote )
102
120
else :
103
- print '* %s (%s)' % ( plugin , remote )
121
+ self . log ( '* %s (%s)' , plugin , remote )
104
122
105
123
def install_url (self , url ):
106
124
url = url .strip ()
@@ -112,94 +130,90 @@ def install_url(self, url):
112
130
name = basename (url )[:- 4 ]
113
131
dirname = join (self .runtime , name )
114
132
if os .path .isdir (dirname ):
115
- print '%s already installed. Upgrading...' % name
133
+ self . log ( '%s already installed. Upgrading...' , name )
116
134
os .chdir (dirname )
117
135
Popen (['git' , 'pull' , '-n' ]).wait ()
118
136
else :
119
- print 'Installing bundle %s...' % name
137
+ self . log ( 'Installing bundle %s...' , name )
120
138
Popen (['git' , 'clone' , '-q' , url , dirname ]).wait ()
121
139
if isfile (join (dirname , 'requires.txt' )):
122
140
with open (join (dirname , 'requires.txt' )) as fd :
123
141
dependencies = [d for d in fd .readlines ()]
124
142
else :
125
- print '%s is not a git url' % url
143
+ self . log ( '%s is not a git url' , url )
126
144
return dirname , dependencies
127
145
128
146
def install (self , args ):
129
- dependencies = set ()
130
- for url in args .url :
131
- if url .endswith ('requires.txt' ):
132
- if isfile (url ):
133
- with open (url ) as fd :
147
+ filename = join (os .path .dirname (__file__ ), 'config.ini' )
148
+ config = ConfigObject (filename = filename )
149
+ if args .complete :
150
+ for name in sorted (config .bundles .keys ()):
151
+ self .log (name )
152
+ for name in sorted (config .themes .keys ()):
153
+ self .log (name )
154
+ else :
155
+ dependencies = set ()
156
+ for url in args .url :
157
+ url = config .bundles .get (url , url )
158
+ url = config .themes .get (url , url )
159
+ if url .endswith ('.txt' ):
160
+ if isfile (url ):
161
+ with open (url ) as fd :
162
+ dependencies = [d for d in fd .readlines ()]
163
+ elif url .startswith ('http' ):
164
+ fd = urlopen (url )
134
165
dependencies = [d for d in fd .readlines ()]
135
- elif url .startswith ('http' ):
136
- fd = urlopen (url )
137
- dependencies = [d for d in fd .readlines ()]
138
- else :
139
- _ , deps = self .install_url (url )
140
- for d in deps :
141
- if d .strip ():
142
- dependencies .add (d )
143
- if dependencies :
144
- print 'Processing dependencies...'
145
- for url in dependencies :
146
- self .install_url (url )
166
+ else :
167
+ _ , deps = self .install_url (url )
168
+ for d in deps :
169
+ if d .strip ():
170
+ dependencies .add (d )
171
+ if dependencies :
172
+ self .log ('Processing dependencies...' )
173
+ for url in dependencies :
174
+ self .install_url (url )
147
175
148
176
def upgrade (self , args ):
149
- if not args .bundle :
150
- print 'all'
151
177
for plugin , dirname , themes in self .get_plugins ():
152
178
if plugin in args .bundle or 'all' in args .bundle :
153
- print 'Upgrading %s...' % plugin
179
+ self . log ( 'Upgrading %s...' , plugin )
154
180
os .chdir (dirname )
155
181
Popen (['git' , 'pull' , '-n' ]).wait ()
156
- elif args .raw :
157
- print plugin
158
182
159
183
def remove (self , args ):
160
- for plugin , dirname , themes in self .get_plugins ():
161
- if not args .bundle :
162
- print plugin
163
- elif plugin in args .bundle :
164
- if plugin in self .dependencies :
165
- print "Don't remove %s!" % plugin
166
- print 'Removing %s...' % plugin
167
- dirname = join (self .runtime , plugin )
168
- if isdir (join (dirname , '.git' )):
169
- shutil .rmtree (dirname )
184
+ if args .bundle :
185
+ for plugin , dirname , themes in self .get_plugins ():
186
+ if plugin in args .bundle :
187
+ if plugin in self .dependencies :
188
+ self .log ("Don't remove %s!" , plugin )
189
+ self .log ('Removing %s...' , plugin )
190
+ dirname = join (self .runtime , plugin )
191
+ if isdir (join (dirname , '.git' )):
192
+ shutil .rmtree (dirname )
170
193
171
194
def theme (self , args ):
172
195
theme = args .theme
173
196
if theme :
174
- if theme .startswith ('http' ):
175
- theme_dir , _ = self .install_url (theme )
176
- for plugin , dirname , themes in self .get_plugins ():
177
- if theme_dir == dirname and len (themes ) == 1 :
178
- theme = themes [0 ]
179
- print 'Activate %s theme...' % theme
180
- with open (join (self .ohmyvim , 'theme.vim' ), 'w' ) as fd :
181
- fd .write (':colo %s\n ' % theme )
182
- else :
183
- for plugin , dirname , themes in self .get_plugins ():
184
- if theme in themes :
185
- print 'Activate %s theme...' % theme
186
- with open (join (self .ohmyvim , 'theme.vim' ), 'w' ) as fd :
187
- fd .write (':colo %s\n ' % theme )
188
- return
189
- for plugin , dirname , themes in self .get_plugins ():
190
- if isdir (join (dirname , '.git' )):
191
- os .chdir (dirname )
192
- p = Popen (['git' , 'remote' , '-v' ], stdout = PIPE )
193
- p .wait ()
194
- remote = p .stdout .read ().split ('\n ' )[0 ]
195
- remote = remote .split ('\t ' )[1 ].split (' ' )[0 ]
196
- if themes :
197
- if args .raw :
198
- for theme in themes :
199
- print theme
200
- else :
201
- print '* %s (%s)' % (plugin , remote )
202
- print '\t - %s' % ', ' .join (themes )
197
+ for plugin , dirname , themes in self .get_plugins ():
198
+ if theme in themes :
199
+ self .log ('Activate %s theme...' , theme )
200
+ with open (join (self .ohmyvim , 'theme.vim' ), 'w' ) as fd :
201
+ fd .write (':colo %s\n ' % theme )
202
+ else :
203
+ for plugin , dirname , themes in self .get_plugins ():
204
+ if isdir (join (dirname , '.git' )):
205
+ os .chdir (dirname )
206
+ p = Popen (['git' , 'remote' , '-v' ], stdout = PIPE )
207
+ p .wait ()
208
+ remote = p .stdout .read ().split ('\n ' )[0 ]
209
+ remote = remote .split ('\t ' )[1 ].split (' ' )[0 ]
210
+ if themes :
211
+ if args .complete :
212
+ for theme in themes :
213
+ self .log (theme )
214
+ else :
215
+ self .log ('* %s (%s)' , plugin , remote )
216
+ self .log ('\t - %s' , ', ' .join (themes ))
203
217
204
218
def profiles (self , args ):
205
219
profiles = join (self .runtime , 'oh-my-vim' , 'profiles' )
@@ -214,9 +228,9 @@ def profiles(self, args):
214
228
if line .startswith ('"' ):
215
229
desc += line .strip (' "\n ' )
216
230
if desc :
217
- print '* %s - %s' % ( name , desc )
231
+ self . log ( '* %s - %s' , name , desc )
218
232
else :
219
- print '* %s' % name
233
+ self . log ( '* %s' , name )
220
234
221
235
222
236
def main (* args ):
@@ -233,16 +247,16 @@ def main(*args):
233
247
p .set_defaults (action = manager .search )
234
248
235
249
p = subparsers .add_parser ('list' )
236
- p .add_argument ('--raw ' , action = 'store_true' , default = False )
250
+ p .add_argument ('--complete ' , action = 'store_true' , default = False )
237
251
p .add_argument ('-u' , '--urls' , action = 'store_true' , default = False )
238
252
p .set_defaults (action = manager .list )
239
253
240
254
p = subparsers .add_parser ('install' , help = 'install a script or bundle' )
255
+ p .add_argument ('--complete' , action = 'store_true' , default = False )
241
256
p .add_argument ('url' , nargs = '*' , default = '' )
242
257
p .set_defaults (action = manager .install )
243
258
244
259
p = subparsers .add_parser ('upgrade' , help = 'upgrade bundles' )
245
- p .add_argument ('--raw' , action = 'store_true' , default = False )
246
260
p .add_argument ('bundle' , nargs = '*' , default = '' )
247
261
p .set_defaults (action = manager .upgrade )
248
262
@@ -251,7 +265,7 @@ def main(*args):
251
265
p .set_defaults (action = manager .remove )
252
266
253
267
p = subparsers .add_parser ('theme' , help = 'list or activate a theme' )
254
- p .add_argument ('--raw ' , action = 'store_true' , default = False )
268
+ p .add_argument ('--complete ' , action = 'store_true' , default = False )
255
269
p .add_argument ('theme' , nargs = '?' , default = '' )
256
270
p .set_defaults (action = manager .theme )
257
271
@@ -262,4 +276,7 @@ def main(*args):
262
276
args = parser .parse_args (args )
263
277
else :
264
278
args = parser .parse_args ()
279
+
265
280
args .action (args )
281
+
282
+ return manager .output
0 commit comments