Esempio n. 1
0
    def emit(self, removal = False):
        u"""
        インデックスファイルを更新する.
        """
        self.lock.acquire()
        try:
            doc = None
            if os.path.isfile(self.filename):
                doc = ElementTree(file = self.filename)
            else:
                root = Element("index")
                doc = ElementTree(root)

            index = -1
            for i, e in enumerate(doc.findall("live")):
                if e.get("server", "") == self.server_address:
                        index = i
                        doc.getroot().remove(e)

            if self.on_list:
                doc.getroot().insert(index, self.live_element())

            f = open(self.filename, "w")
            f.write('<?xml version="1.0" encoding="%s"?>\n' % self.encoding)
            doc.write(f, self.encoding)
            f.close()
        finally:
            self.lock.release()
Esempio n. 2
0
    def add_text_layer(self,pdf, hocrfile, page_num,height, dpi):
      """Draw an invisible text layer for OCR data"""
      p1 = re.compile('bbox((\s+\d+){4})')
      p2 = re.compile('baseline((\s+[\d\.\-]+){2})')
      hocr = ElementTree()
      hocr.parse(hocrfile)
      logging.debug(xml.etree.ElementTree.tostring(hocr.getroot()))
      for c in hocr.getroot():  # Find the <body> tag
          if c.tag != 'body':
              continue
      for page in c: # Each child in the body is a page tag
          if (page.attrib['class'] != "ocr_page"):
              assert ("Why is this hocr not paging properly??")
          if page.attrib['id'] == 'page_%d' %(page_num):
              break

      for line in page.findall(".//{http://www.w3.org/1999/xhtml}span"):
      #for line in page.findall(".//span"):
        if line.attrib['class'] != 'ocr_line':
          continue
        linebox = p1.search(line.attrib['title']).group(1).split()

        try:
          baseline = p2.search(line.attrib['title']).group(1).split()
        except AttributeError:
          baseline = [ 0, 0 ]

        linebox = [float(i) for i in linebox]
        baseline = [float(i) for i in baseline]

        for word in line:
          if word.attrib['class'] != 'ocrx_word':
            continue
          word_text = []
          for child in word.iter():
              if child.text:
                  word_text.append(child.text)
          word.text = ' '.join(word_text)
          logging.debug(word.text)
          #for child in word:
             #if child.tag:
                 #word.text = child.text

          if word.text is None:
            continue
          font_width = pdf.stringWidth(word.text.strip(), 'invisible', 8)
          if font_width <= 0:
            continue
          box = p1.search(word.attrib['title']).group(1).split()
          box = [float(i) for i in box]
          b = self.polyval(baseline, (box[0] + box[2]) / 2 - linebox[0]) + linebox[3]
          text = pdf.beginText()
          text.setTextRenderMode(3)  # double invisible
          text.setFont('invisible', 8)
          text.setTextOrigin(box[0] * 72 / dpi, height - b * 72 / dpi)
          box_width = (box[2] - box[0]) * 72 / dpi
          text.setHorizScale(100.0 * box_width / font_width)
          text.textLine(word.text.strip())
          #logging.debug( "Pg%s: %s" % (page_num,word.text.strip()))
          pdf.drawText(text)
Esempio n. 3
0
def read_addon(dir, deps=None):
	plugin=False
	if not deps:
		deps = {}
		plugin=True
	xml = os.path.join(dir, 'addon.xml')
	
	if os.path.isfile(xml):
#		addon = ElementTree()
#		addon.parse(xml)
		addon = ElementTree(fromstring(quickesc(xml)))
		script = None
		if plugin:
			try: script = addon.find('.//extension[@point="xbmc.python.pluginsource"]').attrib['library']
			except AttributeError: return None
		addonsdir = os.path.dirname(dir)
		try:
			# set any required lib deps
			for mod in addon.findall('.//requires/import'):
				dep = mod.attrib['addon']
				if dep != 'xbmc.python' and not dep in deps:
					deps[dep] = os.path.join(addonsdir, dep, 'lib')
					# check for sub-deps
					read_addon(os.path.join(addonsdir, dep), deps)
			if plugin:
				id = addon.getroot().attrib['id']
				name = addon.getroot().attrib['name']
				thumb = os.path.join(dir, 'icon.png')
				script = os.path.join(dir, script)
				return id, name, script, thumb, os.path.pathsep.join(deps.values())
		except:
			traceback.print_exc(file=sys.stdout)
	return None
    def extractElement(self, elementpath, respdata):

        try:
            tree = ElementTree()
            tree.parse(StringIO(respdata))
        except:
            return None

        # Strip off the top-level item
        if elementpath[0] == '/':
            elementpath = elementpath[1:]
            splits = elementpath.split('/', 1)
            root = splits[0]
            if tree.getroot().tag != root:
                return None
            elif len(splits) == 1:
                return tree.getroot().text
            else:
                elementpath = splits[1]

        e = tree.find(elementpath)
        if e is not None:
            return e.text
        else:
            return None
Esempio n. 5
0
 def remove_input_from_fjr(self, list_of_good_files):
         """
           to remove the input file from fjr in the case of problem with lfn
         """
         from xml.etree.ElementTree import ElementTree, Element
         new_good_list = []               
         no_inp_dir = self.fjrDirectory + 'no_inp'
         if not os.path.isdir(no_inp_dir):
             try:
                 os.mkdir(no_inp_dir)
                 print "no_inp_dir = ", no_inp_dir
             except:
                 print "problem during no_inp_dir creation: ", no_inp_dir
         for file in  list_of_good_files:
             name_of_file = os.path.basename(file)
             #print "name_of_file = " , name_of_file
             oldxmlfile = ElementTree()
             oldxmlfile.parse(file)
             newxmlfile = ElementTree(Element(oldxmlfile.getroot().tag))
             self.recurse(oldxmlfile.getroot(), newxmlfile.getroot())
             new_good_file = no_inp_dir + '/' + name_of_file
             newxmlfile.write(new_good_file)
             new_good_list.append(new_good_file)
         print "new_good_list = ", new_good_list    
         return new_good_list   
def GetDetails(ids,db):

    responses=[]
    start = time()
    for ID in ids:
        #print ID
        request_time = time()-start
        if request_time<1:
            sleep(1-request_time)
            #print "sleeping "+str(1-request_time)

        tree = ElementTree()

        tree.parse(StringIO(amazon.ItemLookup(ItemId=ID,ResponseGroup="OfferFull,ItemAttributes",Condition="New")))

        namespace = tree.getroot().tag[:tree.getroot().tag.find("}")+1]

        start=time()
        if len([t for t in tree.iter(namespace+"Errors")])>0:
            print "item missing or other error"
            pass
        if [t for t in tree.iter(namespace+'TotalOffers')][0].text=='0':
            print "no offers for item "+ID
            pass
        else:
            #
            offers_url = [t for t in tree.iter(namespace+'MoreOffersUrl')][0].text

            url = [t for t in tree.iter(namespace+'DetailPageURL')][0].text
            title = [t for t in tree.iter(namespace+'Title')][0].text
            if len([t for t in tree.iter(namespace+'Merchant')]) > 0:
                merchant = list([t for t in tree.iter(namespace+'Merchant')][0])[0].text
            else:
                merchant = ''
            try:
                price, shipping_price = soupThePrices(offers_url)
            except Exception as e:
                exc_type, exc_value, exc_traceback = sys.exc_info()
                y=traceback.format_exception(exc_type, exc_value, exc_traceback)
                price =  float(list([t for t in tree.iter(namespace+"Price")][0])[0].text)/100.0
                if merchant == "Amazon.com":
                    shipping_price = 0
                else:
                    shipping_price = '???'

            if merchant == "Amazon.com":
                prime = True
            else:
                prime = False

            data = {'Title':title,'Price':price,'URL':url,'ID':ID,'Shipping':shipping_price,'Prime': prime, 'SS': getSS(url)}


            responses.append(data)

    responses.sort(key=lambda student: student['Price'])


    return responses
Esempio n. 7
0
 def parse(self):
     tree=ET()
     tree.parse(self.path)
     print(tree.getroot())
     list=[]
     for account in tree.getroot().getchildren():
         print(account.find("name").text)
         print(account.find("account").text)
         print(account.find("mobile").text)
         print(account.find("id").text)
Esempio n. 8
0
def buildMatrix(xmlFile):
    with open (xmlFile, 'r') as inFile:
        elTree = ElementTree()
        elTree.parse(inFile)
    
    #number of nodes in net
    n = len(elTree.getroot().findall('net'))
    
    #building oriented graph
    A = [[float('inf') for j in range (n)] for i in range (n)]

    for resistors in elTree.getroot().findall('resistor'):
        i = int(resistors.attrib['net_from']) - 1
        j = int(resistors.attrib['net_to']) - 1
        r = float(resistors.attrib['resistance'])
        A[i][j] = 1/(1/A[i][j] + 1/r)
        A[j][i] = 1/(1/A[j][i] + 1/r)

    for diodes in elTree.getroot().findall('diode'):
        i = int(diodes.attrib['net_from']) - 1
        j = int(diodes.attrib['net_to']) - 1
        A[i][j] = 1/(1/A[i][j] + 1/float(diodes.attrib['resistance']))
        A[j][i] = 1/(1/A[j][i] + 1/float(diodes.attrib['reverse_resistance']))

    for capactors in elTree.getroot().findall('capactor'):
        i = int(capactors.attrib['net_from']) - 1
        j = int(capactors.attrib['net_to']) - 1
        r = float(capactors.attrib['resistance'])
        A[i][j] = 1/(1/A[i][j] + 1/r)
        A[j][i] = 1/(1/A[j][i] + 1/r)

    #graph is built
        
    #searching for the shortest ways

    for i in range(n):
        A[i][i] = 0;

    #тут был лишний цикл, который фактически приравнивал результирующую матрицу к начальной, его я вырезал

    for k in range(n):
        for i in range(n):
            for j in range(n):
                d1 = A[i][j]
                d2 = A[i][k] + A[k][j]
                
                if d1 == 0 or d2 == 0:
                    A[i][j] = 0
                else:
                    if (d1 != float('inf') or d2 != float('inf')):
                       A[i][j] = 1/(1/d1 + 1/d2)
                

    return n, A
def cleanRuns(styleneRUNS):
	'''
	If you do not remove the previous runs from styleneRUNS, Stylene will produce
	ERRORS.

	'''
	print styleneRUNS
	tree = ElementTree()
	tree.parse(styleneRUNS)
	tree.getroot().clear()
	tree.write(styleneRUNS, encoding='utf-8')
Esempio n. 10
0
    def reconstruirGame(self,nick):
        #agregar players al game
        treeP = ET()
        treeP.parse(self.playersFile + ".xml")
        root = treeP.getroot()
        self.game = Game(len(root.findall("team/player")),0,len(root.findall('team')))
        for team in root.iter("team"):
            color = team.attrib["color"]
            for tagPlayer in team.iter("player"):
                nombre = tagPlayer.attrib["nombre"]
                x = int(tagPlayer.attrib["iniX"])
                y = int(tagPlayer.attrib["iniY"])
                vidas = int(tagPlayer.attrib["vidas"])
                if nombre == nick:
                    player = Player(nombre, vidas, x, y, tagPlayer.text)
                else:
                    player = PlayerSemiAutomatico(nombre, vidas, x, y, tagPlayer.text)
                player.setTeam(color)
                self.game.AgregarPlayer(player)
        #-------------------------------------------------------------
        treeM = ET()
        treeM.parse(self.mapaFile+".xml") #revisar donde se generara el XML
        root = treeM.getroot()
        config = root.find("config")

        mapa = Mapa("currentGame")
        self.game.tiempo = int(config.attrib["tiempo"])
        mapa.setFondo(config.attrib["BG"])
        mapa.setMusica(config.attrib["BGSound"])
        mapa.stage = config.attrib["stage"]
        print "STAGE!!!!: " + mapa.stage 
        tablero = root.find("tablero")
        cuadros = tablero.find("cuadros")
        for cuadro in cuadros.iter("cuadro"):
            fila = int(cuadro.attrib["fila"])
            col = int(cuadro.attrib["columna"])
            dest = cuadro.attrib["tipo"]
            
            bloque = Bloque(fila, col, dest=="D")
            bloque.setSpriteB(cuadro.text)
            bloque.setPowerUp(cuadro.attrib["tipoPower"], cuadro.attrib["powerup"])
            bloque.tienePowerUp = not(cuadro.attrib["powerup"] == "__EMPTY__")
            if mapa.stage == "STAGE3_4":
                bloque.autoDisappear = False
                bloque.destruir()
            if not mapa.bloques.has_key(fila):
                mapa.bloques[fila] = dict()
            mapa.bloques[fila][col] = bloque

        self.game.mapa = mapa
        GameOverLogic.game = self.game

        return self.game
Esempio n. 11
0
def ScanItemsXML(filename):
    result = []
    file1 = ElementTree(file=filename)
    for item1 in filter(lambda it: it.get('name'), file1.getroot()):
        file2 = ElementTree(file=item1.get('name'))
        for item2 in filter(lambda it: it.get('name'), file2.getroot()):
            file3 = ElementTree(file=item2.get('name'))
            for item3 in file3.getroot():
                name = item3.get('name')
                id_ = item3.get('id')
                if name is not None and id_ is not None and int(id_) > 0:
                    result.append((id_, name))
    return result
 def toXML(self):
     """
     Returns the metadata in the XML format
     """
     Data = self.Data.copy()
     root = Data
     # serialize the root node and return it
     tree = ElementTree(Element('Images'))
     tree.getroot().set('file',Data['children'][0]['value'])
     self.serializeXML(root, tree.getroot())
     # prettify XML and return it
     unformattedXML = tostring(tree.getroot(),encoding='utf8')
     reparsedXML = etree.fromstring(unformattedXML)
     return etree.tostring(reparsedXML, pretty_print = True)
Esempio n. 13
0
File: Factory.py Progetto: Y--/root
def GetDeepNetworkOld(xml_file, returnObj=False):
    tree = ElementTree()
    tree.parse(xml_file)
    roottree = tree.getroot()
    network  = {}
    network["variables"] = []
    for v in roottree.find("Variables"):
        network["variables"].append(v.get('Title'))
    layout = roottree.find("Weights").find("Layout")
    net    = []
    for layer in layout:
        net.append({"Connection": layer.get("Connection"),
        "Nodes": layer.get("Nodes"),
        "ActivationFunction": layer.get("ActivationFunction"),
        "OutputMode": layer.get("OutputMode")
         })
    network["layers"] = net
    Synapses = roottree.find("Weights").find("Synapses")
    synapses = {
        "InputSize": Synapses.get("InputSize"),
        "OutputSize": Synapses.get("OutputSize"),
        "NumberSynapses": Synapses.get("NumberSynapses"),
        "synapses": []
    }
    for i in Synapses.text.split(" "):
        tmp = i.replace("\n", "")
        if len(tmp)>1:
            synapses["synapses"].append(tmp)
    network["synapses"] = synapses
    if returnObj:
        return network
    return json.dumps(network)
