Javi Merino | aace7c0 | 2015-08-10 14:10:47 +0100 | [diff] [blame^] | 1 | # Copyright 2015-2015 ARM Limited |
Kapileshwar Singh | fb8fa1a | 2015-02-11 17:21:04 +0000 | [diff] [blame] | 2 | # |
Javi Merino | aace7c0 | 2015-08-10 14:10:47 +0100 | [diff] [blame^] | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
| 15 | |
Kapileshwar Singh | fb8fa1a | 2015-02-11 17:21:04 +0000 | [diff] [blame] | 16 | |
| 17 | """The idea is to create a wrapper class that |
| 18 | returns a Type of a Class dynamically created based |
| 19 | on the input parameters. Similar to a factory design |
| 20 | pattern |
| 21 | """ |
| 22 | from cr2.base import Base |
| 23 | import re |
| 24 | from cr2.run import Run |
| 25 | |
| 26 | |
Javi Merino | 6f34d90 | 2015-02-21 11:39:09 +0000 | [diff] [blame] | 27 | def default_init(self): |
Kapileshwar Singh | fb8fa1a | 2015-02-11 17:21:04 +0000 | [diff] [blame] | 28 | """Default Constructor for the |
| 29 | Dynamic MetaClass |
| 30 | """ |
| 31 | |
| 32 | super(type(self), self).__init__( |
Kapileshwar Singh | fb8fa1a | 2015-02-11 17:21:04 +0000 | [diff] [blame] | 33 | unique_word=self.unique_word, |
Kapileshwar Singh | 7a70913 | 2015-03-27 17:45:22 +0000 | [diff] [blame] | 34 | parse_raw=self.parse_raw |
Kapileshwar Singh | fb8fa1a | 2015-02-11 17:21:04 +0000 | [diff] [blame] | 35 | ) |
| 36 | |
| 37 | |
| 38 | class DynamicTypeFactory(type): |
| 39 | |
| 40 | """Override the type class to create |
| 41 | a dynamic type on the fly |
| 42 | """ |
| 43 | |
| 44 | def __new__(mcs, name, bases, dct): |
| 45 | """Override the new method""" |
| 46 | return type.__new__(mcs, name, bases, dct) |
| 47 | |
| 48 | def __init__(cls, name, bases, dct): |
| 49 | """Override the constructor""" |
| 50 | super(DynamicTypeFactory, cls).__init__(name, bases, dct) |
| 51 | |
| 52 | |
| 53 | def _get_name(name): |
| 54 | """Internal Method to Change camelcase to |
| 55 | underscores. CamelCase -> camel_case |
| 56 | """ |
| 57 | return re.sub('(?!^)([A-Z]+)', r'_\1', name).lower() |
| 58 | |
| 59 | |
Kapileshwar Singh | 7a70913 | 2015-03-27 17:45:22 +0000 | [diff] [blame] | 60 | def register_dynamic(class_name, unique_word, scope="all", |
| 61 | parse_raw=False): |
Kapileshwar Singh | fb8fa1a | 2015-02-11 17:21:04 +0000 | [diff] [blame] | 62 | """Create a Dynamic Type and register |
| 63 | it with the cr2 Framework""" |
| 64 | |
| 65 | dyn_class = DynamicTypeFactory( |
| 66 | class_name, (Base,), { |
| 67 | "__init__": default_init, |
| 68 | "unique_word": unique_word, |
Kapileshwar Singh | 7a70913 | 2015-03-27 17:45:22 +0000 | [diff] [blame] | 69 | "name": _get_name(class_name), |
| 70 | "parse_raw" : parse_raw |
Kapileshwar Singh | fb8fa1a | 2015-02-11 17:21:04 +0000 | [diff] [blame] | 71 | } |
| 72 | ) |
| 73 | Run.register_class(dyn_class, scope) |
| 74 | return dyn_class |
| 75 | |
| 76 | |
| 77 | def register_class(cls): |
| 78 | """Register a new class implementation |
| 79 | Should be used when the class has |
| 80 | complex helper methods and does not |
| 81 | expect to use the default constructor |
| 82 | """ |
| 83 | |
| 84 | # Check the argspec of the class |
| 85 | Run.register_class(cls) |