/
vimscript.py
113 lines (103 loc) · 4.2 KB
/
vimscript.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
#!/usr/bin/env python
# coding: utf-8
from datetime import date
class VimScript(object):
def __init__(self, name='', url='', version=1.0, uploadDate=date.today(),
vimVersion=7.2, author = '', description = '', files=[]):
self.name = name
self.url = url
self.version = version
self.uploadDate = date
self.vimVersion = vimVersion
self.author = author
self.description = description
self.files = files
def _download(self):
"""Downloads the vim script using its url attr into a temp dir.
"""
import tempfile
import os.path
import re
from urllib import urlretrieve
from urllib2 import urlopen
from BeautifulSoup import BeautifulSoup #HTML Parser
soup = BeautifulSoup(urlopen(self.url))
downloadElt = soup.find(href=re.compile('download_script'))
downloadLink = os.path.join('http://www.vim.org/scripts',
downloadElt['href'])
print "Downloading {0}...".format(downloadElt.string)
tempdir = tempfile.mkdtemp()
filepath = '/'.join([tempdir, downloadElt.string])
urlretrieve(downloadLink, filepath)
return filepath
#TODO: Correctly determine to which directory .vim files should be moved.
def _unpack(self, filepath):
"""Determines the vim script's extension and unpacks it. Sets the
files variable. Removes the archive file and temp dir.
"""
import os.path
root, ext = os.path.splitext(filepath)
if ext == u'.zip':
from zipfile import ZipFile
archive = ZipFile(filepath, 'r')
self.files = [member.filename for member in archive.infolist() if
not member.filename[-1] == '/']
archive.extractall('/home/chris/.vim')
elif ext == u'.tar' or ext == u'.tgz' or ext == u'.bz2':
import tarfile
archive = tarfile.open(filepath)
self.files = [member.name for member in archive.getmembers() if
member.isfile()]
archive.extractall('/home/chris/.vim')
elif (ext == u'.gz' or ext == u'.bz2') and (os.path.splitext(root)[1] ==
u'.tar'):
import tarfile
archive = tarfile.open(filepath)
self.files = [member.name for member in archive.getmembers() if
member.isfile()]
archive.extractall('/home/chris/.vim')
elif ext == u'.vba':
vimExecute(':so %\n:q\n')
self.files=[filepath]
# Cleanup
print "Deleting {0} and {1}".format(filepath, os.path.dirname(filepath))
os.unlink(filepath)
os.rmdir(os.path.dirname(filepath))
def install(self):
"""Download and unpack the script into the vim runtime directory.
"""
filepath = self._download()
self._unpack(filepath)
vimExecute(':helptags /home/chris/.vim/doc\n:q\n')
#self._unpack(u'/home/chris/Downloads/taglist_45.tar')
def uninstall(self):
import os
import os.path
if os.path.splitext(self.files[0])[1] == '.vba':
vimExecute(':RmVimball {0}\n:q\n'.format(self.files[0]))
else:
print "Deleting {0}".format([os.path.join('/home/chris/.vim',
filename) for filename in self.files])
for filename in self.files:
filepath = os.path.join('/home/chris/.vim', filename)
os.unlink(filepath)
vimExecute(':helptags /home/chris/.vim/doc\n:q\n')
def vimExecute(command):
from subprocess import Popen
from tempfile import NamedTemporaryFile
import os
# Create a temporary file with a vim script which sources the
# vimball then quits vim. Open the vimball with vim and run the
# script.
scriptFile = NamedTemporaryFile(mode='w', delete=False)
scriptFile.write(command)
scriptFile.close()
vim = Popen('vim' + ' -s {0}'.format(scriptFile.name),
shell=True)
vim.wait()
os.unlink(scriptFile.name)
if __name__ == "__main__":
script = VimScript('slimv',
'http://www.vim.org/scripts/script.php?script_id=2501')
script.install()
#script.uninstall()