Esempio n. 14
0
File: Factory.py Progetto: Y--/root
def GetNetwork(xml_file):
    tree = ElementTree()
    tree.parse(xml_file)
    roottree = tree.getroot()
    network  = {}
    network["variables"] = []
    for v in roottree.find("Variables"):
        network["variables"].append(v.get('Title'))
    layout = roottree.find("Weights").find("Layout")

    net    = { "nlayers": layout.get("NLayers") }
    for layer in layout:
        neuron_num = int(layer.get("NNeurons"))
        neurons    = { "nneurons": neuron_num }
        i = 0
        for neuron in layer:
            label = "neuron_"+str(i)
            i    += 1
            nsynapses = int(neuron.get('NSynapses'))
            neurons[label] = {"nsynapses": nsynapses}
            if nsynapses == 0:
                break
            text = str(neuron.text)
            wall = text.replace("\n","").split(" ")
            weights = []
            for w in wall:
                if w!="":
                    weights.append(float(w))
            neurons[label]["weights"] = weights
        net["layer_"+str(layer.get('Index'))] = neurons
    network["layout"] = net
    return json.dumps(network)
Esempio n. 15
0
def getTaskList(filename):
    doc = ElementTree(file=filename)
    tasks = doc.getroot()
    taskDict = {}
    for task in tasks.findall("task"):
        name = task.get("name")
        deal = task.get("deal")
        depend = task.get("depend").strip()
        execution = task.find("exec")
        execType = execution.get("type")
        sqlstr = execution.text.strip()
        str = []
        str = [
            command.strip().strip("\n")
            for command in sqlstr.split(";")
            if command.strip("-").strip(" ").strip("\n") != ""
        ]
        # 		print str
        dependList = []
        dependList = [t.strip() for t in depend.split(",") if t.strip(" ") != ""]
        if execType == "proc":
            task = ProcTask(name, dependList, deal, log)
            proc = [[getProc(p), getProcParam(p)] for p in str if getProc(p) != ""]
            task.setProc(proc)

        elif execType == "sql":
            task = SQLTask(name, dependList, deal, log)
            task.setSQL(str)
        taskDict[task.getName()] = task
    return taskDict
Esempio n. 16
0
def testcase_XML(webappName):
    tree = ElementTree()
    tree.parse(const.path + "/tests.result.xml")
    root = tree.getroot()
    lst_node = root.getiterator("set")
    for node in lst_node:
        SubElement(
            node,
            "testcase",
            {
                "component": "core",
                "purpose": "Check if Packaged Web Application can be installed/launch/uninstall successfully",
                "execution_type": "auto",
                "id": webappName,
            },
        )
        cnode = root.getiterator("testcase")
        desnode = cnode[-1]
        SubElement(desnode, "description")
        entrynode = desnode[0]
        SubElement(entrynode, "test_script_entry", {"test_script_expected_result": "0"})
        entryentrynode = root.getiterator("test_script_entry")
        entr = entryentrynode[-1]
        entr.text = (
            "app_user@/opt/usr/media/tct/opt/wrt-manifest-tizen-tests/appinstall.sh " + webappName + "." + Pack_Type
        )
        tree.write(const.path + "/tests.result.xml")
Esempio n. 17
0
def read_config(config_file):
    tree = ElementTree()
    tree.parse(config_file)
    root = tree.getroot()
    server = root.attrib['name']
    server_vm = root.attrib['virtual_machine']
    protocals = root.getchildren()
    acnt = [0,0,0,0]
    cur_datetime = datetime.now()
    guacamole_client_list=[]
    for protocal in protocals:
        pro_name = protocal.attrib['name']
        clients = protocal.getchildren()
        cnt = 0
        for client in clients:
            cnt+=1
            client_name = client.attrib['name']
            client_host = client[0].text
            client_vm = client[1].text
            guacamoleClientInfo = GuacamoleClientInfo('','',server,client_name,pro_name,client_host,client_vm,0,cur_datetime)
            guacamole_client_list.append(guacamoleClientInfo)
        if pro_name=='vnc':
            acnt[0] = cnt
        elif pro_name=='vnc-read-only':
            acnt[1] = cnt
        elif pro_name=='ssh':
            acnt[2] = cnt
        else:
            acnt[3] = cnt
    
    guacamoleServerLoad = GuacamoleServerLoad(server,server_vm,acnt[0],acnt[1],acnt[2],acnt[3],sum(acnt),cur_datetime,0)
    guacamoleServerLoad.guacamole_client_info = guacamole_client_list
    return guacamoleServerLoad
Esempio n. 18
0
    def update_show_indexer_metadata(self, show_obj):
        if self.show_metadata and show_obj and self._has_show_metadata(show_obj):
            sickrage.srLogger.debug(
                    "Metadata provider " + self.name + " updating show indexer info metadata file for " + show_obj.name)

            nfo_file_path = self.get_show_file_path(show_obj)

            try:
                with io.open(nfo_file_path, 'rb') as xmlFileObj:
                    showXML = ElementTree(file=xmlFileObj)

                indexerid = showXML.find('id')

                root = showXML.getroot()
                if indexerid is not None:
                    indexerid.text = str(show_obj.indexerid)
                else:
                    SubElement(root, "id").text = str(show_obj.indexerid)

                # Make it purdy
                indentXML(root)

                showXML.write(nfo_file_path, encoding='UTF-8')
                chmodAsParent(nfo_file_path)

                return True
            except IOError as e:
                sickrage.srLogger.error(
                        "Unable to write file to " + nfo_file_path + " - are you sure the folder is writable? {}".format(e.message))
def parse_taylor_info(taylor_info_file, scene_blocks):
    
    print 'Parsing: ' + taylor_info_file
    
    #parse xml file
    taylor_tree = ElementTree();
    taylor_tree.parse(taylor_info_file);
      
    #find scene paths
    scenes_elm = taylor_tree.getroot().findall('scene');
    
    if scenes_elm is None:
      print "Invalid taylor info file: No scenes element"
      sys.exit(-1);
    
    for s in range(0, len(scenes_elm)):
    
        scene_name = scenes_elm[s].get("path")
        
        if scene_name is None:
            print "Invalid taylor info file: Error parsing scenes"
            sys.exit(-1);
        
        blocks = []; 
        blocks.append(s);       
        parse_scene(scene_name, blocks);
        scene_blocks.append(blocks);
Esempio n. 20
0
    def load_skos_vocab(self, fname):
        """Import a vocabulary into the DB from xml file fname in SKOS format"""
        doc = ElementTree()
        goto = '/'
        try:
            doc.parse(fname)
        except IOError:
            # In case I passed in a string, like in the upload script instead of a file object
            f = NamedTemporaryFile(delete=False)
            f.write(fname)
            f.close()
            doc.parse(f.name)
            os.unlink(f.name)
        except TypeError:
            self.message(40, 'Sorry, that wasn\'t a SKOS RDF file.')
            goto = '/vocabularies/load-skos'
            
        if doc.getroot().tag != TAG('rdf:RDF'):
            self.message(40, "We need a SKOS RDF file. Try again.")
            goto = '/vocabularies/load-skos'
        
        for vocab in doc.findall('.//'+TAG('skos:ConceptScheme')):
            vocab = self.load_vocab_instance(vocab)
            goto = vocab.get_absolute_url()

        for concept in doc.findall('.//'+TAG('skos:Concept')):
            self.load_concept_instance(concept)
        
        return goto
Esempio n. 21
0
def insert_to_Summary(sumaryfile,total_case,pass_case,pass_rate,fail_case,fail_rate,block_case,block_rate):
    try:
        tree = ElementTree()
        tree.parse(sumaryfile)
        root = tree.getroot()
        ntotal_case = root.getiterator("total_case")
        ntotal_case[0].text = str(total_case)
        npass_case = root.getiterator("pass_case")
        npass_case[0].text = str(pass_case)
        npass_case_rate = root.getiterator("pass_rate")
        npass_case_rate[0].text = str(pass_rate)
        nfail_case = root.getiterator("fail_case")
        nfail_case[0].text = str(fail_case)
        nfail_case_rate = root.getiterator("fail_rate")
        nfail_case_rate[0].text = str(fail_rate)
        nblock_case = root.getiterator("block_case")
        nblock_case[0].text = str(block_case) 
        nblock_case_rate = root.getiterator("block_rate")
        nblock_case_rate[0].text = str(block_rate)
        test_end_time = datetime.now().strftime('%m-%d-%H:%M:%S')
        ntest_start_time = root.getiterator("start_at")
        ntest_start_time[0].text = str(test_start_time)
        ntest_end_time = root.getiterator("end_at")
        ntest_end_time[0].text = str(test_end_time)
        device_id_get = root.getiterator("environment")
        device_id_get[0].set("device_id",Device_Ip)
        tree.write(sumaryfile)
    except Exception,e: 
        print Exception,"Insert to report/summart.xml -------------------------> error:",e
Esempio n. 22
0
    def LoadXml(self, xmlFilename = None, anrpBaseDirStr = None):
        #生成 XML 文件的绝对路径
        if xmlFilename == None:
            xmlFilename = self.packageName
        xmlFilename = '%s.anrp.rpd.xml' % xmlFilename
        if anrpBaseDirStr != None:
            xmlFilename = os.path.join(anrpBaseDirStr, xmlFilename)

        tree = ElementTree()
        tree.parse(xmlFilename)

        resPackageDefinedRoot = tree.getroot()

        self.packageName = resPackageDefinedRoot.find('packageName').text
        self.type = resPackageDefinedRoot.find('type').text

        self.resSourceList = []
        for resSourceX in resPackageDefinedRoot.findall('resSourceList'):
            resSourceDict = {}
            if anrpBaseDirStr == None:
                resSourceDict['resPath'] = resSourceX.get('resClass')
            else:
                resSourceDict['resPath'] = os.path.join(anrpBaseDirStr, resSourceX.get('resClass'))
            resSourceDict['resName'] = resSourceX.get('resId')

            self.resSourceList.append(resSourceDict)

        return
 def _render(self, parent):
     qrcode = self._generate()
     if not qrcode:
         return
     
     # Parse SVG and draw elements to the workspace
     output = StringIO()
     qrcode.save(output)
     output.seek(0)
     tree = ElementTree()
     tree.parse(output)
     root = tree.getroot()
 
     vbox = map(int, root.get("viewBox").split())
     vbox = vbox[0]-self.options.padding*self.options.size/10, \
            vbox[1]-self.options.padding*self.options.size/10, \
            vbox[2]+2*self.options.padding*self.options.size/10, \
            vbox[3]+2*self.options.padding*self.options.size/10
     vbox = map(str, vbox)
 
     rect = inkex.etree.SubElement(
             parent,
             inkex.addNS('rect', 'svg'),
             {"x": vbox[0],
              "y": vbox[1],
              "width": vbox[2],
              "height": vbox[3],
              "style": "fill:#fff;"})
     for m in root.getchildren():
         attribs = {}
         for k in m.keys():
             attribs[k] = str(m.get(k))
         inkex.etree.SubElement(parent, inkex.addNS('path', 'svg'), attribs)
Esempio n. 24
0
def result_manifest_XML(result_manifest_xml_file_path,webappFile,auto_Result,manifest_cont):
    try:
        tree = ElementTree()
        tree.parse(result_manifest_xml_file_path + "/result/" + webappFile)
        root = tree.getroot()
        rset = root.getchildren() 
        for mset in rset:
            testcase = mset.findall("set")
            testcase[0].set("name",Test_Flag)
            for mtestcase in testcase:
                cnode = mtestcase.getiterator("testcase")
                if (len(cnode)==1):
                    auto_result =  root.getiterator("auto_result")
                    #auto_result = cnode.getiterator("auto_result")
                    auto_result[0].text = auto_Result
                else:
                    if (len(cnode)==0):
                        SubElement(mtestcase,"testcase", {'component':'Runtime Core','purpose':'Check if packaged web application can be installed/launched/uninstalled successfully','execution_type' : 'auto', 'id' : webappFile.split(".")[0]})
                        result_node = mtestcase.find("testcase")
                        SubElement(result_node,"auto_result")
                        SubElement(result_node,"testcommand")
                        auto_node = result_node.find("auto_result")
                        auto_node.text = auto_Result
                        testcommand_node = result_node.find("testcommand")
                        testcommand_node.text = manifest_cont.decode("utf-8")
        tree.write(result_manifest_xml_file_path + "/result/" + webappFile)
    except Exception,e: 
        print Exception,"Generate manifest.xml error:",e 
Esempio n. 25
0
class ItemDB:
    """
    A simple class to look up information from the items.xml file.
    """
    def __init__(self):
        print "Loading ItemDB"
        self.item_names = {}
        self.itemdb_file = ElementTree(file="data/items.xml")

        for item in self.itemdb_file.getroot():
            if item.get('name'):
                file2 = ElementTree(file=item.get('name'))
                for item2 in file2.getroot():
                    if item2.get('name'):
                        file3 = ElementTree(file=item2.get('name'))
                        for item3 in file3.getroot():
                            item_struct = Item()
                            item_struct.name = item3.get('name')
                            item_struct.weight = int(item3.get('weight', 0))
                            if item3.get('type'):
                                item_struct.type = item3.get('type')
                                item_struct.description = item3.get('description')
                                self.item_names[int(item3.get('id'))] = item_struct

    def getItem(self, item_id):
        return self.item_names[item_id]

    def findId(self, name):
        for item_id in self.item_names:
            if self.item_names[item_id].name == name:
                return item_id
        return -10 #Not found
Esempio n. 26
0
    def process(self, request):
        """
        Process SOAP request
        """
        if request.body is not None and len(request.body) > 0:
            body = urllib.unquote_plus(request.body)
            tree = ElementTree(file=StringIO(body))
            envelope = tree.getroot()
            if envelope is None:
                raise InvalidRequestException('Invalid syntax')
            body = envelope.find("q:Body", namespaces=namespaces)
            if body is None:
                raise InvalidRequestException('Invalid syntax')

            soap_req = body.find("m:GetUserOofSettingsRequest",
                                 namespaces=namespaces)
            if soap_req is not None:
                return self.process_get_request(soap_req)

            soap_req = body.find("m:SetUserOofSettingsRequest",
                                 namespaces=namespaces)
            if soap_req is not None:
                return self.process_set_request(soap_req)

            raise InvalidRequestException('Unknown SOAP request')

        raise InvalidRequestException('No body in request')
