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.

139 lines
5.2 KiB

4 years ago
  1. #!/usr/bin/python
  2. # -- Content-Encoding: UTF-8 --
  3. """
  4. The configuration module.
  5. :copyright: Copyright 2018, Thomas Calmant
  6. :license: Apache License 2.0
  7. :version: 0.3.2
  8. ..
  9. Copyright 2018 Thomas Calmant
  10. Licensed under the Apache License, Version 2.0 (the "License");
  11. you may not use this file except in compliance with the License.
  12. You may obtain a copy of the License at
  13. http://www.apache.org/licenses/LICENSE-2.0
  14. Unless required by applicable law or agreed to in writing, software
  15. distributed under the License is distributed on an "AS IS" BASIS,
  16. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. See the License for the specific language governing permissions and
  18. limitations under the License.
  19. """
  20. import sys
  21. # ------------------------------------------------------------------------------
  22. # Module version
  23. __version_info__ = (0, 3, 2)
  24. __version__ = ".".join(str(x) for x in __version_info__)
  25. # Documentation strings format
  26. __docformat__ = "restructuredtext en"
  27. # ------------------------------------------------------------------------------
  28. class LocalClasses(dict):
  29. """
  30. Associates local classes with their names (used in the jsonclass module)
  31. """
  32. def add(self, cls, name=None):
  33. """
  34. Stores a local class
  35. :param cls: A class
  36. :param name: Custom name used in the __jsonclass__ attribute
  37. """
  38. self[name or cls.__name__] = cls
  39. # ------------------------------------------------------------------------------
  40. class Config(object):
  41. """
  42. This is pretty much used exclusively for the 'jsonclass'
  43. functionality... set use_jsonclass to False to turn it off.
  44. You can change serialize_method and ignore_attribute, or use
  45. the local_classes.add(class) to include "local" classes.
  46. """
  47. def __init__(self, version=2.0, content_type="application/json-rpc",
  48. user_agent=None, use_jsonclass=True,
  49. serialize_method='_serialize',
  50. ignore_attribute='_ignore',
  51. serialize_handlers=None):
  52. """
  53. Sets up a configuration of JSONRPClib
  54. :param version: JSON-RPC specification version
  55. :param content_type: HTTP content type header value
  56. :param user_agent: The HTTP request user agent
  57. :param use_jsonclass: Allow bean marshalling
  58. :param serialize_method: A string that references the method on a
  59. custom class object which is responsible for
  60. returning a tuple of the arguments and a dict
  61. of attributes.
  62. :param ignore_attribute: A string that references the attribute on a
  63. custom class object which holds strings and/or
  64. references of the attributes the class
  65. translator should ignore.
  66. :param serialize_handlers: A dictionary of dump handler functions by
  67. type for additional type support and for
  68. overriding dump of built-in types in utils
  69. """
  70. # JSON-RPC specification
  71. self.version = version
  72. # Change to False to keep __jsonclass__ entries raw.
  73. self.use_jsonclass = use_jsonclass
  74. # it SHOULD be 'application/json-rpc'
  75. # but MAY be 'application/json' or 'application/jsonrequest'
  76. self.content_type = content_type
  77. # Default user agent
  78. if user_agent is None:
  79. user_agent = 'jsonrpclib/{0} (Python {1})'.format(
  80. __version__,
  81. '.'.join(str(ver) for ver in sys.version_info[0:3]))
  82. self.user_agent = user_agent
  83. # The list of classes to use for jsonclass translation.
  84. self.classes = LocalClasses()
  85. # The serialize_method should be a string that references the
  86. # method on a custom class object which is responsible for
  87. # returning a tuple of the constructor arguments and a dict of
  88. # attributes.
  89. self.serialize_method = serialize_method
  90. # The ignore attribute should be a string that references the
  91. # attribute on a custom class object which holds strings and / or
  92. # references of the attributes the class translator should ignore.
  93. self.ignore_attribute = ignore_attribute
  94. # The list of serialize handler functions for jsonclass dump.
  95. # Used for handling additional types and overriding built-in types.
  96. # Functions are expected to have the same parameters as jsonclass dump
  97. # (possibility to call standard jsonclass dump function within).
  98. self.serialize_handlers = serialize_handlers or {}
  99. def copy(self):
  100. """
  101. Returns a shallow copy of this configuration bean
  102. :return: A shallow copy of this configuration
  103. """
  104. new_config = Config(self.version, self.content_type, self.user_agent,
  105. self.use_jsonclass, self.serialize_method,
  106. self.ignore_attribute, None)
  107. new_config.classes = self.classes.copy()
  108. new_config.serialize_handlers = self.serialize_handlers.copy()
  109. return new_config
  110. # Default configuration
  111. DEFAULT = Config()