Best Python code snippet using SeleniumBase
fixup_svg_schematic.py
Source:fixup_svg_schematic.py
...31 if namespace is not None:32 full = ET.QName(node.nsmap[namespace], attr)33 attr = full.text34 return node.get(attr)35def remove_attribute(node: ET.Element, attr: str, namespace: str = None):36 if namespace is not None:37 full = ET.QName(node.nsmap[namespace], attr)38 attr = full.text39 if attr in node.attrib:40 node.attrib.pop(attr)41def replace_color_styles(node: ET.Element, color_var: str):42 style = fetch_attribute_value(node, 'style')43 if style is None:44 return45 styles = style.split(';')46 for idx, el in enumerate(styles):47 [key, val] = el.split(':')48 if key in ['stroke', 'fill'] and val.lower() != 'none':49 styles[idx] = f'{key}:var({color_var})'50 node.attrib['style'] = ';'.join(styles)51def unique_css_class_for_style_string(el_type: str, style: str) -> str:52 if style in reverse_styles:53 return reverse_styles[style]54 class_name = f'{style_prefix}{el_type}_{style_counter[el_type]}'55 reverse_styles[style] = class_name56 styles['.' + class_name] = style57 style_counter[el_type] += 158 return class_name59def unique_id_for_node(node: ET.Element, el_type: str) -> str:60 base = fetch_attribute_value(node, 'label', 'inkscape')61 if base is None:62 base = el_type63 suffix = 064 if base in id_counter:65 suffix = id_counter[base]66 id_counter[base] = suffix + 167 return f'{base}_{suffix}'68def process_node_styles(root: ET.Element, group_type: str = None, color_var: str = None):69 for node in root:70 tag = ET.QName(node).localname71 node.attrib['id'] = unique_id_for_node(node, tag)72 if group_type == 'wire':73 replace_color_styles(node, color_var)74 remove_attribute(node, 'role', 'sodipodi')75 remove_attribute(node, 'nodetypes', 'sodipodi')76 remove_attribute(node, 'transform-center-x', 'inkscape')77 remove_attribute(node, 'transform-center-y', 'inkscape')78 label = fetch_attribute_value(node, 'label', 'inkscape')79 if label is not None and '#' in label:80 [n_grouptype, n_color_var] = label.split('#')81 n_color_var = color_var_name(n_color_var)82 if n_grouptype == 'wire':83 replace_color_styles(node, n_color_var)84 process_node_styles(node, n_grouptype, n_color_var)85 else:86 process_node_styles(node, group_type, color_var)87 if 'style' in node.attrib:88 node.attrib['class'] = unique_css_class_for_style_string(tag, fetch_attribute_value(node, 'style'))89 remove_attribute(node, 'style')90 remove_attribute(node, 'label', 'inkscape')91def main():92 global style_prefix93 cmd_args = parse_arguments()94 # read svg95 svg_tree = ET.parse(cmd_args.input)96 svg_root = svg_tree.getroot()97 # modify svg98 # --> remove inkspace specific elements99 for node in svg_root.findall('{*}metadata'):100 svg_root.remove(node)101 for node in svg_root.findall('{*}namedview'):102 svg_root.remove(node)103 # --> cleanup defs104 for defs in svg_root.findall('{*}defs'):105 for def_node in defs:106 remove_attribute(def_node, 'isstock', 'inkscape')107 remove_attribute(def_node, 'stockid', 'inkscape')108 # --> cleanup root element109 remove_attribute(svg_root, 'width')110 remove_attribute(svg_root, 'height')111 remove_attribute(svg_root, 'docname', 'sodipodi')112 remove_attribute(svg_root, 'version', 'inkscape')113 # --> remove all 'helper' layers114 schematic = None115 for layer in svg_root.findall('svg:g', svg_root.nsmap):116 if fetch_attribute_value(layer, 'groupmode', 'inkscape') == 'layer':117 if fetch_attribute_value(layer, 'label', 'inkscape') != 'Schematic':118 svg_root.remove(layer)119 else:120 schematic = layer121 if schematic == None:122 print("Schematic layer not found")123 exit()124 schematic.attrib['id'] = fetch_attribute_value(schematic, 'label', 'inkscape')125 remove_attribute(schematic, 'label', 'inkscape')126 remove_attribute(schematic, 'groupmode', 'inkscape')127 # --> fix the styles of the schematic elements128 style_prefix = os.path.splitext(os.path.basename(cmd_args.output))[0] + '_'129 process_node_styles(schematic)130 # --> add a style element somewhere in the beginning131 styles[':root'] = ''132 for key, value in color_vars.items():133 styles[':root'] += f'{key}:{value};'134 style = ET.Element('style')135 style.text = '\n'.join(['%s {%s}' % kv for kv in styles.items()])136 svg_root.insert(2, style)137 # --> remove unused namespaces138 ET.cleanup_namespaces(svg_root)139 # write svg140 svg_tree.write(cmd_args.output, pretty_print=True)...
fixup_svg_keyboard.py
Source:fixup_svg_keyboard.py
...24 if namespace is not None:25 full = ET.QName(node.nsmap[namespace], attr)26 attr = full.text27 return node.get(attr)28def remove_attribute(node: ET.Element, attr: str, namespace: str = None):29 if namespace is not None:30 full = ET.QName(node.nsmap[namespace], attr)31 attr = full.text32 if attr in node.attrib:33 node.attrib.pop(attr)34def unique_css_class_for_style_string(el_type: str, style: str) -> str:35 if el_type == 'tspan':36 style = 'pointer-events:none;' + style37 if style in reverse_styles:38 return reverse_styles[style]39 class_name = f'{el_type}_{style_counter[el_type]}'40 reverse_styles[style] = class_name41 styles['.' + class_name] = style42 style_counter[el_type] += 143 return class_name44def unique_id_for_node(node: ET.Element, el_type: str) -> str:45 base = fetch_attribute_value(node, 'label', 'inkscape')46 if base is None:47 base = el_type48 if base in id_counter:49 suffix = id_counter[base]50 id_counter[base] = suffix + 151 return f'{base}_{suffix}'52 else:53 id_counter[base] = 1;54 return base55def process_node_styles(root: ET.Element):56 for node in root:57 tag = ET.QName(node).localname58 node.attrib['id'] = unique_id_for_node(node, tag)59 remove_attribute(node, 'role', 'sodipodi')60 remove_attribute(node, 'nodetypes', 'sodipodi')61 remove_attribute(node, 'transform-center-x', 'inkscape')62 remove_attribute(node, 'transform-center-y', 'inkscape')63 process_node_styles(node);64 if 'style' in node.attrib:65 node.attrib['class'] = unique_css_class_for_style_string(tag, fetch_attribute_value(node, 'style'))66 remove_attribute(node, 'style')67 remove_attribute(node, 'label', 'inkscape')68def main():69 cmd_args = parse_arguments()70 # read svg71 svg_tree = ET.parse(cmd_args.input)72 svg_root = svg_tree.getroot()73 # modify svg74 # --> remove inkspace specific elements75 for node in svg_root.findall('{*}metadata'):76 svg_root.remove(node)77 for node in svg_root.findall('{*}namedview'):78 svg_root.remove(node)79 # --> cleanup defs80 for defs in svg_root.findall('{*}defs'):81 for def_node in defs:82 remove_attribute(def_node, 'isstock', 'inkscape')83 remove_attribute(def_node, 'stockid', 'inkscape')84 # --> cleanup root element85 remove_attribute(svg_root, 'width')86 remove_attribute(svg_root, 'height')87 remove_attribute(svg_root, 'docname', 'sodipodi')88 remove_attribute(svg_root, 'version', 'inkscape')89 # --> process all layers90 for layer in svg_root.findall('svg:g', svg_root.nsmap):91 layer.attrib['id'] = fetch_attribute_value(layer, 'label', 'inkscape')92 remove_attribute(layer, 'label', 'inkscape')93 remove_attribute(layer, 'groupmode', 'inkscape')94 # --> merge the styles of the elements95 process_node_styles(layer)96 # --> add a style element somewhere in the beginning97 style = ET.Element('style')98 style.text = '\n'.join(['%s {%s}' % kv for kv in styles.items()])99 svg_root.insert(2, style)100 # --> remove unused namespaces101 ET.cleanup_namespaces(svg_root)102 # write svg103 svg_tree.write(cmd_args.output, pretty_print=True)104if __name__ == "__main__":...
scripts.py
Source:scripts.py
...27 28 :Return: None, modify things in place29 """30 if node.has_attribute("xmlns") :31 node.remove_attribute("xmlns")32 if node.nodename() in ("svg","g","path","rect","image","defs","metadata") :33 node.set_nodename("svg:%s" % node.nodename())34 for child in node.children() :35 to_svg_namespace(child)3637def remove_attribute (node, attr_name, nodename=None) :38 """Remove a specific attribute39 from all nodes below node.40 41 :Parameters:42 - `node` (:class:`SVGElement`) - base node43 to consider, usually an :class:`SVGScene`44 - `attr_name` (str) - name of the45 attribute to discard46 - `nodename` (str) - if not None, restrict47 the action of this function to node48 with the given nodename49 50 :Return: None, modify things in place51 """52 if (nodename is None) or (node.nodename() == nodename) :53 if node.has_attribute(attr_name) :54 node.remove_attribute(attr_name)55 for child in node.children() :56 remove_attribute(child,attr_name,nodename)
...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!