def parse_scenes_info(scenes_info_file, model_dirs, ply_paths):
    
    print 'Parsing: ' + scenes_info_file
    
    #parse xml file
    bof_tree = ElementTree();
    bof_tree.parse(scenes_info_file);
    
    scenes_elm = bof_tree.getroot().findall('scene');
    
    if scenes_elm is None:
      print "Invalid bof info file: No scenes element"
      sys.exit(-1);
         
 
    #find scene paths
    for s in range(0, len(scenes_elm)):
        path = scenes_elm[s].get("output_dir");
        ply_file = scenes_elm[s].get("ply_path");
        
        if path is None:
            print "Invalid info file: Error parsing scene path"
            sys.exit(-1);
            
        if ply_file is None:
            print "Invalid info file: Error parsing ply_file"
            sys.exit(-1);
        
        model_dirs.append(path); 
        ply_paths.append(ply_file);  
Esempio n. 28
0
def parse_config(name):
    tree = ElementTree()
    tree.parse(name)
    items = []
    for item in list(tree.getroot()):
        items.append(process_element(item))
    return items
def qrcode_render(content, size, padding, version, em, ec, newline, parent):
    
    if newline:
        content = content.replace("\\n", "\n")
        content = content.replace("\\r", "\r")
    
    # Generate QR Code - call web service
    qrcode = qrcode_generate(content, size, padding, version, em, ec)
    #if not result:
    #    return
    
    # Parse SVG and draw elements to the workspace
    tree = ElementTree()
    tree.parse(StringIO(qrcode))
    root = tree.getroot()
    xmlns = "{http://www.w3.org/2000/svg}"
    modules = list(root.getiterator(xmlns + "rect"))
    for m in modules:
        # <rect x="32" y="32" width="8" height="8" style="fill:rgb(0,0,0);" />
        x = m.attrib["x"]
        y = m.attrib["y"]
        w = m.attrib["width"]
        h = m.attrib["height"]
        style = m.attrib["style"]
        qrcode_draw_module((w,h), (x,y), style, parent)
Esempio n. 30
0
    def __init__(self, xmlfile = None):
        tree = ElementTree()
        tree.parse(xmlfile)
        root = tree.getroot()

        self._serial = root[1].text
        self._sdate = root[2].text
        self._tdate = email.utils.parsedate(self._sdate)
        uccgrp = root[3]
        grpreg = root[4]

        for ucc in uccgrp:
            prefix = ucc[0].text
            for grp in ucc[2]:
                length = int(grp[1].text)
                start, end = grp[0].text.split('-')
                self._range_grp[prefix + start] = length
                self._range_grp[prefix + end] = length

        for grp in grpreg:
            prefix = grp[0].text.replace('-','')
            for reg in grp[2]:
                length = int(reg[1].text)
                start, end = reg[0].text.split('-')
                self._range_reg[prefix + start] = length
                self._range_reg[prefix + end] = length
Esempio n. 31
0
        'bamana/propernames', ToolboxCorpusReader, '.*\.txt', encoding='utf-8')

bailleul = LazyCorpusLoader(
        'bamana/bailleul', ToolboxCorpusReader, r'bailleul.txt', encoding='utf-8')

lexicon = ElementTree(bailleul.xml('bailleul.txt'))

for file in propernames.fileids():
    for e in ElementTree(propernames.xml(file)).findall('record'):
        ge = Element('ge')
        ge.text = e.find('lx').text
        e.append(ge)
        ps = Element('ps')
        ps.text = 'n.prop'
        e.append(ps)
        lexicon.getroot().append(e)

wl = {}
wl_detone = {}

def normalize_bailleul(word):
    return u''.join([c for c in word if c not in u'.-'])

for entry in lexicon.findall('record'):
    lemmas = [normalize_bailleul(entry.find('lx').text)]
    lemmas.extend([normalize_bailleul(e.text) for e in entry.findall('va')])
    try:
        ps = set(entry.find('ps').text.split('/'))
    except AttributeError:
        ps = set([])
    try:
Esempio n. 32
0
class firewalld:
    __TREE = None
    __ROOT = None
    __CONF_FILE = '/etc/firewalld/zones/public.xml'

    # 初始化配置文件XML对象
    def __init__(self):
        if self.__TREE: return
        self.__TREE = ElementTree()
        self.__TREE.parse(self.__CONF_FILE)
        self.__ROOT = self.__TREE.getroot()

    # 获取端口列表
    def GetAcceptPortList(self):
        mlist = self.__ROOT.getchildren()
        data = []
        for p in mlist:
            if p.tag != 'port': continue
            tmp = p.attrib
            port = p.attrib['port']
            data.append(tmp)
        return data

    # 添加端口放行
    def AddAcceptPort(self, port, pool='tcp'):
        # 检查是否存在
        if self.CheckPortAccept(pool, port): return True
        attr = {"protocol": pool, "port": port}
        Port = Element("port", attr)
        self.__ROOT.append(Port)
        self.Save()
        return True

    # 删除端口放行
    def DelAcceptPort(self, port, pool='tcp'):
        # 检查是否存在
        if not self.CheckPortAccept(pool, port): return True
        mlist = self.__ROOT.getchildren()
        m = False
        for p in mlist:
            if p.tag != 'port': continue
            if p.attrib['port'] == port:
                self.__ROOT.remove(p)
                m = True
        if m:
            self.Save()
            return True
        return False

    # 添加UDP端口放行
    def AddUpdPort(self, port, pool='udp'):
        # 检查是否存在
        if self.CheckPortAccept(pool, port): return True
        attr = {"protocol": pool, "port": port}
        Port = Element("port", attr)
        self.__ROOT.append(Port)
        self.Save()
        return True

    # 删除UDP端口放行
    def DelUdpPort(self, port, pool='udp'):
        # 检查是否存在
        if not self.CheckPortAccept(pool, port): return True
        mlist = self.__ROOT.getchildren()
        m = False
        for p in mlist:
            if p.tag != 'port': continue
            if p.attrib['port'] == port:
                self.__ROOT.remove(p)
                m = True
        if m:
            self.Save()
            return True
        return False

    # 检查端口是否已放行
    def CheckPortAccept(self, pool, port):
        for p in self.GetAcceptPortList():
            if p['port'] == port and p['protocol'] == pool: return True
        return False

    # 获取屏蔽IP列表
    def GetDropAddressList(self):
        mlist = self.__ROOT.getchildren()
        data = []
        for ip in mlist:

            if ip.tag != 'rule': continue
            tmp = {}
            ch = ip.getchildren()
            a = None
            for c in ch:
                tmp['type'] = None
                if c.tag == 'drop': tmp['type'] = 'drop'
                if c.tag == 'source':

                    tmp['address'] = c.attrib['address']
                if tmp['type']:
                    data.append(tmp)
        return data

    # 获取 reject 信息
    def GetrejectLIST(self):
        mlist = self.__ROOT.getchildren()
        data = []
        for ip in mlist:
            #print(ip)
            if ip.tag != 'rule': continue
            tmp = {}
            ch = ip.getchildren()
            a = None
            flag = None
            for c in ch:
                tmp['type'] = None
                if c.tag == 'reject': tmp['type'] = 'reject'
                if c.tag == 'source':

                    tmp['address'] = c.attrib['address']
                if c.tag == 'port':

                    tmp['protocol'] = c.attrib['protocol']
                    tmp['port'] = c.attrib['port']
                if tmp['type']:
                    data.append(tmp)
        return data

# 获取 accept 信息

    def Getacceptlist(self):
        mlist = self.__ROOT.getchildren()
        data = []
        for ip in mlist:

            if ip.tag != 'rule': continue
            tmp = {}
            ch = ip.getchildren()
            a = None
            flag = None
            for c in ch:
                tmp['type'] = None
                if c.tag == 'accept': tmp['type'] = 'accept'
                if c.tag == 'source':

                    tmp['address'] = c.attrib['address']
                if c.tag == 'port':
                    tmp['protocol'] = c.attrib['protocol']
                    tmp['port'] = c.attrib['port']
                if tmp['type']:
                    data.append(tmp)
        return data

# 获取所有信息

    def Get_All_Info(self):
        data = {}
        data['drop_ip'] = self.GetDropAddressList()
        data['reject'] = self.GetrejectLIST()
        data['accept'] = self.Getacceptlist()
        return data

# 判断是否存在

    def Chekc_info(self, port, address, pool, type):
        data = self.Get_All_Info()
        if type == 'accept':
            for i in data['accept']:
                #print(i['address'], i['protocol'], i['port'])
                if i['address'] == address and i['protocol'] == pool and i[
                        'port'] == port:
                    return True
                else:
                    return False
        elif type == 'reject':
            for i in data['accept']:
                # print(i['address'], i['protocol'], i['port'])
                if i['address'] == address and i['protocol'] == pool and i[
                        'port'] == port:
                    return True
                else:
                    return False
        else:
            return False

    def AddDropAddress(self, address):
        # 检查是否存在
        if self.CheckIpDrop(address): return True
        attr = {"family": 'ipv4'}
        rule = Element("rule", attr)
        attr = {"address": address}
        source = Element("source", attr)
        drop = Element("drop", {})
        rule.append(source)
        rule.append(drop)
        self.__ROOT.append(rule)
        self.Save()
        return 'OK'

    # 删除IP屏蔽
    def DelDropAddress(self, address):
        # 检查是否存在
        if not self.CheckIpDrop(address): return True
        mlist = self.__ROOT.getchildren()
        for ip in mlist:
            if ip.tag != 'rule': continue
            ch = ip.getchildren()
            for c in ch:

                if c.tag != 'source': continue
                if c.attrib['address'] == address:
                    self.__ROOT.remove(ip)
                    self.Save()
                    return True
        return False

# 添加端口放行并且指定IP

    def Add_Port_IP(self, port, address, pool, type):
        if type == 'accept':
            # 判断是否存在
            if self.Chekc_info(port, address, pool, type): return True
            attr = {"family": 'ipv4'}
            rule = Element("rule", attr)
            attr = {"address": address}
            source = Element("source", attr)
            attr = {'port': str(port), 'protocol': pool}
            port_info = Element("port", attr)
            accept = Element("accept", {})
            rule.append(source)
            rule.append(port_info)
            rule.append(accept)
            self.__ROOT.append(rule)
            self.Save()
            return True

        elif type == 'reject':
            # 判断是否存在
            if self.Chekc_info(port, address, pool, type): return True
            attr = {"family": 'ipv4'}
            rule = Element("rule", attr)
            attr = {"address": address}
            source = Element("source", attr)
            attr = {'port': str(port), 'protocol': pool}
            port_info = Element("port", attr)
            reject = Element("reject", {})
            rule.append(source)
            rule.append(port_info)
            rule.append(reject)
            self.__ROOT.append(rule)
            self.Save()
            return True
        else:
            return False

# 删除指定端口的=。=

    def Del_Port_IP(self, port, address, pool, type):
        if type == 'accept':
            a = None
            for i in self.__ROOT:
                if i.tag == 'rule':
                    tmp = {}
                    for c in i.getchildren():
                        tmp['type'] = None
                        if c.tag == 'accept': tmp['type'] = 'accept'
                        if c.tag == 'source':
                            tmp['address'] = c.attrib['address']
                        if c.tag == 'port':
                            tmp['protocol'] = c.attrib['protocol']
                            tmp['port'] = c.attrib['port']
                        if tmp['type']:
                            if tmp['port'] == port and tmp[
                                    'address'] == address and tmp[
                                        'type'] == type and tmp[
                                            'protocol'] == pool:
                                self.__ROOT.remove(i)
                        self.Save()
            return True

        elif type == 'reject':
            for i in self.__ROOT:
                if i.tag == 'rule':
                    tmp = {}
                    for c in i.getchildren():
                        tmp['type'] = None
                        if c.tag == 'reject': tmp['type'] = 'reject'
                        if c.tag == 'source':
                            tmp['address'] = c.attrib['address']
                        if c.tag == 'port':
                            tmp['protocol'] = c.attrib['protocol']
                            tmp['port'] = c.attrib['port']
                        if tmp['type']:
                            if tmp['port'] == port and tmp[
                                    'address'] == address and tmp[
                                        'type'] == type and tmp[
                                            'protocol'] == pool:
                                self.__ROOT.remove(i)
                                self.Save()
            return True

    # 检查IP是否已经屏蔽
    def CheckIpDrop(self, address):
        for ip in self.GetDropAddressList():
            if ip['address'] == address: return True
        return False

    # 取服务状态
    def GetServiceStatus(self):
        import psutil
        for pid in psutil.pids():
            if psutil.Process(pid).name() == 'firewalld': return True
        return False

    # 服务控制
    def FirewalldService(self, type):
        os.system('systemctl ' + type + ' firewalld.service')
        return public.returnMsg(True, 'SUCCESS')

    # 保存配置
    def Save(self):
        self.format(self.__ROOT)
        self.__TREE.write(self.__CONF_FILE, 'utf-8')
        os.system('firewall-cmd --reload')

    # 整理配置文件格式
    def format(self, em, level=0):
        i = "\n" + level * "  "
        if len(em):
            if not em.text or not em.text.strip():
                em.text = i + "  "
            for e in em:
                self.format(e, level + 1)
            if not e.tail or not e.tail.strip():
                e.tail = i
        if level and (not em.tail or not em.tail.strip()):
            em.tail = i
Esempio n. 33
0
    def export_skin_weights(self):

        format, ext = str(self.format.text()), str(self.formatExt.text())
        format = self.name_pat.sub( \
         lambda m: '%(name)' + m.group(1)[1:], format)
        path = Path(self.path.text())

        # Validate selection.
        sel = selected()
        if not sel:
            error('Selection is empty.')

        # Find the skin cluster.
        sc = mel.findRelatedSkinCluster(sel[0])
        skin_cluster = None
        for s in sel:
            sc = mel.findRelatedSkinCluster(s)
            if sc:
                skin_cluster = ls(sc)[0]
                break
        if not skin_cluster:
            error('No skin cluster found.')

        for mesh in sel:

            mesh_element = Element('Mesh', name=mesh)
            xml_tree = ElementTree(mesh_element)

            sc = mel.findRelatedSkinCluster(mesh)
            if not sc: continue
            sc = ls(sc)[0]

            influences = sc.influenceObjects()
            inf_tag = SubElement(mesh_element, 'Influences')
            for i, inf in enumerate(influences):
                SubElement(inf_tag, 'Influence', index=str(i), name=inf)
            #joints = ls(ios, type='joint')
            #if len(joints) < len(ios):
            #	error('Remove non-joint influences before exporting to Massive.')

            # TODO: progress bar

            name = format % dict(name=mesh.name().replace('|', '') \
             .replace(':', '.')) + ext
            with open(path / name, 'w') as f:

                #f.write(os.linesep + '# influences')
                #for i, inf in enumerate(ios):
                #	if inf in influences:
                #		inf_index = influences.index(j)
                #	else:
                #		influences += (inf,)
                #		inf_index = len(influences)
                #f.write('%sdeformer %d %s' % (os.linesep, i, inf))

                #f.write(os.linesep)

                vertices = SubElement(mesh_element, 'Vertices')

                #f.write(os.linesep + '# weights')
                for i, vtx in enumerate(mesh.vtx):
                    vertex = SubElement(vertices,
                                        'Vertex',
                                        pos=str(vtx.getPosition()))
                    #f.write('%s%d: ' % (os.linesep, vi))
                    for ii, inf in enumerate(influences):
                        weight_val = skinPercent(sc,
                                                 '%s.vtx[%d]' % (mesh, i),
                                                 transform=inf,
                                                 query=True)
                        if weight_val:
                            SubElement(vertex,
                                       'Weight',
                                       influence=str(ii),
                                       value=str(weight_val))
                            #f.write(' %d %f' % (ii, weight_val))

                #f.write(os.linesep + ':')
                rough_string = tostring(xml_tree.getroot(), 'utf-8')
                reparsed = minidom.parseString(rough_string)
                f.write(reparsed.toprettyxml(indent='\t'))
