default.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/env python3
  2. #
  3. # Copyright (c) 2022 Project CHIP Authors
  4. # All rights reserved.
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. '''This file should contain default argument classes.
  19. Base class is InputArgument. It defines the abstract interface to be
  20. implemented and offers a way to compute a KLV value through output().
  21. Other classes that derive InputArgument directly will be referenced
  22. as default classes throughout the docstrings.
  23. The default classes should not be used to instantiate arguments.
  24. If one wants to add another argument, a custom class should be derived
  25. from one of the default classes.
  26. '''
  27. import base64
  28. import logging
  29. class InputArgument:
  30. '''Base class for any input argument that will be added to KLV.
  31. The user will define its arguments as instances of InputArgument
  32. by setting the "type" attribute of ArgumentParser add_argument to
  33. an instance of a derived class. This means that all derived classes
  34. must accept an additional "arg" parameter in the constructor. In the
  35. end, the list of arguments will be parsed into objects derived from
  36. InputArgument (or default derived classes), which decouples the object
  37. creation from its processing.
  38. Abstract methods:
  39. key: Should be overwritten by final classes to return a "magic number".
  40. length: Can be overwritten by default classes to specify a default value
  41. (e.g. int arguments with a default length value of 4); can also
  42. be overwritten by final classes to specify a custom value for a
  43. certain argument.
  44. encode: Should be overwritten to generate the correct bytes array from
  45. its internal value.
  46. Main usage is to iterate over an iterable entity of InputArguments and call
  47. the output() method to generate the (K, L, V) tuple. Note that the output()
  48. method should not be implemented, since its a common functionality across
  49. all InputArgument classes.
  50. '''
  51. def __init__(self):
  52. self.val = None
  53. def key(self):
  54. logging.error("key() should be implemented in derived classes.")
  55. def length(self):
  56. logging.error("length() should be implemented in derived classes.")
  57. def encode(self):
  58. logging.error("encode() should be implemented in derived classes.")
  59. def output(self):
  60. out = (self.key(), self.length(), self.encode())
  61. logging.info("'{}' length: {}".format(type(self).__name__, self.length()))
  62. return out
  63. class IntArgument(InputArgument):
  64. def __init__(self, arg):
  65. super().__init__()
  66. self.val = int(arg, 0)
  67. def length(self):
  68. return 4
  69. def encode(self):
  70. return self.val.to_bytes(self.length(), "little")
  71. class Base64Argument(InputArgument):
  72. def __init__(self, arg):
  73. super().__init__()
  74. self.val = base64.b64decode(arg)
  75. def length(self):
  76. return len(self.encode())
  77. def encode(self):
  78. return base64.b64encode(self.val)
  79. class StrArgument(InputArgument):
  80. def __init__(self, arg):
  81. super().__init__()
  82. self.val = str(arg)
  83. def length(self):
  84. return len(self.encode())
  85. def encode(self):
  86. return str.encode(self.val)
  87. def max_length(self):
  88. return 32
  89. class FileArgument(InputArgument):
  90. def __init__(self, arg):
  91. super().__init__()
  92. with open(arg, "rb") as _file:
  93. self.val = _file.read()
  94. def length(self):
  95. return len(self.val)
  96. def encode(self):
  97. return self.val