|
961 |
|
961 |
|
962 |
|
962 |
|
963 |
class AppManager(object): |
963 |
class AppManager(object): |
|
|
964 |
_locale = None |
964 |
_cache = [] |
965 |
_cache = [] |
965 |
_package_manager = None |
966 |
_package_manager = None |
966 |
_pickle_file = os.path.join(CACHE_DIR, '.apps.%(locale)s.pkl') |
967 |
_pickle_file = os.path.join(CACHE_DIR, '.apps.%(locale)s.pkl') |
|
968 |
|
969 |
|
969 |
@classmethod |
970 |
@classmethod |
970 |
def _invalidate_pickle_cache(cls): |
971 |
def _invalidate_pickle_cache(cls): |
971 |
pickle_pattern = re.sub(r'%\(.*?\).', '*', cls._pickle_file) |
972 |
if cls._pickle_file: |
972 |
for pickle_file in glob(os.path.join(CACHE_DIR, pickle_pattern)): |
973 |
pickle_pattern = re.sub(r'%\(.*?\).', '*', cls._pickle_file) |
|
|
974 |
for pickle_file in glob(pickle_pattern): |
975 |
try: |
976 |
os.unlink(pickle_file) |
977 |
except OSError: |
978 |
pass |
979 |
|
980 |
@classmethod |
981 |
def _get_pickle_cache_file(cls): |
982 |
if cls._pickle_file: |
983 |
return cls._pickle_file % {'locale': cls._locale} |
984 |
|
985 |
@classmethod |
986 |
def _save_pickle_cache(cls, cache): |
987 |
pickle_file = cls._get_pickle_cache_file() |
988 |
if pickle_file: |
973 |
try: |
989 |
try: |
974 |
os.unlink(pickle_file) |
990 |
with open(pickle_file, 'wb') as fd: |
975 |
except OSError: |
991 |
dump(cache, fd, 2) |
976 |
pass |
992 |
except (IOError, PickleError): |
|
|
993 |
return False |
994 |
else: |
995 |
return True |
977 |
|
996 |
|
978 |
@classmethod |
997 |
@classmethod |
|
|
998 |
def _load_pickle_cache(cls): |
999 |
pickle_file = cls._get_pickle_cache_file() |
1000 |
if pickle_file: |
1001 |
try: |
1002 |
with open(pickle_file, 'rb') as fd: |
1003 |
return load(fd) |
1004 |
except (IOError, PickleError): |
1005 |
return None |
1006 |
|
1007 |
@classmethod |
1008 |
def _relevant_ini_files(cls): |
1009 |
return glob(os.path.join(CACHE_DIR, '*.ini')) |
1010 |
|
1011 |
@classmethod |
1012 |
def _build_app_from_ini(cls, ini): |
1013 |
return cls._AppClass.from_ini(ini, locale=cls._locale) |
1014 |
|
1015 |
@classmethod |
979 |
def clear_cache(cls): |
1016 |
def clear_cache(cls): |
980 |
ucr_load() |
1017 |
ucr_load() |
981 |
cls._cache[:] = [] |
1018 |
cls._cache[:] = [] |
|
987 |
@classmethod |
1024 |
@classmethod |
988 |
def _get_every_single_app(cls): |
1025 |
def _get_every_single_app(cls): |
989 |
if not cls._cache: |
1026 |
if not cls._cache: |
990 |
locale = get_locale() or 'en' |
1027 |
cls._locale = get_locale() or 'en' |
991 |
pickle_file = cls._pickle_file % {'locale': locale} |
|
|
992 |
try: |
1028 |
try: |
993 |
with open(pickle_file, 'rb') as fd: |
1029 |
cached_apps = cls._load_pickle_cache() |
994 |
cls._cache = load(fd) |
1030 |
if cached_apps is not None: |
995 |
except (IOError, PickleError): |
1031 |
cls._cache = cached_apps |
996 |
for ini in glob(os.path.join(CACHE_DIR, '*.ini')): |
1032 |
app_logger.debug('Loaded %d apps from cache' % len(cls._cache)) |
997 |
app = cls._AppClass.from_ini(ini, locale=locale) |
|
|
998 |
if app is not None: |
999 |
cls._cache.append(app) |
1000 |
cls._cache.sort() |
1001 |
try: |
1002 |
with open(pickle_file, 'wb') as fd: |
1003 |
dump(cls._cache, fd, 2) |
1004 |
except (IOError, PickleError): |
1005 |
app_logger.warn('Unable to cache apps') |
1006 |
else: |
1033 |
else: |
1007 |
app_logger.debug('Saved %d apps into cache' % len(cls._cache)) |
1034 |
for ini in cls._relevant_ini_files(): |
1008 |
else: |
1035 |
app = cls._build_app_from_ini(ini) |
1009 |
app_logger.debug('Loaded %d apps from cache' % len(cls._cache)) |
1036 |
if app is not None: |
|
|
1037 |
cls._cache.append(app) |
1038 |
cls._cache.sort() |
1039 |
if cls._save_pickle_cache(cls._cache): |
1040 |
app_logger.debug('Saved %d apps into cache' % len(cls._cache)) |
1041 |
else: |
1042 |
app_logger.warn('Unable to cache apps') |
1043 |
finally: |
1044 |
cls._locale = None |
1010 |
return cls._cache |
1045 |
return cls._cache |
1011 |
|
1046 |
|
1012 |
@classmethod |
1047 |
@classmethod |