Esempio n. 34
0
    selectStateWithEle = createTableStateEle.find('.//selectStatementWithCTE')

    ## 找到 全部 with 表
    withClauseEle = selectStateWithEle.find('withClause')
    if withClauseEle is not None:
        for cteStatementEle in withClauseEle.findall('cteStatement'):

            tmpTableName = cteStatementEle.find('identifier').text

            #-记录临时表名
            tmptableSElt = SubElement(createSElt, 'Tmptable')
            tmptablenameSElt = SubElement(tmptableSElt, 'tableName')
            tmptablenameSElt.text = tmpTableName

            selectStateElt = cteStatementEle.find('.//regularBody')
            get_columnList(selectStateElt, tmptableSElt)
            get_formList(selectStateElt, tmptableSElt)
            get_where(selectStateElt, tmptableSElt)

    selectSElt = SubElement(createSElt, 'Select')

    get_columnList(selectStateWithEle, selectSElt)
    get_formList(selectStateWithEle, createSElt)
    get_where(selectStateWithEle, createSElt)

    ##对生成后的xml美化
    newtree = ElementTree(createSElt)
    root = newtree.getroot()
    prettyXml(root, '\t', '\n')
    newtree.write('output.xml')
Esempio n. 35
0
def draw_colorbar(tree: ET.ElementTree,
                  colormap: matplotlib.cm.ScalarMappable,
                  invert: bool,
                  voxels_per_mm3: float,
                  title="counts/mm³"):
    """
    Expand the SVG to the right and put a colorbar there.
    :param tree: The XML element tree
    :param colormap: the color map to draw
    :param invert: Invert the color map intensities when labeling
    :param voxels_per_mm3: # of voxels per cubic millimeter
    """
    imin, imax = colormap.get_clim()
    imin_scale = imin * voxels_per_mm3
    imax_scale = imax * voxels_per_mm3
    if invert:
        imax_scale, imin_scale = -imin_scale, -imax_scale
        imin, imax = imax, imin
    svg = tree.getroot()
    g = svg.getchildren()[0]
    background_rect = [_ for _ in g.getchildren() if _.tag.endswith("rect")][0]
    old_width = int(svg.attrib["width"])
    new_width = old_width + 100
    svg.attrib["width"] = new_width
    height = int(svg.attrib["height"]) - 30
    top = 10
    left = old_width

    background_rect.attrib["width"] = str(new_width)
    svg.attrib["width"] = str(new_width)
    style_elem = ET.Element(STYLE_TAG)
    style_elem.text = """
    .title { font: bold 8pt sans-serif;}
    .annotation {font: bold 5pt sans-serif;}
    """
    svg.insert(0, style_elem)
    n_rects = 50
    rect_height = height / n_rects
    rect_y = np.linspace(20, 20 + height, 50)
    colors = \
        (colormap.to_rgba(np.linspace(imax, imin, 50)) * 255).astype(int)
    for y, (red, green, blue, alpha) in zip(rect_y, colors):
        relem = ET.Element(
            RECT_TAG,
            dict(x=str(old_width),
                 y=str(y),
                 width="50",
                 height=str(rect_height),
                 fill="#%02x%02x%02x" % (red, green, blue)))
        relem.attrib["stroke-width"] = "0"
        g.append(relem)
    title_elem = ET.Element(TEXT_TAG, dict(x=str(old_width), y="15"))
    title_elem.attrib["class"] = "title"
    title_elem.text = title
    g.append(title_elem)
    max_elem = ET.Element(TEXT_TAG, dict(x=str(old_width + 52), y="25"))
    max_elem.attrib["class"] = "annotation"
    max_elem.text = str(int(imax_scale))
    g.append(max_elem)
    min_elem = ET.Element(TEXT_TAG,
                          dict(x=str(old_width + 52), y=str(height + 20)))
    min_elem.attrib["class"] = "annotation"
    min_elem.text = str(int(imin_scale))
    g.append(min_elem)
Esempio n. 36
0
def main(argv=sys.argv[1:]):
    parser = argparse.ArgumentParser(
        description='Run the test command passed as an argument and ensures'
        'that the expected result file is generated.')
    parser.add_argument('result_file',
                        help='The path to the xunit result file')
    parser.add_argument(
        '--command',
        nargs='+',
        help='The test command to execute. '
        'It must be passed after other arguments since it collects all '
        'following options.')
    parser.add_argument(
        '--env',
        nargs='+',
        help=
        'Extra environment variables to set when running, e.g. FOO=foo BAR=bar'
    )
    parser.add_argument(
        '--append-env',
        nargs='+',
        help=
        'Extra environment variables to append, or set, when running, e.g. FOO=foo BAR=bar'
    )
    parser.add_argument('--output-file',
                        help='The path to the output log file')
    parser.add_argument(
        '--generate-result-on-success',
        action='store_true',
        default=False,
        help='Generate a result file if the command returns with code zero')
    parser.add_argument('--skip-test',
                        action='store_true',
                        default=False,
                        help='Skip the test')

    if '--command' in argv:
        index = argv.index('--command')
        argv, command = argv[0:index + 1] + ['dummy'], argv[index + 1:]
    args = parser.parse_args(argv)
    args.command = command

    # if result file exists remove it before test execution
    if os.path.exists(args.result_file):
        os.remove(args.result_file)

    # create folder if necessary
    if not os.path.exists(os.path.dirname(args.result_file)):
        try:
            os.makedirs(os.path.dirname(args.result_file))
        except OSError as e:
            # catch case where folder has been created in the mean time
            if e.errno != errno.EEXIST:
                raise

    if args.skip_test:
        # generate a skipped test result file
        skipped_result_file = _generate_result(args.result_file, skip=True)
        with open(args.result_file, 'w') as h:
            h.write(skipped_result_file)
        return 0

    # generate result file with one failed test
    # in case the command segfaults or timeouts and does not generate one
    failure_result_file = _generate_result(
        args.result_file,
        failure_message='The test did not generate a result file.')
    with open(args.result_file, 'w') as h:
        h.write(failure_result_file)

    # collect output / exception to generate more detailed result file
    # if the command fails to generate it
    output = ''
    output_handle = None
    if args.output_file:
        output_path = os.path.dirname(args.output_file)
        if not os.path.exists(output_path):
            os.makedirs(output_path)
        output_handle = open(args.output_file, 'wb')

    def log(msg, **kwargs):
        print(msg, **kwargs)
        if output_handle:
            output_handle.write((msg + '\n').encode())
            output_handle.flush()

    env = None
    if args.env or args.append_env:
        env = dict(os.environ)
        if args.env:
            log('-- run_test.py: extra environment variables:')
            for env_str in args.env:
                key, value = separate_env_vars(env_str, 'env', parser)
                log(' - {0}={1}'.format(key, value))
                env[key] = value
        if args.append_env:
            log('-- run_test.py: extra environment variables to append:')
            for env_str in args.append_env:
                key, value = separate_env_vars(env_str, 'append-env', parser)
                log(' - {0}={1}'.format(key, value))
                if key not in env:
                    env[key] = ''
                if not env[key].endswith(os.pathsep):
                    env[key] += os.pathsep
                env[key] += value

    log("-- run_test.py: invoking following command in '%s':\n - %s" %
        (os.getcwd(), ' '.join(args.command)))
    if output_handle:
        output_handle.write('\n'.encode())
        output_handle.flush()

    try:
        proc = subprocess.Popen(args.command,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT,
                                env=env)
        while True:
            line = proc.stdout.readline()
            if not line:
                break
            decoded_line = line.decode()
            print(decoded_line, end='')
            output += decoded_line
            if output_handle:
                output_handle.write(line)
                output_handle.flush()
        proc.wait()
        rc = proc.returncode
        if output_handle:
            # separate progress of this script from subprocess output
            output_handle.write('\n\n'.encode())
        log('-- run_test.py: return code ' + str(rc),
            file=sys.stderr if rc else sys.stdout)
    except Exception as e:
        if output_handle:
            # separate subprocess output from progress of this script
            output_handle.write('\n\n'.encode())
        log('-- run_test.py: invocation failed: ' + str(e), file=sys.stderr)
        output += str(e)
        rc = 1

    if not rc and args.generate_result_on_success:
        # generate result file with one passed test
        # if it was expected that no result file was generated
        # and the command returned with code zero
        log("-- run_test.py: generate result file '%s' with successful test" %
            args.result_file)
        success_result_file = _generate_result(args.result_file)
        with open(args.result_file, 'w') as h:
            h.write(success_result_file)

    elif os.path.exists(args.result_file):
        # check if content of result file has actually changed
        with open(args.result_file, 'r') as h:
            not_changed = h.read() == failure_result_file

        if not_changed:
            log("-- run_test.py: generate result file '%s' with failed test" %
                args.result_file,
                file=sys.stderr)
            # regenerate result file to include output / exception of the invoked command
            failure_result_file = _generate_result(
                args.result_file,
                failure_message='The test did not generate a result file:\n\n'
                + output)
            with open(args.result_file, 'w') as h:
                h.write(failure_result_file)

        log("-- run_test.py: verify result file '%s'" % args.result_file)
        # if result file exists ensure that it contains valid xml
        # unit test suites are not good about screening out
        # illegal unicode characters
        tree = None
        try:
            tree = ElementTree(None, args.result_file)
        except ParseError as e:
            modified = _tidy_xml(args.result_file)
            if not modified:
                log("Invalid XML in result file '%s': %s" %
                    (args.result_file, str(e)),
                    file=sys.stderr)
            else:
                try:
                    tree = ElementTree(None, args.result_file)
                except ParseError as e:
                    log("Invalid XML in result file '%s' (even after trying to tidy it): %s"
                        % (args.result_file, str(e)),
                        file=sys.stderr)

        if not tree:
            # set error code when result file is not parsable
            rc = 1
        else:
            # set error code when result file contains errors or failures
            root = tree.getroot()
            num_errors = int(root.attrib.get('errors', 0))
            num_failures = int(root.attrib.get('failures', 0))
            if num_errors or num_failures:
                rc = 1

    # ensure that a result file exists at the end
    if not rc and not os.path.exists(args.result_file):
        log(
            '-- run_test.py: override return code since no result file was '
            'generated',
            file=sys.stderr)
        rc = 1

    return rc
def find_and_remove_cp_file(skufile, dir_1, outDir):
    with open(skufile, "r") as sku_f:
        sku_lines_1 = sku_f.readlines()
    test_sku = []
    for i_sku, test_sku1 in enumerate(sku_lines_1):
        #for i_file,test_file in enumerate(test_filelist_lines):
        #print test_sku
        test_sku1 = test_sku1.strip().strip('\n').strip('\r')
        test_sku.append(test_sku1)

    f_list = os.listdir(dir_1 + "/Annotations/")
    for file_comp4 in f_list:
        #print fname
        basename = os.path.splitext(file_comp4)[0]
        orig_xml = dir_1 + "/Annotations/" + basename + ".xml"
        orig_pic = dir_1 + "/JPEGImages/" + basename + ".jpg"
        # ppsring= "cp "+orig_xml+" "+des_dir+"/Annotations/"
        # assert Popen_do(ppsring),ppsring+" error!"
        # ppsring= "cp "+orig_pic+" "+des_dir+"/JPEGImages/"
        # assert Popen_do(ppsring),ppsring+" error!"
        if os.path.splitext(file_comp4)[1] == '.xml':
            file_tmp = dir_1 + "/Annotations/" + file_comp4
            treeA = ElementTree()
            treeA.parse(file_tmp)
            width = int(treeA.find('size/width').text)
            height = int(treeA.find('size/height').text)
            depth = int(treeA.find('size/depth').text)
            if width == 0 or height == 0 or depth == 0:
                print file_comp4, "width==0 or height==0 or depth==0"
                break
            bfind_one_space = False
            rootA = treeA.getroot()
            #print rootA.tag
            children = rootA.findall('object')
            for obj in children:
                xmlname = obj.find('name').text
                if xmlname in test_sku:
                    bfind_one_space = True
                    break
            if bfind_one_space == True:
                for obj in children:
                    xmlname = obj.find('name').text
                    if xmlname not in test_sku:
                        name_s = obj.findall('name')
                        pose_s = obj.findall('pose')
                        truncated_s = obj.findall('truncated')
                        difficult_s = obj.findall('difficult')
                        bndbox_s = obj.findall('bndbox')
                        for oobj in name_s:
                            obj.remove(oobj)
                        for oobj in pose_s:
                            obj.remove(oobj)
                        for oobj in truncated_s:
                            obj.remove(oobj)
                        for oobj in difficult_s:
                            obj.remove(oobj)
                        for oobj in bndbox_s:
                            # xmin_s = oobj.findall('xmin')
                            # ymin_s = oobj.findall('ymin')
                            # xmax_s = oobj.findall('xmax')
                            # ymax_s = oobj.findall('ymax')
                            # for ooobj in xmin_s:
                            #   oobj.remove(ooobj)
                            # for ooobj in ymin_s:
                            #   oobj.remove(ooobj)
                            # for ooobj in xmax_s:
                            #   oobj.remove(ooobj)
                            # for ooobj in ymax_s:
                            #   oobj.remove(ooobj)
                            obj.remove(oobj)
                        rootA.remove(obj)
            if bfind_one_space == True:
                #print file_comp4
                #print treeA
                treeA.write(outDir + "/Annotations/" + file_comp4,
                            encoding="utf-8",
                            xml_declaration=False)
                orig_pic = dir_1 + "//JPEGImages/" + basename + ".jpg"
                ppsring = "cp " + orig_pic + " " + outDir + "/JPEGImages/"
                assert Popen_do(ppsring), ppsring + " error!"
