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.

json_backend.py 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. ## @package EditorialModel.backend.json_backend
  3. # @brief Handle json files
  4. #
  5. # Allows an editorial model to be loaded/saved in
  6. # json format
  7. import json
  8. import datetime
  9. from Lodel.utils.mlstring import MlString
  10. def date_cast(date):
  11. if len(date):
  12. return datetime.datetime(date)
  13. else:
  14. return None
  15. def int_or_none(i):
  16. if len(i):
  17. return int(i)
  18. else:
  19. return None
  20. ## Manages a Json file based backend structure
  21. class EmBackendJson(object):
  22. cast_methods = {
  23. 'uid': int,
  24. 'rank': int,
  25. 'class_id': int,
  26. 'fieldgroup_id': int,
  27. 'rel_to_type_id': int_or_none,
  28. 'rel_field_id': int_or_none,
  29. 'optional': bool,
  30. 'internal': bool,
  31. 'string': MlString.load,
  32. 'help_text': MlString.load,
  33. 'date_create': date_cast,
  34. 'date_update': date_cast,
  35. }
  36. ## Constructor
  37. #
  38. # @param json_file str: path to the json_file used as data source
  39. def __init__(self, json_file):
  40. with open(json_file) as json_data:
  41. self.data = json.loads(json_data.read())
  42. ## Loads the data in the data source json file
  43. #
  44. # @return list
  45. def load(self):
  46. data = {}
  47. for index, component in self.data.items():
  48. attributes = {}
  49. for attr_name, attr_value in component.items():
  50. if attr_name in EmBackendJson.cast_methods:
  51. attributes[attr_name] = EmBackendJson.cast_methods[attr_name](attr_value)
  52. else:
  53. attributes[attr_name] = attr_value
  54. data[int(index)] = attributes
  55. return data
  56. ## Saves the data in the data source json file
  57. #
  58. # @return bool
  59. # @todo à implémenter
  60. def save(self):
  61. return True