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

import os 

import django 

from django.conf import settings 

from rosetta.conf import settings as rosetta_settings 

from django.core.cache import cache 

from datetime import datetime 

try: 

    from django.utils import timezone 

except: 

    timezone = None 

 

 

try: 

    set 

except NameError: 

    from sets import Set as set   # Python 2.3 fallback 

 

 

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() 

27    if timezone is None: 

        return dt.strftime('%Y-%m-%d %H:%M%z') 

    if not dt.tzinfo: 

        tz = timezone.get_current_timezone() 

        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: 

51        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'))) 

55        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') 

58        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 

67    for localepath in settings.LOCALE_PATHS: 

        if os.path.isdir(localepath): 

            paths.append(localepath) 

 

    # project/app/locale 

    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], {}, {}, [appname[p + 1:]]), appname[p + 1:]) 

        else: 

            app = __import__(appname, {}, {}, []) 

 

        apppath = os.path.normpath(os.path.abspath(os.path.join(os.path.dirname(app.__file__), 'locale'))) 

 

        # django apps 

        if 'contrib' in apppath and 'django' in apppath and not django_apps: 

            continue 

 

        # third party external 

        if not third_party_apps and abs_project_path not in apppath: 

            continue 

 

        # local apps 

92        if not project_apps and abs_project_path in apppath: 

            continue 

 

71        if os.path.isdir(apppath): 

            paths.append(apppath) 

 

    ret = set() 

    langs = (lang,) 

100    if u'-' in lang: 

        _l, _c = map(lambda x: x.lower(), lang.split(u'-')) 

        langs += (u'%s_%s' % (_l, _c), u'%s_%s' % (_l, _c.upper()), ) 

103    elif u'_' in lang: 

        _l, _c = map(lambda x: x.lower(), lang.split(u'_')) 

        langs += (u'%s-%s' % (_l, _c), u'%s-%s' % (_l, _c.upper()), ) 

 

    paths = map(os.path.normpath, paths) 

    paths = list(set(paths)) 

    for path in paths: 

        for lang_ in langs: 

            dirname = os.path.join(path, lang_, 'LC_MESSAGES') 

            for fn in ('django.po', 'djangojs.po',): 

                filename = os.path.join(dirname, fn) 

                if os.path.isfile(filename): 

                    ret.add(os.path.abspath(filename)) 

    return list(ret) 

 

 

def pagination_range(first, last, current): 

    r = [] 

 

    r.append(first) 

    if first + 1 < last: 

        r.append(first + 1) 

 

    if current - 2 > first and current - 2 < last: 

        r.append(current - 2) 

    if current - 1 > first and current - 1 < last: 

        r.append(current - 1) 

    if current > first and current < last: 

        r.append(current) 

    if current + 1 < last and current + 1 > first: 

        r.append(current + 1) 

    if current + 2 < last and current + 2 > first: 

        r.append(current + 2) 

 

    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