Esempio n. 38
0
    return int(time.time() * 1000000 % 256)


for n in range(1000):
    #x = random.random() * 256
    #y = random.random() * 256
    x = rand()
    y = rand()
    cv.create_oval(x - 2, y - 2, x + 2, y + 2)
root.mainloop()

import commands
s = commands.getoutput("date	+'DATE:	%Y-%m-%d'")
print(s)
ret = commands.getstatusoutput("date	+'DATE:	%Y-%m-%d'")
print(ret)

import popen2
stdout, stdin, stderr = popen2.popen3('cat	/etc/passwd')
for s in stdout:
    print(s)

import os
print(os.listdir('.'))

import urllib.request
from xml.etree.ElementTree import ElementTree
f = urllib.request.urlopen("http://zip.cgis.biz/xml/zip.php?zn=1500001")
et = ElementTree(f)
for e in et.getroot():
    print(e)
Esempio n. 39
0
        kp_region_sum_ls = kp_mineralization_ls
    not_found_uid_ls = []
    for time_series_ls in kp_region_sum_ls:
        uid = time_series_ls.pop(0)
        uid_changed = MapUID340to500(uid, uid340set, uiddict340to500)
        if uid_changed != uid:
            print("UID changed:", uid, "-->", uid_changed)
        uid = uid_changed
        print(uid)
        #crfreporter.InsertInventoryData assumes data series are strings
        time_series_ls = [str(x) for x in time_series_ls]
        crfreporter.InsertInventoryData(uid, variablels, time_series_ls,
                                        options.f5, not_found_uid_ls,
                                        start_year, int(options.f4))

    if len(not_found_uid_ls) != 0:
        print("The following",
              len(not_found_uid_ls),
              "UID not found",
              file=sys.stderr)
        for item in not_found_uid_ls:
            print(item, file=sys.stderr)

    print("Pretty print xml for humans")
    PrettyPrint(t.getroot(), 0, "   ")
    print("Writing xml to:", options.f3)
    if not options.f3 is None:
        t.write(options.f3)
        print("Done")
    print("Exit program")
Esempio n. 40
0
class City():

    # note that we are are grabbing a different
    # data URL then in city list

    def __init__(self, dataurl):
        self.tree = ElementTree()

        try:
            urlhandle = urllib.request.urlopen(dataurl)
        except IOError:
            print(("[Error] Unable to open the data url: " + dataurl))
            sys.exit(1)

        self.tree.parse(urlhandle)

    def get_quantity(self, path):
        """Get the quatity contained at the XML XPath"""
        return self.tree.findtext(path)

    def get_attribute(self, path, attribute):
        """Get the attribute of the element at XPath path"""
        element = self.tree.find(path)
        if element is not None and attribute in element.attrib:
            return element.attrib[attribute]
        return None

    def get_available_quantities(self):
        """Get a list of all the available quatities in the form of their
        XML XPaths
        """

        pathlist = []
        # we are getting the full XPath with the attribute strings
        # this output is pretty long so maybe it would be wise
        # to also have an option to get the XPath without the attributes
        # self._get_all_xpaths(pathlist,"",self.tree.getroot())

        self._get_all_xpaths_with_attributes(pathlist, "", self.tree.getroot())
        return pathlist

    # This nasty little function recursively traverses
    # an element tree to get all the available XPaths
    # you have to pass in the pathlist you want to contain
    # the list
    def _get_all_xpaths(self, pathlist, path, element):
        children = element.getchildren()
        if not children:
            pathlist.append(path + "/" + element.tag)
        else:
            for child in children:
                self._get_all_xpaths(pathlist, path + "/" + element.tag, child)

    def _make_attribute_list(self, attrib):
        xpathattrib = ""
        for attribute, value in list(attrib.items()):
            xpathattrib = xpathattrib + "[@" + attribute + "='" + value + "']"
        return xpathattrib

    # This nasty little function recursively traverses
    # an element tree to get all the available XPaths
    # you have to pass in the pathlist you want to contain
    # the list
    def _get_all_xpaths_with_attributes(self, pathlist, path, element):
        children = element.getchildren()
        if not children:
            xpathattrib = self._make_attribute_list(element.attrib)

            if path == "":
                pathlist.append(element.tag + xpathattrib)
            else:
                pathlist.append(path + "/" + element.tag + xpathattrib)

        else:
            xpathattrib = self._make_attribute_list(element.attrib)

            for child in children:
                # skip the root tag
                if element.tag == "siteData":
                    self._get_all_xpaths_with_attributes(pathlist, path, child)
                else:
                    # we avoid the opening / since we start below the root
                    if path == "":
                        self._get_all_xpaths_with_attributes(
                            pathlist, element.tag + xpathattrib, child)
                    else:
                        self._get_all_xpaths_with_attributes(
                            pathlist, path + "/" + element.tag + xpathattrib,
                            child)

    # This function will break is thre is any change in the city weather
    # XML format
    def get_available_forecast_names(self):
        forecasts = self.tree.findall('forecastGroup/forecast/period')
        forecastnames = []
        for forecast in forecasts:
            forecastnames.append(forecast.get("textForecastName"))
        return forecastnames

    # This function will break is thre is any change in the city weather
    # XML format
    def get_available_forecast_periods(self):
        forecasts = self.tree.findall('forecastGroup/forecast/period')
        forecastnames = []
        for forecast in forecasts:
            forecastnames.append(forecast.text)
        return forecastnames
Esempio n. 41
0
import json
from PIL import Image
from tqdm import tqdm
from xml.etree.ElementTree import ElementTree, Element

src = r'D:/jyzdata/jyzbig/'
sampleSrc = r'D:/jyzdata/sample.xml'

fileList = os.listdir(src)
jsonFileList = [x for x in fileList if x.endswith('json')]
#jsonFileList = jsonFileList[:3]
for jsonFile in tqdm(jsonFileList):
    #获取sample文件
    tree = ElementTree()
    tree.parse(sampleSrc)
    root = tree.getroot()
    print(jsonFile[:-5])
    with open(src + jsonFile) as f:
        temp = json.loads(f.read())
        imagePath = temp['imagePath']
        img = Image.open(src + imagePath)
        w, h = img.size
        boxNum = len(temp['shapes'])
        size = Element('size')
        width = Element('width')
        height = Element('height')
        depth = Element('depth')
        width.text = str(w)
        height.text = str(h)
        depth.text = '3'
        size.append(width)
# Run this script from the command line and pass a .xspf file as the argument

from xml.etree.ElementTree import ElementTree
import sys

xmldoc = ElementTree()
try:
  xmldoc.parse(sys.argv[1])
except IndexError:
  sys.exit("you must pass the playlist file as a command line argument")
except:
  sys.exit("that file probably isn't a .xspf playlist file...")

root = xmldoc.getroot()
tracklist = root[1]

total = 0

for track in tracklist:
  for element in track:
    if element.tag.split('}')[1] == 'duration':
      total += int(element.text)

total /= 1000

hours = total / 3600
hours_string = "1 hour" if (hours == 1) else str(hours) + " hours"

minutes = (total % 3600) / 60
minutes_string = "1 minute" if (minutes == 1) else str(minutes) + " minutes"
Esempio n. 43
0
class TestResultXmlFormatter(ITestRunListener):
    u'''
    Formatter that formats test results to XML.
    '''

    # Key of property that represents name of host.
    PROPERTY_KEY_HOST_NAME = u'net.hostname'

    # Pattern of trace of first line.
    PATTERN_TRACE_FIRST_LINE = re.compile(
        ur'\A(?P<type>[^:]+)(?::\s*(?P<message>.*))?\Z')

    def __init__(self, device_properties):
        u'''
        Initialize this object.

        Parameters :
            device_properties : Properties of the device.
        '''
        self.__tree = ElementTree(Element(u'testsuite'))
        self.__test_suite_element = self.__tree.getroot()
        self.__device_properties = device_properties
        self.create_properties_element(self.__test_suite_element)

    def create_properties_element(self, parent_element):
        u'''
        Create properties element.

        Parameters :
            parent_element : Element of parent of properties element.
        '''
        properties = SubElement(self.__tree.getroot(), u'properties')
        for entry in self.__device_properties.entrySet():
            SubElement(properties, u'property', {entry.key: entry.value})

    def testRunStarted(self, runName, testCount):
        self.__test_suite_element.attrib[u'name'] = runName
        self.__test_suite_element.attrib[u'hostname'] = \
            self.__device_properties[self.PROPERTY_KEY_HOST_NAME]
        self.__test_suite_element.attrib[u'tests'] = unicode(testCount)
        now = datetime.now()
        self.__test_suite_element.attrib[u'timestamp'] = now.isoformat()
        self.__failed_test_count = 0
        self.__error_test_count = 0

    def testStarted(self, test):
        self.__current_testcase_element = SubElement(
            self.__test_suite_element, u'testcase', {
                u'classname': test.getClassName(),
                u'name': test.getTestName()
            })
        self.__current_test_started_time = datetime.now()

    def testFailed(self, status, test, trace):
        if status == ITestRunListener.TestFailure.FAILURE:
            tag = u'failure'
            self.__failed_test_count += 1
        else:
            tag = u'error'
            self.__error_test_count += 1

        failed_element = SubElement(self.__current_testcase_element, tag)
        match = self.PATTERN_TRACE_FIRST_LINE.match(trace.splitlines(False)[0])
        if match:
            failed_element.attrib[u'type'] = match.group(u'type')
            message = match.group(u'message')
            if message:
                failed_element.attrib[u'message'] = message
        failed_element.text = trace

    def testEnded(self, test, testMetrics):
        elapsed_time = datetime.now() - self.__current_test_started_time
        self.__current_testcase_element.set(
            u'time',
            u'%d.%d' % (elapsed_time.days * 60 * 60 + elapsed_time.seconds,
                        elapsed_time.microseconds))

    def testRunFailed(self, errorMessage):
        system_err_element = SubElement(self.__test_suite_element,
                                        u'system-err')
        system_err_element.text = errorMessage

    def testRunEnded(self, elapsedTime, testMetrics):
        self.__test_suite_element.attrib[u'time'] = unicode(
            float(elapsedTime) / 1000)
        self.__test_suite_element.attrib[u'errors'] = unicode(
            self.__error_test_count)
        self.__test_suite_element.attrib[u'failures'] = unicode(
            self.__failed_test_count)

    def get_result(self):
        u'''
        Get result as ElementTree.

        Return:
            ElementTree that represents result of test.
        '''
        return self.__tree
