-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvimballfile.py
More file actions
74 lines (57 loc) · 2.04 KB
/
vimballfile.py
File metadata and controls
74 lines (57 loc) · 2.04 KB
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
"""
Read vimball files.
"""
import re
class VimballInfo:
"""Class with attributes describing each file in the vimball archive."""
def __init__(self, filename, body):
self.filename = filename
self.body = body
class VimballFile:
""" Class with methods to read, list vimball files.
vba = VimballFile(file)
file: Either the path to the file.
The file will be opened and closed by VimballFile.
"""
def __init__(self, file):
self.file = file
self.NameToInfo = {} # Find file info given name
self.filelist = [] # List of ZipInfo instances for archive
lines = []
with open(file, 'r') as f:
lines = f.readlines()[3:]
# FIXME: Check the file format.
while len(lines) != 0:
filename = re.sub('\t\[\[\[1\n$', '', lines[0]).replace('\\', '/')
lnum = int(lines[1])
body = ''.join(lines[2:lnum + 2])
lines = lines[lnum + 2:]
info = VimballInfo(filename, body)
self.filelist.append(info)
self.NameToInfo[filename] = info
def namelist(self):
"""Return a list of file names in the archive."""
return [data.filename for data in self.filelist]
def infolist(self):
"""Return a list of class VimballInfo instances for files in the
archive."""
return self.filelist
def getinfo(self, name):
"""Return the instance of VimballInfo given 'name'."""
info = self.NameToInfo.get(name)
if info is None:
raise KeyError(
'There is no item named %r in the archive' % name)
return info
def has_pkg(self, name):
return self.NameToInfo.has_key(name)
def read(self, name, *default):
"""Return file bytes (as a string) for name."""
if self.NameToInfo.has_key(name):
return self.NameToInfo.get(name).body
else:
if default:
return default[0]
else:
return None
# vim:set et ts=4 sw=4: