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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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(
  29. '<class str>, <class dict> or <class MlString> expected, but %s found' % type(arg))
  30. # @brief Return a translation given a lang
  31. # @param lang str | None : If None return default lang translation
  32. def get(self, lang=None):
  33. lang = self.__default_lang if lang is None else lang
  34. if not self.lang_is_valid(lang):
  35. raise ValueError("Invalid lang : '%s'" % lang)
  36. elif lang in self.values:
  37. return self.values[lang]
  38. else:
  39. return str(self)
  40. # @brief Set a translation
  41. # @param lang str : the lang
  42. # @param val str | None: the translation if None delete the translation
  43. def set(self, lang, val):
  44. if not self.lang_is_valid(lang):
  45. raise ValueError("Invalid lang : '%s'" % lang)
  46. if not isinstance(val, str) and val is not None:
  47. raise ValueError("Expected a <class str> as value but got %s" % type(val))
  48. if val is None:
  49. del(self.values[lang])
  50. else:
  51. self.values[lang] = val
  52. # @brief Checks that given lang is valid
  53. # @param lang str : the lang
  54. @classmethod
  55. def lang_is_valid(cls, lang):
  56. if not isinstance(lang, str):
  57. raise ValueError('Invalid value for lang. Str expected but %s found' % type(lang))
  58. return lang in cls.langs
  59. # @brief Get or set the default lang
  60. @classmethod
  61. def default_lang(cls, lang=None):
  62. if lang is None:
  63. return cls.__default_lang
  64. if not cls.lang_is_valid(lang):
  65. raise ValueError('lang "%s" is not valid"' % lang)
  66. cls.__default_lang = lang
  67. # @brief Return a mlstring loaded from a json string
  68. # @param json_str str : Json string
  69. @classmethod
  70. def from_json(cls, json_str):
  71. return MlString(json.loads(json_str))
  72. def __hash__(self):
  73. res = ''
  74. for lang in sorted(list(self.values.keys())):
  75. res = hash((res, lang, self.values[lang]))
  76. return res
  77. def d_hash(self):
  78. m = hashlib.md5()
  79. for lang in sorted(list(self.values.keys())):
  80. m.update(bytes(lang + ";" + self.values[lang], 'utf-8'))
  81. return int.from_bytes(m.digest(), byteorder='big')
  82. def __eq__(self, a):
  83. return hash(self) == hash(a)
  84. # @return The default langage translation or any available translation
  85. def __str__(self):
  86. if self.__default_lang in self.values:
  87. return self.values[self.__default_lang]
  88. else:
  89. return self.values[list(self.values.keys())[0]]
  90. def __repr__(self):
  91. return repr(self.values)