Esempio n. 44
0
class Collada(object):
    def __init__(self, config, material_man=None, clips=None):
        # config keys:
        #   donotmerge      bool
        #   path            string
        #   customnormals   bool
        #   filetype        string  : cgf | cgaanm | chrcaf
        #   rcpath          string
        #   unit            string  : meter | centimeter
        #   onlymaterials   bool
        #   scenename       string
        self.config = config
        self.tree = None
        self.material_man = material_man
        self.controllers = {}
        self.clips = self.adjust_clips(clips)
        #self.temporary_path = '\\'.join(self.config['path'].split('\\')[:-1])
        #self.temporary_file = ps.path.join(temporary_path, 'tempfile.dae')

    def adjust_clips(self, clips):
        if not clips:
            return ()
        for clip in clips:
            clip.adjust_name(self.config['scenename'])
            clip.adjust_time()
        return clips

    def recursive_strip(self, elem):
        if elem.tail:
            elem.tail = elem.tail.strip()
        if elem.text:
            elem.text = elem.text.strip()
        for child in elem:
            self.recursive_strip(child)

    def roundx(self, text):
        # Doesn't actually round right now.
        lines = text.split('\n')
        new_text = []
        logging.info('Rounding {0} lines.'.format(len(lines)))
        for line in lines:
            if not line:
                continue
            line = line.strip()
            if not line:
                logging.error('Line "{0}" not valid.'.format(repr(line)))
            items = line.split(' ')
            # theoretically do rounding.
            new_text.extend(items)
        return ' '.join(new_text)

    def recursive_adjust_nodes(self, rootnode):
        nodes = rootnode.findall('node')
        for node in nodes:
            self.recursive_adjust_nodes(node)
        self.adjust_instance_materials(rootnode)

    def adjust_instance_materials(self, node):
        inst = node.find('instance_geometry')
        if inst is None:
            inst = node.find('instance_controller')
            if inst is None:
                return
        bind_mat = inst.find('bind_material')
        if bind_mat is not None:
            tech = bind_mat.find('technique_common')
            if tech is not None:
                for inst_mat in tech:
                    if '.' in inst_mat.get('symbol'):
                        # If multiple material libraries are in a scene the materials will be named
                        # <matlib>.<matname>. '.' can't be used for names by the user, so it's safe
                        # to just split the material name if it occurs.
                        newname = self.material_man.material_dict_old[
                            inst_mat.get('symbol').split('.')[-1]].name
                    else:
                        newname = self.material_man.material_dict_old[
                            inst_mat.attrib['symbol']].name
                    inst_mat.attrib['symbol'] = newname
                    inst_mat.attrib['target'] = '#{0}'.format(newname)

    def adjust_asset(self):
        logging.info('Adjusting asset.')
        asset = self.root.find('asset')
        tool = asset.find('contributor').find('authoring_tool')
        tool.text = 'Softimage Crosswalk exporter featuring SoftCry exporter by Ande'
        url = asset.find('contributor').find('url')
        if not url:
            url = SubElement(asset.find('contributor'), 'url')
        url.text = 'https://github.com/Schlechtwetterfront/softcry'
        unit = asset.find('unit')
        if self.config['unit'] != 'meter':
            del unit.attrib['meter']
            unit.set(self.config['unit'], '1')
        else:
            unit.set('meter', '1')
        unit.set('name', self.config['unit'])
        up_axis = asset.find('up_axis')
        up_axis.text = 'Y_UP'
        logging.info('Adjusted asset.')

    def replace_library_images(self):
        logging.info('Replacing Library Images.')
        lib_images = self.root.find('library_images')
        if lib_images is None:
            logging.info('No images.')
            return
        self.root.remove(lib_images)
        self.root.append(self.material_man.get_lib_images())
        logging.info('Finished replacing Library Images.')

    def replace_library_effects(self):
        logging.info('Replacing library effects.')
        lib_effects = self.root.find('library_effects')
        if lib_effects is None:
            return
        self.root.remove(lib_effects)
        self.root.append(self.material_man.get_lib_fx())
        logging.info('Finished replacing library effects.')

    def replace_library_materials(self):
        logging.info('Replacing Library Materials.')
        lib_materials = self.root.find('library_materials')
        if lib_materials is None:
            logging.error('No materials.')
            return
        self.root.remove(lib_materials)
        self.root.append(self.material_man.get_lib_materials())
        logging.info('Finished replacing library materials.')

    def adjust_library_geometries(self):
        logging.info('Preparing Library Geometries.')
        lib_geoms = self.root.find('library_geometries')
        if lib_geoms is None:
            logging.error('No geometries.')
            return
        for geom in lib_geoms:
            logging.info('Preparing Geometry {0}'.format(geom.get('id')))
            mesh = geom[0]
            # Adjust material name.
            triangles = mesh.findall('triangles')
            for tris in triangles:
                print tris.get('material')
                print self.material_man.material_dict_old.keys()
                if '.' in tris.get('material'):
                    # If multiple material libraries are in a scene the materials will be named
                    # <matlib>.<matname>. '.' can't be used for names by the user, so it's safe
                    # to just split the material name if it occurs.
                    tris.set(
                        'material', self.material_man.material_dict_old[
                            tris.get('material').split('.')[-1]].name)
                else:
                    tris.attrib[
                        'material'] = self.material_man.material_dict_old[
                            tris.attrib['material']].name
                p = tris.find('p')
                if p is not None:
                    p.text = self.roundx(p.text)
            logging.info('Finished preparing {0}.'.format(geom.get('id')))

    def adjust_library_controllers(self):
        logging.info('Adjusting Library Controllers.')
        lib_controllers = self.root.find('library_controllers')
        if lib_controllers is None:
            logging.info('No controllers.')
            return
        for controller in lib_controllers:
            logging.info('Preparing Controller "{0}".'.format(
                controller.get('id')))
            for skin in controller:
                v_weights = skin.find('vertex_weights')
                if v_weights is not None:
                    v = v_weights.find('v')
                    if v is not None:
                        lines = v.text.split('\n')
                        new_lines = []
                        for line in lines:
                            new_line = line.strip().split(' ')
                            new_lines.extend(new_line)
                        v.text = ' '.join(new_lines)
        logging.info('Finished adjusting controllers.')

    def adjust_visual_scenes(self):
        logging.info('Adjusting Visual Scenes.')
        scenes = self.root.find('library_visual_scenes')
        if scenes is None:
            logging.error('No scenes.')
            return
        vis_scene = scenes[0]
        root_nodes = list(vis_scene)
        logging.info('Creating CryExport Node.')
        cryexportnode = SubElement(vis_scene, 'node')
        cryexportnode.attrib['id'] = 'CryExportNode_{0}'.format(
            self.config['scenename'])
        extra = SubElement(cryexportnode, 'extra')
        tech = SubElement(extra, 'technique', profile='CryEngine')
        props = SubElement(tech, 'properties')
        ft = 'fileType={0}'.format(self.config['filetype'])
        flags = [ft]
        if self.config['donotmerge']:
            flags.append('DoNotMerge')
        if self.config['customnormals']:
            flags.append('CustomNormals')
        if self.config['f32']:
            flags.append('UseF32VertexFormat')
        props.text = '\n\t\t'.join(flags)
        logging.info('Applied flags "{0}" to CryExport Node.'.format(
            ' '.join(flags)))
        # Remove nodes.
        for node in root_nodes:
            vis_scene.remove(node)
            cryexportnode.append(node)
        logging.info('Reparented nodes.')
        self.recursive_adjust_nodes(cryexportnode)
        logging.info('Finished adjusting Visual Scenes.')

    def add_library_animation_clips(self):
        logging.info('Adding Library Animation Clips.')
        lib_anims = self.root.find('library_animations')
        if (lib_anims is not None) and self.clips:
            lib_clips = SubElement(self.root, 'library_animation_clips')
            for clip in self.clips:
                clip_node = SubElement(lib_clips, 'animation_clip')
                clip_node.set('start', str(clip.start))
                clip_node.set('end', str(clip.end))
                clip_node.set('id', clip.name)
                clip_node.set('name', clip.name)
                for anim in lib_anims:
                    inst_anim = SubElement(clip_node, 'instance_animation')
                    inst_anim.set('url', '#{0}'.format(anim.get('id')))
        logging.info('Added {0} clips.'.format(len(self.clips)))

    def remove_xmlns(self):
        lines = []
        with open(self.config['path'], 'r') as fh:
            for line in fh:
                if line.startswith('<?'):
                    continue
                elif ('COLLADA' in line) and ('xmlns' in line):
                    lines.append('<COLLADA>\n')
                else:
                    lines.append(line)
        with open(self.config['path'], 'w') as fh:
            fh.writelines(lines)
        logging.info('Removed xmlns.')

    def adjust(self):
        logging.info('Starting adjust.')
        self.remove_xmlns()

        logging.info('Reading .dae.')
        self.tree = ElementTree(file=self.config['path'])
        self.root = self.tree.getroot()
        logging.info('Finished reading .dae.')

        self.material_man.create_old_name_dict()

        self.recursive_strip(self.root)

        self.adjust_asset()

        self.replace_library_images()
        self.replace_library_effects()
        self.replace_library_materials()
        self.adjust_library_geometries()
        # Animations?
        self.adjust_library_controllers()
        self.adjust_visual_scenes()

        self.add_library_animation_clips()

        indent(self.root)

        self.tree = ElementTree(element=self.root)

        logging.info('Finished adjust.')

    def write(self, path=None):
        filepath = path or self.config['path']
        self.tree.write(filepath)
def remove_Anotations(AnnotationDir, outDir):
    print "==========="
    # removelist_key = ["Maxwellhouse","G7"]
    # removelist_key.append("Oldtown")
    # removelist_key.append("Hougu")
    # removelist_key.append("Kapalapi")
    # removelist_key.append("Moka")
    # removelist_key.append("coffee")
    # removelist_key.append("kopiko")
    # removelist_key.append("package1")
    # removelist_key.append("package3")
    # removelist_key.append("package4")
    # removelist_key.append("package5")
    # removelist_key.append("package10")
    # removelist_key.append("package11")
    # removelist_key.append("package17")
    removelist_key = ["package1"]
    removelist_key.append("package2")
    removelist_key.append("package3")
    removelist_key.append("package4")
    removelist_key.append("package5")
    removelist_key.append("package6")
    removelist_key.append("package7")
    removelist_key.append("package8")
    removelist_key.append("package9")
    removelist_key.append("package10")
    removelist_key.append("package11")
    removelist_key.append("package12")
    removelist_key.append("package13")
    removelist_key.append("package14")
    removelist_key.append("package15")
    removelist_key.append("package16")
    removelist_key.append("package17")
    f_list = os.listdir(AnnotationDir)
    i = 0
    for file_comp4 in f_list:
        if os.path.splitext(file_comp4)[1] == '.xml':
            file_tmp = AnnotationDir + "/" + file_comp4
            treeA = ElementTree()
            treeA.parse(file_tmp)
            width = int(treeA.find('size/width').text)
            height = int(treeA.find('size/height').text)
            depth = int(treeA.find('size/depth').text)
            if width == 0 or height == 0 or depth == 0:
                print file_comp4, "width==0 or height==0 or depth==0"
                break
            bfind_one_space = False

            # if JPEG_Dir!="":
            #     im = cv2.imread(JPEG_Dir+os.path.splitext(file_comp4)[0]+".jpg")
            #     #sp = im.shape
            #     imheight = im.shape[0]
            #     imwidth = im.shape[1]
            #     imdepth = im.shape[2]
            #     if imwidth!=width or imheight!=height or imdepth!=depth :
            #         bfind_one_space = True
            #         print file_comp4,"width,height,depth error"
            #         treeA.find('size/width').text = str(imwidth)
            #         treeA.find('size/height').text =str(imheight)
            #         treeA.find('size/depth').text =str(imdepth)
            # anno = treeA.find("annotation")
            # children = anno.getchildren()
            # for child in children:
            #     if child.tag=="object":
            #         if child.find('name').text in removelist_key:
            #             bfind_one_space = True
            #             children.remove(child)

            rootA = treeA.getroot()
            print rootA.tag
            children = rootA.findall('object')
            for obj in children:
                xmlname = obj.find('name').text
                xmlname = xmlname.strip()
                # if xmlname=="Others1" :
                #     i=i+1
                #     print file_comp4
                #     bfind_one_space = True
                #     #obj.set("name","Mini Oreo SDW 55g*24  Strawberry")
                #     obj.find('name').text="others1"
                xmin = int(obj.find('bndbox').find('xmin').text)
                ymin = int(obj.find('bndbox').find('ymin').text)
                xmax = int(obj.find('bndbox').find('xmax').text)
                ymax = int(obj.find('bndbox').find('ymax').text)
                if xmin <= 0:
                    bfind_one_space = True
                    obj.find('bndbox').find('xmin').text = str(1)
                if ymin <= 0:
                    bfind_one_space = True
                    obj.find('bndbox').find('ymin').text = str(1)
                if xmax >= width:
                    bfind_one_space = True
                    obj.find('bndbox').find('xmax').text = str(width - 1)
                if ymax >= height:
                    bfind_one_space = True
                    obj.find('bndbox').find('ymax').text = str(height - 1)
                if xmin >= xmax or ymin >= ymax:
                    print file_comp4

                if xmlname in removelist_key:
                    print xmlname
                    bfind_one_space = True
                    name_s = obj.findall('name')
                    pose_s = obj.findall('pose')
                    truncated_s = obj.findall('truncated')
                    difficult_s = obj.findall('difficult')
                    bndbox_s = obj.findall('bndbox')
                    for oobj in name_s:
                        obj.remove(oobj)
                    for oobj in pose_s:
                        obj.remove(oobj)
                    for oobj in truncated_s:
                        obj.remove(oobj)
                    for oobj in difficult_s:
                        obj.remove(oobj)
                    for oobj in bndbox_s:
                        # xmin_s = oobj.findall('xmin')
                        # ymin_s = oobj.findall('ymin')
                        # xmax_s = oobj.findall('xmax')
                        # ymax_s = oobj.findall('ymax')
                        # for ooobj in xmin_s:
                        #   oobj.remove(ooobj)
                        # for ooobj in ymin_s:
                        #   oobj.remove(ooobj)
                        # for ooobj in xmax_s:
                        #   oobj.remove(ooobj)
                        # for ooobj in ymax_s:
                        #   oobj.remove(ooobj)
                        obj.remove(oobj)
                    rootA.remove(obj)
            if bfind_one_space == True:
                #print file_comp4
                #print treeA
                treeA.write(outDir + file_comp4,
                            encoding="utf-8",
                            xml_declaration=False)
    print i
class ParseXml:
    def __init__(self, file_name):
        """
        Constructor
        :param file_name: full path of xml file
        """
        self.file_name = file_name
        try:
            self.tree = ElementTree(file=self.file_name)
            self.root = self.tree.getroot()
        except Exception as e:
            logger.error(e)

    def get_attr_values(self, node_path, attr_name):
        """
        get the attribute value of some node
        :param node_path: node path where attribute locates
        :param attr_name:  attribute name
        :return: list of attribute values
        """
        try:
            attr_values = []
            nodes = self.root.findall(node_path)

            if len(nodes) > 0:
                for node in nodes:
                    attr_values.append(node.get(attr_name))

            return attr_values

        except Exception as e:
            logger.error(e)

    def get_node_text(self, parent_node, target_node):
        """
        get one or more child node text under the same parent node
        :param parent_node: parent node
        :param target_node: target node
        :return: dic of target node: key=tag name of parent node, value=text content of target node
        """
        try:
            dic_node_value = {}
            for nd in self.root.iter(parent_node):
                dic_node_value[nd.get('name')] = nd.find(target_node).text
            return dic_node_value
        except Exception as e:
            logger.error(e)

    def get_nodes_text_by_parent_name(self, parent_name, target_node):
        """
        get text list of target nodes
        :param parent_name: 'name' attribute value of parent node
        :param target_node: target node
        :return: text list of target nodes
        """
        try:
            list_text = []
            for nd in self.root.findall(".//*[@name='" + parent_name + "']//" +
                                        target_node):
                list_text.append(nd.text)
            return list_text
        except Exception as e:
            logger.error(e)

    def get_nodes(self, target_node):
        """
        get node obj
        :param target_node: target node
        :return: list of node obj
        """
        try:
            return self.root.findall(".//" + target_node)
        except Exception as e:
            logger.error(e)
Esempio n. 47
0
def GetGRL(runlist,
           stable=False,
           ready=False,
           queryXML='queryHandler.xml',
           queryDir='data',
           querySkip=False):

    from xml.etree.ElementTree import ElementTree, Element, SubElement

    grl = []

    # Use AtlRunQuery
    if not querySkip:
        RunAtlRunQuery(runlist, stable, ready, queryXML, queryDir)

    # Parse xml -- stolen from AtlDataSumQuery.py
    queryFile = '/'.join([queryDir, queryXML])
    tree = ElementTree()
    tree.parse(queryFile)
    if tree.getroot().tag != 'LumiRangeCollection':
        log.error("can't find 'LumiRangeCollection' tag in file=%s" % queryXML)
        sys.exit()

    for lbc in tree.findall('NamedLumiRange/LumiBlockCollection'):
        runnum = int(lbc.findtext('Run', '-1'))
        if runnum < 0:
            log.error("can't find 'Run' tag in file %s" % queryXML)
            sys.exit()

        for lbr in lbc.findall('LBRange'):
            lbstart = int(lbr.get('Start', '-1'))
            lbend = int(lbr.get('End', '-1'))

            if lbstart < 0 or lbend < 0:
                log.error(
                    "can't find LBRange attributes for run=%d in file=%s" %
                    (runnum, queryXML))
                sys.exit()

            # Find this run in our original list
            thisrun = None
            for i, rundata in enumerate(runlist):
                if rundata['run'] == runnum:
                    thisrun = rundata
                    thisrun['lbbeg'] = lbstart
                    thisrun['lbend'] = lbend
                    runlist.pop(i)

            # Append
            if thisrun:
                grl.append(thisrun)

                if opts.options > -1:
                    StartTime = thisrun['StartTime']
                    EndTime = thisrun['EndTime']
                    beg_ = ACT.time.ctime(StartTime / 1.0E9)
                    end_ = ACT.time.ctime(EndTime / 1.0E9)
                    log.info(
                        'Grl append: run=%d lbbeg=%d lbend=%d time=%s--%s' %
                        (runnum, lbstart, lbend, beg_, end_))

    return grl
