diff options
author | Sascha Silbe <sascha-pgp@silbe.org> | 2013-11-16 20:04:58 (GMT) |
---|---|---|
committer | Sascha Silbe <sascha-pgp@silbe.org> | 2013-11-16 20:04:58 (GMT) |
commit | 95f705afe324f1f3e5b1d07a234c4026c7dd867c (patch) | |
tree | cb476c0ba82ffe25a8b252f3f1ffd9b776b17b92 | |
parent | 96e12d2411dbf103832fbe3dcdad026f3fcd5180 (diff) |
fsemulation: replace sugar-base dependency by python-xdg
Use xdg.Mime and xdg.BaseDirectory instead of sugar.mime so we don't
depend on sugar-base.
-rw-r--r-- | fsemulation.py | 46 |
1 files changed, 38 insertions, 8 deletions
diff --git a/fsemulation.py b/fsemulation.py index fb6ba39..13a5d27 100644 --- a/fsemulation.py +++ b/fsemulation.py @@ -21,8 +21,8 @@ import time import dbus import xapian - -import sugar.mime +import xdg.BaseDirectory +import xdg.Mime DS_DBUS_SERVICE = 'org.laptop.sugar.DataStore' @@ -955,6 +955,7 @@ class FSEmulation(object): self._root_dir = RootDirectory(self, 0550) self._object_id_to_title_name = {} self._title_name_to_object_id = {} + self._mime_type_exts = self._load_mime_type_exts() def resolve(self, path, follow_links=False): assert isinstance(path, unicode) @@ -1059,20 +1060,49 @@ class FSEmulation(object): del self._object_id_to_title_name[object_id] def _guess_extension(self, mime_type, object_id): - extension = None - if not mime_type: file_name = self.data_store.get_data(object_id) if file_name: try: - mime_type = sugar.mime.get_for_file(file_name) + mime_type = xdg.Mime.get_type(file_name) finally: os.remove(file_name) - if mime_type: - extension = sugar.mime.get_primary_extension(mime_type) + return self._mime_type_exts.get(mime_type) + + def _load_mime_type_exts(self): + """Return a heuristic mapping from MIME type to file name extension - return extension + Return a map from MIME type to the best guess for its primary + (preferred) file name extension. + + As most MIME type databases are not designed for this task, it's + just a crude heuristic that will be off even for common MIME + types. + """ + globs2_paths = list(xdg.BaseDirectory.load_data_paths( + os.path.join('mime', 'globs2'))) + rev_exts = {} + # System locations usually give a better estimate of the + # primary extension for a MIME type, so check them first. + for path in reversed(globs2_paths): + for line in open(path): + line = line.strip() + if line.startswith('#') or not line: + continue + weight_, type_name, glob_pattern = line.split(':', 2) + if type_name in rev_exts: + # There's already a better match (globs2 is sorted + # by weight). + continue + if not glob_pattern.startswith('*.'): + continue + ext = glob_pattern[2:] + if '*' in ext or '[' in ext: + continue + rev_exts[type_name] = ext + + return rev_exts def safe_name(name): |