glasswall.content_management.config_elements.config_element
1from typing import Optional, Union 2 3import glasswall 4from glasswall.content_management.errors.switches import SwitchNotFound 5from glasswall.content_management.switches import Switch 6 7 8class ConfigElement: 9 """ A Content Management Policy configuration element which has a name, and can have attributes, switches, and subelements. """ 10 11 def __init__(self, 12 name: str, 13 attributes: dict = {}, 14 switches: list = [], 15 subelements: list = [], 16 default: Optional[str] = None, 17 default_switches: list = [], 18 config: dict = {}, 19 switches_module: "glasswall.content_management.switches" = Switch 20 ): 21 self._indent = 0 22 self.name = name 23 self.attributes = attributes or {} 24 self.switches = switches or [] 25 self.subelements = subelements or [] 26 self.default = default 27 self.default_switches = default_switches or [] 28 self.config = config or {} 29 self.switches_module = switches_module 30 31 # Add default switches 32 for switch in self.default_switches: 33 self.add_switch(switch) 34 35 # Add customised switches provided in `config` 36 for switch_name, switch_value in self.config.items(): 37 # If switch is in switches_module, add it to this config element 38 if hasattr(self.switches_module, switch_name): 39 self.add_switch(getattr( 40 self.switches_module, 41 switch_name 42 )(value=switch_value)) 43 44 # Otherwise, create a new Switch and add it 45 else: 46 self.add_switch(Switch(name=switch_name, value=switch_value)) 47 48 # Sort self.switches by .name and .value 49 self.switches.sort() 50 51 def __str__(self): 52 return self.text 53 54 def __getattr__(self, name): 55 # Try to return matching Switch from nonexistant attribute 56 switch = next(iter(s for s in self.switches if s.name == name), None) 57 58 if switch: 59 return switch 60 61 # or matching subelement from a WordSearch textSearchConfig 62 if isinstance(self, glasswall.content_management.config_elements.textSearchConfig): 63 subelement = next(iter(s for s in self.subelements if s.name == name), None) 64 if subelement: 65 return subelement 66 67 raise AttributeError(name) 68 69 def __repr__(self): 70 """ Change string representation of object. """ 71 return f'ConfigElement("{self.name}")' 72 73 def __lt__(self, other): 74 """ Used for sorting. Sort by "name" then "switches". """ 75 return (self.name.lower(), self.switches,) < (other.name.lower(), other.switches,) 76 77 @property 78 def text(self): 79 """ String representation of XML. """ 80 indent = " " * 4 * self._indent 81 82 string = f"{indent}<{self.name}" 83 # Sort attributes by lowercase key, lowercase value 84 for k, v in sorted(self.attributes.items(), key=lambda kv: (str(kv[0]).lower(), str(kv[1]).lower())): 85 string += f' {k}="{v}"' 86 string += ">" 87 for subelement in self.subelements: 88 subelement._indent = self._indent + 1 89 string += f"\n{subelement.text}" 90 for switch in self.switches: 91 switch._indent = self._indent + 1 92 string += f"\n{switch.text}" 93 string += f"\n{indent}</{self.name}>" 94 95 return string 96 97 def get_switch_names(self): 98 """ Returns a sorted list of unique Switch.name values from self.switches. """ 99 return sorted(set(switch.name for switch in self.switches)) 100 101 def remove_switch(self, switch: Union[Switch, str]): 102 """ Removes all Switch instances from self.switches that match arg "switch". 103 104 Args: 105 switch (Union[Switch, str]): A Switch instance or str to match Switch.name. 106 107 Returns: 108 self 109 110 Raises: 111 glasswall.content_management.errors.switches.SwitchNotFound: The switch was not found. 112 """ 113 if isinstance(switch, Switch): 114 # If switch is not in self.switches, raise error. 115 if switch not in self.switches: 116 raise SwitchNotFound(switch) 117 118 while switch in self.switches: 119 # Remove arg "switch" from self.switches using the builtin list .remove method 120 self.switches.remove(switch) 121 122 elif isinstance(switch, str): 123 # If no Switch in self.switches has a .name matching arg "switch", raise error. 124 switch_names = self.get_switch_names() 125 if switch not in switch_names: 126 raise SwitchNotFound(f"'{switch}' not in {switch_names}") 127 128 # Recreate self.switches, excluding all switches with the same .name as arg "switch" 129 self.switches = [f for f in self.switches if f.name != switch] 130 131 else: 132 raise TypeError(switch) 133 134 def add_switch(self, switch: Switch, replace: bool = True): 135 """ Adds a Switch instance to self.switches. 136 137 Args: 138 switch (Switch): A Switch instance. 139 replace (bool, optional): Default True. Deletes any pre-existing Switch with the same .name attribute in self.switches. 140 141 Returns: 142 self 143 """ 144 if replace: 145 # Recreate self.switches, excluding all switches with the same .name as arg "switch" 146 self.switches = [f for f in self.switches if f.name != switch.name] 147 148 # Append the new switch 149 self.switches.append(switch) 150 151 # Sort self.switches by .name 152 self.switches.sort() 153 154 return self
class
ConfigElement:
11class ConfigElement: 12 """ A Content Management Policy configuration element which has a name, and can have attributes, switches, and subelements. """ 13 14 def __init__(self, 15 name: str, 16 attributes: dict = {}, 17 switches: list = [], 18 subelements: list = [], 19 default: Optional[str] = None, 20 default_switches: list = [], 21 config: dict = {}, 22 switches_module: "glasswall.content_management.switches" = Switch 23 ): 24 self._indent = 0 25 self.name = name 26 self.attributes = attributes or {} 27 self.switches = switches or [] 28 self.subelements = subelements or [] 29 self.default = default 30 self.default_switches = default_switches or [] 31 self.config = config or {} 32 self.switches_module = switches_module 33 34 # Add default switches 35 for switch in self.default_switches: 36 self.add_switch(switch) 37 38 # Add customised switches provided in `config` 39 for switch_name, switch_value in self.config.items(): 40 # If switch is in switches_module, add it to this config element 41 if hasattr(self.switches_module, switch_name): 42 self.add_switch(getattr( 43 self.switches_module, 44 switch_name 45 )(value=switch_value)) 46 47 # Otherwise, create a new Switch and add it 48 else: 49 self.add_switch(Switch(name=switch_name, value=switch_value)) 50 51 # Sort self.switches by .name and .value 52 self.switches.sort() 53 54 def __str__(self): 55 return self.text 56 57 def __getattr__(self, name): 58 # Try to return matching Switch from nonexistant attribute 59 switch = next(iter(s for s in self.switches if s.name == name), None) 60 61 if switch: 62 return switch 63 64 # or matching subelement from a WordSearch textSearchConfig 65 if isinstance(self, glasswall.content_management.config_elements.textSearchConfig): 66 subelement = next(iter(s for s in self.subelements if s.name == name), None) 67 if subelement: 68 return subelement 69 70 raise AttributeError(name) 71 72 def __repr__(self): 73 """ Change string representation of object. """ 74 return f'ConfigElement("{self.name}")' 75 76 def __lt__(self, other): 77 """ Used for sorting. Sort by "name" then "switches". """ 78 return (self.name.lower(), self.switches,) < (other.name.lower(), other.switches,) 79 80 @property 81 def text(self): 82 """ String representation of XML. """ 83 indent = " " * 4 * self._indent 84 85 string = f"{indent}<{self.name}" 86 # Sort attributes by lowercase key, lowercase value 87 for k, v in sorted(self.attributes.items(), key=lambda kv: (str(kv[0]).lower(), str(kv[1]).lower())): 88 string += f' {k}="{v}"' 89 string += ">" 90 for subelement in self.subelements: 91 subelement._indent = self._indent + 1 92 string += f"\n{subelement.text}" 93 for switch in self.switches: 94 switch._indent = self._indent + 1 95 string += f"\n{switch.text}" 96 string += f"\n{indent}</{self.name}>" 97 98 return string 99 100 def get_switch_names(self): 101 """ Returns a sorted list of unique Switch.name values from self.switches. """ 102 return sorted(set(switch.name for switch in self.switches)) 103 104 def remove_switch(self, switch: Union[Switch, str]): 105 """ Removes all Switch instances from self.switches that match arg "switch". 106 107 Args: 108 switch (Union[Switch, str]): A Switch instance or str to match Switch.name. 109 110 Returns: 111 self 112 113 Raises: 114 glasswall.content_management.errors.switches.SwitchNotFound: The switch was not found. 115 """ 116 if isinstance(switch, Switch): 117 # If switch is not in self.switches, raise error. 118 if switch not in self.switches: 119 raise SwitchNotFound(switch) 120 121 while switch in self.switches: 122 # Remove arg "switch" from self.switches using the builtin list .remove method 123 self.switches.remove(switch) 124 125 elif isinstance(switch, str): 126 # If no Switch in self.switches has a .name matching arg "switch", raise error. 127 switch_names = self.get_switch_names() 128 if switch not in switch_names: 129 raise SwitchNotFound(f"'{switch}' not in {switch_names}") 130 131 # Recreate self.switches, excluding all switches with the same .name as arg "switch" 132 self.switches = [f for f in self.switches if f.name != switch] 133 134 else: 135 raise TypeError(switch) 136 137 def add_switch(self, switch: Switch, replace: bool = True): 138 """ Adds a Switch instance to self.switches. 139 140 Args: 141 switch (Switch): A Switch instance. 142 replace (bool, optional): Default True. Deletes any pre-existing Switch with the same .name attribute in self.switches. 143 144 Returns: 145 self 146 """ 147 if replace: 148 # Recreate self.switches, excluding all switches with the same .name as arg "switch" 149 self.switches = [f for f in self.switches if f.name != switch.name] 150 151 # Append the new switch 152 self.switches.append(switch) 153 154 # Sort self.switches by .name 155 self.switches.sort() 156 157 return self
A Content Management Policy configuration element which has a name, and can have attributes, switches, and subelements.
ConfigElement( name: str, attributes: dict = {}, switches: list = [], subelements: list = [], default: Optional[str] = None, default_switches: list = [], config: dict = {}, switches_module: <module 'glasswall.content_management.switches' from '/home/vsts/work/1/s/projects/gw-python-wrapper/glasswall/content_management/switches/__init__.py'> = <class 'glasswall.content_management.switches.switch.Switch'>)
14 def __init__(self, 15 name: str, 16 attributes: dict = {}, 17 switches: list = [], 18 subelements: list = [], 19 default: Optional[str] = None, 20 default_switches: list = [], 21 config: dict = {}, 22 switches_module: "glasswall.content_management.switches" = Switch 23 ): 24 self._indent = 0 25 self.name = name 26 self.attributes = attributes or {} 27 self.switches = switches or [] 28 self.subelements = subelements or [] 29 self.default = default 30 self.default_switches = default_switches or [] 31 self.config = config or {} 32 self.switches_module = switches_module 33 34 # Add default switches 35 for switch in self.default_switches: 36 self.add_switch(switch) 37 38 # Add customised switches provided in `config` 39 for switch_name, switch_value in self.config.items(): 40 # If switch is in switches_module, add it to this config element 41 if hasattr(self.switches_module, switch_name): 42 self.add_switch(getattr( 43 self.switches_module, 44 switch_name 45 )(value=switch_value)) 46 47 # Otherwise, create a new Switch and add it 48 else: 49 self.add_switch(Switch(name=switch_name, value=switch_value)) 50 51 # Sort self.switches by .name and .value 52 self.switches.sort()
text
80 @property 81 def text(self): 82 """ String representation of XML. """ 83 indent = " " * 4 * self._indent 84 85 string = f"{indent}<{self.name}" 86 # Sort attributes by lowercase key, lowercase value 87 for k, v in sorted(self.attributes.items(), key=lambda kv: (str(kv[0]).lower(), str(kv[1]).lower())): 88 string += f' {k}="{v}"' 89 string += ">" 90 for subelement in self.subelements: 91 subelement._indent = self._indent + 1 92 string += f"\n{subelement.text}" 93 for switch in self.switches: 94 switch._indent = self._indent + 1 95 string += f"\n{switch.text}" 96 string += f"\n{indent}</{self.name}>" 97 98 return string
String representation of XML.
def
get_switch_names(self):
100 def get_switch_names(self): 101 """ Returns a sorted list of unique Switch.name values from self.switches. """ 102 return sorted(set(switch.name for switch in self.switches))
Returns a sorted list of unique Switch.name values from self.switches.
104 def remove_switch(self, switch: Union[Switch, str]): 105 """ Removes all Switch instances from self.switches that match arg "switch". 106 107 Args: 108 switch (Union[Switch, str]): A Switch instance or str to match Switch.name. 109 110 Returns: 111 self 112 113 Raises: 114 glasswall.content_management.errors.switches.SwitchNotFound: The switch was not found. 115 """ 116 if isinstance(switch, Switch): 117 # If switch is not in self.switches, raise error. 118 if switch not in self.switches: 119 raise SwitchNotFound(switch) 120 121 while switch in self.switches: 122 # Remove arg "switch" from self.switches using the builtin list .remove method 123 self.switches.remove(switch) 124 125 elif isinstance(switch, str): 126 # If no Switch in self.switches has a .name matching arg "switch", raise error. 127 switch_names = self.get_switch_names() 128 if switch not in switch_names: 129 raise SwitchNotFound(f"'{switch}' not in {switch_names}") 130 131 # Recreate self.switches, excluding all switches with the same .name as arg "switch" 132 self.switches = [f for f in self.switches if f.name != switch] 133 134 else: 135 raise TypeError(switch)
Removes all Switch instances from self.switches that match arg "switch".
Args: switch (Union[Switch, str]): A Switch instance or str to match Switch.name.
Returns: self
Raises: glasswall.content_management.errors.switches.SwitchNotFound: The switch was not found.
def
add_switch( self, switch: glasswall.content_management.switches.switch.Switch, replace: bool = True):
137 def add_switch(self, switch: Switch, replace: bool = True): 138 """ Adds a Switch instance to self.switches. 139 140 Args: 141 switch (Switch): A Switch instance. 142 replace (bool, optional): Default True. Deletes any pre-existing Switch with the same .name attribute in self.switches. 143 144 Returns: 145 self 146 """ 147 if replace: 148 # Recreate self.switches, excluding all switches with the same .name as arg "switch" 149 self.switches = [f for f in self.switches if f.name != switch.name] 150 151 # Append the new switch 152 self.switches.append(switch) 153 154 # Sort self.switches by .name 155 self.switches.sort() 156 157 return self
Adds a Switch instance to self.switches.
Args: switch (Switch): A Switch instance. replace (bool, optional): Default True. Deletes any pre-existing Switch with the same .name attribute in self.switches.
Returns: self