Esempio n. 48
0
class ElementTreeProcessor(ConfigProcessor, ):

    def __init__(self, namespace, forcemenu=False, **kwargs):
        self.__namespace = namespace
        self.__path = []
        self.__tree = None
        self.forcemenu = forcemenu
        self.__interval = re.compile('((\\d+)\\s*w){0,1}\\s*((\\d+)\\s*d){0,1}\\s*((\\d+)\\s*h){0,1}\\s*((\\d+)\\s*m){0,1}\\s*((\\d+)\\s*s){0,1}')
        ConfigProcessor.__init__(self, **kwargs)

    @property
    def namespace(self):
        return self.__namespace

    def process(self):
        enforce((self.__tree is None), 'Cannot execute processing while another processing or validation routine is still active.')
        self.validate_only = False
        self.__tree = ElementTree(file=self.file)
        self.__path = []
        self._process_child_elements(self.__tree.getroot())
        self.__tree = None

    def validate(self):
        enforce((self.__tree is None), 'Cannot execute validation while another processing or validation routine is still active.')
        self.__tree = ElementTree(file=self.file)
        self.validate_only = True
        self.__path = []
        self._process_child_elements(self.__tree.getroot())
        self.__tree = None
        return True

    def _process_child_elements(self, element, ignore=[]):
        self.__path.append(None)
        for e in element:
            self.__path.pop()
            dsz.script.CheckStop()
            if (e.tag in ignore):
                continue
            self.__path.append(e.tag)
            modname = '.'.join(([self.namespace] + self.__path))
            try:
                __import__(modname)
            except ImportError as exc:
                enforce(False, (lambda : ("Tag handler '%s' not found for element '<%s>'; reason: %s." % (modname, e.tag, exc))))
            handler = sys.modules[modname]
            marker = e.get('marker')
            display = e.get('name')
            generic_display = ('"%s": ["attribs": %s, "text": %s]' % (e.tag, e.attrib, repr(e.text)))
            if (display is None):
                display = generic_display
            error_marker = (marker if (marker is not None) else ((('__generic_error_marker__::' + self.file) + '::') + generic_display))
            period = e.get('period')
            if (period is not None):
                period = self.__interval.match(period)
                enforce((period is not None), (lambda : ('%s -- error in period time delta format string.' % display)))
                qc = (lambda x: (int(x) if (x is not None) else 0))
                period = datetime.timedelta(weeks=qc(period.group(2)), days=qc(period.group(4)), hours=qc(period.group(6)), minutes=qc(period.group(8)), seconds=qc(period.group(10)))
            taghandler = eval((('handler.' + e.tag.title()) + 'TagHandler(element=e, _processor=self, _marker=marker, _period=period, _display=display, _error_marker=error_marker)'))
            if self.validate_only:
                enforce(taghandler.validate(), (lambda : ("Validation error for tag '<%s>'; text='%s'; attributes=%s" % (e.tag, repr(e.text), e.attrib))))
            else:
                if (not taghandler.marker_check()):
                    continue
                try:
                    ret = taghandler.process()
                    if (ret == True):
                        if ((marker is not None) or (marker != 'None')):
                            ops.survey.complete(marker)
                        else:
                            ops.survey.complete(error_marker)
                    elif (ret == False):
                        if ((marker is not None) and (marker != 'None')):
                            ops.survey.error(marker)
                        else:
                            ops.survey.error(error_marker)
                except:
                    if ((marker is not None) and (marker != 'None')):
                        ops.survey.error(marker)
                    else:
                        ops.survey.error(error_marker)
                    raise 
        self.__path.pop()
        return True
Esempio n. 49
0
        if re.match(rx, os.path.join(root, file)):
            if os.stat(os.path.join(root, file)).st_size > 0:
                build_parameters = []
                display_name = "unknown"
                description = ""
                start_time = 0
                duration = 0
                monitoring_data = os.path.join(root, "monitoring.data")
                build_xml_path = os.path.join(root, "build.xml")

                if os.path.isfile(build_xml_path):
                    logger.info(
                        "Try to parse Jenkins build XML-file: {0}".format(
                            build_xml_path))
                    build_xml.parse(build_xml_path)
                    build_tag = build_xml.getroot()

                    for params in build_tag:
                        if params.tag == 'actions':
                            parameters = params.find('.//parameters')
                            for parameter in parameters:
                                name = parameter.find('name')
                                value = parameter.find('value')
                                build_parameters.append(
                                    [name.text, value.text])
                        elif params.tag == 'startTime':
                            start_time = int(params.text)
                        elif params.tag == 'displayName':
                            display_name = params.text
                        elif params.tag == 'duration':
                            duration = int(params.text)
Esempio n. 50
0
def get_root(bucket, key):
    response = s3.get_object(Bucket=bucket, Key=key)
    xmldata = response['Body'].read().decode('utf-8')
    tree = ElementTree(fromstring(xmldata))
    root = tree.getroot()
    return root
Esempio n. 51
0
def test_data_refresh(request):
    builds_dir = "/var/lib/jenkins/jobs"
    # builds_dir="C:\\work\\reportdata"
    build_xml = ElementTree()
    rx = re.compile(r'/var/lib/jenkins/jobs/.+?/builds/\d+?/build\.xml')
    # rx = re.compile(r'C:\\work\\reportdata.+?\\builds\\\d+?\\build\.xml')
    logger.info("Refreshing test data")
    for root, dirs, files in os.walk(builds_dir):
        for file in files:
            if re.match(rx, os.path.join(root, file)):
                if os.stat(os.path.join(root, file)).st_size > 0:
                    build_parameters = []
                    display_name = "unknown"
                    description = ""
                    start_time = 0
                    duration = 0
                    build_xml_path = os.path.join(root, "build.xml")
                    if os.path.isfile(build_xml_path):
                        logger.info(
                            "Try to parse Jenkins build XML-file: {0}".format(
                                build_xml_path))
                        with open(build_xml_path, "r") as fixfile:
                            data = fixfile.read()
                        data = data.replace("&#x", "")
                        with open(build_xml_path, "w") as fixfile:
                            fixfile.write(data)
                        build_xml.parse(build_xml_path)
                        build_tag = build_xml.getroot()

                        for params in build_tag:
                            if params.tag == 'actions':
                                parameters = params.find('.//parameters')
                                for parameter in parameters:
                                    name = parameter.find('name')
                                    value = parameter.find('value')
                                    build_parameters.append(
                                        [name.text, value.text])
                                userId = params.find('.//userId')
                                if userId is not None:
                                    started_by = userId.text
                                    if not User.objects.filter(
                                            login=started_by).exists():
                                        u = User(login=started_by)
                                        u.save()
                                        user_id = u.id
                                    else:
                                        u = User.objects.get(login=started_by)
                                        user_id = u.id
                                else:
                                    user_id = 0
                            elif params.tag == 'startTime':
                                start_time = int(params.text)
                            elif params.tag == 'displayName':
                                display_name = params.text
                            elif params.tag == 'duration':
                                duration = int(params.text)
                            elif params.tag == 'description':
                                description = params.text
                        if Test.objects.filter(path=root).exists():
                            test = Test.objects.get(path=root)
                            logger.info("Updating test, id: {0}".format(
                                str(test.id)))
                            test.display_name = display_name
                            test.start_time = start_time
                            test.end_time = start_time + duration
                            test.description = description
                            test.parameters = build_parameters
                            test.started_by_id = user_id
                            test.save()
    response = {
        "message": {
            "text": "Tests information was updated.",
            "type": "info",
            "msg_params": {}
        }
    }

    return JsonResponse(response, safe=False)
def process_file(filename):
    try:
        zf = zipfile.ZipFile(filename)
    except zipfile.BadZipfile:
        sys.stderr.write("%s is not an StarOffice file!\n" % filename)
        return 2
    try:
        mf = zf.open("META-INF/manifest.xml")
    except KeyError:
        sys.stderr.write("%s is not an StarOffice file!\n" % filename)
        return 3
    #print mf.read()
    tree = ElementTree()
    tree.parse(mf)
    r = tree.getroot()

    # getiterator() is deprecated but 2.6 does not have iter()
    try:
        elements = list(r.iter())
    except:
        elements = list(r.getiterator())

    target = "content.xml"
    is_encrypted = False
    key_size = 16
    for i in range(0, len(elements)):
        element = elements[i]
        if element.get(
                "{http://openoffice.org/2001/manifest}full-path") == target:
            for j in range(i + 1, i + 1 + 3):
                element = elements[j]
                data = element.get(
                    "{http://openoffice.org/2001/manifest}checksum")
                if data:
                    is_encrypted = True
                    checksum = data
                data = element.get(
                    "{http://openoffice.org/2001/manifest}initialisation-vector"
                )
                if data:
                    iv = data
                data = element.get("{http://openoffice.org/2001/manifest}salt")
                if data:
                    salt = data
                data = element.get(
                    "{http://openoffice.org/2001/manifest}iteration-count")
                if data:
                    iteration_count = data
                data = element.get(
                    "{http://openoffice.org/2001/manifest}algorithm-name")
                if data:
                    assert data == "Blowfish CFB"

    if not is_encrypted:
        sys.stderr.write("%s is not an encrypted StarOffice file!\n" %
                         filename)
        return 4

    checksum = base64.b64decode(checksum)
    checksum = binascii.hexlify(checksum).decode("ascii")
    iv = binascii.hexlify(base64.b64decode(iv)).decode("ascii")
    salt = binascii.hexlify(base64.b64decode(salt)).decode("ascii")

    try:
        content = zf.open(target).read()
    except KeyError:
        sys.stderr.write(
            "%s is not an encrypted StarOffice file, '%s' missing!\n" %
            (filename, target))
        return 5

    algorithm_type = 0
    checksum_type = 0
    key_size = 16

    original_length = len(content)
    if original_length >= 1024:
        length = 1024
        original_length = 1024
    else:
        # pad to make length multiple of 8
        pad = b"00000000"
        pad_length = original_length % 8
        if pad_length > 0:
            content = content + pad[0:pad_length]
        length = len(content)

    sys.stdout.write("%s:$sxc$*%s*%s*%s*%s*%s*%d*%s*%d*%s*%d*%d*%s\n" % \
            (os.path.basename(filename), algorithm_type,
            checksum_type, iteration_count, key_size, checksum, len(iv) / 2,
            iv, len(salt) / 2, salt, original_length, length,
            binascii.hexlify(content[:length]).decode("ascii")))
Esempio n. 53
0
    def getLexFromXML(self, fileDirPath):
        # If file or directory does not exists
        if os.path.exists(fileDirPath) == False:
            return ""

        # Declare lexycon string as UTF-8
        lexStr = u""
        tree = ElementTree()
        # List of file names in directory
        fileList = []

        # Get file path correct for current OS
        fileDirPath = os.path.normpath(fileDirPath)

        # If path leads to directory
        if os.path.isfile(fileDirPath) == False:
            # If directory is empty
            if os.listdir(fileDirPath) == []:
                return ""

            # Get all .xml files
            for fileName in os.listdir(fileDirPath):
                if fileName.endswith(".xml"):
                    fileList.append(fileName)
        # If path leads to actual file
        else:
            if fileDirPath.endswith(".xml"):
                fileList.append(os.path.basename(fileDirPath))
                fileDirPath = os.path.dirname(fileDirPath)

        print("Collecting lexicons...")
        for fileName in fileList:
            tree.parse(os.path.join(fileDirPath, fileName))
            xmlRoot = tree.getroot()

            # For each chunk
            for chunk in xmlRoot.findall("chunk"):
                # For each sentence
                for sentence in chunk.findall("sentence"):
                    # For each token
                    for token in sentence.findall("tok"):
                        # For each lexycon
                        for lex in token.findall("lex"):
                            # If 'disamb' exists and equals 'true'
                            if ("disamb" in lex.attrib) and (lex.attrib["disamb"] == "1"):
                                # If 'interp' exists
                                if lex.find("ctag").text == "interp":
                                    # If lexycon contains comma
                                    if lex.find("base").text == ",":
                                        lexStr += " comma_after "
                                        break
                                    else:
                                        lexStr += " other_after "
                                        break
                                else:
                                    lexStr += "\n" + token.find("orth").text + " " + lex.find("base").text + " " + lex.find("ctag").text
                                break
                    # End of sentence
                    lexStr += "\n"
        # End of loop
        print("Done")
        return lexStr
Esempio n. 54
0
def main(argv):
    global pricingService
    global apiKey
    #Default values - will change these
    fileIn = ''
    fileOut = ''
    groupParts = False

    pricingService = ''  #Which pricing service to use (currently 'F' = FindChips)

    #####
    # COMMENT OUT FOR COMMAND LINE - THIS IS HERE FOR DEVELOPMENT TESTING ONLY
    #####
    #    fileIn = ''  #replace with filename
    #    groupParts = True
    #    apiKey = ''    #Put your API key here
    #    pricingService= 'F'
    #    fileOut = ''   #replace with filename
    ######

    ########################################################
    #Process the Command Line
    logger.info('Command Line Arguments: %s', str(argv))

    try:
        opts, args = getopt.getopt(
            argv, "hgfa:i:o:",
            ["help", "group", "apikey=", "input=", "output="])
    except getopt.GetoptError:
        printUsage()
        logger.error('Invalid argument list provided')
        sys.exit(2)

    for opt, arg in opts:
        if opt in ("-i", "--input"):
            fileIn = arg

        if opt in ("-o", "--output"):
            fileOut = arg

        if opt in ("-a", "--apikey"):
            apiKey = arg

        if opt in ("-f", "--findchips"):
            pricingService = opt

        if opt in ("-g", "--group"):
            groupParts = True

        if opt in ("-h", "--help"):
            printUsage()

    #Perform error checking to make sure cmd-line params passed, files exist etc.
    checkParams(fileIn)

    #If no output file specified, create one:
    if fileOut == '':
        fileOut = fileIn[:fileIn.rfind(
            '.')] + '_BOM'  #strip the extension off the Input file
        logger.info('No OUTPUT Filename specified - using: %s', fileOut)

    ########################################################
    #Parse and process the XML netlist
    eTree = ElementTree()
    eTree.parse(fileIn)
    root = eTree.getroot()
    section = root.find(
        tagComponent)  #Find the component-level in the XML Netlist

    #Loop through each component
    for component in section:
        processComponent(
            listOutput, component,
            groupParts)  #Extract compoonent info and add to listOutput

    #Get Pricing if we need to.  Need a Manufacturer Part No, and a Pricing Service to be specified
    if fldMfgPartNo in CSVFieldNames and pricingService != '':
        getPricing(listOutput)

    ########################################################
    #Generate the Output CSV File
    with open(fileOut + '.csv', 'wb') as fOut:
        csvWriter = csv.DictWriter(fOut,
                                   delimiter=',',
                                   fieldnames=CSVFieldNames)
        csvWriter.writeheader()
        utf8Output = []
        for row in listOutput:
            utf8Row = {}
            for key in row:
                utf8Row[key] = row[key].encode('utf-8')
            utf8Output.append(utf8Row)
        csvWriter.writerows(utf8Output)

    #Print and Log the file creation
    print('Created CSV File')
    logger.info('Created CSV file with %i items: %s', len(listOutput),
                fileOut + '.csv')

    ########################################################
    #Generate the Output XML File

    parent = Element('schematic')  #Create top-level XML element

    #Loop through each Component and create a child of the XML top-level
    for listItem in listOutput:
        child = SubElement(parent, 'component')

        #Loop through each attribute of the component.
        #  We do it this way, in order to preserve order of elements - more logical output
        for key in CSVFieldNames:
            if key in listItem:
                attribute = SubElement(
                    child,
                    key.replace(' ', '_').replace('&', '_')
                )  #XML doesn't like spaces in element names, so replace them with "_"
                attribute.text = listItem[key]

    #Output to XML file
    ET = ElementTree(parent)
    ET.write(fileOut + '.xml')

    #Print and Log the file creation
    print('Created XML File')
    logger.info('Created XML file with %i items: %s', len(listOutput),
                fileOut + '.xml')

    #Close the log file - we're done!
    logging.shutdown()
