glasswall.content_management.switches.switch
1from typing import Optional 2 3from glasswall.content_management.errors.switches import RestrictedValue 4 5 6class Switch: 7 """ A Content Management Policy switch which has a name and a value, and can have attributes. """ 8 9 def __init__(self, name: str, value: str, attributes: Optional[dict] = None, restrict_values: Optional[list] = None): 10 self._indent = 0 11 self.restrict_values = restrict_values or [] 12 self.name = name 13 self.value = value or "" 14 self.attributes = attributes or {} 15 16 def __str__(self): 17 return self.text 18 19 def __repr__(self): 20 """ Change string representation of object. """ 21 return f'Switch("{self.name}", "{self.value}")' 22 23 def __lt__(self, other): 24 """ Used for sorting. Sort by "name" then "value". """ 25 return (self.name.lower(), self.value.lower(),) < (other.name.lower(), other.value.lower(),) 26 27 @property 28 def value(self): 29 return self._value 30 31 @value.setter 32 def value(self, value): 33 self._value = value 34 if self.restrict_values and value not in self.restrict_values: 35 raise RestrictedValue(f"{self.name} has an unexpected value: '{value}'. Its value is restricted to: {self.restrict_values}") 36 37 @property 38 def text(self): 39 """ String representation of XML. """ 40 indent = " " * 4 * self._indent 41 42 string = f"{indent}<{self.name}" 43 # Sort attributes by lowercase key, lowercase value 44 for k, v in sorted(self.attributes.items(), key=lambda kv: (kv[0].lower(), kv[1].lower())): 45 string += f' {k}="{v}"' 46 string += f">{self.value}</{self.name}>" 47 48 return string
class
Switch:
9class Switch: 10 """ A Content Management Policy switch which has a name and a value, and can have attributes. """ 11 12 def __init__(self, name: str, value: str, attributes: Optional[dict] = None, restrict_values: Optional[list] = None): 13 self._indent = 0 14 self.restrict_values = restrict_values or [] 15 self.name = name 16 self.value = value or "" 17 self.attributes = attributes or {} 18 19 def __str__(self): 20 return self.text 21 22 def __repr__(self): 23 """ Change string representation of object. """ 24 return f'Switch("{self.name}", "{self.value}")' 25 26 def __lt__(self, other): 27 """ Used for sorting. Sort by "name" then "value". """ 28 return (self.name.lower(), self.value.lower(),) < (other.name.lower(), other.value.lower(),) 29 30 @property 31 def value(self): 32 return self._value 33 34 @value.setter 35 def value(self, value): 36 self._value = value 37 if self.restrict_values and value not in self.restrict_values: 38 raise RestrictedValue(f"{self.name} has an unexpected value: '{value}'. Its value is restricted to: {self.restrict_values}") 39 40 @property 41 def text(self): 42 """ String representation of XML. """ 43 indent = " " * 4 * self._indent 44 45 string = f"{indent}<{self.name}" 46 # Sort attributes by lowercase key, lowercase value 47 for k, v in sorted(self.attributes.items(), key=lambda kv: (kv[0].lower(), kv[1].lower())): 48 string += f' {k}="{v}"' 49 string += f">{self.value}</{self.name}>" 50 51 return string
A Content Management Policy switch which has a name and a value, and can have attributes.
Switch( name: str, value: str, attributes: Optional[dict] = None, restrict_values: Optional[list] = None)
text
40 @property 41 def text(self): 42 """ String representation of XML. """ 43 indent = " " * 4 * self._indent 44 45 string = f"{indent}<{self.name}" 46 # Sort attributes by lowercase key, lowercase value 47 for k, v in sorted(self.attributes.items(), key=lambda kv: (kv[0].lower(), kv[1].lower())): 48 string += f' {k}="{v}"' 49 string += f">{self.value}</{self.name}>" 50 51 return string
String representation of XML.