|
| 1 | +from distutils.dir_util import copy_tree |
| 2 | +from bs4 import BeautifulSoup |
| 3 | +from lxml import objectify, etree |
| 4 | + |
| 5 | +from xml.dom import minidom |
| 6 | +import os |
| 7 | + |
| 8 | +class PlottingTemplate: |
| 9 | + def __init__(self,ws,lib,cell,view) -> None: #for pin terminals |
| 10 | + self.ws = ws |
| 11 | + self.lib = lib |
| 12 | + self.cell = cell |
| 13 | + self.view = view |
| 14 | + |
| 15 | + pass |
| 16 | + |
| 17 | + #Import ploting template from existing maestro plotting template |
| 18 | + def import_plotting_template(self, pt_lib, pt_cell, pt_view, pTemplate): |
| 19 | + |
| 20 | + |
| 21 | + #copy the exisitng template |
| 22 | + print(self.ws['getWorkingDir']()); |
| 23 | + cellview = self.ws.dd.GetObj(pt_lib,pt_cell,pt_view) #get the cellview for a filelocation on disk |
| 24 | + path = cellview.write_path #get filepath from cellview |
| 25 | + print(path); |
| 26 | + filepath = path +"/plottingTemplates/"+ pTemplate + "/" + pTemplate+ "_0.cmpt"; |
| 27 | + with open(filepath, 'r') as f: |
| 28 | + file = f.read() |
| 29 | + soup = BeautifulSoup(bytes(file, encoding='utf8'),"xml") |
| 30 | + xml_object = objectify.parse(filepath) #get_etree |
| 31 | + |
| 32 | + self.root = objectify.fromstring(bytes(file, encoding='utf8')) |
| 33 | + attrib = self.root.attrib |
| 34 | + #Now that we have the XML we should convert to (remove excess data) a YML/Object format for saving |
| 35 | + self.root |
| 36 | + self.soup = soup |
| 37 | + |
| 38 | + #get Markers? |
| 39 | + |
| 40 | + print(soup); |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + #export the loaded template to new maestro cellview |
| 45 | + def export_plotting_template(self,pt_lib,pt_cell,pt_view,pTemplate): |
| 46 | + |
| 47 | + cellview = self.ws.dd.GetObj(pt_lib,pt_cell,pt_view) #get the cellview for a filelocation on disk |
| 48 | + path = cellview.write_path #get filepath from cellview |
| 49 | + #MIN DATA FOR CMPT.group file |
| 50 | + |
| 51 | + # <?xml version="1.0" encoding="UTF-8" ?> |
| 52 | + # <!--StateFileType saveLoadState--> |
| 53 | + # <topLevel> |
| 54 | + # <IndexedProp Name="graphFileList"> |
| 55 | + # <SimpleProp Value="Template2_0.cmpt" Type="stringValue" /> |
| 56 | + # </IndexedProp> |
| 57 | + # </topLevel> |
| 58 | + |
| 59 | + # file = open(filename, 'w',encoding='utf8') |
| 60 | + # file.write(str(self.soup)) |
| 61 | + # file.close() |
| 62 | + |
| 63 | + filename = path +"/plottingTemplates/"+ pTemplate + "/" + pTemplate+ "_0.cmpt"; |
| 64 | + os.makedirs(os.path.dirname(filename), exist_ok=True) |
| 65 | + #for now just relay the template |
| 66 | + file = open(filename, 'w',encoding='utf8') |
| 67 | + file.write(str(self.soup)) |
| 68 | + file.close() |
| 69 | + |
| 70 | + etree.tostring(self.root, pretty_print=True) |
| 71 | + self.updateMaestroXML(pt_lib,pt_cell,pt_view,pTemplate) |
| 72 | + |
| 73 | + # root = minidom.Document() |
| 74 | + |
| 75 | + # xml = root.createElement('root') |
| 76 | + # root.appendChild(xml) |
| 77 | + |
| 78 | + # productChild = root.createElement('product') |
| 79 | + # productChild.setAttribute('name', 'Geeks for Geeks') |
| 80 | + |
| 81 | + # xml.appendChild(productChild) |
| 82 | + |
| 83 | + # xml_str = root.toprettyxml(indent ="\t") |
| 84 | + |
| 85 | + # save_path_file = "gfg.xml" |
| 86 | + |
| 87 | + def updateMaestroXML(self,pt_lib,pt_cell,pt_view,pTemplate): |
| 88 | + cellview = self.ws.dd.GetObj(pt_lib,pt_cell,pt_view) #get the cellview for a filelocation on disk |
| 89 | + path = cellview.write_path #get filepath from cellview |
| 90 | + filepath = path + "/maestro.sdb" |
| 91 | + with open(filepath, 'r') as f: |
| 92 | + file = f.read() |
| 93 | + |
| 94 | + maestroXML = BeautifulSoup(bytes(file, encoding='utf8'),"xml") |
| 95 | + plottingOptions = maestroXML.find_all("plottingoption") |
| 96 | + #check if plotting templates are an option |
| 97 | + plottingTemplates = [option for option in plottingOptions if "allplottingtemplate" in option.next] #find the plotting templates option |
| 98 | + |
| 99 | + #TODO check if there isnt a plotting templates already |
| 100 | + if(plottingTemplates is None): |
| 101 | + pass |
| 102 | + plottingTemplates = plottingTemplates[0] |
| 103 | + #plottingTemplates.append("<") |
| 104 | + |
| 105 | + new_plottingtemplate = maestroXML.new_tag("plottingtemplate") |
| 106 | + #<templatelocation>/orpheus_tests/templatetest1/maestro/plottingTemplates/</templatelocation> |
| 107 | + new_plottingtemplate.string = pTemplate |
| 108 | + location =maestroXML.new_tag("templatelocation") |
| 109 | + templates_filepath = path +"/plottingTemplates/"; |
| 110 | + location.string = templates_filepath |
| 111 | + new_plottingtemplate.append(location) |
| 112 | + plottingTemplates.append(new_plottingtemplate) |
| 113 | + |
| 114 | + print(maestroXML) |
| 115 | + print(plottingOptions) |
| 116 | + |
| 117 | + file = open(filepath, 'w') |
| 118 | + file.write(str(maestroXML)) |
| 119 | + file.close() |
0 commit comments