Esempio n. 55
0
        for element in self.elements:
            element.write(outFile)

    def writeTRACKS(self, outFile):
        outFile.write("$TRACK\n")
        for track in self.tracks:
            outFile.write(track.boardRep())
        outFile.write("$EndTRACK\n")
        for zones in self.polygon:
            outFile.write(zones.boardRep())

    def writeGRAPHICS(self, outFile):
        for graphic in self.plain:
            outFile.write(graphic.boardRep())


if __name__ == "__main__":
    fileName = input("Input Filename: ")
    outFileName = input("Output Filename: ")

    name = fileName.replace("/", "\\")
    name = name.split("\\")[-1]
    name = name.split(".")[0]

    node = ElementTree(file=fileName)
    node = node.getroot()

    brd = Board(node)

    brd.write(open(outFileName, "a"))
Esempio n. 56
0
def listFiles(dir):
    fileList = []
    for root, dirs, files in os.walk(dir):
        for name in files:
            fileList.append(os.path.join(root, name))
    return fileList


for filename in listFiles(defaultDir):
    if not filename.endswith(".bpmx"):
        #print('\n' + filename)
        continue

    ET = ElementTree(file=filename)
    root = ET.getroot()

    for child in root:
        if child.tag == '{http://www.ibm.com/xtools/bpmn/2.0}process':

            flagProcessFound = True
            process = child

            allDataInAssObjects = []
            allDataOutAssObjects = []
            errorText = ''

            # find all call Activities in this process
            for callActivity in process.findall('bpmn:callActivity', ns):
                # find all dataInputAssociations and dataOutputAssociations in this process
                dataInAssObjects = callActivity.findall(
Esempio n. 57
0
def _run_test(parser, args, failure_result_file, output_handle):
    output = ''

    def log(msg, **kwargs):
        print(msg, **kwargs)
        if output_handle:
            output_handle.write((msg + '\n').encode())
            output_handle.flush()

    env = None
    if args.env or args.append_env:
        env = dict(os.environ)
        if args.env:
            log('-- run_test.py: extra environment variables:')
            previous_key = None
            updated_env_keys = set()
            for env_str in args.env:
                # if CMake has split a single value containing semicolons
                # into multiple arguments they are put back together here
                if previous_key and '=' not in env_str:
                    key = previous_key
                    value = env[key] + ';' + env_str
                else:
                    key, value = separate_env_vars(env_str, 'env', parser)
                env[key] = value
                updated_env_keys.add(key)
                previous_key = key
            for key in updated_env_keys:
                log(' - {0}={1}'.format(key, env[key]))
        if args.append_env:
            log('-- run_test.py: extra environment variables to append:')
            previous_key = None
            for env_str in args.append_env:
                # if CMake has split a single value containing semicolons
                # into multiple arguments they are put back together here
                if previous_key and '=' not in env_str:
                    key = previous_key
                    value = env[key] + ';' + env_str
                    log(' - {0}+={1}'.format(key, env_str))
                else:
                    key, value = separate_env_vars(env_str, 'append-env',
                                                   parser)
                    log(' - {0}+={1}'.format(key, value))
                if key not in env:
                    env[key] = ''
                if not env[key].endswith(os.pathsep):
                    env[key] += os.pathsep
                env[key] += value
                previous_key = key

    log("-- run_test.py: invoking following command in '%s':\n - %s" %
        (os.getcwd(), ' '.join(args.command)))
    if output_handle:
        output_handle.write('\n'.encode())
        output_handle.flush()

    encodings = ['utf-8']
    if locale.getpreferredencoding(False) not in encodings:
        encodings.append(locale.getpreferredencoding(False))

    try:
        proc = subprocess.Popen(args.command,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.STDOUT,
                                env=env)
        while True:
            line = proc.stdout.readline()
            if not line:
                break
            for i, encoding in enumerate(encodings):
                try:
                    decoded_line = line.decode(encoding)
                except UnicodeDecodeError:
                    if i == len(encodings) - 1:
                        raise
                else:
                    break
            print(decoded_line, end='')
            output += decoded_line
            if output_handle:
                output_handle.write(decoded_line.encode())
                output_handle.flush()
        proc.wait()
        rc = proc.returncode
        if output_handle:
            # separate progress of this script from subprocess output
            output_handle.write('\n\n'.encode())
        log('-- run_test.py: return code ' + str(rc),
            file=sys.stderr if rc else sys.stdout)
    except Exception as e:
        if output_handle:
            # separate subprocess output from progress of this script
            output_handle.write('\n\n'.encode())
        log('-- run_test.py: invocation failed: ' + str(e), file=sys.stderr)
        output += str(e)
        rc = 1

    if not rc and args.generate_result_on_success:
        # generate result file with one passed test
        # if it was expected that no result file was generated
        # and the command returned with code zero
        log("-- run_test.py: generate result file '%s' with successful test" %
            args.result_file)
        success_result_file = _generate_result(args.result_file)
        with open(args.result_file, 'w') as h:
            h.write(success_result_file)

    elif os.path.exists(args.result_file):
        # check if content of result file has actually changed
        with open(args.result_file, 'r', encoding='utf-8') as h:
            content = h.read()

        if content == failure_result_file:
            log("-- run_test.py: generate result file '%s' with failed test" %
                args.result_file,
                file=sys.stderr)
            # regenerate result file to include output / exception of the invoked command
            failure_result_file = _generate_result(
                args.result_file,
                failure_message='The test did not generate a result file:\n\n'
                + output)
            with open(args.result_file, 'w') as h:
                h.write(failure_result_file)
        else:
            # prefix classname attributes
            if args.result_file.endswith('.gtest.xml') and args.package_name:
                prefix = ' classname="'
                pattern = '%s(?!%s)' % (prefix, args.package_name)
                new_content = re.sub(pattern, prefix + args.package_name + '.',
                                     content)
                if new_content != content:
                    log('-- run_test.py: inject classname prefix into gtest '
                        "result file '%s'" % args.result_file)
                    with open(args.result_file, 'w') as h:
                        h.write(new_content)

        log("-- run_test.py: verify result file '%s'" % args.result_file)
        # if result file exists ensure that it contains valid xml
        # unit test suites are not good about screening out
        # illegal unicode characters
        tree = None
        try:
            tree = ElementTree(None, args.result_file)
        except ParseError as e:
            modified = _tidy_xml(args.result_file)
            if not modified:
                log("Invalid XML in result file '%s': %s" %
                    (args.result_file, str(e)),
                    file=sys.stderr)
            else:
                try:
                    tree = ElementTree(None, args.result_file)
                except ParseError as e:
                    log("Invalid XML in result file '%s' (even after trying to tidy it): %s"
                        % (args.result_file, str(e)),
                        file=sys.stderr)

        if not tree:
            # set error code when result file is not parsable
            rc = 1
        else:
            # set error code when result file contains errors or failures
            root = tree.getroot()
            num_errors = int(root.attrib.get('errors', 0))
            num_failures = int(root.attrib.get('failures', 0))
            if num_errors or num_failures:
                rc = 1

    # ensure that a result file exists at the end
    if not rc and not os.path.exists(args.result_file):
        log(
            '-- run_test.py: override return code since no result file was '
            'generated',
            file=sys.stderr)
        rc = 1

    return rc
Esempio n. 58
0
logger = logging.getLogger('rawbracket')
hdlr = logging.FileHandler('rawbracket.log')
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)

EXP = "Exposure"
UFRAW_BATCH = whereis('ufraw-batch')

ufrawrc = os.path.expanduser("~/.ufrawrc")
logger.info("reading %s" % (ufrawrc))
tree = ElementTree()
tree.parse(ufrawrc)
ufraw = tree.getroot()

exposure = tree.find(EXP)
if exposure is not None:
    ev = float(exposure.text)
else:
    ev = 0.0
    exposure = SubElement(ufraw, EXP)

def get_ufraw_conf(ufrawrc):
    
    logger.info("reading %s" % (ufrawrc))
    conf = ElementTree()
    conf.parse(ufrawrc)
    ufraw = conf.getroot()
    exposure = conf.find(EXP)
Esempio n. 59
0
    def mutate_tree(tree: ET.ElementTree):

        worldbody = tree.getroot().find("./worldbody")
        rgba = [
            "0 1 0 1",
            "0 0 1 1",
            "0 1 1 1",
            "1 0 0 1",
            "1 0 1 1",
            "1 1 0 1",
            "1 1 1 1",
        ]

        if worldbody:
            for i in range(n_blocks):
                pos = " ".join(map(str, goal_space.sample()))
                name = f"block{i}"

                body = ET.SubElement(worldbody,
                                     "body",
                                     attrib=dict(name=name, pos=pos))
                ET.SubElement(
                    body,
                    "geom",
                    attrib=dict(
                        name=name,
                        type="box",
                        mass="1",
                        size=".05 .025 .017",
                        rgba=rgba[i],
                        condim="6",
                        solimp="0.99 0.99 "
                        "0.01",
                        solref="0.01 1",
                    ),
                )
                ET.SubElement(body,
                              "freejoint",
                              attrib=dict(name=f"block{i}joint"))

        for change in changes:
            parent = re.sub("/[^/]*$", "", change.path)
            element_to_change = tree.find(parent)
            if isinstance(element_to_change, ET.Element):
                print("setting", change.path, "to", change.value)
                name = re.search("[^/]*$", change.path)[0]
                element_to_change.set(name, change.value)

        for actuators in tree.iter("actuator"):
            for actuator in list(actuators):
                if actuator.get("joint") not in dofs:
                    print("removing", actuator.get("name"))
                    actuators.remove(actuator)
        for body in tree.iter("body"):
            for joint in body.findall("joint"):
                if not joint.get("name") in dofs:
                    print("removing", joint.get("name"))
                    body.remove(joint)

        parent = Path(temp[xml_filepath].name).parent

        for include_elt in tree.findall("*/include"):
            original_abs_path = rel_to_abs(include_elt.get("file"))
            tmp_abs_path = Path(temp[original_abs_path].name)
            include_elt.set("file", str(tmp_abs_path.relative_to(parent)))

        for compiler in tree.findall("compiler"):
            abs_path = rel_to_abs(compiler.get("meshdir"))
            compiler.set("meshdir", str(abs_path))

        return tree
Esempio n. 60
0
def setup_city_gates(net: sumolib.net.Net, stats: ET.ElementTree,
                     gate_count: str, city_radius: float):
    """
    Generate the requested amount of city gates based on the network and insert them into stats.
    """

    gate_count = find_gate_count_auto(
        city_radius) if gate_count == "auto" else int(gate_count)
    assert gate_count >= 0, "Number of city gates cannot be negative"

    # Find existing gates to determine how many we need to insert
    xml_gates = stats.find("cityGates")
    if xml_gates is None:
        xml_gates = ET.SubElement(stats.getroot(), "cityGates")
    xml_entrances = xml_gates.findall("entrance")
    n = gate_count - len(xml_entrances)
    if n < 0:
        logging.debug(
            f"[gates] {gate_count} city gate were requested, but there are already {len(xml_entrances)} defined"
        )
    if n <= 0:
        return

    # Find all nodes that are dead ends, i.e. nodes that only have one neighbouring node
    # and at least one of the connecting edges is a road (as opposed to path) and allows private vehicles
    dead_ends = [
        node for node in net.getNodes()
        if len(node.getNeighboringNodes()) == 1 and any([
            any([lane.allows("private") for lane in edge.getLanes()])
            for edge in node.getIncoming() + node.getOutgoing()
        ])
    ]

    # The user cannot get more gates than there are dead ends
    n = min(n, len(dead_ends))
    logging.debug(f"[gates] Identified {len(dead_ends)} dead ends")
    logging.debug(f"[gates] Inserting {n} new city gates")

    # Find n unit vectors pointing in different directions
    # If n = 4 and base_rad = 0 we get the cardinal directions:
    #      N
    #      |
    # W<---o--->E
    #      |
    #      S
    base_rad = random.random() * math.tau
    rads = [(base_rad + i * math.tau / n) % math.tau for i in range(0, n)]
    directions = [(math.cos(rad), math.sin(rad)) for rad in rads]

    for direction in directions:
        # Find the dead ends furthest in each direction using the dot product and argmax. Those nodes will be our gates.
        # Dead ends are removed from the list to avoid duplicates.
        gate_index = int(
            np.argmax(
                [np.dot(node.getCoord(), direction) for node in dead_ends]))
        gate = dead_ends[gate_index]
        dead_ends.remove(gate)

        # Decide proportion of the incoming and outgoing vehicles coming through this gate
        # These numbers are relatively to the values of the other gates
        # The number is proportional to the number of lanes allowing private vehicles
        incoming_lanes = sum([
            len([lane for lane in edge.getLanes() if lane.allows("private")])
            for edge in gate.getIncoming()
        ])
        outgoing_lanes = sum([
            len([lane for lane in edge.getLanes() if lane.allows("private")])
            for edge in gate.getOutgoing()
        ])
        incoming_traffic = (1 + random.random()) * outgoing_lanes
        outgoing_traffic = (1 + random.random()) * incoming_lanes

        # Add entrance to stats file
        edge, pos = (gate.getOutgoing()[0], 0) if len(gate.getOutgoing()) > 0 \
            else (gate.getIncoming()[0], gate.getIncoming()[0].getLength())
        logging.debug(
            f"[gates] Adding entrance to statistics, edge: {edge.getID()}, incoming traffic: {incoming_traffic}, outgoing "
            f"traffic: {outgoing_traffic}")
        ET.SubElement(xml_gates,
                      "entrance",
                      attrib={
                          "edge": edge.getID(),
                          "incoming": str(incoming_traffic),
                          "outgoing": str(outgoing_traffic),
                          "pos": str(pos)
                      })