forked from web2py/web2py
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentparser.py
More file actions
executable file
·132 lines (120 loc) · 4.26 KB
/
Copy pathcontentparser.py
File metadata and controls
executable file
·132 lines (120 loc) · 4.26 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cStringIO
import re
import sys
import tarfile
import urllib
import xml.parsers.expat as expat
"""
Update script for contenttype.py module.
Usage: python contentupdate.py /path/to/contenttype.py
If no path is specified, script will look for contenttype.py in current
working directory.
Internet connection is required to perform the update.
"""
OVERRIDE = [
('.pdb', 'chemical/x-pdb'),
('.xyz', 'chemical/x-pdb')
]
class MIMEParser(dict):
def __start_element_handler(self, name, attrs):
if name == 'mime-type':
if self.type:
for extension in self.extensions:
self[extension] = self.type
self.type = attrs['type'].lower()
self.extensions = []
elif name == 'glob':
pattern = attrs['pattern']
if pattern.startswith('*.'):
self.extensions.append(pattern[1:].lower())
def __init__(self, fileobj):
dict.__init__(self)
self.type = ''
self.extensions = ''
parser = expat.ParserCreate()
parser.StartElementHandler = self.__start_element_handler
parser.ParseFile(fileobj)
for extension, contenttype in OVERRIDE:
self[extension] = contenttype
if __name__ == '__main__':
try:
path = sys.argv[1]
except:
path = 'contenttype.py'
vregex = re.compile('database version (?P<version>.+?)\.?\n')
sys.stdout.write('Checking contenttype.py database version:')
sys.stdout.flush()
try:
pathfile = open(path)
try:
current = pathfile.read()
finally:
pathfile.close()
cversion = re.search(vregex, current).group('version')
sys.stdout.write('\t[OK] version %s\n' % cversion)
except Exception, e:
sys.stdout.write('\t[ERROR] %s\n' % e)
exit()
sys.stdout.write('Checking freedesktop.org database version:')
sys.stdout.flush()
try:
search = re.search(
'(?P<url>http://freedesktop.org/.+?/shared-mime-info-(?P<version>.+?)\.tar\.(?P<type>[gb]z2?))',
urllib.urlopen('http://www.freedesktop.org/wiki/Software/shared-mime-info').read())
url = search.group('url')
assert url is not None
nversion = search.group('version')
assert nversion is not None
ftype = search.group('type')
assert ftype is not None
sys.stdout.write('\t[OK] version %s\n' % nversion)
except:
sys.stdout.write('\t[ERROR] unknown version\n')
exit()
if cversion == nversion:
sys.stdout.write('\nContenttype.py database is up to date\n')
exit()
try:
raw_input('\nContenttype.py database updates are available from:\n%s (approx. 0.5MB)\nPress enter to continue or CTRL-C to quit now\nWARNING: this will replace contenttype.py file content IN PLACE' % url)
except:
exit()
sys.stdout.write('\nDownloading new database:')
sys.stdout.flush()
fregex = re.compile('^.*/freedesktop\.org\.xml$')
try:
io = cStringIO.StringIO()
io.write(urllib.urlopen(url).read())
sys.stdout.write('\t[OK] done\n')
except Exception, e:
sys.stdout.write('\t[ERROR] %s\n' % e)
exit()
sys.stdout.write('Installing new database:')
sys.stdout.flush()
try:
tar = tarfile.TarFile.open(fileobj=io, mode='r:%s' % ftype)
try:
for content in tar.getnames():
if fregex.match(content):
xml = tar.extractfile(content)
break
finally:
tar.close()
data = MIMEParser(xml)
io = cStringIO.StringIO()
io.write('CONTENT_TYPE = {\n')
for key in sorted(data):
io.write(' \'%s\': \'%s\',\n' % (key, data[key]))
io.write(' }')
io.seek(0)
contenttype = open('contenttype.py', 'w')
try:
contenttype.write(re.sub(vregex, 'database version %s.\n' % nversion, re.sub('CONTENT_TYPE = \{(.|\n)+?\}', io.getvalue(), current)))
finally:
contenttype.close()
if not current.closed:
current.close()
sys.stdout.write('\t\t\t[OK] done\n')
except Exception, e:
sys.stdout.write('\t\t\t[ERROR] %s\n' % e)