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.1KB

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