|
| 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 | +
|
| 20 | +Base class is InputArgument. It defines the abstract interface to be |
| 21 | +implemented and offers a way to compute a KLV value through output(). |
| 22 | +Other classes that derive InputArgument directly will be referenced |
| 23 | +as default classes throughout the docstrings. |
| 24 | +
|
| 25 | +The default classes should not be used to instantiate arguments. |
| 26 | +If one wants to add another argument, a custom class should be derived |
| 27 | +from one of the default classes. |
| 28 | +''' |
| 29 | + |
| 30 | +import base64 |
| 31 | +import logging |
| 32 | + |
| 33 | + |
| 34 | +class InputArgument: |
| 35 | + '''Base class for any input argument that will be added to KLV. |
| 36 | +
|
| 37 | + The user will define its arguments as instances of InputArgument |
| 38 | + by setting the "type" attribute of ArgumentParser add_argument to |
| 39 | + an instance of a derived class. This means that all derived classes |
| 40 | + must accept an additional "arg" parameter in the constructor. In the |
| 41 | + end, the list of arguments will be parsed into objects derived from |
| 42 | + InputArgument (or default derived classes), which decouples the object |
| 43 | + creation from its processing. |
| 44 | +
|
| 45 | + Abstract methods: |
| 46 | + key: Should be overwritten by final classes to return a "magic number". |
| 47 | + length: Can be overwritten by default classes to specify a default value |
| 48 | + (e.g. int arguments with a default length value of 4); can also |
| 49 | + be overwritten by final classes to specify a custom value for a |
| 50 | + certain argument. |
| 51 | + encode: Should be overwritten to generate the correct bytes array from |
| 52 | + its internal value. |
| 53 | +
|
| 54 | + Main usage is to iterate over an iterable entity of InputArguments and call |
| 55 | + the output() method to generate the (K, L, V) tuple. Note that the output() |
| 56 | + method should not be implemented, since its a common functionality across |
| 57 | + all InputArgument classes. |
| 58 | + ''' |
| 59 | + |
| 60 | + def __init__(self): |
| 61 | + self.val = None |
| 62 | + |
| 63 | + def key(self): |
| 64 | + logging.error("key() should be implemented in derived classes.") |
| 65 | + |
| 66 | + def length(self): |
| 67 | + logging.error("length() should be implemented in derived classes.") |
| 68 | + |
| 69 | + def encode(self): |
| 70 | + logging.error("encode() should be implemented in derived classes.") |
| 71 | + |
| 72 | + def output(self): |
| 73 | + out = (self.key(), self.length(), self.encode()) |
| 74 | + logging.info("'{}' length: {}".format(type(self).__name__, self.length())) |
| 75 | + return out |
| 76 | + |
| 77 | + |
| 78 | +class IntArgument(InputArgument): |
| 79 | + |
| 80 | + def __init__(self, arg): |
| 81 | + super().__init__() |
| 82 | + self.val = int(arg, 0) |
| 83 | + |
| 84 | + def length(self): |
| 85 | + return 4 |
| 86 | + |
| 87 | + def encode(self): |
| 88 | + return self.val.to_bytes(self.length(), "little") |
| 89 | + |
| 90 | + |
| 91 | +class Base64Argument(InputArgument): |
| 92 | + |
| 93 | + def __init__(self, arg): |
| 94 | + super().__init__() |
| 95 | + self.val = base64.b64decode(arg) |
| 96 | + |
| 97 | + def length(self): |
| 98 | + return len(self.encode()) |
| 99 | + |
| 100 | + def encode(self): |
| 101 | + return base64.b64encode(self.val) |
| 102 | + |
| 103 | + |
| 104 | +class StrArgument(InputArgument): |
| 105 | + |
| 106 | + def __init__(self, arg): |
| 107 | + super().__init__() |
| 108 | + self.val = str(arg) |
| 109 | + |
| 110 | + def length(self): |
| 111 | + return len(self.encode()) |
| 112 | + |
| 113 | + def encode(self): |
| 114 | + return str.encode(self.val) |
| 115 | + |
| 116 | + def max_length(self): |
| 117 | + return 32 |
| 118 | + |
| 119 | + |
| 120 | +class FileArgument(InputArgument): |
| 121 | + |
| 122 | + def __init__(self, arg): |
| 123 | + super().__init__() |
| 124 | + with open(arg, "rb") as _file: |
| 125 | + self.val = _file.read() |
| 126 | + |
| 127 | + def length(self): |
| 128 | + return len(self.val) |
| 129 | + |
| 130 | + def encode(self): |
| 131 | + return self.val |
0 commit comments