def WriteXML(edges,nodes, out = 'graph'): ns = "http://www.w3.org/2001/XMLSchema-instance" nsg = "http://www.gexf.net/1.2draft" # Configure one attribute with set() ET.register_namespace('xsi',ns) #some name root = Element('gexf') root.set('version', '1.2') root.set('xmlns',ns) root.set('xmlns:xsi',nsg) graph = SubElement(root, 'graph') graph.set('mode',Def.GraphMode()) graph.set('defaultedgetype',Def.Arcs) Def.DeclareAttributes(graph) nodesE = SubElement(graph,'nodes') for node in nodes: node.xmlElement(nodesE) edgesE = SubElement(graph,'edges') for edge in edges: edge.xmlElement(edgesE) output = './tempgraph123123.xml' tree = ET.ElementTree(root) out = "{}.gexf".format(out) #tree.write("page.xml",xml_declaration=True,encoding='utf-8',method="xml",default_namespace=ns) xml = ET.tostring(root).decode("utf-8") tree.write(output, encoding="utf-8", xml_declaration=True, default_namespace=None, method="xml") vkb.xml(output,out) os.remove(output) print("Writing graph to {}".format(out))
def write_problems(df): file_path = '../Data/qa_mock_exams/all_problems.xml' document = ET.Element('problems') df.apply(lambda r: add_to_xml(document, r), axis=1) xml_text = ET.tostring(document, encoding='unicode', method="xml") pretty_xml_text = vkb.xml(xml_text) vkb.xml(pretty_xml_text, file_path)
def generateCAMT053(self, realAccount, transactionAccount, camtinput): iBANFlag = "" CAMT053FileProcessing.outputFileName = "AutoCAMT053" + Util.get_unique_number( 5) CAMT053InputData.Random = CAMT053FileProcessing.random + "-" + Util.get_unique_number( 5) CAMT053InputData.date = datetime.today().isoformat() CAMT053InputData.Dt = date.today().isoformat() # CAMT053FileProcessing.path = str(Path.home()) # CAMT053FileProcessing.path = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) CAMT053FileProcessing.path = os.environ.get( 'myHome') # str(Path.home()) CAMT053FileProcessing.paramFilePath = CAMT053FileProcessing.path + "inputCAMT&PAIN\\" if not os.path.exists('inputCAMT&PAIN'): os.makedirs(CAMT053FileProcessing.paramFilePath) self.createParam(CAMT053FileProcessing.outputFileName) self.takeInputsForCAMT053FileProcessing(realAccount, transactionAccount, camtinput) # Root = self.initiateXML() rootElement = Element("Document") tree = etree.ElementTree(rootElement) rootElement.set("xmlns", "urn:iso:std:iso:20022:tech:xsd:camt.053.001.02") rootElement.set("xmlns:xsd", "http://www.w3.org/2001/XMLSchema") rootElement.set("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance") BkToCstmrStmt = Element(CAMT053Tags.BkToCstmrStmtTag) rootElement.append(BkToCstmrStmt) self.createGrpHdr(BkToCstmrStmt) self.createStmt(BkToCstmrStmt) CAMT053FileProcessing.camtFilepath = CAMT053FileProcessing.path + "\\inputCAMT&PAIN\\" + \ CAMT053FileProcessing.outputFileName + ".att" tempFileName = CAMT053FileProcessing.path + "\\inputCAMT&PAIN\\TempCAMTFile" + ".att" tree.write(open(tempFileName, 'wb'), xml_declaration=True, encoding='utf-8') vkb.xml(tempFileName, CAMT053FileProcessing.camtFilepath)
def createParam(self, outputFileName): document = Element("Head") # tree = ElementTree(document) tree = etree.ElementTree(document) a1 = Element("a1") a1.text = "xxxxxx" # CAMT053FileProcessing.custID document.append(a1) CAMT053InputData.BICOrBEI = ApplicationConfig.get('BICOrBEI') a2 = Element("a2") a2.text = CAMT053InputData.BICOrBEI document.append(a2) a4 = Element("a4") a4.text = CAMT053InputData.camtFormat document.append(a4) incomingPath = ApplicationConfig.get( 'INCOMINGFILEPATH') + '/' + outputFileName + ".att" a9 = Element("a9") a9.text = incomingPath document.append(a9) a10 = Element("a10") a10.text = outputFileName document.append(a10) a20 = Element("a20") a20.text = "VAM" document.append(a20) CAMT053FileProcessing.paramFilePath = CAMT053FileProcessing.path + "\\inputCAMT&PAIN\\" + \ CAMT053FileProcessing.outputFileName + ".param" tempFileName = CAMT053FileProcessing.path + "\\inputCAMT&PAIN\\TempFile" + ".param" tree.write(open(tempFileName, 'wb')) line = "" file = open(tempFileName) for line in file: line = line.replace('<Head>', '') line = line.replace('</Head>', '') file.close() vkb.xml(line, CAMT053FileProcessing.paramFilePath)
def normalizeSPace(dirList): for folder in dirList: files = (listXMLfiles(folder)) for file in files: pretty = vkb.xml(file) normalized = re.sub(r'> ', r'>', pretty) normalized = re.sub(r' </', r'</', normalized) with open(file, 'w') as output: output.write(normalized) # with open(file, 'r+') as file: # old=file.read() # normalized=re.sub(r'> ', r'>', old) # normalized=re.sub(r' </', r'</', normalized) # file.seek(0) # file.truncate() # file.write(normalized) # file.close() return
def generate_xmls_content(num=100, beautify=False): if beautify: import vkbeautify result = [] for i in range(num): root = ET.Element("root") ET.SubElement(root, "var", name="id", value=str(uuid4())) ET.SubElement(root, "var", name="level", value=str(random.randint(1, 100))) objects = ET.SubElement(root, "objects") for _ in range(random.randint(1, 10)): ET.SubElement(objects, "object", name=get_random_string()) xml_text = ET.tostring(root, encoding='unicode') if beautify: xml_text = vkbeautify.xml(xml_text) # return String, non-std lib result.append(xml_text) return result
def test_xml_beautify_with_custom_tab(self): xml_expected = '<a>\n <b></b>\n</a>' xml_pretty = vkb.xml('<a><b></b></a>', 8) #set tab 8 spaces self.assertEqual(xml_pretty, xml_expected)
self.graph.parse(source=data) # build a storage storage = SQLStorage('test1') # read an rdf file in the storage # storage.graph.load('http://dbpedia.org/resource/Semantic_Web') try: storage.graph.parse( source='http://publications.europa.eu/mdr/resource/authority' '/licence/skos/licences-skos.rdf') except IntegrityError: pass # storage.graph.load('http://www.dcat-ap.de/def/licenses/1_0.rdf') # print(vkb.xml(storage.graph.serialize())) qres = storage.graph.query("""SELECT DISTINCT ?license ?label ?definition WHERE { ?license rdfs:isDefinedBy ?definition . ?license rdfs:label ?label . }""") res = vkb.xml(qres.serialize()) # print(res) # storage.graph.save()
region = ET.SubElement(regions, "Region") ET.SubElement(region, "RegionDataType").text = "1" ET.SubElement(region, "PhysicalDeltaX").text = "0.05417" ET.SubElement(region, "PhysicalDeltaY").text = "0.05417" ET.SubElement(region, "PhysicalUnitsXDirection").text = "3" ET.SubElement(region, "PhysicalUnitsYDirection").text = "3" ET.SubElement(region, "ReferencePixelX0").text = "1" ET.SubElement(region, "ReferencePixelY0").text = "1" ET.SubElement(region, "RegionLocationMinX0").text = "0" ET.SubElement(region, "RegionLocationMaxX1").text = "240" ET.SubElement(region, "RegionLocationMinY0").text = "0" ET.SubElement(region, "RegionLocationMaxY1").text = "320" ET.SubElement(region, "RegionSpatialFormat").text = "1" image = ET.SubElement(regions, "Image") ET.SubElement(image, "PixelsPerMeterX").text = "1846.15385" ET.SubElement(image, "PixelsPerMeterY").text = "1846.15385" buffer = StringIO.StringIO() tree = ET.ElementTree(regions) tree.encoding = 'utf-8' tree.write(buffer) xml_string = buffer.getvalue() buffer.close() pretty_string = vkb.xml(xml_string) freeformBase64 = base64.b64encode(pretty_string) mp4Freeform.modifyFreeformData("H3HC4T82.mp4", "no.uio.test", "Calibrate", freeformBase64, 1)
def write_problems(self, to_path, problems): document = self.get_xml_document(problems) xml_text = ET.tostring(document, encoding='unicode', method="xml") pretty_xml_text = vkb.xml(xml_text) vkb.xml(pretty_xml_text, to_path)
def test_xml_beautify_with_custom_tab(): xml_expected = '<a>\n <b></b>\n</a>' xml_pretty = vkb.xml('<a><b></b></a>', 8) #set tab 8 spaces assert xml_pretty == xml_expected
def main(): creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first # time. if os.path.exists('token.pickle'): with open('token.pickle', 'rb') as token: creds = pickle.load(token) # If there are no (valid) credentials available, let the user log in. if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( 'credentials.json', SCOPES) creds = flow.run_local_server() # Save the credentials for the next run with open('token.pickle', 'wb') as token: pickle.dump(creds, token) service = build('sheets', 'v4', credentials=creds) # # shutil.rmtree('vocab', ignore_errors=True) # os.mkdir('vocab') sheet_metadata = service.spreadsheets().get(spreadsheetId=SPREADSHEetree_ID).execute() sheets = sheet_metadata.get('sheets', '') for sheet in sheets: title = sheet.get('properties').get('title') filename = sheet.get('properties').get('title').lower().replace(' ', '_') filename = filename.replace('/', '_') print(title, filename) googlesheet = service.spreadsheets().values().get(spreadsheetId=SPREADSHEetree_ID, range="{}!A2:C".format(title)).execute() vocab = etree.Element("opts") parentelement = None parentelementvalue = None for value in googlesheet.get('values', []): if len(value) == 1: element = etree.SubElement(vocab, "opt") element.text=value[0] elif len(value) == 2: element = etree.SubElement(vocab, "opt") element.text=value[0] desc = etree.SubElement(element, 'desc') desc.text=value[1] elif len(value) == 3: if value[0] != parentelementvalue: parentelement = etree.SubElement(vocab, "opt") parentelement.text=value[0] parentelementvalue = value[0] element = etree.SubElement(parentelement, "opt") element.text=value[1] desc = etree.SubElement(element, 'desc') desc.text=value[2] else: ValueError("Too many levels! Go implement some more!") tree = etree.ElementTree(vocab) tree.write("vocab/temp.xml", encoding="utf-8", pretty_print=True) vkbeautify.xml("vocab/temp.xml", "vocab/{}.xml".format(filename)) os.remove("vocab/temp.xml")
except: print(f'Mismatch between desired icon size ({height}) and actual image size ' f'({iconimg.size[0]}). Use the -r switch to resize icons to desired size.') sys.exit(1) # Cleanup any old files for imgFile in glob.glob(f'*{set}-iconset.png'): os.remove(imgFile) # Save the icon sheet image file sheetimg.save(sheetfile) # Convert the tree to an xml string xml = tree.tostring(xmlroot, method='xml', encoding='utf8').decode() # Beautify and write out the generated xml tree vkbeautify.xml(xml, f'{iconsetName}.xml') ## Create iconset zipped '.dis' file ## zf = ZipFile(f'{iconsetName}.zip', mode='w') zf.write(f'{iconsetName}.xml') for imgFile in glob.glob('*-iconset.png'): zf.write(imgFile) zf.close() if os.path.exists(f'{iconsetName}.dis'): os.remove(f'{iconsetName}.dis') os.rename(f'{iconsetName}.zip', f'{iconsetName}.dis') ## (--intfiles option) cleanup or keep the intermediate files if not args['intfiles']: print('Cleaning up...') os.remove(f'{iconsetName}.xml') for imgFile in glob.glob('*-iconset.png'): os.remove(imgFile) else:
regions.set('version', '1.0') region = ET.SubElement(regions, "Region") ET.SubElement(region, "RegionDataType").text = str(regionDataType) ET.SubElement(region, "PhysicalDeltaX").text = str(sampleSpacing) ET.SubElement(region, "PhysicalDeltaY").text = str(sampleSpacing) ET.SubElement( region, "PhysicalUnitsXDirection").text = str(physicalUnitsXDirection) ET.SubElement( region, "PhysicalUnitsYDirection").text = str(physicalUnitsYDirection) ET.SubElement(region, "ReferencePixelX0").text = halfWidth ET.SubElement(region, "ReferencePixelY0").text = "0" ET.SubElement(region, "RegionLocationMinX0").text = "0" ET.SubElement(region, "RegionLocationMaxX1").text = width ET.SubElement(region, "RegionLocationMinY0").text = "0" ET.SubElement(region, "RegionLocationMaxY1").text = height ET.SubElement(region, "RegionSpatialFormat").text = str(regionSpatialFormat) image = ET.SubElement(regions, "Image") ET.SubElement(image, "PixelsPerMeterX").text = str(100.0 / sampleSpacing) ET.SubElement(image, "PixelsPerMeterY").text = str(100.0 / sampleSpacing) tree = ET.ElementTree(regions) xmlBuffer = StringIO.StringIO() tree.write(xmlBuffer) metadataXMLString = vkb.xml(xmlBuffer.getvalue()) metadataXMLBase64 = base64.b64encode(metadataXMLString) mp4FreeformLibrary.modifyFreeformData(mpegFilename, "com.ge.med.vscan", "Calibration", metadataXMLBase64, 1) xmlBuffer.close()
def test_xml_beautify_and_save(tmpdir): file = tmpdir.join('output.xml') xml_pretty = vkb.xml('<a><b></b></a>', file.strpath) assert xml_pretty == 20
def main(argv): inputfile = '' outputfile = '' action = False try: opts, args = getopt.getopt(argv, "hi:o:a:", ["ifile=", "ofile=", "action="]) except getopt.GetoptError: print('main.py -i <inputfile> -o <outputfile> -a <action>') sys.exit(2) for opt, arg in opts: if opt == '-h': print('usage: app.py -i <inputfile> -o <outputfile> -a <action>') print(' to beautify: no <action> ') print(' to minify: set <action> to min') print('') print( ' Example: python3 app.py -i basic.json -o result.json -a min' ) print('') sys.exit() elif opt in ("-i", "--ifile"): inputfile = arg elif opt in ("-o", "--ofile"): outputfile = arg elif opt in ("-a", "--action"): action = arg if not inputfile: print('usage: main.py -i <inputfile> -o <outputfile> -a <action>' '\ninput file is mandatory') sys.exit(2) if inputfile[-4:] == '.css': if outputfile: if action == 'min': print(vkb.css.min(inputfile, outputfile, True)) else: print(vkb.css(inputfile, outputfile)) else: if action == 'min': print(vkb.css.min(inputfile, True)) else: print(vkb.css(inputfile)) if inputfile[-5:] == '.json': if outputfile: if action == 'min': print(vkb.json.min(inputfile, outputfile)) else: print(vkb.json(inputfile, outputfile)) else: if action == 'min': print(vkb.json.min(inputfile)) else: print(vkb.json(inputfile)) if inputfile[-4:] == '.xml': if outputfile: if action == 'min': print(vkb.xml.min(inputfile, outputfile, False)) else: print(vkb.xml(inputfile, outputfile)) else: if action == 'min': print(vkb.xml.min(inputfile, False)) else: print(vkb.xml(inputfile))
def test_xml_beautify(): xml_expected = '<a>\n <b></b>\n</a>' xml_pretty = vkb.xml('<a><b></b></a>') assert xml_pretty == xml_expected
import os from getmeta import get_youtube_json_for, create_mp3_filename_from_title, make_xml, add_new_item import xml.etree.cElementTree as ET import vkbeautify as beautify ORIGINAL = '../podcastMA/index.rss' TARGET = 'ATTEMPT.rss' MP3 = "Modern Agile Show #44 _ Interview with Jeff 'Cheezy' Morgan-wpbVe8l3CXM.mp3" def rewrite_rss_file(oldname, newname, mp3file): if oldname == newname: raise Exception("old and new name should not be the same - unsafe.") # Why duplicate all of this? Isn't this just one thing? filesize = os.stat(mp3file).st_size base_filename,_ = os.path.splitext(mp3file) youtube = get_youtube_json_for(base_filename) new_filename = create_mp3_filename_from_title(youtube.title) # This seems simple enough new_podcast = make_xml(new_filename, filesize, youtube) # Read, append, write (what OUGHT to be here) document = ET.parse(oldname) add_new_item(document, new_podcast) with open(newname,"w") as docfile docfile.write( beautify.xml(document) )
def test_xml_beautify(self): xml_expected = '<a>\n <b></b>\n</a>' xml_pretty = vkb.xml('<a><b></b></a>') self.assertEqual(xml_pretty, xml_expected)
def prettyprint_xml(text, method='stdlib-xml', indent=" "*4): """ Args: text: method: Returns: pretty, indented xml str Alternatives: * xmllint command line tool (from libxml2-utils) * xml_pp - from XML::Twig perl module. * xmlstarlet - * tidy - * saxon-lint * * xmlpp - http://xmlpp.codeplex.com/ * pyxml * xmlformatter - command line tool - https://github.com/pamoller/xmlformatter * XMLLayout (2011) Refs: * https://stackoverflow.com/questions/749796/pretty-printing-xml-in-python * https://stackoverflow.com/questions/16090869/how-to-pretty-print-xml-from-the-command-line * https://stackoverflow.com/questions/3844360/best-way-to-generate-xml """ if method is None: method = 'stdlib-xml' if indent is None: indent = " "*4 if method == 'lxml': # https://stackoverflow.com/a/3844432/3241277 import lxml pretty = lxml.etree.tostring(lxml.etree.fromstring(text), pretty_print=True) elif method == 'vkbeautify': # https://stackoverflow.com/a/41455013/3241277 import vkbeautify pretty = vkbeautify.xml(text) elif method in ('beautifulsoup', 'bs'): # https://stackoverflow.com/a/39482716/3241277 import bs4 bs = bs4.BeautifulSoup(text, 'xml') pretty = bs.prettify() elif method == 'yattag': # https://stackoverflow.com/a/23634596/3241277 import yattag pretty = yattag.indent(text) else: # if method == 'stdlib-xml': # https://stackoverflow.com/a/749839/3241277 import xml.dom.minidom print("Converting text", type(text)) print("indent: %r" % indent) tree = xml.dom.minidom.parseString(text) pretty = tree.toprettyxml(indent=indent) return pretty
def export_csv_to_global_anthro_notes(language='en'): orc_char = u"\uFFFC" APPLICATION_TO_BIBLICAL_SOURCE = '3 Application to biblical source' comment_list = ET.Element("CommentList") csv_rows = unicode_utils.load_unicode_csv_file_rows('scriptsCopy/data/Biblical Culture Notes Content.csv') ocm_choice_processing = '' increment = 0 section_topic_processing = '' contents = None bullet_processing = False for row in csv_rows: ocm_choice = row['ocm_choice'] if not ocm_choice: continue print (unicode(row['refs']), unicode(row['ocm_choice']), unicode(row['¶_content_' + language])) if ocm_choice.lower() != ocm_choice_processing.lower(): append_references_section(contents) ocm_choice_processing = ocm_choice thread = 'OCM ' + ocm_choice.split()[0] first_ref = None main_comment = create_comment(comment_list, thread, increment, language) increment += 1 contents = ET.SubElement(main_comment, "Contents") ocm_title = ET.SubElement(contents, "p", {'class': 'ocm-title'}) ET.SubElement(ocm_title, "a", {'href': 'toggle:{}'.format(thread)}) bold = ET.SubElement(ocm_title, "bold") title = " ".join(ocm_choice.split()[1:]) bold.text = u"{}".format(title.capitalize()) section_topic = row['section_topic'] is_app_biblical_source = section_topic.lower() == APPLICATION_TO_BIBLICAL_SOURCE.lower() if section_topic.lower() != section_topic_processing.lower(): section_topic_processing = section_topic section_title = ET.SubElement(contents, "div", {'class': 'section-title'}) ET.SubElement(section_title, "a", {'href': 'cancel:{}'.format(thread)}) bold = ET.SubElement(section_title, "b") bold.text = u" ".join(section_topic.split()[1:]).capitalize() extra_classes = ' active' if is_app_biblical_source else '' section_content = ET.SubElement(contents, "div", {'class': 'section-content{}'.format(extra_classes)}) if not row['¶_content_' + language]: continue content = unicode(row['¶_content_' + language]) if row['bullet']: if not bullet_processing: ul = ET.SubElement(section_content, "ul") bullet_processing = True li = ET.SubElement(ul, "li") li.text = content continue else: bullet_processing = False if is_app_biblical_source: ref = row['refs'] para = ET.SubElement(section_content, "p", attrib={'class': 'scrtext'}) span_ref = ET.SubElement(para, "span", attrib={'class': 'verseref'}) span_ref.text = ref span_content = ET.SubElement(para, "span", attrib={'class': 'commentary'}) span_content.text = content if first_ref is None: first_ref = ref comment = main_comment else: reattached_comment = create_comment(comment_list, thread, increment, language, first_ref) increment += 1 comment = reattached_comment ET.SubElement(reattached_comment, "Reattached").text = orc_char.join([ref, '', str(0), '', '']) comment.set("VerseRef", first_ref) continue para = ET.SubElement(section_content, "p") para.text = content append_references_section(contents) tree = ET.ElementTree(comment_list) filepath = "uploadsFork/Notes_Biblical_Culture_Notes_2017{}.xml".format(language) tree.write(filepath, encoding="utf-8", xml_declaration=True) with open(filepath, 'r') as f: text = f.read() text = vkbeautify.xml(text) with open(filepath, 'wb') as f: f.write(text.replace('\r', '').replace('\n', '\r\n')) print "Output: " + filepath