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:12:45 (GMT) |
commit | 4b9958ec20e6da3c77ae3860bb070e504b926ac3 (patch) | |
tree | 6ab54e8a1f704191a88861616c2325549401b45f | |
parent | 75fb8e0f3d8717c60090e87274dc3c124a4ae873 (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 2a73310..8e89fef 100644 --- a/fsemulation.py +++ b/fsemulation.py @@ -26,8 +26,8 @@ import time import dbus import xapian - -import sugar.mime +import xdg.BaseDirectory +import xdg.Mime DS_DBUS_SERVICE = 'org.laptop.sugar.DataStore' @@ -988,6 +988,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) @@ -1092,20 +1093,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): |