Forked mumble-django project from https://bitbucket.org/Svedrin/mumble-django
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.9 KiB

  1. # -*- coding: utf-8 -*-
  2. """
  3. * Copyright (C) 2010, Marco Bonetti <mbonetti@gmail.com>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. """
  23. import re, string, sys, os
  24. from django.conf import settings
  25. try:
  26. set
  27. except NameError:
  28. from sets import Set as set # Python 2.3 fallback
  29. def find_pos(lang, include_djangos = False, include_rosetta = False):
  30. """
  31. scans a couple possible repositories of gettext catalogs for the given
  32. language code
  33. """
  34. paths = []
  35. # project/locale
  36. parts = settings.SETTINGS_MODULE.split('.')
  37. project = __import__(parts[0], {}, {}, [])
  38. paths.append(os.path.join(os.path.dirname(project.__file__), 'locale'))
  39. # django/locale
  40. if include_djangos:
  41. paths.append(os.path.join(os.path.dirname(sys.modules[settings.__module__].__file__), 'locale'))
  42. # settings
  43. for localepath in settings.LOCALE_PATHS:
  44. if os.path.isdir(localepath):
  45. paths.append(localepath)
  46. # project/app/locale
  47. for appname in settings.INSTALLED_APPS:
  48. if 'rosetta' == appname and include_rosetta == False:
  49. continue
  50. p = appname.rfind('.')
  51. if p >= 0:
  52. app = getattr(__import__(appname[:p], {}, {}, [appname[p+1:]]), appname[p+1:])
  53. else:
  54. app = __import__(appname, {}, {}, [])
  55. apppath = os.path.join(os.path.dirname(app.__file__), 'locale')
  56. if os.path.isdir(apppath):
  57. paths.append(apppath)
  58. ret = set()
  59. rx=re.compile(r'(\w+)/../\1')
  60. langs = (lang,)
  61. if u'-' in lang:
  62. _l,_c = map(lambda x:x.lower(),lang.split(u'-'))
  63. langs += (u'%s_%s' %(_l, _c), u'%s_%s' %(_l, _c.upper()), )
  64. elif u'_' in lang:
  65. _l,_c = map(lambda x:x.lower(),lang.split(u'_'))
  66. langs += (u'%s-%s' %(_l, _c), u'%s-%s' %(_l, _c.upper()), )
  67. for path in paths:
  68. for lang_ in langs:
  69. dirname = rx.sub(r'\1', '%s/%s/LC_MESSAGES/' %(path,lang_))
  70. for fn in ('django.po','djangojs.po',):
  71. if os.path.isfile(dirname+fn):
  72. ret.add(os.path.abspath(dirname+fn))
  73. return list(ret)
  74. def pagination_range(first,last,current):
  75. r = []
  76. r.append(first)
  77. if first + 1 < last: r.append(first+1)
  78. if current -2 > first and current -2 < last: r.append(current-2)
  79. if current -1 > first and current -1 < last: r.append(current-1)
  80. if current > first and current < last: r.append(current)
  81. if current + 1 < last and current+1 > first: r.append(current+1)
  82. if current + 2 < last and current+2 > first: r.append(current+2)
  83. if last-1 > first: r.append(last-1)
  84. r.append(last)
  85. r = list(set(r))
  86. r.sort()
  87. prev = 10000
  88. for e in r[:]:
  89. if prev + 1 < e:
  90. try:
  91. r.insert(r.index(e), '...')
  92. except ValueError:
  93. pass
  94. prev = e
  95. return r