No Description
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.

mlstring.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #-*- coding: utf-8 -*-
  2. import copy
  3. import hashlib
  4. import json
  5. ##@brief Stores multilangage string
  6. class MlString(object):
  7. __default_lang = 'eng'
  8. langs = [
  9. 'fre',
  10. 'eng',
  11. 'ger',
  12. 'esp',
  13. ]
  14. ##@brief Create a new MlString instance
  15. # @param arg str | dict : Can be a json string, a string or a dict. It could be also a MlString object
  16. def __init__(self, arg):
  17. self.values = dict()
  18. if isinstance(arg, str):
  19. self.values[self.__default_lang] = arg
  20. elif isinstance(arg, dict):
  21. for lang, value in arg.items():
  22. if not self.lang_is_valid(lang):
  23. raise ValueError('Invalid lang found in argument : "%s"' % lang)
  24. self.values[lang] = value
  25. elif isinstance(arg, MlString):
  26. self.values = copy.copy(arg.values)
  27. else:
  28. raise ValueError('<class str>, <class dict> or <class MlString> expected, but %s found' % type(arg))
  29. ##@brief Return a translation given a lang
  30. # @param lang str | None : If None return default lang translation
  31. def get(self, lang = None):
  32. lang = self.__default_lang if lang is None else lang
  33. if not self.lang_is_valid(lang):
  34. raise ValueError("Invalid lang : '%s'" % lang)
  35. elif lang in self.values:
  36. return self.values[lang]
  37. else:
  38. return str(self)
  39. ##@brief Set a translation
  40. # @param lang str : the lang
  41. # @param val str | None: the translation if None delete the translation
  42. def set(self, lang, val):
  43. if not self.lang_is_valid(lang):
  44. raise ValueError("Invalid lang : '%s'" % lang)
  45. if not isinstance(val, str) and val is not None:
  46. raise ValueError("Expected a <class str> as value but got %s" % type(val))
  47. if val is None:
  48. del(self.values[lang])
  49. else:
  50. self.values[lang] = val
  51. ##@brief Checks that given lang is valid
  52. # @param lang str : the lang
  53. @classmethod
  54. def lang_is_valid(cls, lang):
  55. if not isinstance(lang, str):
  56. raise ValueError('Invalid value for lang. Str expected but %s found' % type(lang))
  57. return lang in cls.langs
  58. ##@brief Get or set the default lang
  59. @classmethod
  60. def default_lang(cls, lang = None):
  61. if lang is None:
  62. return cls.__default_lang
  63. if not cls.lang_is_valid(lang):
  64. raise ValueError('lang "%s" is not valid"' % lang)
  65. cls.__default_lang = lang
  66. ##@brief Return a mlstring loaded from a json string
  67. # @param json_str str : Json string
  68. @classmethod
  69. def from_json(cls, json_str):
  70. return MlString(json.loads(json_str))
  71. def __hash__(self):
  72. res = ''
  73. for lang in sorted(list(self.values.keys())):
  74. res = hash((res, lang, self.values[lang]))
  75. return res
  76. def d_hash(self):
  77. m = hashlib.md5()
  78. for lang in sorted(list(self.values.keys())):
  79. m.update(bytes(lang+";"+self.values[lang], 'utf-8'))
  80. return int.from_bytes(m.digest(), byteorder='big')
  81. def __eq__(self, a):
  82. return hash(self) == hash(a)
  83. ## @return The default langage translation or any available translation
  84. def __str__(self):
  85. if self.__default_lang in self.values:
  86. return self.values[self.__default_lang]
  87. else:
  88. return self.values[list(self.values.keys())[0]]
  89. def __repr__(self):
  90. return repr(self.values)