Sort the activities by mtime to ensure a stable ordering. Part of #2716

This commit is contained in:
Dan Winship 2007-08-16 10:36:20 -04:00
parent 4ad4fe9ec8
commit f876a63b0b

View File

@ -69,15 +69,15 @@ class BundleRegistry(gobject.GObject):
def __init__(self): def __init__(self):
gobject.GObject.__init__(self) gobject.GObject.__init__(self)
self._bundles = {} self._bundles = []
self._search_path = [] self._search_path = []
self._service_manager = _ServiceManager() self._service_manager = _ServiceManager()
def get_bundle(self, service_name): def get_bundle(self, service_name):
"""Returns an bundle given his service name""" """Returns an bundle given his service name"""
if self._bundles.has_key(service_name): for bundle in self._bundles:
return self._bundles[service_name] if bundle.get_service_name() == service_name:
else: return bundle
return None return None
def add_search_path(self, path): def add_search_path(self, path):
@ -86,20 +86,30 @@ class BundleRegistry(gobject.GObject):
self._scan_directory(path) self._scan_directory(path)
def __iter__(self): def __iter__(self):
return self._bundles.values().__iter__() return self._bundles.__iter__()
def _scan_directory(self, path): def _scan_directory(self, path):
if os.path.isdir(path): if not os.path.isdir(path):
return
# Sort by mtime to ensure a stable activity order
bundles = {}
for f in os.listdir(path): for f in os.listdir(path):
if not f.endswith('.activity'):
continue
bundle_dir = os.path.join(path, f) bundle_dir = os.path.join(path, f)
if os.path.isdir(bundle_dir) and \ if os.path.isdir(bundle_dir):
bundle_dir.endswith('.activity'): bundles[bundle_dir] = os.stat(bundle_dir).st_mtime
self.add_bundle(bundle_dir)
bundle_dirs = bundles.keys()
bundle_dirs.sort(lambda d1,d2: cmp(bundles[d1], bundles[d2]))
for dir in bundle_dirs:
self.add_bundle(dir)
def add_bundle(self, bundle_path): def add_bundle(self, bundle_path):
bundle = Bundle(bundle_path) bundle = Bundle(bundle_path)
if bundle.is_valid(): if bundle.is_valid():
self._bundles[bundle.get_service_name()] = bundle self._bundles.append(bundle)
self._service_manager.add(bundle) self._service_manager.add(bundle)
self.emit('bundle-added', bundle) self.emit('bundle-added', bundle)
return True return True
@ -108,7 +118,7 @@ class BundleRegistry(gobject.GObject):
def get_activities_for_type(self, mime_type): def get_activities_for_type(self, mime_type):
result = [] result = []
for bundle in self._bundles.values(): for bundle in self._bundles:
if bundle.get_mime_types() and mime_type in bundle.get_mime_types(): if bundle.get_mime_types() and mime_type in bundle.get_mime_types():
result.append(bundle) result.append(bundle)
return result return result