# Coverage for /Users/marco/Code/django-rosetta/rosetta/poutil.py : 82.35%

## 127 statements 112 run 15 missing 0 excluded 20 partial

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | from datetime import datetime from django.conf import settings from rosetta.conf import settings as rosetta_settings import django import os import inspect from django.apps import AppConfig, apps from django.utils import timezone from django.core.cache import caches cache = caches[rosetta_settings.ROSETTA_CACHE_NAME] def timestamp_with_timezone(dt=None): """ Return a timestamp with a timezone for the configured locale. If all else fails, consider localtime to be UTC. """ dt = dt or datetime.now() 19 ↛ 20 line 19 didn't jump to line 20, because the condition on line 19 was never true if timezone is None: return dt.strftime('%Y-%m-%d %H:%M%z') 21 ↛ 26 line 21 didn't jump to line 26, because the condition on line 21 was never false if not dt.tzinfo: tz = timezone.get_current_timezone() 23 ↛ 24 line 23 didn't jump to line 24, because the condition on line 23 was never true if not tz: tz = timezone.utc dt = dt.replace(tzinfo=timezone.get_current_timezone()) return dt.strftime("%Y-%m-%d %H:%M%z") def find_pos(lang, project_apps=True, django_apps=False, third_party_apps=False): """ scans a couple possible repositories of gettext catalogs for the given language code """ paths = [] # project/locale parts = settings.SETTINGS_MODULE.split('.') project = __import__(parts[0], {}, {}, []) abs_project_path = os.path.normpath(os.path.abspath(os.path.dirname(project.__file__))) if project_apps: 43 ↛ 45 line 43 didn't jump to line 45, because the condition on line 43 was never false if os.path.exists(os.path.abspath(os.path.join(os.path.dirname(project.__file__), 'locale'))): paths.append(os.path.abspath(os.path.join(os.path.dirname(project.__file__), 'locale'))) 45 ↛ 49 line 45 didn't jump to line 49, because the condition on line 45 was never false if os.path.exists(os.path.abspath(os.path.join(os.path.dirname(project.__file__), '..', 'locale'))): paths.append(os.path.abspath(os.path.join(os.path.dirname(project.__file__), '..', 'locale'))) # django/locale if django_apps: django_paths = cache.get('rosetta_django_paths') 51 ↛ 52 line 51 didn't jump to line 52, because the condition on line 51 was never true if django_paths is None: django_paths = [] for root, dirnames, filename in os.walk(os.path.abspath(os.path.dirname(django.__file__))): if 'locale' in dirnames: django_paths.append(os.path.join(root, 'locale')) continue cache.set('rosetta_django_paths', django_paths, 60 * 60) paths = paths + django_paths # settings for localepath in settings.LOCALE_PATHS: 61 ↛ 60 line 61 didn't jump to line 60, because the condition on line 61 was never false if os.path.isdir(localepath): paths.append(localepath) # project/app/locale has_appconfig = False for appname in settings.INSTALLED_APPS: if rosetta_settings.EXCLUDED_APPLICATIONS and appname in rosetta_settings.EXCLUDED_APPLICATIONS: continue p = appname.rfind('.') if p >= 0: app = getattr(__import__(appname[:p], {}, {}, [str(appname[p + 1:])]), appname[p + 1:]) else: app = __import__(appname, {}, {}, []) # For django 1.7, an imported INSTALLED_APP can be an AppConfig instead # of an app module. This code converts the AppConfig to its application # module. 77 ↛ 82 line 77 didn't jump to line 82, because the condition on line 77 was never false if django.VERSION[0:2] >= (1, 7): if inspect.isclass(app) and issubclass(app, AppConfig): has_appconfig = True continue app_path = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(app.__file__), 'locale'))) # django apps if 'contrib' in app_path and 'django' in app_path and not django_apps: continue # third party external if not third_party_apps and abs_project_path not in app_path: continue # local apps 93 ↛ 94 line 93 didn't jump to line 94, because the condition on line 93 was never true if not project_apps and abs_project_path in app_path: continue 96 ↛ 66 line 96 didn't jump to line 66, because the condition on line 96 was never false if os.path.isdir(app_path): paths.append(app_path) # Handling with AppConfigs is a wee messy, but we can simply get all the # app paths directly from apps object 101 ↛ 124 line 101 didn't jump to line 124, because the condition on line 101 was never false if has_appconfig: for app_ in apps.get_app_configs(): if rosetta_settings.EXCLUDED_APPLICATIONS and app_.name in rosetta_settings.EXCLUDED_APPLICATIONS: continue app_path = app_.path # django apps if 'contrib' in app_path and 'django' in app_path and not django_apps: continue # third party external if not third_party_apps and abs_project_path not in app_path: continue # local apps 116 ↛ 117 line 116 didn't jump to line 117, because the condition on line 116 was never true if not project_apps and abs_project_path in app_path: continue 119 ↛ 121 line 119 didn't jump to line 121, because the condition on line 119 was never false if os.path.exists(os.path.abspath(os.path.join(app_path, 'locale'))): paths.append(os.path.abspath(os.path.join(app_path, 'locale'))) if os.path.exists(os.path.abspath(os.path.join(app_path, '..', 'locale'))): paths.append(os.path.abspath(os.path.join(app_path, '..', 'locale'))) ret = set() langs = [lang, ] if u'-' in lang: _l, _c = map(lambda x: x.lower(), lang.split(u'-', 1)) langs += [u'%s_%s' % (_l, _c), u'%s_%s' % (_l, _c.upper()), u'%s_%s' % (_l, _c.capitalize())] elif u'_' in lang: _l, _c = map(lambda x: x.lower(), lang.split(u'_', 1)) langs += [u'%s-%s' % (_l, _c), u'%s-%s' % (_l, _c.upper()), u'%s_%s' % (_l, _c.capitalize())] paths = map(os.path.normpath, paths) paths = list(set(paths)) for path in paths: # Exclude paths if path not in rosetta_settings.ROSETTA_EXCLUDED_PATHS: for lang_ in langs: dirname = os.path.join(path, lang_, 'LC_MESSAGES') for fn in rosetta_settings.POFILENAMES: filename = os.path.join(dirname, fn) if os.path.isfile(filename): ret.add(os.path.abspath(filename)) return list(sorted(ret)) def pagination_range(first, last, current): r = [] r.append(first) 151 ↛ 154 line 151 didn't jump to line 154, because the condition on line 151 was never false if first + 1 < last: r.append(first + 1) 154 ↛ 155 line 154 didn't jump to line 155, because the condition on line 154 was never true if current - 2 > first and current - 2 < last: r.append(current - 2) 156 ↛ 157 line 156 didn't jump to line 157, because the condition on line 156 was never true if current - 1 > first and current - 1 < last: r.append(current - 1) 158 ↛ 159 line 158 didn't jump to line 159, because the condition on line 158 was never true if current > first and current < last: r.append(current) 160 ↛ 162 line 160 didn't jump to line 162, because the condition on line 160 was never false if current + 1 < last and current + 1 > first: r.append(current + 1) 162 ↛ 165 line 162 didn't jump to line 165, because the condition on line 162 was never false if current + 2 < last and current + 2 > first: r.append(current + 2) 165 ↛ 167 line 165 didn't jump to line 167, because the condition on line 165 was never false if last - 1 > first: r.append(last - 1) r.append(last) r = list(set(r)) r.sort() prev = 10000 for e in r[:]: if prev + 1 < e: try: r.insert(r.index(e), '...') except ValueError: pass prev = e return r