def test_encoding(self): self.formatter = xmlformatter.Formatter(encoding_output="UTF-8") # Different output encoding: self.assertEqual(self.formatter.format_file("t18.xml"), self.readfile("t18_utf-8.xml")) self.formatter = xmlformatter.Formatter(encoding_input="ISO-8859-1") # Set right input encoding and output it self.assertEqual(self.formatter.format_file("t19.xml"), self.readfile("t19.xml")) # Umls erverywhere self.assertEqual(self.formatter.format_file("t30.xml"), self.readfile("t30.xml"))
def test_compress(self): self.formatter = xmlformatter.Formatter(compress=True) self.assertEqual(self.formatter.format_file("t2.xml"), self.readfile("t2_compressed.xml")) self.assertEqual(self.formatter.format_file("t4.xml"), self.readfile("t4_compressed.xml")) self.assertEqual(self.formatter.format_file("t7.xml"), self.readfile("t7_compressed.xml")) self.assertEqual(self.formatter.format_file("t8.xml"), self.readfile("t8_compressed.xml")) self.assertEqual(self.formatter.format_file("t9.xml"), self.readfile("t9_compressed.xml")) self.assertEqual(self.formatter.format_file("t10.xml"), self.readfile("t10_compressed.xml")) self.assertEqual(self.formatter.format_file("t12.xml"), self.readfile("t12_compressed.xml")) self.assertEqual(self.formatter.format_file("t13.xml"), self.readfile("t13_compressed.xml")) self.assertEqual(self.formatter.format_file("t14.xml"), self.readfile("t14_compressed.xml")) self.assertEqual(self.formatter.format_file("t15.xml"), self.readfile("t15_compressed.xml")) self.assertEqual(self.formatter.format_file("t16.xml"), self.readfile("t16_compressed.xml")) self.assertEqual(self.formatter.format_file("t17.xml"), self.readfile("t17_compressed.xml")) self.assertEqual(self.formatter.format_file("t20.xml"), self.readfile("t20_compressed.xml"))
def add_default_x(file_name): print('正在处理:%s' % file_name) file_path = os.path.join(dir_path, file_name) file_soup = BeautifulSoup(open(file_path), "lxml") note_list = file_soup.find_all('note') rest_list = [] for note in note_list: if str(note).find('rest') != -1: rest_list.append(note) # sort_rest_list = [] # for rest in rest_list: # voice = int(rest.find('voice').string) # if voice == 2: # print('\t多声部') # return x_list = get_x_list(file_name.replace('.xml', '.txt')) if len(x_list) == len(rest_list): index = 0 for rest in rest_list: rest['default-x'] = x_list[index] index += 1 save_path = os.path.join(save_dir_path, file_name) new_file = open(save_path, 'w') formatter = xmlformatter.Formatter() new_file.write(formatter.format_string(file_soup.prettify())) new_file.close() else: print('\t多Staff')
def test_pretty_precede(self): self.formatter = xmlformatter.Formatter(preserve=["precede"]) self.assertEqual(self.formatter.format_file("t6.xml"), self.readfile("t6_pretty.xml")) self.formatter.preserve = ['root'] self.assertEqual(self.formatter.format_file("t21.xml"), self.readfile("t21_precede.xml"))
def dump(self, filename): formatter = xmlformatter.Formatter(indent="1", indent_char="\t", encoding_output="ISO-8859-1", preserve=["literal"]) raw = formatter.format_string(self.raw) with open(filename, "w") as f: f.write(raw)
def serialize_xml(org_xml): import xmlformatter import xml # org_xml = re.sub(r'>[^<]+<', '><', org_xml) try: formatter = xmlformatter.Formatter() return formatter.format_string(org_xml).decode('utf-8') except xml.parsers.expat.ExpatError: return org_xml
def test_pretty(self): self.formatter = xmlformatter.Formatter() self.assertEqual(self.formatter.format_file("t1.xml"), self.readfile("t1_pretty.xml")) self.assertEqual(self.formatter.format_file("t2.xml"), self.readfile("t2_pretty.xml")) self.assertEqual(self.formatter.format_file("t4.xml"), self.readfile("t4_pretty.xml")) self.assertEqual(self.formatter.format_file("t5.xml"), self.readfile("t5_pretty.xml")) self.assertEqual(self.formatter.format_file("t7.xml"), self.readfile("t7_pretty.xml")) self.assertEqual(self.formatter.format_file("t8.xml"), self.readfile("t8_pretty.xml")) self.assertEqual(self.formatter.format_file("t9.xml"), self.readfile("t9_pretty.xml")) self.assertEqual(self.formatter.format_file("t10.xml"), self.readfile("t10_pretty.xml")) self.assertEqual(self.formatter.format_file("t11.xml"), self.readfile("t11_pretty.xml")) self.assertEqual(self.formatter.format_file("t12.xml"), self.readfile("t12_pretty.xml")) self.assertEqual(self.formatter.format_file("t13.xml"), self.readfile("t13_pretty.xml")) self.assertEqual(self.formatter.format_file("t14.xml"), self.readfile("t14_pretty.xml")) self.assertEqual(self.formatter.format_file("t15.xml"), self.readfile("t15_pretty.xml")) self.assertEqual(self.formatter.format_file("t16.xml"), self.readfile("t16_pretty.xml")) self.assertEqual(self.formatter.format_file("t17.xml"), self.readfile("t17_pretty.xml")) self.assertEqual(self.formatter.format_file("t20.xml"), self.readfile("t20_pretty.xml")) self.assertEqual(self.formatter.format_file("t21.xml"), self.readfile("t21_pretty.xml")) self.assertEqual(self.formatter.format_file("t25.xml"), self.readfile("t25_pretty.xml")) self.assertEqual(self.formatter.format_file("t24.xml"), self.readfile("t24.xml")) self.assertEqual(self.formatter.format_file("t26.xml"), self.readfile("t26.xml")) self.assertEqual(self.formatter.format_file("t27.xml"), self.readfile("t27.xml")) self.assertEqual(self.formatter.format_file("t28.xml"), self.readfile("t28_pretty.xml")) self.assertEqual(self.formatter.format_file("t31.xml"), self.readfile("t31_pretty.xml"))
def format_code(data, is_xml=False, sort_keys=False): """ Parses data and formats it """ if is_xml: ET.fromstring(data) # Make sure XML is valid formatter = xmlformatter.Formatter(indent=2, indent_char=' ', encoding_output='UTF-8', preserve=['literal']) return formatter.format_string(data) else: obj = json.loads(data, object_pairs_hook=OrderedDict) return json.dumps( obj, sort_keys=sort_keys, indent=2, ensure_ascii=False ).encode('UTF-8')
def format_code(data, is_xml=False): """ Parses data and formats it """ if is_xml: ET.fromstring(data) # Make sure XML is valid formatter = xmlformatter.Formatter(indent=2, indent_char=' ', encoding_output='UTF-8', preserve=['literal']) return formatter.format_string(data) else: obj = json.loads(data) output = StringIO() json.dump(obj, output, sort_keys=True, indent=2) return output.getvalue()
def main(): parser = argparse.ArgumentParser( description="Dump a binary EVTX file into XML.") parser.add_argument("inputfile", type=str, help="Path to the Windows EVTX event log file") parser.add_argument("outputfile", type=str, help="Path to the output XML file") args = parser.parse_args() newfilename = args.outputfile file = open(newfilename, "w") print("Writing output to " + newfilename) error_count = 0 output_interval = 10000 output_count = 0 #file.write("<Events>") fmtr = xmlformatter.Formatter(compress=True) with evtx.Evtx(args.inputfile) as log: for chunk in log.chunks(): for record in chunk.records(): try: s = fmtr.format_string(record.xml()) file.write(s + "\n") #root = ET.fromstring(s) #print root.findall('EventData') #print root.find('{http://schemas.microsoft.com/win/2004/08/events/event}EventID') output_count = output_count + 1 if (output_count % output_interval == 0): print "[Working] %d records written to file." % output_count except UnicodeDecodeError: error_count = error_count + 1 print "UnicodeDecode Error encountered skipping entry Errors[%d]" % error_count continue #file.write("</Events>") #print norm_count, error_count file.close()
def format_xml(): try: _value = str(value.get('1.0', 'end-1c')).strip() formatter = xmlformatter.Formatter(indent="1", indent_char="\t", encoding_output="UTF-8", preserve=["literal"]) _value = formatter.format_string(_value) if _value: value.delete('1.0', END) value.insert(END, _value) except: global outtxt_index tag = 'odd' if outtxt_index % 2 == 0: tag = 'even' outtxt_index += 1 out_str = str( datetime.now()) + ': ' + str('Unable to format as xml') + '\n' start_txt_index = float(outtxt.index(END)) - 1 end_txt_index = str(floor(start_txt_index)) + '.' + str(len(out_str)) outtxt.insert(END, out_str) outtxt.tag_add(tag, start_txt_index, end_txt_index) outtxt.see(END)
def auto_format(self): workspace_path = os.path.normpath( os.path.join(os.path.dirname(__file__), "..", "..", "..")) max_col = 79 use_prettier = True use_format_black = True # Else, oca-autopep8 use_clean_import_isort = True use_html5print = False enable_xml_formatter = False # Else, prettier-xml # Manual format with def with programmer style for path_file in self.lst_path_file: relative_path = path_file[len(self.module_path) + 1:] if path_file.endswith(".py"): # TODO not optimal, too many write for nothing if not use_format_black: lst_line_write = [] has_change = False with open(path_file, "r") as source: for line in source.readlines(): if (line.lstrip().startswith("def ") or line.lstrip().startswith("return ") ) and len(line) > max_col - 1: has_change = True next_tab_space = line.find("(") + 1 first_cut = max_col first_cut = line.rfind(", ", 0, first_cut) + 1 first_part = line[:first_cut] last_part = line[first_cut:].lstrip() str_line = ( f"{first_part}\n{' ' * next_tab_space}{last_part}" ) lst_line_write.append(str_line[:-1]) else: lst_line_write.append(line[:-1]) if has_change: self.write_file_lst_content(relative_path, lst_line_write) elif path_file.endswith(".js"): if use_prettier: cmd = f"prettier --write --tab-width 4 {path_file}" result = self.subprocess_cmd(cmd) if result: _logger.info(f"prettier {result.decode()}") elif use_html5print: with open(path_file, "r") as source: lines = source.read() try: lines_out = html5print.JSBeautifier.beautify( lines, 4) self.write_file_str(relative_path, lines_out) except Exception as e: _logger.error(e) _logger.error(f"Check file {path_file}") else: cmd = ( f"cd {workspace_path};." f" .venv/bin/activate;css-html-prettify.py {path_file}" ) result = self.subprocess_cmd(cmd) if result: _logger.warning(result) elif path_file.endswith(".scss") or path_file.endswith(".css"): if use_prettier: cmd = f"prettier --write {path_file}" result = self.subprocess_cmd(cmd) if result: _logger.info(f"prettier {result.decode()}") elif use_html5print: with open(path_file, "r") as source: lines = source.read() try: lines_out = html5print.CSSBeautifier.beautify( lines, 2) self.write_file_str(relative_path, lines_out) except Exception as e: _logger.error(e) _logger.error(f"Check file {path_file}") else: cmd = ( f"cd {workspace_path};." f" .venv/bin/activate;css-html-prettify.py {path_file}" ) result = self.subprocess_cmd(cmd) if result: _logger.warning(result) elif path_file.endswith(".html"): if use_prettier: cmd = f"prettier --write {path_file}" result = self.subprocess_cmd(cmd) if result: _logger.info(f"prettier {result.decode()}") elif use_html5print: with open(path_file, "r") as source: lines = source.read() try: lines_out = html5print.HTMLBeautifier.beautify( lines, 4) self.write_file_str(relative_path, lines_out) except Exception as e: _logger.error(e) _logger.error(f"Check file {path_file}") else: cmd = ( f"cd {workspace_path};." f" .venv/bin/activate;css-html-prettify.py {path_file}" ) result = self.subprocess_cmd(cmd) if result: _logger.warning(result) elif path_file.endswith(".xml"): if use_prettier and not enable_xml_formatter: if "/data/" in path_file: # Super size --print-width, because wrapping data break information for translation and # visual data cmd = ("prettier --xml-whitespace-sensitivity ignore" " --prose-wrap always --tab-width 4" " --no-bracket-spacing --print-width 999999999" f" --write {path_file}") else: cmd = ( "prettier --xml-whitespace-sensitivity ignore" " --prose-wrap always --tab-width 4" " --no-bracket-spacing --print-width 120 --write" f" {path_file}") result = self.subprocess_cmd(cmd) if result: _logger.info(f"prettier {result.decode()}") # Optimize import python if use_clean_import_isort: cmd = ( f"cd {workspace_path};./.venv/bin/isort --profile black -l 79" f" {self.module_path}") result = self.subprocess_cmd(cmd) if result: _logger.info(f"isort {result.decode()}") # Automatic format # TODO check diff before and after format to auto improvement of generation if use_format_black: cmd = (f"cd {workspace_path};./.venv/bin/black -l" f" {max_col} --experimental-string-processing -t py37" f" {self.module_path}") result = self.subprocess_cmd(cmd) if result: _logger.warning(result) else: maintainer_path = os.path.join(workspace_path, "script", "OCA_maintainer-tools") cpu_count = os.cpu_count() cmd = (f"cd {maintainer_path};. env/bin/activate;cd" f" {workspace_path};oca-autopep8 -j{cpu_count}" f" --max-line-length {max_col} -ari {self.module_path}") result = self.subprocess_cmd(cmd) if result: _logger.warning(result) if enable_xml_formatter: formatter = xmlformatter.Formatter( indent="4", indent_char=" ", selfclose=True, correct=True, preserve=["pre"], blanks=True, ) for path_file in self.lst_path_file: if path_file.endswith(".xml"): relative_path = path_file[len(self.module_path) + 1:] self.write_file_binary(relative_path, formatter.format_file(path_file))
commit_msg = [] for git_if in git_props.children: if git_if.name != None: # print '[' + git_if.name + ' > ' + git_if.string + ']' svn_if = props.find(git_if.name) # print '>>[' + svn_if.name + ' > ' + svn_if.string + ']' if svn_if != None and svn_if.string > git_if.string: commit_msg.append('Replace version ' + git_if.string + ' with ' + svn_if.string + ' for ' + git_if.name) git_if.string = svn_if.string # print soup # target_str = str(soup.prettify(soup.original_encoding, 'minimal')) # doc_root = html.fromstring(str(git_soup)) # output_str = etree.tostring(doc_root, encoding=git_soup.original_encoding, pretty_print=False) for msg in commit_msg: print msg print '%s interfaces changed' % len(commit_msg) if len(commit_msg) > 0: print '%FIN IWI: ' + ', '.join(commit_msg) formatter = xmlformatter.Formatter(indent="4", indent_char=" ", encoding_output="utf-8", preserve=["literal"]) with open(git_dir + "pom.xml", "wb") as pom_file: pom_file.write(formatter.format_string(str(git_soup)))
def test_disable_correction(self): self.formatter = xmlformatter.Formatter(correct=False) self.assertEqual(self.formatter.format_file("t1.xml"), self.readfile("t1.xml"))
def test_compressed_precede(self): self.formatter = xmlformatter.Formatter(preserve=["precede"], compress=True) self.assertEqual(self.formatter.format_file("t6.xml"), self.readfile("t6_compressed.xml"))
def serialize_xml(org_xml): import xmlformatter org_xml = re.sub(r'>[^<]+<', '><', org_xml) formatter = xmlformatter.Formatter() return formatter.format_string(org_xml).decode('utf-8')
def test_disable_inline_formatting(self): self.formatter = xmlformatter.Formatter(inline=False) self.assertEqual(self.formatter.format_file("t29.xml"), self.readfile("t29_pretty.xml"))
import xmlformatter formatter = xmlformatter.Formatter(compress=True, encoding_output="UTF-8") def format_xml(xml): return formatter.format_string(xml)
#! /usr/bin/env python """ A command-line tool to merge completed tasks from a Freeplane/Docear to-do list. """ import os, os.path, sys import argparse import glob import xml.etree.cElementTree as ET import xmlformatter formatter = xmlformatter.Formatter() def isChecked(node): return node.find("./icon[@BUILTIN='checked']") is not None def modifyRootNodeText(root, newtext): rootnode = root.find("node") # print rootnode.get("TEXT") rootnode.set("TEXT", newtext) # print rootnode.get("TEXT") # print ET.tostring(rootnode) # rootnode.append() return root def modifyDetails(node, newtext): """ Add an html <p>newtext</p> entry in details of node
def test_indent_char(self): self.formatter = xmlformatter.Formatter(indent_char="\t", indent="2") self.assertEqual(self.formatter.format_file("t22.xml"), self.readfile("t22_pretty.xml"))
if str.startswith('['): return 'json' if str.startswith('<'): return 'xml' return 'sql' selected_text = os.getenv("POPCLIP_TEXT") str_type = detect_string_type(selected_text) res = None if str_type == 'json': res = jsbeautifier.beautify(selected_text) if str_type == 'xml': formatter = xmlformatter.Formatter(indent="1", indent_char="\t", encoding_output="UTF-8", preserve=["literal"]) res = formatter.format_string(selected_text) if str_type == 'sql': res = sqlparse.format(selected_text, keyword_case='upper', reindent_aligned=True, use_space_around_operators=True, strip_whitespace=True, strip_comments=True) if res is None: res = selected_text print res
import requests import xmltodict import bs4 import json import xmlformatter xml_format = xmlformatter.Formatter().format_string INDEX_API_REF_PREFIX = '/app/developer/qbdesktop/docs/api-reference/qbdesktop/' r = requests.get('https://static.developer.intuit.com/output_html/qbdesktop/index.html') r = bs4.BeautifulSoup(r.content) l = [e.get_attribute_list('href')[0] for e in r.find_all(attrs={'class': 'reference internal'})] p = [i.split(INDEX_API_REF_PREFIX)[1] for i in l if i.startswith(INDEX_API_REF_PREFIX)] for o in p: print(f'starting {o} ...') r = requests.get(f'https://static.developer.intuit.com/output_html/qbdesktop/docs/api-reference/qbdesktop/{o}.html') r = bs4.BeautifulSoup(r.content) c = r.find(attrs={'class': 'code'}) c = list(list(list(c.children)[0].children)[0]) xml = [] comment = None for e in c: if isinstance(e, bs4.element.NavigableString): s = str(e) else: s = e.text