import locale
+if locale.getlocale()[0] is None:
+ locale.setlocale(locale.LC_COLLATE, '')
+
import collate.errors
try:
pass
collator = None
+preferred_locale = None
+
+def _get_collator(backend, locale_code):
+ for code in [locale_code,
+ locale_code.split("_")[0],
+ locale.getlocale(locale.LC_COLLATE)[0],
+ locale.getlocale(locale.LC_COLLATE)[0].split("_")[0],
+ locale.getdefaultlocale()[0],
+ locale.getdefaultlocale()[0].split("_")[0],
+ None]:
+ try:
+ return default.Collator(code)
+ except collate.errors.InvalidLocaleError:
+ pass
def set_locale(locale_code):
global collator
+ global preferred_locale
+ preferred_locale = locale_code
if collator is None or collator.locale != locale_code:
- for code in [locale_code,
- locale_code.split("_")[0],
- locale.getdefaultlocale()[0],
- locale.getdefaultlocale()[0].split("_")[0],
- None]:
- try:
- collator = default.Collator(code)
- except collate.errors.InvalidLocaleError:
- pass
- else:
- break
+ c = _get_collator(default, locale_code)
+ if c is not None:
+ collator = c
else:
raise collate.errors.InvalidLocaleError(locale_code)
-def get_locale():
- return collator.locale
-
def set_backend(backend):
- pass
+ global collator
+ global default
+
+ c = _get_collator(backend, preferred_locale)
+ if c is not None:
+ collator = c
+ default = backend
+ else:
+ raise collate.errors.InvalidLocaleError(locale_code)
def key(string):
return collator.key(string)
def cmp(string1, string2):
return collator.cmp(string1, string2)
-set_locale(locale.getdefaultlocale()[0])
+try:
+ set_locale(locale.getlocale()[0])
+except collate.errors.InvalidLocaleError:
+ # There's no way this should fail unless the C locale system is
+ # fucked or missing all data.
+ import collator.syslocale
+ set_backend(collator.syslocale)
+ set_locale(locale.getlocale(locale.LC_COLLATE)[0])
-import sys
import locale
+
import collate.errors
import collate._abcollator
class Collator(collate._abcollator.Collator):
- def __init__(self, locale_code, strict=False):
+ def __init__(self, locale_code):
+ default = locale.getdefaultlocale()[0]
+ for locale in [locale_code, default]:
+ try:
+ locale.setlocale(locale.LC_COLLATE, locale_code)
+ except locale.Error as err:
+ pass
+ else:
+ break
+ else:
+ raise collate.errors.InvalidLocaleError("no locale found")
+ self.locale = locale.getlocale()[0]
try:
self.__encoding = locale_code.split(".")[1]
except IndexError:
- self.__encoding = locale_code.split(sys.getdefaultencoding())
- try:
- locale.setlocale(locale.LC_COLLATE, locale_code)
- except locale.Error as err:
- if strict:
- raise collate.errors.InvalidLocaleError(str(err))
- try:
- locale.setlocale(
- locale.LC_COLLATE, locale.getdefaultlocale()[0])
- except locale.Error as err:
- raise collate.errors.InvalidLocaleError(str(err))
- self.locale = locale.getdefaultlocale()[0]
+ self.__encoding = locale_code.split(locale.getpreferredencoding())
def key(self, string):
try:
--- /dev/null
+#!/usr/bin/env python
+
+import os
+import sys
+import locale
+
+import collate
+
+def main(argv):
+ lines = []
+ encoding = locale.getpreferredencoding()
+
+ if not argv:
+ argv.append("-")
+ for filename in argv:
+ if filename == "-":
+ fileobj = sys.stdin
+ else:
+ fileobj = open(filename, "rU")
+ for line in fileobj:
+ line = line.strip()
+ line = line.decode(encoding, "replace")
+ lines.append(line)
+ lines.sort(key=collate.key)
+
+ for line in lines:
+ print line.encode(encoding, "replace")
+
+if __name__ == "__main__":
+ try:
+ locale.setlocale(locale.LC_ALL, '')
+ except locale.Error:
+ pass
+
+ main(sys.argv[1:])