This repository was archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathformats.py
176 lines (161 loc) · 8.96 KB
/
formats.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'''A directory of file formats and their properties'''
ICON_MAP = None
def get_icon(format_):
global ICON_MAP
if ICON_MAP is None:
ICON_MAP = dict((
# Format, icon
# Where:
# * format is canonical format from resource_formats.json
# * icon is in ckanext/dgu/theme/src/images/fugue/
('HTML', 'globe--arrow'),
('JPEG', 'image'),
('TIFF', 'image'),
('Database', 'database-sql'),
('API', 'server-cloud'),
('TXT', 'document-text'),
('PDF', 'document-pdf'),
('RTF', 'document-word'),
('Zip', 'folder-zipper'),
#'Torrent'
('DOC', 'document-word'),
('ODT', 'document-word'),
('PPT', 'document-powerpoint'),
('ODP', 'document-powerpoint'),
('XLS', 'document-excel'),
('SHP', 'globe-model'),
('CSV', 'document-invoice'),
('PSV', 'document-invoice'),
('TSV', 'document-invoice'),
('JSON', 'document-node'),
('XML', 'document-code'),
('RSS', 'feed-document'),
('ODS', 'document-excel'),
('WMS', 'globe-model'),
('KML', 'globe-model'),
#'NetCDF',
('IATI', 'document-code'),
('iCalendar', 'calendar-day'),
('RDF', 'document-rdf'),
('RDFa', 'document-rdf'),
('SPARQL', 'document-rdf'),
('SPARQL web form', 'document-rdf'),
('OWL', 'document-rdf'),
))
return ICON_MAP.get(format_)
import re
class Formats(object):
@classmethod
def by_display_name(cls):
'''Returns the formats data as a dict keyed by the display name'''
if not hasattr(cls, '_by_display_name'):
data = cls.get_data()
cls._by_display_name = {}
for format_dict in data:
cls._by_display_name[format_dict['display_name']] = format_dict
return cls._by_display_name
@classmethod
def by_mime_type(cls):
'''Returns the formats data as a dict keyed by mime type'''
if not hasattr(cls, '_by_mime_type'):
data = cls.get_data()
cls._by_mime_type = {}
for format_dict in data:
for mime_type in format_dict['mime_types']:
cls._by_mime_type[mime_type] = format_dict
return cls._by_mime_type
@classmethod
def by_extension(cls):
'''Returns the formats data as a dict keyed by filename extension'''
if not hasattr(cls, '_by_extension'):
data = cls.get_data()
cls._by_extension = {}
for format_dict in data:
for extension in format_dict['extensions']:
cls._by_extension[extension] = format_dict
return cls._by_extension
@classmethod
def by_reduced_name(cls):
'''Returns the formats data as a dict keyed by "reduced" names for
each format. This is helpful for matching against user-inputted formats.
e.g. "TXT / .Zip" is "txt/zip"'''
if not hasattr(cls, '_by_reduced'):
data = cls.get_data()
cls._by_reduced = {}
for format_dict in data:
for name in [format_dict['display_name']] + list(format_dict['extensions']) \
+ list(format_dict['alternative_names']):
reduced_name = cls.reduce(name)
cls._by_reduced[reduced_name] = format_dict
return cls._by_reduced
@staticmethod
def reduce(format_name):
format_name = format_name.strip().lower()
if format_name.startswith('.'): format_name = format_name[1:]
return re.sub('[^a-z/+]', '', format_name)
@classmethod
def match(cls, raw_resource_format):
'''Given a format that may be badly formatted, try and match it to
a known format and return that.
If no match is found, returns None.
'''
# Try exact match
if raw_resource_format in cls.by_display_name():
return cls.by_display_name()[raw_resource_format]
# Try canonised match
reduced_raw = cls.reduce(raw_resource_format)
if reduced_raw in cls.by_reduced_name():
return cls.by_reduced_name()[reduced_raw]
@classmethod
def get_data(cls):
'''Returns the list of data formats, each one as a dict
e.g. [{'display_name': 'TXT', 'extensions': ('txt',), 'extension': 'txt',
'mime_types': ('text/plain',), 'openness': 1},
...]
'''
if not hasattr(cls, '_data'):
# store the data here so it only loads when first used, rather
# than on module load
data_flat = (
# Display name, alternative names, extensions (lower case), mime-types, openness, icon-name
('HTML', ('web page', 'website'), ('html', 'htm', 'asp', 'php'), ('text/html',), 0, 'globe--arrow'),
('JPEG', (), ('jpg','jpeg'), ('image/jpg',), 0, 'image'),
('TIFF', (), ('tifflzw','tiff'), ('image/tiff',), 0, 'image'),
('Database', ('database','sql'), (), (), 0, 'database-sql'),
('API', ('api',), (), (), 0, 'server-cloud'),
('TXT', (), ('txt',), ('text/plain',), 1, 'document-text'),
('PDF', (), ('pdf',), ('application/pdf',), 1, 'document-pdf'),
('RTF', (), ('rtf',), ('application/rtf',), 1, 'document-word'),
('Zip', (), ('zip',), ('application/x-zip', 'application/x-compressed', 'application/x-zip-compressed', 'application/zip', 'multipart/x-zip', 'application/x-gzip'), 1, 'folder-zipper'),
('Torrent', (), ('torrent',), ('application/x-bittorrent',), 1, ''),
('DOC', ('word',), ('doc', 'docx', 'mcw'), ('application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/vnd.ms-word.document.macroEnabled.12'), 1, 'document-word'),
('ODT', (), ('odt',), ('application/vnd.oasis.opendocument.text', 'application/x-vnd.oasis.opendocument.text'), 1, 'document-word'),
('PPT', ('powerpoint',), ('ppt', 'pptx', 'ppz'), ('application/mspowerpoint', 'application/vnd.ms-powerpoint.presentation.macroEnabled.12', 'application/vnd.ms-powerpoint'), 1, 'document-powerpoint'),
('ODP', (), ('odp',), ('application/vnd.oasis.opendocument.presentation', 'application/x-vnd.oasis.opendocument.presentation'), 1, 'document-powerpoint'),
('XLS', ('excel',), ('xls', 'xlsx', 'xlb'), ('application/excel', 'application/x-excel', 'application/x-msexcel', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel.sheet.binary.macroenabled.12', 'application/vnd.ms-excel.sheet.macroenabled.12', 'application/vnd.msexcel'), 2, 'document-excel'),
('SHP', ('shapefile', 'esri shapefile',), ('shp',), (), 2, 'globe-model'),
('CSV', ('csvfile',), ('csv',), ('text/csv','text/comma-separated-values'), 3, 'document-invoice'),
('PSV', (), ('psv',), ('text/psv','text/pipe-separated-values'), 3, 'document-invoice'),
('JSON', (), ('json',), ('application/json', 'text/x-json'), 3, 'document-node'),
('XML', (), ('xml',), ('text/xml','application/xml'), 3, 'document-code'),
('RSS', (), ('rss',), ('text/rss+xml',), 3, 'feed-document'),
('ODS', (), ('ods',), ('application/vnd.oasis.opendocument.spreadsheet',), 3, 'document-excel'),
('WMS', (), ('wms',), ('application/vnd.ogc.wms_xml',), 3, 'globe-model'),
('KML', (), ('kml',), ('application/vnd.google-earth.kml+xml',), 3, 'globe-model'),
('NetCDF', (), ('cdf', 'netcdf'), ('application/x-netcdf',), 3, ''),
('IATI', (), ('iati',), ('application/x-iati+xml',), 3, 'document-code'),
('iCalendar', ('iCal', 'ICS'), ('ics', 'ical'), ('text/calendar',), 3, 'calendar-day'),
('RDF', ('rdf/xml','Turtle','N-Triples'), ('rdf','ttl','nt'), ('application/rdf+xml','text/turtle'), 5, 'document-rdf'),
('RDFa', ('html+rdfa',), (), (), 5, 'document-rdf'),
('SPARQL', (), (), ('application/sparql-results+xml', 'application/sparql-query'), 5, 'document-rdf'),
('SPARQL web form', (), (), (), 5, 'document-rdf'),
)
cls._data = []
for line in data_flat:
display_name, alternative_names, extensions, mime_types, openness, icon = line
format_dict = dict(zip(('display_name', 'alternative_names', 'extensions', 'mime_types', 'openness', 'icon'), line))
format_dict['extension'] = extensions[0] if extensions else ''
cls._data.append(format_dict)
return cls._data
# Mime types which give not much clue to the format
VAGUE_MIME_TYPES = set(('application/octet-stream',))