glasswall.content_management.policies.word_search
1import glasswall 2from glasswall.content_management.config_elements.config_element import ConfigElement 3from glasswall.content_management.policies.policy import Policy 4from glasswall.content_management.switches.switch import Switch 5 6 7class WordSearch(Policy): 8 """ A content management policy for Word Search. 9 10 WordSearch(default="allow", config={ 11 "textSearchConfig": { 12 "@libVersion": "core2", 13 "textList": [ 14 {"name": "textItem", "switches": [ 15 {"name": "text", "value": "generic"}, 16 {"name": "textSetting", "@replacementChar": "*", "value": "redact"}, 17 ]}, 18 ] 19 } 20 }) 21 """ 22 23 def __init__(self, default: str = "allow", config: dict = {}): 24 self.default = default 25 self.default_config_elements = [ 26 glasswall.content_management.config_elements.pdfConfig(default=default), 27 glasswall.content_management.config_elements.pptConfig(default=default), 28 glasswall.content_management.config_elements.sysConfig(interchange_type="xml"), 29 glasswall.content_management.config_elements.tiffConfig(default=default), 30 glasswall.content_management.config_elements.wordConfig(default=default), 31 glasswall.content_management.config_elements.xlsConfig(default=default), 32 ] 33 self.config = config 34 35 super().__init__( 36 default=self.default, 37 default_config_elements=self.default_config_elements, 38 config=self.config, 39 ) 40 41 def add_textItem(self, text: str, replacementChar: str, textSetting: str = "redact", **kwargs): 42 """ Adds a textItem to the textSearchConfig textList subelements. """ 43 textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None) 44 if not textSearchConfig: 45 # Add textSearchConfig to ConfigElements 46 textSearchConfig = ConfigElement( 47 name="textSearchConfig", 48 attributes={"libVersion": kwargs.get("libVersion", "core2")}, 49 subelements=[ConfigElement(name="textList")] 50 ) 51 self.add_config_element( 52 config_element=textSearchConfig, 53 ) 54 55 # Select the ConfigElement named textList 56 textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList")) 57 58 # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates. 59 # (cannot redact "generic" with "*" and also redact "generic" with "@") 60 for textItem in textList.subelements.copy(): 61 for switch in textItem.switches: 62 if switch.name == "text" and switch.value.lower() == text.lower(): 63 # Remove textItem from tetList.subelements using the builtin list .remove method 64 textList.subelements.remove(textItem) 65 break 66 67 # Add textItem to end of textList.subelements 68 textList.subelements.append( 69 ConfigElement( 70 name="textItem", 71 switches=[ 72 Switch(name="text", value=text), 73 Switch(name="textSetting", attributes={"replacementChar": replacementChar}, value=textSetting) 74 ], 75 ) 76 ) 77 78 # Don't sort textList: this preserves top-down order for redaction settings. 79 80 def remove_textItem(self, text: str): 81 """ Removes a textItem from the textSearchConfig textList subelements. """ 82 textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None) 83 if not textSearchConfig: 84 raise glasswall.content_management.errors.config_elements.ConfigElementNotFound("textSearchConfig") 85 86 # Select the ConfigElement named textList 87 textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList")) 88 89 all_textItem_texts = [ 90 switch.value.lower() 91 for textItem in textList.subelements 92 for switch in textItem.switches 93 if switch.name == "text" 94 ] 95 if text not in all_textItem_texts: 96 raise glasswall.content_management.errors.switches.SwitchNotFound(f"No switch found with name: 'text' and value: '{text}'") 97 98 # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates. 99 # (cannot redact "generic" with "*" and also redact "GeNeRiC" with "@") 100 for textItem in textList.subelements.copy(): 101 for switch in textItem.switches: 102 if switch.name == "text" and switch.value.lower() == text.lower(): 103 textList.subelements.remove(textItem) 104 break
10class WordSearch(Policy): 11 """ A content management policy for Word Search. 12 13 WordSearch(default="allow", config={ 14 "textSearchConfig": { 15 "@libVersion": "core2", 16 "textList": [ 17 {"name": "textItem", "switches": [ 18 {"name": "text", "value": "generic"}, 19 {"name": "textSetting", "@replacementChar": "*", "value": "redact"}, 20 ]}, 21 ] 22 } 23 }) 24 """ 25 26 def __init__(self, default: str = "allow", config: dict = {}): 27 self.default = default 28 self.default_config_elements = [ 29 glasswall.content_management.config_elements.pdfConfig(default=default), 30 glasswall.content_management.config_elements.pptConfig(default=default), 31 glasswall.content_management.config_elements.sysConfig(interchange_type="xml"), 32 glasswall.content_management.config_elements.tiffConfig(default=default), 33 glasswall.content_management.config_elements.wordConfig(default=default), 34 glasswall.content_management.config_elements.xlsConfig(default=default), 35 ] 36 self.config = config 37 38 super().__init__( 39 default=self.default, 40 default_config_elements=self.default_config_elements, 41 config=self.config, 42 ) 43 44 def add_textItem(self, text: str, replacementChar: str, textSetting: str = "redact", **kwargs): 45 """ Adds a textItem to the textSearchConfig textList subelements. """ 46 textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None) 47 if not textSearchConfig: 48 # Add textSearchConfig to ConfigElements 49 textSearchConfig = ConfigElement( 50 name="textSearchConfig", 51 attributes={"libVersion": kwargs.get("libVersion", "core2")}, 52 subelements=[ConfigElement(name="textList")] 53 ) 54 self.add_config_element( 55 config_element=textSearchConfig, 56 ) 57 58 # Select the ConfigElement named textList 59 textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList")) 60 61 # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates. 62 # (cannot redact "generic" with "*" and also redact "generic" with "@") 63 for textItem in textList.subelements.copy(): 64 for switch in textItem.switches: 65 if switch.name == "text" and switch.value.lower() == text.lower(): 66 # Remove textItem from tetList.subelements using the builtin list .remove method 67 textList.subelements.remove(textItem) 68 break 69 70 # Add textItem to end of textList.subelements 71 textList.subelements.append( 72 ConfigElement( 73 name="textItem", 74 switches=[ 75 Switch(name="text", value=text), 76 Switch(name="textSetting", attributes={"replacementChar": replacementChar}, value=textSetting) 77 ], 78 ) 79 ) 80 81 # Don't sort textList: this preserves top-down order for redaction settings. 82 83 def remove_textItem(self, text: str): 84 """ Removes a textItem from the textSearchConfig textList subelements. """ 85 textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None) 86 if not textSearchConfig: 87 raise glasswall.content_management.errors.config_elements.ConfigElementNotFound("textSearchConfig") 88 89 # Select the ConfigElement named textList 90 textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList")) 91 92 all_textItem_texts = [ 93 switch.value.lower() 94 for textItem in textList.subelements 95 for switch in textItem.switches 96 if switch.name == "text" 97 ] 98 if text not in all_textItem_texts: 99 raise glasswall.content_management.errors.switches.SwitchNotFound(f"No switch found with name: 'text' and value: '{text}'") 100 101 # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates. 102 # (cannot redact "generic" with "*" and also redact "GeNeRiC" with "@") 103 for textItem in textList.subelements.copy(): 104 for switch in textItem.switches: 105 if switch.name == "text" and switch.value.lower() == text.lower(): 106 textList.subelements.remove(textItem) 107 break
A content management policy for Word Search.
WordSearch(default="allow", config={ "textSearchConfig": { "@libVersion": "core2", "textList": [ {"name": "textItem", "switches": [ {"name": "text", "value": "generic"}, {"name": "textSetting", "@replacementChar": "*", "value": "redact"}, ]}, ] } })
WordSearch(default: str = 'allow', config: dict = {})
26 def __init__(self, default: str = "allow", config: dict = {}): 27 self.default = default 28 self.default_config_elements = [ 29 glasswall.content_management.config_elements.pdfConfig(default=default), 30 glasswall.content_management.config_elements.pptConfig(default=default), 31 glasswall.content_management.config_elements.sysConfig(interchange_type="xml"), 32 glasswall.content_management.config_elements.tiffConfig(default=default), 33 glasswall.content_management.config_elements.wordConfig(default=default), 34 glasswall.content_management.config_elements.xlsConfig(default=default), 35 ] 36 self.config = config 37 38 super().__init__( 39 default=self.default, 40 default_config_elements=self.default_config_elements, 41 config=self.config, 42 )
def
add_textItem( self, text: str, replacementChar: str, textSetting: str = 'redact', **kwargs):
44 def add_textItem(self, text: str, replacementChar: str, textSetting: str = "redact", **kwargs): 45 """ Adds a textItem to the textSearchConfig textList subelements. """ 46 textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None) 47 if not textSearchConfig: 48 # Add textSearchConfig to ConfigElements 49 textSearchConfig = ConfigElement( 50 name="textSearchConfig", 51 attributes={"libVersion": kwargs.get("libVersion", "core2")}, 52 subelements=[ConfigElement(name="textList")] 53 ) 54 self.add_config_element( 55 config_element=textSearchConfig, 56 ) 57 58 # Select the ConfigElement named textList 59 textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList")) 60 61 # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates. 62 # (cannot redact "generic" with "*" and also redact "generic" with "@") 63 for textItem in textList.subelements.copy(): 64 for switch in textItem.switches: 65 if switch.name == "text" and switch.value.lower() == text.lower(): 66 # Remove textItem from tetList.subelements using the builtin list .remove method 67 textList.subelements.remove(textItem) 68 break 69 70 # Add textItem to end of textList.subelements 71 textList.subelements.append( 72 ConfigElement( 73 name="textItem", 74 switches=[ 75 Switch(name="text", value=text), 76 Switch(name="textSetting", attributes={"replacementChar": replacementChar}, value=textSetting) 77 ], 78 ) 79 ) 80 81 # Don't sort textList: this preserves top-down order for redaction settings.
Adds a textItem to the textSearchConfig textList subelements.
def
remove_textItem(self, text: str):
83 def remove_textItem(self, text: str): 84 """ Removes a textItem from the textSearchConfig textList subelements. """ 85 textSearchConfig = next(iter(c for c in self.config_elements if c.name == "textSearchConfig"), None) 86 if not textSearchConfig: 87 raise glasswall.content_management.errors.config_elements.ConfigElementNotFound("textSearchConfig") 88 89 # Select the ConfigElement named textList 90 textList = next(iter(s for s in textSearchConfig.subelements if s.name == "textList")) 91 92 all_textItem_texts = [ 93 switch.value.lower() 94 for textItem in textList.subelements 95 for switch in textItem.switches 96 if switch.name == "text" 97 ] 98 if text not in all_textItem_texts: 99 raise glasswall.content_management.errors.switches.SwitchNotFound(f"No switch found with name: 'text' and value: '{text}'") 100 101 # If textList has a subelement textItem that contains a switch named "text" with the same .value as arg "text", delete it to avoid duplicates. 102 # (cannot redact "generic" with "*" and also redact "GeNeRiC" with "@") 103 for textItem in textList.subelements.copy(): 104 for switch in textItem.switches: 105 if switch.name == "text" and switch.value.lower() == text.lower(): 106 textList.subelements.remove(textItem) 107 break
Removes a textItem from the textSearchConfig textList subelements.