|
1 | | -# Copyright (c) 2019-2020 AnimatedLEDStrip |
| 1 | +# Copyright (c) 2018-2021 AnimatedLEDStrip |
2 | 2 | # |
3 | | -# Permission is hereby granted, free of charge, to any person obtaining a copy |
4 | | -# of this software and associated documentation files (the "Software"), to deal |
5 | | -# in the Software without restriction, including without limitation the rights |
6 | | -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
7 | | -# copies of the Software, and to permit persons to whom the Software is |
8 | | -# furnished to do so, subject to the following conditions: |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
9 | 9 | # |
10 | | -# The above copyright notice and this permission notice shall be included in |
11 | | -# all copies or substantial portions of the Software. |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
12 | 12 | # |
13 | | -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
14 | | -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
15 | | -# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
16 | | -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
17 | | -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
18 | | -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
19 | | -# THE SOFTWARE. |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +# THE SOFTWARE. |
20 | 20 |
|
21 | | -import json |
22 | | -from typing import AnyStr |
| 21 | +from typing import List, Optional, Dict |
23 | 22 |
|
24 | | -from .param_usage import ParamUsage |
25 | | -from .utils import check_data_type |
| 23 | + |
| 24 | +class AnimationParameter(object): |
| 25 | + """Specifies an animation parameter that can be sent to an animation""" |
| 26 | + |
| 27 | + def __init__(self, name: str = '', description: str = '', default=None, data_type=None): |
| 28 | + self.name: str = name |
| 29 | + self.description: str = description |
| 30 | + self.default = default |
| 31 | + self.data_type = data_type |
| 32 | + |
| 33 | + def json_dict(self) -> Dict: |
| 34 | + return { |
| 35 | + 'type': 'AnimationParameter', |
| 36 | + 'name': self.name, |
| 37 | + 'description': self.description, |
| 38 | + 'default': self.default, |
| 39 | + } |
26 | 40 |
|
27 | 41 |
|
28 | 42 | class AnimationInfo(object): |
29 | 43 | """Stores information about an animation the server can run""" |
30 | 44 |
|
31 | | - def __init__(self): |
32 | | - self.name: str = '' |
33 | | - self.abbr: str = '' |
34 | | - self.description: str = '' |
35 | | - self.signature_file: str = '' |
36 | | - self.repetitive: bool = False |
37 | | - self.minimum_colors: int = 0 |
38 | | - self.unlimited_colors: bool = False |
39 | | - self.center: 'ParamUsage' = ParamUsage.NOTUSED |
40 | | - self.delay: 'ParamUsage' = ParamUsage.NOTUSED |
41 | | - self.direction: 'ParamUsage' = ParamUsage.NOTUSED |
42 | | - self.distance: 'ParamUsage' = ParamUsage.NOTUSED |
43 | | - self.spacing: 'ParamUsage' = ParamUsage.NOTUSED |
44 | | - self.delay_default: int = 50 |
45 | | - self.distance_default: int = -1 |
46 | | - self.spacing_default: int = 3 |
47 | | - |
48 | | - @classmethod |
49 | | - def from_json(cls, input_str: AnyStr) -> 'AnimationInfo': |
50 | | - """Create an AnimationInfo instance from a JSON representation""" |
51 | | - # Parse the JSON |
52 | | - input_json = json.loads(input_str[5:]) |
53 | | - |
54 | | - # Create a new AnimationInfo instance |
55 | | - new_instance = cls() |
56 | | - |
57 | | - # Get each property from the JSON, reverting to defaults if it can't be found |
58 | | - new_instance.name = input_json.get('name', new_instance.name) |
59 | | - new_instance.abbr = input_json.get('abbr', new_instance.abbr) |
60 | | - new_instance.description = input_json.get('description', new_instance.description) |
61 | | - new_instance.signature_file = input_json.get('signatureFile', new_instance.signature_file) |
62 | | - new_instance.repetitive = input_json.get('repetitive', new_instance.repetitive) |
63 | | - new_instance.minimum_colors = input_json.get('minimumColors', new_instance.minimum_colors) |
64 | | - new_instance.unlimited_colors = input_json.get('unlimitedColors', new_instance.unlimited_colors) |
65 | | - new_instance.center = ParamUsage[input_json.get('center', new_instance.center.name)] |
66 | | - new_instance.delay = ParamUsage[input_json.get('delay', new_instance.delay.name)] |
67 | | - new_instance.direction = ParamUsage[input_json.get('direction', new_instance.direction.name)] |
68 | | - new_instance.distance = ParamUsage[input_json.get('distance', new_instance.distance.name)] |
69 | | - new_instance.spacing = ParamUsage[input_json.get('spacing', new_instance.spacing.name)] |
70 | | - new_instance.delay_default = input_json.get('delayDefault', new_instance.delay_default) |
71 | | - new_instance.distance_default = input_json.get('distanceDefault', new_instance.distance_default) |
72 | | - new_instance.spacing_default = input_json.get('spacingDefault', new_instance.spacing_default) |
73 | | - |
74 | | - # Double check that everything has the right data type |
75 | | - new_instance.check_data_types() |
76 | | - |
77 | | - return new_instance |
78 | | - |
79 | | - def check_data_types(self) -> bool: |
80 | | - """Check that all parameter types are correct""" |
81 | | - good_types = True |
82 | | - |
83 | | - good_types = good_types and check_data_type('name', self.name, str) |
84 | | - good_types = good_types and check_data_type('abbr', self.abbr, str) |
85 | | - good_types = good_types and check_data_type('description', self.description, str) |
86 | | - good_types = good_types and check_data_type('signature_file', self.signature_file, str) |
87 | | - good_types = good_types and check_data_type('repetitive', self.repetitive, bool) |
88 | | - good_types = good_types and check_data_type('minimum_colors', self.minimum_colors, int) |
89 | | - good_types = good_types and check_data_type('unlimited_colors', self.unlimited_colors, bool) |
90 | | - good_types = good_types and check_data_type('center', self.center, ParamUsage) |
91 | | - good_types = good_types and check_data_type('delay', self.delay, ParamUsage) |
92 | | - good_types = good_types and check_data_type('direction', self.direction, ParamUsage) |
93 | | - good_types = good_types and check_data_type('distance', self.distance, ParamUsage) |
94 | | - good_types = good_types and check_data_type('spacing', self.spacing, ParamUsage) |
95 | | - good_types = good_types and check_data_type('delay_default', self.delay_default, int) |
96 | | - good_types = good_types and check_data_type('distance_default', self.distance_default, int) |
97 | | - good_types = good_types and check_data_type('spacing_default', self.spacing_default, int) |
98 | | - |
99 | | - return good_types |
| 45 | + def __init__(self, |
| 46 | + name: str = '', |
| 47 | + abbr: str = '', |
| 48 | + description: str = '', |
| 49 | + run_count_default: int = 0, |
| 50 | + minimum_colors: int = 0, |
| 51 | + unlimited_colors: bool = False, |
| 52 | + dimensionality: Optional[List[str]] = None, |
| 53 | + int_params: Optional[List[AnimationParameter]] = None, |
| 54 | + double_params: Optional[List[AnimationParameter]] = None, |
| 55 | + string_params: Optional[List[AnimationParameter]] = None, |
| 56 | + location_params: Optional[List[AnimationParameter]] = None, |
| 57 | + distance_params: Optional[List[AnimationParameter]] = None, |
| 58 | + rotation_params: Optional[List[AnimationParameter]] = None, |
| 59 | + equation_params: Optional[List[AnimationParameter]] = None): |
| 60 | + self.name: str = name |
| 61 | + self.abbr: str = abbr |
| 62 | + self.description: str = description |
| 63 | + self.run_count_default: int = run_count_default |
| 64 | + self.minimum_colors: int = minimum_colors |
| 65 | + self.unlimited_colors: bool = unlimited_colors |
| 66 | + |
| 67 | + if dimensionality is None: |
| 68 | + self.dimensionality: List[str] = [] |
| 69 | + else: |
| 70 | + self.dimensionality: List[str] = dimensionality |
| 71 | + |
| 72 | + if int_params is None: |
| 73 | + self.int_params: List[AnimationParameter] = [] |
| 74 | + else: |
| 75 | + self.int_params: List[AnimationParameter] = int_params |
| 76 | + |
| 77 | + if double_params is None: |
| 78 | + self.double_params: List[AnimationParameter] = [] |
| 79 | + else: |
| 80 | + self.double_params: List[AnimationParameter] = double_params |
| 81 | + |
| 82 | + if string_params is None: |
| 83 | + self.string_params: List[AnimationParameter] = [] |
| 84 | + else: |
| 85 | + self.string_params: List[AnimationParameter] = string_params |
| 86 | + |
| 87 | + if location_params is None: |
| 88 | + self.location_params: List[AnimationParameter] = [] |
| 89 | + else: |
| 90 | + self.location_params: List[AnimationParameter] = location_params |
| 91 | + |
| 92 | + if distance_params is None: |
| 93 | + self.distance_params: List[AnimationParameter] = [] |
| 94 | + else: |
| 95 | + self.distance_params: List[AnimationParameter] = distance_params |
| 96 | + |
| 97 | + if rotation_params is None: |
| 98 | + self.rotation_params: List[AnimationParameter] = [] |
| 99 | + else: |
| 100 | + self.rotation_params: List[AnimationParameter] = rotation_params |
| 101 | + |
| 102 | + if equation_params is None: |
| 103 | + self.equation_params: List[AnimationParameter] = [] |
| 104 | + else: |
| 105 | + self.equation_params: List[AnimationParameter] = equation_params |
| 106 | + |
| 107 | + def json_dict(self) -> Dict: |
| 108 | + return { |
| 109 | + 'name': self.name, |
| 110 | + 'abbr': self.abbr, |
| 111 | + 'description': self.description, |
| 112 | + 'runCountDefault': self.run_count_default, |
| 113 | + 'minimumColors': self.minimum_colors, |
| 114 | + 'unlimitedColors': self.unlimited_colors, |
| 115 | + 'dimensionality': self.dimensionality, |
| 116 | + 'intParams': self.int_params, |
| 117 | + "doubleParams": self.double_params, |
| 118 | + "stringParams": self.string_params, |
| 119 | + "locationParams": self.location_params, |
| 120 | + "distanceParams": self.distance_params, |
| 121 | + "rotationParams": self.rotation_params, |
| 122 | + "equationParams": self.equation_params, |
| 123 | + } |
0 commit comments