def testJoin(self): self.assertEquals("/tmp/config/foo/bar", basedir.save_config_path("foo/bar")) self.assertEquals("/tmp/config/foo/bar", basedir.save_config_path("foo", "bar")) self.assertEquals("/tmp/share/foo/bar", basedir.save_data_path("foo/bar")) self.assertEquals("/tmp/share/foo/bar", basedir.save_data_path("foo", "bar")) self.assertEquals(["/tmp/share/foo/bar"], list(basedir.load_data_paths("foo/bar"))) self.assertEquals(["/tmp/share/foo/bar"], list(basedir.load_data_paths("foo", "bar"))) self.assertEquals(["/tmp/config/foo/bar"], list(basedir.load_config_paths("foo/bar"))) self.assertEquals(["/tmp/config/foo/bar"], list(basedir.load_config_paths("foo", "bar")))
def _cache_database(): global exts, globs, literals, _cache_uptodate _cache_uptodate = True exts = {} # Maps extensions to types globs = [] # List of (glob, type) pairs literals = {} # Maps liternal names to types def _import_glob_file(path): """Loads name matching information from a MIME directory.""" for line in file(path): if line.startswith('#'): continue line = line[:-1] type, pattern = line.split(':', 1) mtype = lookup(type) if pattern.startswith('*.'): rest = pattern[2:] if not ('*' in rest or '[' in rest or '?' in rest): exts[rest] = mtype continue if '*' in pattern or '[' in pattern or '?' in pattern: globs.append((pattern, mtype)) else: literals[pattern] = mtype for path in basedir.load_data_paths(os.path.join('mime', 'globs')): _import_glob_file(path) # Sort globs by length globs.sort(lambda a, b: cmp(len(b[0]), len(a[0])))
def testJoin(self): self.assertEquals('/tmp/config/foo/bar', basedir.save_config_path('foo/bar')) self.assertEquals('/tmp/config/foo/bar', basedir.save_config_path('foo', 'bar')) self.assertEquals('/tmp/share/foo/bar', basedir.save_data_path('foo/bar')) self.assertEquals('/tmp/share/foo/bar', basedir.save_data_path('foo', 'bar')) self.assertEquals(['/tmp/share/foo/bar'], list(basedir.load_data_paths('foo/bar'))) self.assertEquals(['/tmp/share/foo/bar'], list(basedir.load_data_paths('foo', 'bar'))) self.assertEquals(['/tmp/config/foo/bar'], list(basedir.load_config_paths('foo/bar'))) self.assertEquals(['/tmp/config/foo/bar'], list(basedir.load_config_paths('foo', 'bar')))
def _load(self): "Loads comment for current language. Use get_comment() instead." resource = os.path.join('mime', self.media, self.subtype + '.xml') for path in basedir.load_data_paths(resource): doc = minidom.parse(path) if doc is None: continue for comment in doc.documentElement.getElementsByTagNameNS(FREE_NS, 'comment'): lang = comment.getAttributeNS(XML_NAMESPACE, 'lang') or 'en' goodness = 1 + (lang in i18n.langs) if goodness > self._comment[0]: self._comment = (goodness, _get_node_data(comment)) if goodness == 2: return
def install_mime_info(application, package_file = None): """Copy 'package_file' as ~/.local/share/mime/packages/<application>.xml. If package_file is None, install <app_dir>/<application>.xml. If already installed, does nothing. May overwrite an existing file with the same name (if the contents are different)""" application += '.xml' if not package_file: package_file = os.path.join(rox.app_dir, application) new_data = file(package_file).read() # See if the file is already installed package_dir = os.path.join('mime', 'packages') resource = os.path.join(package_dir, application) for x in basedir.load_data_paths(resource): try: old_data = file(x).read() except: continue if old_data == new_data: return # Already installed global _cache_uptodate _cache_uptodate = False # Not already installed; add a new copy try: # Create the directory structure... new_file = os.path.join(basedir.save_data_path(package_dir), application) # Write the file... file(new_file, 'w').write(new_data) # Update the database... if os.path.isdir('/uri/0install/zero-install.sourceforge.net'): command = '/uri/0install/zero-install.sourceforge.net/bin/update-mime-database' else: command = 'update-mime-database' if os.spawnlp(os.P_WAIT, command, command, basedir.save_data_path('mime')): os.unlink(new_file) raise Exception(_("The '%s' command returned an error code!\n" \ "Make sure you have the freedesktop.org shared MIME package:\n" \ "http://www.freedesktop.org/standards/shared-mime-info.html") % command) except: rox.report_exception()