示例#1
0
文件: manager.py 项目: f4b10/X4GA
    def StoreXml(self, dValue):
        try:
            cBasePath=self.GetUpdatePath()
            now    = datetime.datetime.utcnow()
            nowFile='%s-%s' % (now.strftime('%Y%m%d-%H%M%S'), '%04d' % (now.microsecond /1000))
            nowXml ='%s:%s' % (now.strftime('%Y%m%d-%H:%M:%S'), '%04d' % (now.microsecond /1000))
            doc=Document()
            root = doc.createElement("dbUpdate")
            for k in ['table', 'operation']:
                root.setAttribute( k, dValue[k] )
            root.setAttribute( 'datetime', '%s' % nowXml )
            doc.appendChild(root)

            root.appendChild(self.CreateVersionNode(doc))
            root.appendChild(self.CreateRecordNode(doc, dValue))

            pathName=os.path.join(cBasePath, '%s' % nowFile)
            self._mkdir_recursive(pathName)
            fileName=os.path.join(pathName, '%s.xml' % nowFile )
            doc.writexml( open(fileName, 'w'),
                          indent="  ",
                          addindent="  ",
                          newl='\n')
            doc.unlink()
            lEsito=True
        except:
            lEsito=False
        return lEsito
def CopyBinaries(out_dir, out_project_dir, src_package, shared):
    # Copy jar files to libs.
    libs_dir = os.path.join(out_project_dir, "libs")
    if not os.path.exists(libs_dir):
        os.mkdir(libs_dir)

    if shared:
        libs_to_copy = ["xwalk_core_library_java_app_part.jar"]
    elif src_package:
        libs_to_copy = ["jsr_305_javalib.jar"]
    else:
        libs_to_copy = ["xwalk_core_library_java.jar"]

    for lib in libs_to_copy:
        source_file = os.path.join(out_dir, "lib.java", lib)
        target_file = os.path.join(libs_dir, lib)
        shutil.copyfile(source_file, target_file)

    if shared:
        return

    print "Copying binaries..."
    # Copy assets.
    res_raw_dir = os.path.join(out_project_dir, "res", "raw")
    res_value_dir = os.path.join(out_project_dir, "res", "values")
    if not os.path.exists(res_raw_dir):
        os.mkdir(res_raw_dir)
    if not os.path.exists(res_value_dir):
        os.mkdir(res_value_dir)

    paks_to_copy = [
        "icudtl.dat",
        # Please refer to XWALK-3516, disable v8 use external startup data,
        # reopen it if needed later.
        # 'natives_blob.bin',
        # 'snapshot_blob.bin',
        "xwalk.pak",
        "xwalk_100_percent.pak",
    ]

    pak_list_xml = Document()
    resources_node = pak_list_xml.createElement("resources")
    string_array_node = pak_list_xml.createElement("string-array")
    string_array_node.setAttribute("name", "xwalk_resources_list")
    pak_list_xml.appendChild(resources_node)
    resources_node.appendChild(string_array_node)
    for pak in paks_to_copy:
        source_file = os.path.join(out_dir, pak)
        target_file = os.path.join(res_raw_dir, pak)
        shutil.copyfile(source_file, target_file)
        item_node = pak_list_xml.createElement("item")
        item_node.appendChild(pak_list_xml.createTextNode(pak))
        string_array_node.appendChild(item_node)
    pak_list_file = open(os.path.join(res_value_dir, "xwalk_resources_list.xml"), "w")
    pak_list_xml.writexml(pak_list_file, newl="\n", encoding="utf-8")
    pak_list_file.close()

    # Copy native libraries.
    source_dir = os.path.join(out_dir, XWALK_CORE_SHELL_APK, "libs")
    distutils.dir_util.copy_tree(source_dir, libs_dir)
def generate_tool_conf(parsed_models, tool_conf_dest, galaxy_tool_path, default_category):
    # for each category, we keep a list of models corresponding to it
    categories_to_tools = dict()
    for model in parsed_models:
        category = strip(model[0].opt_attribs.get("category", default_category))
        if category not in categories_to_tools:
            categories_to_tools[category] = []
        categories_to_tools[category].append(model[1])
                
    # at this point, we should have a map for all categories->tools
    doc = Document()
    toolbox_node = doc.createElement("toolbox")
    
    if galaxy_tool_path is not None and not galaxy_tool_path.strip().endswith("/"):
        galaxy_tool_path = galaxy_tool_path.strip() + "/"
    if galaxy_tool_path is None:
        galaxy_tool_path = ""
    
    for category, filenames in categories_to_tools.iteritems():
        section_node = doc.createElement("section")
        section_node.setAttribute("id", "section-id-" + "".join(category.split()))
        section_node.setAttribute("name", category)
    
        for filename in filenames:
            tool_node = doc.createElement("tool")
            tool_node.setAttribute("file", galaxy_tool_path + filename)
            toolbox_node.appendChild(section_node)
            section_node.appendChild(tool_node)
        toolbox_node.appendChild(section_node)

    doc.appendChild(toolbox_node)
    doc.writexml(open(tool_conf_dest, 'w'), indent="    ", addindent="    ", newl='\n', encoding="UTF-8")
    print("Generated Galaxy tool_conf.xml in [%s]\n" % tool_conf_dest)
def CopyBinaries(out_dir, out_project_dir, src_package, shared):
  # Copy jar files to libs.
  libs_dir = os.path.join(out_project_dir, 'libs')
  if not os.path.exists(libs_dir):
    os.mkdir(libs_dir)

  if shared:
    libs_to_copy = ['xwalk_core_library_java_app_part.jar']
  elif src_package:
    libs_to_copy = ['jsr_305_javalib.jar', ]
  else:
    libs_to_copy = ['xwalk_core_library_java.jar', ]

  for lib in libs_to_copy:
    source_file = os.path.join(out_dir, 'lib.java', lib)
    target_file = os.path.join(libs_dir, lib)
    shutil.copyfile(source_file, target_file)

  if shared:
    return

  print 'Copying binaries...'
  # Copy assets.
  res_raw_dir = os.path.join(out_project_dir, 'res', 'raw')
  res_value_dir = os.path.join(out_project_dir, 'res', 'values')
  if not os.path.exists(res_raw_dir):
    os.mkdir(res_raw_dir)
  if not os.path.exists(res_value_dir):
    os.mkdir(res_value_dir)

  paks_to_copy = [
      'icudtl.dat',
      # Please refer to XWALK-3516, disable v8 use external startup data,
      # reopen it if needed later.
      # 'natives_blob.bin',
      # 'snapshot_blob.bin',
      'xwalk.pak',
  ]

  pak_list_xml = Document()
  resources_node = pak_list_xml.createElement('resources')
  string_array_node = pak_list_xml.createElement('string-array')
  string_array_node.setAttribute('name', 'xwalk_resources_list')
  pak_list_xml.appendChild(resources_node)
  resources_node.appendChild(string_array_node)
  for pak in paks_to_copy:
    source_file = os.path.join(out_dir, pak)
    target_file = os.path.join(res_raw_dir, pak)
    shutil.copyfile(source_file, target_file)
    item_node = pak_list_xml.createElement('item')
    item_node.appendChild(pak_list_xml.createTextNode(pak))
    string_array_node.appendChild(item_node)
  pak_list_file = open(os.path.join(res_value_dir,
                                    'xwalk_resources_list.xml'), 'w')
  pak_list_xml.writexml(pak_list_file, newl='\n', encoding='utf-8')
  pak_list_file.close()

  # Copy native libraries.
  source_dir = os.path.join(out_dir, XWALK_CORE_SHELL_APK, 'libs')
  distutils.dir_util.copy_tree(source_dir, libs_dir)
示例#5
0
    def extract_xml(self, rootnodes, name=None):
        '''
        Store extracted information in an xml file.\n
        You are expected to provide a list of rootnodes instead of a single one
        to reduce duplicate parsing xml strings.\n
        '''

        def __generator():
            for rootnode in rootnodes:
                yield self._extract(rootnode)

        # Initialize variables
        filename = f'{name or self.name}.xml'
        if os.path.exists(filename):
            doc = parse(filename)
        else:
            doc = Document()
            root = doc.createElement(f'{name or self.name}')
            doc.appendChild(root)
        root = doc.firstChild
        list_of_values = __generator()
        # Append new elements
        for values in list_of_values:
            item = doc.createElement('item')
            for key, value in values.items():
                key = doc.createElement(str(key))
                for each in value:
                    key.appendChild(doc.createElement('text') \
                                    .appendChild(doc.createTextNode(str(each))))
                item.appendChild(key)
            root.appendChild(item)
        # Write xml files
        with open(filename, 'wt', encoding='utf-8') as fout:
            doc.writexml(fout, indent=' '*4, addindent=' '*4, newl='\n', encoding='utf-8')
示例#6
0
文件: addon.py 项目: Brainiarc7/Kodi
def addToFavorite(game):
    doc = None
    root = None

    if (not os.path.isfile(FAVOURITES_PATH)):            
        doc = Document()
        root = doc.createElement("favourites")
        doc.appendChild(root)
    else:
        doc = parse( FAVOURITES_PATH )
        root = doc.documentElement

    favNode = doc.createElement("favourite")
    root.appendChild(favNode)

    favNode.setAttribute( "name", game.title)
    favNode.setAttribute( "thumb", game.thumbImage)

    url = getGameLaunchUrlAction(game)

    textNode = doc.createTextNode(url)
    favNode.appendChild(textNode)
 
    doc.writexml(open(FAVOURITES_PATH, 'w'))
 
    doc.unlink()
示例#7
0
    def createSVG(s, outfile, r = 1):
        src = parse(s.filename)
        new = Document()
        svg = src.getElementsByTagName('svg')[0]
        new.appendChild(svg)
        defs = new.getElementsByTagName('defs')[0]
        #rec0 = new.getElementById(s.rec_name)
        #print (rec0.appendChil)
        
        title = svg.getElementsByTagName('title')[0]
        if 'data-type' in title.attributes and title.attributes['data-type'].value == 'shape':
            s.shapegen(defs, new )
        else:
            s.gen(r, defs, new )
        svg = new.getElementsByTagName('svg')[0]
        #svg.appendChild(rect)
        #svg.setAttribute('width', "1000")
        #svg.setAttribute('height', "1000")
        #use = new.createElement('use')
        #use.setAttribute("xlink:href", "#fractal")
        #use.setAttribute("stroke-width", "4")
        #use.setAttribute("transform", "translate(100,-300) scale(8)")
        #use.setAttribute("stroke", "black")
        #use.setAttribute("stroke-width", "2")
        #svg.appendChild(use)

        with open(outfile, "w") as f:
            new.writexml(f, newl="\n", addindent="    ", indent="    ")
示例#8
0
    def printInherTree(self):
        """Creates and print out minidom structure => inheritance tree of whole Model"""

        # create minidom-document
        doc = Document()

        # create model element
        model = doc.createElement("model")
        doc.appendChild(model)

        # loop through all parent/base classes
        for cl in self.classInstances:
            if len(cl.inheritance) == 0:
                entry = doc.createElement("class")
                entry.setAttribute("name", cl.name)
                entry.setAttribute("kind", cl.kind)
                model.appendChild(entry)

                children = self.__findInheritanceRecursive(cl.name)
                if len(children) != 0:
                    for ch in children:
                        entry.appendChild(ch)
                # else:
                #    entry.appendChild(Document().createTextNode(""))  # insert empty node, to prevent having self closing node

            elif len(cl.inheritance) > 1:   # check if conflict in the cl is possible
                if self.detectConflict(cl, cl):
                    # print("conflict")
                    raise Exception("Conflict in class: "+cl.name)

        doc.writexml(outputSrc, "", " "*indentation, "\n", encoding="utf-8")
示例#9
0
 def saveTextureList(self, *args, **kwargs):
   ''' write an xml file with a list of the scenes textures and timestamps '''
   fileNodes = pm.ls(type='file')
   sceneName = path.getSceneName()
   xmlFileName = sceneName+'_textureList'
   
   doc = Document()
   textureList = doc.createElement('textureList')
   textureList.setAttribute('sceneName', sceneName)
   doc.appendChild(textureList)
   
   for node in fileNodes:
     fileTextureName = pm.getAttr(node+'.fileTextureName')
     if os.path.isfile(fileTextureName):
       time = os.path.getmtime(fileTextureName)
       
       textureNode = doc.createElement('textureNode')
       textureNode.setAttribute('nodeName', node)
       textureList.appendChild(textureNode)
       
       texturePath = doc.createElement('path')
       texturePath.appendChild(doc.createTextNode(fileTextureName) )
       textureNode.appendChild(texturePath)
       
       textureTime = doc.createElement('time')   
       textureTime.appendChild(doc.createTextNode(str(time) ) )
       textureNode.appendChild(textureTime)
     
   f = open(self.settingsPath+xmlFileName+'.xml', 'w+')
   #f.write(doc.toprettyxml(indent='    ') ) #This is super slow !!!!!
   doc.writexml(f)
   f.close()
示例#10
0
	def __init__(self, context):
		self.output_file = context.options.output_file
		doc = Document()
		collection = doc.createElement("TorrentCollection")
		doc.appendChild(collection)
		f=open(self.output_file, 'w')
		doc.writexml(f)
示例#11
0
def make_SVG():
    new = Document()
    svg = new.createElement("svg")
    svg.setAttribute('width',"600")
    svg.setAttribute('height', "600")
    svg.setAttribute('xmlns', "http://www.w3.org/2000/svg")
    svg.setAttribute('xmlns:xlink', "http://www.w3.org/1999/xlink")
    svg.setAttribute('xmlns:ev',"http://www.w3.org/2001/xml-events")
    defs = new.createElement('defs')
    svg.appendChild(defs)
    new.appendChild(svg)

    titles = new.createElement('g')
    titles.setAttribute('id',"titles")
    svg.appendChild(titles)

    g = new.createElement('g')
    g.setAttribute('id', 'main')
    rect = new.createElement('rect')
    rect.setAttribute('fill', "white")
    rect.setAttribute( 'height', "1000")
    rect.setAttribute( 'width', "1000")
    rect.setAttribute('x', '0')
    rect.setAttribute('y', '0')
    #g.appendChild(rect)
    svg.appendChild(g)
    fname = abspath ( 'static/fractals/{0}.svg'.format(time()) )
    with open(fname, "w") as f:
        new.writexml(f, newl="\n", addindent="    ", indent="    ")
    return fname
示例#12
0
    def create_fake_config_file(self):
        doc = Document()
        emc = doc.createElement("EMC")
        doc.appendChild(emc)

        storagetype = doc.createElement("StorageType")
        storagetypetext = doc.createTextNode("gold")
        emc.appendChild(storagetype)
        storagetype.appendChild(storagetypetext)

        ecomserverip = doc.createElement("EcomServerIp")
        ecomserveriptext = doc.createTextNode("1.1.1.1")
        emc.appendChild(ecomserverip)
        ecomserverip.appendChild(ecomserveriptext)

        ecomserverport = doc.createElement("EcomServerPort")
        ecomserverporttext = doc.createTextNode("10")
        emc.appendChild(ecomserverport)
        ecomserverport.appendChild(ecomserverporttext)

        ecomusername = doc.createElement("EcomUserName")
        ecomusernametext = doc.createTextNode("user")
        emc.appendChild(ecomusername)
        ecomusername.appendChild(ecomusernametext)

        ecompassword = doc.createElement("EcomPassword")
        ecompasswordtext = doc.createTextNode("pass")
        emc.appendChild(ecompassword)
        ecompassword.appendChild(ecompasswordtext)

        self.config_file_path = self.tempdir + '/' + config_file_name
        f = open(self.config_file_path, 'w')
        doc.writexml(f)
        f.close()
def CopyBinaries(out_dir):
  """cp out/Release/<pak> out/Release/xwalk_core_library/res/raw/<pak>
     cp out/Release/lib.java/<lib> out/Release/xwalk_core_library/libs/<lib>
     cp out/Release/xwalk_core_shell_apk/libs/*
        out/Release/xwalk_core_library/libs
  """

  print 'Copying binaries...'
  # Copy assets.
  res_raw_dir = os.path.join(
      out_dir, LIBRARY_PROJECT_NAME, 'res', 'raw')
  res_value_dir = os.path.join(
      out_dir, LIBRARY_PROJECT_NAME, 'res', 'values')
  if not os.path.exists(res_raw_dir):
    os.mkdir(res_raw_dir)
  if not os.path.exists(res_value_dir):
    os.mkdir(res_value_dir)

  paks_to_copy = [
      'icudtl.dat',
      'xwalk.pak',
  ]

  pak_list_xml = Document()
  resources_node = pak_list_xml.createElement('resources')
  string_array_node = pak_list_xml.createElement('string-array')
  string_array_node.setAttribute('name', 'xwalk_resources_list')
  pak_list_xml.appendChild(resources_node)
  resources_node.appendChild(string_array_node)
  for pak in paks_to_copy:
    source_file = os.path.join(out_dir, pak)
    target_file = os.path.join(res_raw_dir, pak)
    shutil.copyfile(source_file, target_file)
    item_node = pak_list_xml.createElement('item')
    item_node.appendChild(pak_list_xml.createTextNode(pak))
    string_array_node.appendChild(item_node)
  pak_list_file = open(os.path.join(res_value_dir,
                                    'xwalk_resources_list.xml'), 'w')
  pak_list_xml.writexml(pak_list_file, newl='\n', encoding='utf-8')
  pak_list_file.close()

  # Copy jar files to libs.
  libs_dir = os.path.join(out_dir, LIBRARY_PROJECT_NAME, 'libs')
  if not os.path.exists(libs_dir):
    os.mkdir(libs_dir)

  libs_to_copy = [
      'xwalk_core_library_java_app_part.jar',
      'xwalk_core_library_java_library_part.jar',
  ]

  for lib in libs_to_copy:
    source_file = os.path.join(out_dir, 'lib.java', lib)
    target_file = os.path.join(libs_dir, lib)
    shutil.copyfile(source_file, target_file)

  # Copy native libraries.
  source_dir = os.path.join(out_dir, XWALK_CORE_SHELL_APK, 'libs')
  target_dir = libs_dir
  distutils.dir_util.copy_tree(source_dir, target_dir)
示例#14
0
class SharedSourceTree2XMLFile:
	def __init__(self, fileName = 'resultXML', obj = None, parent = None):
		self.fileName = fileName
		self.rootItem = obj
		self.SEP = os.sep
		self.doc = Document()
		self.filePrepare()

	def __del__(self):
		self.fileName = None
		self.rootItem = None
		self.doc = None

	def filePrepare(self):
		self.doc.appendChild(self.treeSharedDataToXML(self.rootItem))

		#print self.doc.toprettyxml()
		try :
			fileName = Path.multiPath(Path.tempStruct, 'server', self.fileName)
			with open(fileName, 'wb') as f :
				#f.write(doc.toprettyxml())   ## без доп параметров неправильно отображает дерево
				self.doc.writexml(f, encoding = 'utf-8')
			#with open(fileName, 'rb') as f :
			#	print 'Create item File %s :\n%s\n---END---\n' % (fileName, f.read())
		except UnicodeError , err:
			print '[SharedSourceTree2XMLFile.filePrepare() : File not saved]', err
		except IOError, err :
			print '[in SharedSourceTree2XMLFile.filePrepare()] IOError:', err
示例#15
0
    def generateDirectoryFragments( self ):
        wxs = Document()
        wix = wxs.createElement( "Wix" )
        wix.setAttribute( "xmlns", "http://schemas.microsoft.com/wix/2006/wi" )
        wxs.appendChild( wix )
        for _dirref in sorted( self.dirs.keys() ):
            fragment = wxs.createElement( "Fragment" )
            wix.appendChild( fragment )
            directoryRef = wxs.createElement( "DirectoryRef" )
            _dirrefId = getUniqueDirectoryId( _dirref )
            if _dirref  == ".":
                _dirrefId = "INSTALLDIR"
            directoryRef.setAttribute( "Id", _dirrefId )
            fragment.appendChild( directoryRef )
            for _dir in self.dirs[ _dirref ]:
                dirElement = wxs.createElement( "Directory" )
                if not _dirref == ".":
                    _id = getUniqueDirectoryId( os.path.join( _dirref, _dir ) )
                else:
                    _id = getUniqueDirectoryId( _dir )
                dirElement.setAttribute( "Id", _id )
                dirElement.setAttribute( "Name", _dir )
                directoryRef.appendChild( dirElement )

        outfile = os.path.join( self.imageDir(), "_directories.wxs" )
        out = open( outfile, 'w' )
        wxs.writexml( out, "", "    ", "\n", encoding = "utf-8" )
        out.close()

        objfile = outfile.replace( "wxs", "wix" ) + "obj"
        utils.system( "candle -o %s %s" % ( objfile, outfile ) )
        return objfile
示例#16
0
def save_pairs(pairs, path):
    doc = Document()
    corpus = doc.createElement('entailment-corpus')
    doc.appendChild(corpus)
    
    i = 0
    for pair in pairs:
        try:
            text, hypothesis, entailment = pair
        except ValueError:
            print "Too many values: ", pair
            continue
        i += 1
        pair = doc.createElement('pair')
        pair.setAttribute('id', str(i))
        pair.setAttribute('value', str(entailment).upper())
        corpus.appendChild(pair)
        
        t = doc.createElement('t')
        pair.appendChild(t)
        
        t_text = doc.createTextNode(text)
        t.appendChild(t_text)
        
        h = doc.createElement('h')
        pair.appendChild(h)
        
        h_text = doc.createTextNode(hypothesis)
        h.appendChild(h_text)
    f = open(path, 'w')
    doc.writexml(f, addindent=' ', newl='\n')
def writeDocument(sourcePath,targetPath,xmlFileName):

    if sourcePath == None or targetPath == None:
        return False

    ## Added May2016. warn user if capabilities are not correct, exit if not a valid layer
    if not dla.checkServiceCapabilities(sourcePath,False):
        return False
    if not dla.checkServiceCapabilities(targetPath,False):
        return False

    desc = arcpy.Describe(sourcePath)
    descT = arcpy.Describe(targetPath)

    xmlDoc = Document()
    root = xmlDoc.createElement('SourceTargetMatrix')
    xmlDoc.appendChild(root)
    root.setAttribute("version",'1.1')
    root.setAttribute("xmlns:esri",'http://www.esri.com')
    
    dataset = xmlDoc.createElement("Datasets")
    root.appendChild(dataset)
    prj = dla.getProject()
    setSourceTarget(dataset,xmlDoc,"Project",prj.filePath)
    setSourceTarget(dataset,xmlDoc,"Source",sourcePath)
    setSourceTarget(dataset,xmlDoc,"Target",targetPath)

    setSpatialReference(dataset,xmlDoc,desc,"Source")
    setSpatialReference(dataset,xmlDoc,descT,"Target")

    setSourceTarget(dataset,xmlDoc,"ReplaceBy","")

    fieldroot = xmlDoc.createElement("Fields")
    root.appendChild(fieldroot)

    fields = getFields(descT)
    sourceFields = getFields(desc)
    sourceNames = [field.name[field.name.rfind(".")+1:] for field in sourceFields]
    upperNames = [nm.upper() for nm in sourceNames]

    #try:
    for field in fields:

        fNode = xmlDoc.createElement("Field")
        fieldroot.appendChild(fNode)
        fieldName = field.name[field.name.rfind(".")+1:]
        matchSourceFields(xmlDoc,fNode,field,fieldName,sourceNames,upperNames)

    # write the source field values
    setSourceFields(root,xmlDoc,sourceFields)
    setTargetFields(root,xmlDoc,fields)
    # Should add a template section for value maps, maybe write domains...
    # could try to preset field mapping and domain mapping...

    # add data to the document
    writeDataSample(xmlDoc,root,sourceNames,sourcePath,10)
    # write it out
    xmlDoc.writexml( open(xmlFileName, 'wt', encoding='utf-8'),indent="  ",addindent="  ",newl='\n')
    xmlDoc.unlink()
示例#18
0
def writexml(umlayout,filename):
    for clas in umlayout.get_clases():
        c=clasrec(clas)
        doc = Document()
        doc.appendChild(c)
        fp = open( clas["name"]+".xml","w")
        # writexml(self, writer, indent='', addindent='', newl='', encoding=None)
        doc.writexml(fp, "    ", "", "\n", "UTF-8")
示例#19
0
def convertToXml(sfofile, xml):
	doc = Document()
	sfo = doc.createElement("sfo")


	with open(sfofile, 'rb') as fp:
		stuff = {}
		data = fp.read()
		offset = 0
		header = Header()
		header.unpack(data[offset:offset+len(header)])
		if debug:
			print header
			print
		if header.magic != SFO_MAGIC:
			print "Error:  SFO MAGIC does not equal PSF (%xh).  Are you sure this is a SFO file?" % SFO_MAGIC
			sys.exit(2)
		if header.version != 0x00000101:
			print "Error:  Found header version: (%08xh), but we where expecting header version (00000101h)." % header.version
			print "\tPlease submit an issue at https://github.com/ChillyWillyGuru/PSL1GHT"
			sys.exit(2)
		offset += len(header)
		off1 = header.KeyOffset
		off2 = header.ValueOffset
		for x in xrange(header.PairCount):
			entry = Entry()
			entry.unpack(data[offset:offset+len(entry)])
			if debug and not pretty:
				print entry
				print
			if debug and pretty:
				print entry.PrettyPrint(data, off1, off2)
				print
			key = nullterm(data[off1+entry.key_off:])
			valuenode = doc.createElement("value")
			valuenode.setAttribute("name", key)
			if entry.value_type == SFO_STRING:
				value = nullterm(data[off2+entry.value_off:])
				valuenode.setAttribute("type", "string")
				valuenode.appendChild(doc.createTextNode(value))
			else:
				value = struct.unpack('<I', data[entry.value_off + off2:entry.value_off + off2 + 4])[0]
				valuenode.setAttribute("type", "integer")
				valuenode.appendChild(doc.createTextNode("%d" % value))
			sfo.appendChild(valuenode)
			stuff[key] = value
			offset += len(entry)
		if not debug and pretty:
			for k,v in stuff.items():
				print '%s : %s' % (k, v)
				print '========================'
		if not debug and not pretty:
			print stuff

	doc.appendChild(sfo)
	file = open(xml, "wb" )
	doc.writexml(file, '', '\t', '\n' )
	file.close()
示例#20
0
def writeFile(fileName, persons):
    
    doc = Document()
     
    root = doc.createElement("PersonList")
    doc.appendChild(root)

    id = 0;
         
    for person in persons:

        personList = doc.createElement("Person")
        personList.setAttribute("id", str(id))
        id += 1
        root.appendChild(personList)
        
        print person
        # Create Element
        tempChild = doc.createElement("name")
        personList.appendChild(tempChild)
     
        # Write Text
        nodeText = doc.createTextNode(person.name)
        tempChild.appendChild(nodeText)

        # Create Element
        tempChild = doc.createElement("gender")
        personList.appendChild(tempChild)
     
        # Write Text
        nodeText = doc.createTextNode(person.gender)
        tempChild.appendChild(nodeText)

        # Create Element
        tempChild = doc.createElement("phone")
        personList.appendChild(tempChild)
     
        # Write Text
        nodeText = doc.createTextNode(person.phone)
        tempChild.appendChild(nodeText)

        # Create Element
        tempChild = doc.createElement("email")
        personList.appendChild(tempChild)
     
        # Write Text
        nodeText = doc.createTextNode(person.email)
        tempChild.appendChild(nodeText)

    # Write File 
    doc.writexml( open((fileName+".xml"), 'w'),
                   indent="  ",
                   addindent="  ",
                   newl='\n')
     
    doc.unlink()

    print "ÀÉ®×¼g¤J§¹¦¨!"
示例#21
0
def create():
    matrikelnr_start = 922011                       # erste Matrikelnr. die vergeben wird
    # xml Dokument erzeugen
    document = Document()    
    # root Element erzeugen
    root = document.createElement("Studierende")
    document.appendChild(root)
    # child Elemente hinzufügen
    for i in range(10):                             # Anzahl der zu erzeugenden Studenten
        student = document.createElement("Student")
        student.setAttribute("id", str(i))
        root.appendChild(student)                   # Student dem root-Objekt hinzufügen
        # Matrikelnr. dem Student als child hinzufügen
        matrikel = document.createElement("Matrikelnr")
        student.appendChild(matrikel)
        matrikelnr = document.createTextNode(str(matrikelnr_start))
        matrikel.appendChild(matrikelnr)
        matrikelnr_start+=10                        # fortlaufende Matrikelnr., damit eindeutig
        # Nachname dem Student als child hinzufügen
        nachnameelem = document.createElement("Nachname")
        student.appendChild(nachnameelem)
        nachname = document.createTextNode(random.choice(list_nachname)) # Nachnamen zufällig aus Liste wählen
        nachnameelem.appendChild(nachname)
        # Vorname dem Student als child hinzufügen
        vornameelem = document.createElement("Vorname")
        student.appendChild(vornameelem)
        vorname = document.createTextNode(random.choice(list_vorname))  # Vorname zufällig aus Liste wählen
        vornameelem.appendChild(vorname)
        # Art der Anmeldung (PA/NA) dem Student als child hinzufügen
        anmeldung = document.createElement("Pflichtanmeldung")
        student.appendChild(anmeldung)
        pflichtanmeldung = document.createTextNode(random.choice(list_pflichtanmeldung))    # zufällige Auswahl PA oder NA
        anmeldung.appendChild(pflichtanmeldung)
        # 
        # Prioritäten für Seminare
        prioritaeten = [1,2,3]                  # Prioritäten die verwendet werden dürfen: unterschiedlich, entspr. der Anzahl der Seminare
        for j in range (1,4):                   # entsprechend der ID's der Seminare, beginnend mit ID=1
            seminar = document.createElement("Seminar")
            seminar.setAttribute("id", str(j))  # Seminar als Atrribut die ID vergeben
            student.appendChild(seminar)        # Seminar dem Studenten als child hinzufügen
            # dem Seminar eine Priorität vergeben
            l = random.choice(prioritaeten)     # Priorität zufällig auswählen
            prioritaet = document.createTextNode(str(l))
            seminar.appendChild(prioritaet)     # Priorität dem Seminar hinzufügen
            prioritaeten.remove(l)              # damit laut Vorgabe keine gleichen Prioritäten
        # Anmeldezeit dem Studenten als child hinzufügen       
        anmeldezeit = document.createElement("Anmeldezeitpunkt")
        student.appendChild(anmeldezeit)
        zeitpunkt = document.createTextNode(str(datetime.datetime(2015, random.randint(2,3), random.randint(1,28), random.randint(7,18), random.randint(1,59), random.randint(1,59))))
        anmeldezeit.appendChild(zeitpunkt)
    # Ausgabe
    # print xml # Ausgabe auf Konsole
    # print(document.toprettyxml(indent="    "))
    f = open("Studierende.xml", "w", encoding='utf-8')      # Ausgabe in Datei (Anm.: je nach Einstellung der Entwicklungsumgebung muss encoding='utf-8' entfernt werden und/oder der Datei-PATH geändert werden)
    document.writexml(f, "", "\t", "\n")
    f.close()
    
#g = create()
示例#22
0
    def save_filename(self, fname):
        if fname == '[ New file... ]':
            self.new_file_popup = Popup(title="Please choose a filename.", content = GetNewFilenameLayout(self), size_hint = (.5, .75))
            self.new_file_popup.open()
            return

        print("writing to", fname)

        pbuilder = self.parent.parent
        pbuilder.active_filename = os.path.basename(fname)
        new_particle = Document()
        particle_values = new_particle.createElement("particleEmitterConfig")
        new_particle.appendChild(particle_values)

        particle_values.appendChild(self.xml_from_attribute(new_particle, 'texture', ('name'), (pbuilder.demo_particle.texture_path)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'sourcePositionVariance', ('x', 'y'), (pbuilder.demo_particle.emitter_x_variance, pbuilder.demo_particle.emitter_y_variance)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'gravity', ('x', 'y'), (pbuilder.demo_particle.gravity_x, pbuilder.demo_particle.gravity_y)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'emitterType', ('value'), (pbuilder.demo_particle.emitter_type)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'maxParticles', ('value'), (pbuilder.demo_particle.max_num_particles)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'particleLifeSpan', ('value'), (pbuilder.demo_particle.life_span)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'particleLifespanVariance', ('value'), (pbuilder.demo_particle.life_span_variance)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'startParticleSize', ('value'), (pbuilder.demo_particle.start_size)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'startParticleSizeVariance', ('value'), (pbuilder.demo_particle.start_size_variance)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'finishParticleSize', ('value'), (pbuilder.demo_particle.end_size)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'FinishParticleSizeVariance', ('value'), (pbuilder.demo_particle.end_size_variance)))

        particle_values.appendChild(self.xml_from_attribute(new_particle, 'angle', ('value'), (math.degrees(pbuilder.demo_particle.emit_angle))))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'angleVariance', ('value'), (math.degrees(pbuilder.demo_particle.emit_angle_variance))))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'rotationStart', ('value'), (math.degrees(pbuilder.demo_particle.start_rotation))))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'rotationStartVariance', ('value'), (math.degrees(pbuilder.demo_particle.start_rotation_variance))))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'rotationEnd', ('value'), (math.degrees(pbuilder.demo_particle.end_rotation))))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'rotationEndVariance', ('value'), (math.degrees(pbuilder.demo_particle.end_rotation_variance))))

        particle_values.appendChild(self.xml_from_attribute(new_particle, 'speed', ('value'), (pbuilder.demo_particle.speed)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'speedVariance', ('value'), (pbuilder.demo_particle.speed_variance)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'radialAcceleration', ('value'), (pbuilder.demo_particle.radial_acceleration)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'radialAccelVariance', ('value'), (pbuilder.demo_particle.radial_acceleration_variance)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'tangentialAcceleration', ('value'), (pbuilder.demo_particle.tangential_acceleration)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'tangentialAccelVariance', ('value'), (pbuilder.demo_particle.tangential_acceleration_variance)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'maxRadius', ('value'), (pbuilder.demo_particle.max_radius)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'maxRadiusVariance', ('value'), (pbuilder.demo_particle.max_radius_variance)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'minRadius', ('value'), (pbuilder.demo_particle.min_radius)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'rotatePerSecond', ('value'), (math.degrees(pbuilder.demo_particle.rotate_per_second))))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'rotatePerSecondVariance', ('value'), (math.degrees(pbuilder.demo_particle.rotate_per_second_variance))))

        particle_values.appendChild(self.xml_from_attribute(new_particle, 'startColor', ('red', 'green', 'blue', 'alpha'), pbuilder.demo_particle.start_color))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'startColorVariance', ('red', 'green', 'blue', 'alpha'), pbuilder.demo_particle.start_color_variance))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'finishColor', ('red', 'green', 'blue', 'alpha'), pbuilder.demo_particle.end_color))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'finishColorVariance', ('red', 'green', 'blue', 'alpha'), pbuilder.demo_particle.end_color_variance))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'blendFuncSource', ('value'), (pbuilder.demo_particle.blend_factor_source)))
        particle_values.appendChild(self.xml_from_attribute(new_particle, 'blendFuncDestination', ('value'), (pbuilder.demo_particle.blend_factor_dest)))

        with open(os.path.join(self.get_user_effects_path(), fname), 'w') as outf:
            new_particle.writexml(outf, indent = "  ", newl = "\n")

        Clock.schedule_once(partial(self.save_thumbnail, os.path.join(self.get_user_effects_path(),os.path.splitext(fname)[0]+'.png')), .5)
        self.save_particle_popup.dismiss()
        self.save_particle_popup_content = SaveParticlePopupContents(self)
示例#23
0
 def writexml(self, filename):
     self.session.info("saving session XML file %s" % filename)
     f = open(filename, "w")
     Document.writexml(self, writer=f, indent="", addindent="  ", newl="\n", encoding="UTF-8")
     f.close()
     if self.session.user is not None:
         uid = pwd.getpwnam(self.session.user).pw_uid
         gid = os.stat(self.session.sessiondir).st_gid
         os.chown(filename, uid, gid)
    def __save_xml(self, file):
        doc = Document()
        xml3d = doc.createElement("xml3d")
        doc.appendChild(xml3d)

        for material in self.materials:
            MaterialLibrary.save_material_xml(material, xml3d)

        doc.writexml(file, "", "  ", "\n", "UTF-8")
示例#25
0
	def write_items(self, items):
		doc = Document()
		collection = doc.createElement("HotelesCollection")
		doc.appendChild(collection)
		for item in items :			
			item_node = self.__write_item(item, doc)
			doc.documentElement.appendChild(item_node)
		f=open(self.output_file, 'w')
		doc.writexml(f, encoding='utf-8')
    def saveXML(self, f, stats):
        doc = Document()
        xml3d = doc.createElement("xml3d")
        doc.appendChild(xml3d)
        for material in self.materials.values():
            MaterialLibrary.save_material_xml(material, xml3d)

        for asset in self.assets:
            self.asset_xml(asset, xml3d)
        doc.writexml(f, "", "  ", "\n", "UTF-8")
示例#27
0
def convert_file(playlist):
    '''
    Playlist parser
    '''
    channel_count = 0
    EOL = '\n'
    header = playlist.readline()
    # Catching an exeption if the 1-st string in a *nix-formatted .m3u is empty
    try:
        if header[-2] == '\r':
            EOL = '\r' + EOL # Windows EOL convention
    except IndexError: pass
    if '#EXTM3U' in header:
        xml_config = Document()
        comment = xml_config.createComment(' This file is created by m3u2xml convertor ')
        xml_config.appendChild(comment)
        demo = xml_config.createElement('demo')
        xml_config.appendChild(demo)
        channels = xml_config.createElement('channels')
        demo.appendChild(channels)
        groups = xml_config.createElement('groups')
        demo.appendChild(groups)
        channel_info = []
        groups_list = {}
        # Extra protocols can be added if necessary
        PROTOCOLS = ['udp', 'http', 'rtmpe', 'rtp', 'rtsp']
        try:
            start_number = int(_START_No)
        except ValueError:
            start_number = 1
        for line in playlist:
            if line.split(':')[0] == '#EXTINF':
                channel_info.append(line.split(',')[-1].replace(EOL, '').decode(_ENC))
                if 'group-title=' in line and _ADDON.getSetting('groups') == 'true':
                    current_group = re.search(
                    r'group-title=".*?"', line).group().replace('group-title=', '').replace('\"', '').decode(_ENC)
                    groups_list[current_group] = []
            elif line.split(':')[0] in PROTOCOLS:
                if _ADDON.getSetting('useproxy') == 'true':
                    channel_info.append(_PROXY + line.replace(EOL, '').replace(':/', '').replace('@', ''))
                else:
                    channel_info.append(line.replace(EOL, ''))
                channel_num = start_number + channel_count
                chan_rec_create(xml_config, channels, channel_info, channel_num)
                if groups_list:
                    groups_list[current_group].append(channel_num)
                channel_count += 1
                channel_info = []
        if groups_list:
            groups_create(xml_config, demo, groups_list)
        out_xml = codecs.open(_PVR_CONFIG, encoding='utf-8', mode='w')
        xml_config.writexml(out_xml, newl='\n')
        out_xml.close()
    playlist.close()
    return channel_count
示例#28
0
    def __init__(self):
        if os.path.exists(self.configpath) is False:
            os.mkdir(self.configpath)
        if os.path.exists(self.configpath+self.configfilename) is False:
            xml_handle = open(self.configpath+self.configfilename, 'w')
            xmlconfig = Document()
            configmaintag = xmlconfig.createElement('xmlconfig')
            xmlconfig.appendChild(configmaintag)
            xmlconfig.writexml(xml_handle, indent="\n", addindent="  ", newl="  ")

            xml_handle.close()
示例#29
0
文件: xmlwriter1.py 项目: gsomlo/core
 def writexml(self, filename):
     """
     Commit to file
     """
     logger.info("saving session XML file %s", filename)
     f = open(filename, "w")
     Document.writexml(self, writer=f, indent="", addindent="  ", newl="\n", encoding="UTF-8")
     f.close()
     if self.scenarioPlan.coreSession.user is not None:
         uid = pwd.getpwnam(self.scenarioPlan.coreSession.user).pw_uid
         gid = os.stat(self.scenarioPlan.coreSession.session_dir).st_gid
         os.chown(filename, uid, gid)
    def save_xml_doc(self):
        material_dict = self.materials.material_dict
        doc = Document()
        filename = self.parent.filename
        materials_xml = doc.createElement("materials")
        doc.appendChild(materials_xml)
        for name, material in material_dict.items():
            material_xml = doc.createElement("material")
            materials_xml.appendChild(material_xml)
            name_xml = doc.createElement("name")
            material_xml.appendChild(name_xml)
            name_text = doc.createTextNode(name)
            name_xml.appendChild(name_text)
            surface_color_xml = doc.createElement( "surface_color")
            material_xml.appendChild(surface_color_xml)
            red_xml = doc.createElement("red")
            surface_color_xml.appendChild(red_xml)
            green_xml = doc.createElement("green")
            surface_color_xml.appendChild(green_xml)
            blue_xml = doc.createElement("blue")
            surface_color_xml.appendChild(blue_xml)
            red_text = doc.createTextNode(str(material.get_surface_colour()[0]))
            red_xml.appendChild(red_text)
            green_text = doc.createTextNode(str(material.get_surface_colour()[1]))
            green_xml.appendChild(green_text)
            blue_text = doc.createTextNode(str(material.get_surface_colour()[2]))
            blue_xml.appendChild(blue_text)
            transparency_xml = doc.createElement("transparency")
            material_xml.appendChild(transparency_xml)
            transparency_text = doc.createTextNode(str(material.get_transparency()))
            transparency_xml.appendChild(transparency_text)
            reflectance_method_xml = doc.createElement("reflectance_method")
            material_xml.appendChild(reflectance_method_xml)
            reflectance_method_text = doc.createTextNode(str(material.get_reflectance_method()))
            reflectance_method_xml.appendChild(reflectance_method_text)
            reflectance_xml = doc.createElement("reflectance")
            material_xml.appendChild(reflectance_xml)
            reflectance_text = doc.createTextNode(str(material.get_reflectance()))
            reflectance_xml.appendChild(reflectance_text)
            slip_coefficient_xml = doc.createElement("slip_coefficient")
            material_xml.appendChild(slip_coefficient_xml)
            slip_coefficient_text = doc.createTextNode(str(material.get_slip_coefficient()))
            slip_coefficient_xml.appendChild(slip_coefficient_text)
            imperviousness_xml = doc.createElement("imperviousness")
            material_xml.appendChild(imperviousness_xml)
            imperviousness_text = doc.createTextNode(str(material.get_imperviousness()))
            imperviousness_xml.appendChild(imperviousness_text)

        doc.writexml( open(filename+".xml", 'w'),
                          indent="  ",
                          addindent="  ",
                          newl='\n')
        doc.unlink()
def writeXml(tmp, imgname, w, h, d, bboxes):
    doc = Document()
    annotation = doc.createElement('annotation')
    doc.appendChild(annotation)
    folder = doc.createElement('folder')
    annotation.appendChild(folder)

    folder_txt = doc.createTextNode("VOC2007")

    folder.appendChild(folder_txt)

    filename = doc.createElement('filename')
    annotation.appendChild(filename)
    filename_txt = doc.createTextNode(imgname)
    filename.appendChild(filename_txt)
    source = doc.createElement('source')
    annotation.appendChild(source)
    database = doc.createElement('database')
    source.appendChild(database)
    database_txt = doc.createTextNode("My Database")
    database.appendChild(database_txt)

    annotation_new = doc.createElement('annotation')
    source.appendChild(annotation_new)
    annotation_new_txt = doc.createTextNode("VOC2007")
    annotation_new.appendChild(annotation_new_txt)
    image = doc.createElement('image')
    source.appendChild(image)
    image_txt = doc.createTextNode("flickr")
    image.appendChild(image_txt)
    #owner
    owner = doc.createElement('owner')
    annotation.appendChild(owner)
    flickrid = doc.createElement('flickrid')
    owner.appendChild(flickrid)
    flickrid_txt = doc.createTextNode("NULL")
    flickrid.appendChild(flickrid_txt)
    ow_name = doc.createElement('name')
    owner.appendChild(ow_name)
    ow_name_txt = doc.createTextNode("idannel")
    ow_name.appendChild(ow_name_txt)
    #onee#
    #twos#
    size = doc.createElement('size')
    annotation.appendChild(size)
    width = doc.createElement('width')
    size.appendChild(width)
    width_txt = doc.createTextNode(str(w))
    width.appendChild(width_txt)
    height = doc.createElement('height')
    size.appendChild(height)
    height_txt = doc.createTextNode(str(h))
    height.appendChild(height_txt)
    depth = doc.createElement('depth')
    size.appendChild(depth)
    depth_txt = doc.createTextNode(str(d))
    depth.appendChild(depth_txt)
    #twoe#
    segmented = doc.createElement('segmented')
    annotation.appendChild(segmented)
    segmented_txt = doc.createTextNode("0")
    segmented.appendChild(segmented_txt)
    for bbox in bboxes:
        #threes#
        object_new = doc.createElement("object")
        annotation.appendChild(object_new)
        name = doc.createElement('name')
        object_new.appendChild(name)
        name_txt = doc.createTextNode(str(bbox[4]))
        name.appendChild(name_txt)
        pose = doc.createElement('pose')
        object_new.appendChild(pose)
        pose_txt = doc.createTextNode("Unspecified")
        pose.appendChild(pose_txt)
        truncated = doc.createElement('truncated')
        object_new.appendChild(truncated)
        truncated_txt = doc.createTextNode("0")
        truncated.appendChild(truncated_txt)
        difficult = doc.createElement('difficult')
        object_new.appendChild(difficult)
        difficult_txt = doc.createTextNode("0")
        difficult.appendChild(difficult_txt)
        #threes-1#
        bndbox = doc.createElement('bndbox')
        object_new.appendChild(bndbox)
        xmin = doc.createElement('xmin')
        bndbox.appendChild(xmin)
        xmin_txt = doc.createTextNode(str(bbox[0]))
        xmin.appendChild(xmin_txt)
        ymin = doc.createElement('ymin')
        bndbox.appendChild(ymin)
        ymin_txt = doc.createTextNode(str(bbox[1]))
        ymin.appendChild(ymin_txt)
        xmax = doc.createElement('xmax')
        bndbox.appendChild(xmax)
        xmax_txt = doc.createTextNode(str(bbox[2]))
        xmax.appendChild(xmax_txt)
        ymax = doc.createElement('ymax')
        bndbox.appendChild(ymax)
        ymax_txt = doc.createTextNode(str(bbox[3]))
        ymax.appendChild(ymax_txt)

    tempfile = tmp + "%s.xml" % imgname
    with codecs.open(tempfile, 'w', 'utf-8') as f:
        doc.writexml(f,
                     indent='\t',
                     newl='\n',
                     addindent='\t',
                     encoding='utf-8')
    return 187
示例#32
0
def report_app_service(request):

    try:
        tmp_id = request.POST.get("id")

        app_service_info = AppServiceInfo.objects.filter(id=tmp_id)[0]
        report_status = app_service_info.report_status

        customer_info = CustomerInfo.objects.filter(
            id=app_service_info.customer_id_id)[0]
        customerId = customer_info.customer_id
        customer_report_status = customer_info.report_status

        doc = Document()
        xml_obj = XML()
        basicInfo = xml_obj.create_child_node(doc, doc, 'basicInfo')
        if int(report_status) == 1:  # 新增未上报
            NodeInfo = xml_obj.create_child_node(doc, basicInfo, 'newInfo')
            xml_file = local_xml_path + "/basic_add_app_service_info.xml"
            xsd_file = local_xsd_path + "/basic_add_app_service_info.xsd"
        elif int(report_status) == 3:  # 更新未上报
            NodeInfo = xml_obj.create_child_node(doc, basicInfo, 'updateInfo')
            xml_file = local_xml_path + "/basic_update_app_service_info.xml"
            xsd_file = local_xsd_path + "/basic_update_app_service_info.xsd"

        # cdn许可证号
        cdnId = BusinessUnit.objects.all()[0].unit_licence
        xml_obj.add_value_to_node(doc, NodeInfo, 'cdnId', cdnId)

        customerInfo = xml_obj.create_child_node(doc, NodeInfo, 'customerInfo')

        xml_obj.add_value_to_node(doc, customerInfo, 'customerId', customerId)

        # 判断客户信息是否已经上报过,如果没有上报添加上客户信息,防止客户信息没有上报,就去上报服务信息
        if int(customer_report_status) == "1":
            xml_obj.add_value_to_node(doc, customerInfo, 'unitName', unitName)
            xml_obj.add_value_to_node(doc, customerInfo, 'unitNature',
                                      unitNature)
            xml_obj.add_value_to_node(doc, customerInfo, 'idType', idType)
            xml_obj.add_value_to_node(doc, customerInfo, 'idNumber', idNumber)
            xml_obj.add_value_to_node(doc, customerInfo, 'Add', Add)

        serviceInfo = xml_obj.create_child_node(doc, customerInfo,
                                                'serviceInfo')
        xml_obj.add_value_to_node(doc, serviceInfo, 'serviceId',
                                  app_service_info.service_id)
        for i in json.loads(app_service_info.service_content):
            xml_obj.add_value_to_node(doc, serviceInfo, 'serviceContent', i)

        # 当只有新增上报的时候,才报服务下的域名信息。如果客户的域名有变化,单独上报。
        # 更新上报只是针对服务自己的信息变化了(服务内容)。不包含其它
        if int(report_status) == 1:
            # 域名信息
            domain_info = DomainInfo.objects.filter(
                app_service_id_id=app_service_info.id)
            for d in domain_info:
                domainInfo = xml_obj.create_child_node(doc, serviceInfo,
                                                       'domainInfo')
                xml_obj.add_value_to_node(doc, domainInfo, 'domainId',
                                          d.domain_id)
                xml_obj.add_value_to_node(doc, domainInfo, 'domain', d.domain)
                xml_obj.add_value_to_node(doc, domainInfo, 'regId',
                                          d.record_num)

        timeStamp = time.strftime('%Y-%m-%d %H:%M:%S',
                                  time.localtime(time.time()))
        xml_obj.add_value_to_node(doc, basicInfo, 'timeStamp', timeStamp)

        print("xml_file:", xml_file)
        # 写入xml文件
        with open(xml_file, "w") as f:
            doc.writexml(f, addindent=' ' * 4, newl='\n', encoding="UTF-8")

        # 利用xsd校验xml文件
        ret = check_xml_file(xsd_file, xml_file)
        if ret != 1:
            print("%s check Fail!!!" % xml_file)
            return return_msg(1)

        # FTP上报xml文件到管局
        return_code = ftp_report_business_unit(report_status, xml_file, tmp_id)

        # 错误处理
        return return_msg(return_code)
    except:
        print(traceback.print_exc())
        return return_msg(4)
示例#33
0
    def yolo2voc(self):
        # 创建一个保存xml标签文件的文件夹
        if not os.path.exists(self.xmls_path):
            os.mkdir(self.xmls_path)

        # # 读取每张图片,获取图片的尺寸信息(shape)
        # imgs = os.listdir(self.imgs_path)
        # for img_name in imgs:
        #     img = cv2.imread(os.path.join(self.imgs_path, img_name))
        #     height, width, depth = img.shape
        #     # print(height, width, depth)   # h 就是多少行(对应图片的高度), w就是多少列(对应图片的宽度)
        #
        # # 读取每一个txt标签文件,取出每个目标的标注信息
        # all_names = set()
        # txts = os.listdir(self.txts_path)
        # # 使用列表生成式过滤出只有后缀名为txt的标签文件
        # txts = [txt for txt in txts if txt.split('.')[-1] == 'txt']
        # print(len(txts), txts)
        # # 11 ['0002030.txt', '0002031.txt', ... '0002039.txt', '0002040.txt']
        # for txt_name in txts:
        #     txt_file = os.path.join(self.txts_path, txt_name)
        #     with open(txt_file, 'r') as f:
        #         objects = f.readlines()
        #         for object in objects:
        #             object = object.strip().split(' ')
        #             print(object)  # ['2', '0.506667', '0.553333', '0.490667', '0.658667']

        # 把上面的两个循环改写成为一个循环:
        imgs = os.listdir(self.imgs_path)
        imgs = [img for img in imgs if img.endswith('jpg')]
        txts = os.listdir(self.txts_path)
        txts = [
            txt for txt in txts
            if not txt.split('.')[0] == "classes" and not txt.endswith('jpg')
        ]  # 过滤掉classes.txt文件
        print(txts)
        # 注意,这里保持图片的数量和标签txt文件数量相等,且要保证名字是一一对应的   (后面改进,通过判断txt文件名是否在imgs中即可)
        if len(imgs) == len(txts):  # 注意:./Annotation_txt 不要把classes.txt文件放进去
            map_imgs_txts = [(img, txt) for img, txt in zip(imgs, txts)]
            txts = [txt for txt in txts if txt.split('.')[-1] == 'txt']
            print(len(txts), txts)
            for img_name, txt_name in map_imgs_txts:
                # 读取图片的尺度信息
                print("读取图片:", img_name)
                # img = cv2.imread(os.path.join(self.imgs_path, img_name))
                height_img, width_img, depth_img = 1080, 1920, 3  ###img.shape
                print(height_img, width_img,
                      depth_img)  # h 就是多少行(对应图片的高度), w就是多少列(对应图片的宽度)

                # 获取标注文件txt中的标注信息
                all_objects = []
                txt_file = os.path.join(self.txts_path, txt_name)
                with open(txt_file, 'r') as f:
                    objects = f.readlines()
                    for object in objects:
                        object = object.strip().split(' ')
                        all_objects.append(object)
                        print(
                            object
                        )  # ['2', '0.506667', '0.553333', '0.490667', '0.658667']

                # 创建xml标签文件中的标签
                xmlBuilder = Document()
                # 创建annotation标签,也是根标签
                annotation = xmlBuilder.createElement("annotation")

                # 给标签annotation添加一个子标签
                xmlBuilder.appendChild(annotation)

                # 创建子标签folder
                folder = xmlBuilder.createElement("folder")
                # 给子标签folder中存入内容,folder标签中的内容是存放图片的文件夹,例如:JPEGImages
                folderContent = xmlBuilder.createTextNode(
                    self.imgs_path.split('/')[-1])  # 标签内存
                folder.appendChild(folderContent)  # 把内容存入标签
                annotation.appendChild(
                    folder)  # 把存好内容的folder标签放到 annotation根标签下

                # 创建子标签filename
                filename = xmlBuilder.createElement("filename")
                # 给子标签filename中存入内容,filename标签中的内容是图片的名字,例如:000250.jpg
                filenameContent = xmlBuilder.createTextNode(
                    txt_name.split('.')[0] + '.jpg')  # 标签内容
                filename.appendChild(filenameContent)
                annotation.appendChild(filename)

                # 把图片的shape存入xml标签中
                size = xmlBuilder.createElement("size")
                # 给size标签创建子标签width
                width = xmlBuilder.createElement("width")  # size子标签width
                widthContent = xmlBuilder.createTextNode(str(width_img))
                width.appendChild(widthContent)
                size.appendChild(width)  # 把width添加为size的子标签
                # 给size标签创建子标签height
                height = xmlBuilder.createElement("height")  # size子标签height
                heightContent = xmlBuilder.createTextNode(
                    str(height_img))  # xml标签中存入的内容都是字符串
                height.appendChild(heightContent)
                size.appendChild(height)  # 把width添加为size的子标签
                # 给size标签创建子标签depth
                depth = xmlBuilder.createElement("depth")  # size子标签width
                depthContent = xmlBuilder.createTextNode(str(depth_img))
                depth.appendChild(depthContent)
                size.appendChild(depth)  # 把width添加为size的子标签
                annotation.appendChild(size)  # 把size添加为annotation的子标签

                # 每一个object中存储的都是['2', '0.506667', '0.553333', '0.490667', '0.658667']一个标注目标
                for object_info in all_objects:
                    # 开始创建标注目标的label信息的标签
                    object = xmlBuilder.createElement("object")  # 创建object标签
                    # 创建label类别标签
                    # 创建name标签
                    imgName = xmlBuilder.createElement("name")  # 创建name标签
                    imgNameContent = xmlBuilder.createTextNode(
                        self.classes[int(object_info[0])])
                    imgName.appendChild(imgNameContent)
                    object.appendChild(imgName)  # 把name添加为object的子标签

                    # 创建pose标签
                    pose = xmlBuilder.createElement("pose")
                    poseContent = xmlBuilder.createTextNode("Unspecified")
                    pose.appendChild(poseContent)
                    object.appendChild(pose)  # 把pose添加为object的标签

                    # 创建truncated标签
                    truncated = xmlBuilder.createElement("truncated")
                    truncatedContent = xmlBuilder.createTextNode("0")
                    truncated.appendChild(truncatedContent)
                    object.appendChild(truncated)

                    # 创建difficult标签
                    difficult = xmlBuilder.createElement("difficult")
                    difficultContent = xmlBuilder.createTextNode("0")
                    difficult.appendChild(difficultContent)
                    object.appendChild(difficult)

                    # 先转换一下坐标
                    # (objx_center, objy_center, obj_width, obj_height)->(xmin,ymin, xmax,ymax)
                    x_center = float(object_info[1]) * width_img + 1
                    y_center = float(object_info[2]) * height_img + 1
                    xminVal = int(x_center - 0.5 * float(object_info[3]) *
                                  width_img)  # object_info列表中的元素都是字符串类型
                    yminVal = int(y_center -
                                  0.5 * float(object_info[4]) * height_img)
                    xmaxVal = int(x_center +
                                  0.5 * float(object_info[3]) * width_img)
                    ymaxVal = int(y_center +
                                  0.5 * float(object_info[4]) * height_img)

                    # 创建bndbox标签(三级标签)
                    bndbox = xmlBuilder.createElement("bndbox")
                    # 在bndbox标签下再创建四个子标签(xmin,ymin, xmax,ymax) 即标注物体的坐标和宽高信息
                    # 在voc格式中,标注信息:左上角坐标(xmin, ymin) (xmax, ymax)右下角坐标
                    # 1、创建xmin标签
                    xmin = xmlBuilder.createElement("xmin")  # 创建xmin标签(四级标签)
                    xminContent = xmlBuilder.createTextNode(str(xminVal))
                    xmin.appendChild(xminContent)
                    bndbox.appendChild(xmin)
                    # 2、创建ymin标签
                    ymin = xmlBuilder.createElement("ymin")  # 创建ymin标签(四级标签)
                    yminContent = xmlBuilder.createTextNode(str(yminVal))
                    ymin.appendChild(yminContent)
                    bndbox.appendChild(ymin)
                    # 3、创建xmax标签
                    xmax = xmlBuilder.createElement("xmax")  # 创建xmax标签(四级标签)
                    xmaxContent = xmlBuilder.createTextNode(str(xmaxVal))
                    xmax.appendChild(xmaxContent)
                    bndbox.appendChild(xmax)
                    # 4、创建ymax标签
                    ymax = xmlBuilder.createElement("ymax")  # 创建ymax标签(四级标签)
                    ymaxContent = xmlBuilder.createTextNode(str(ymaxVal))
                    ymax.appendChild(ymaxContent)
                    bndbox.appendChild(ymax)

                    object.appendChild(bndbox)
                    annotation.appendChild(object)  # 把object添加为annotation的子标签
                f = open(
                    os.path.join(self.xmls_path,
                                 txt_name.split('.')[0] + '.xml'), 'w')
                xmlBuilder.writexml(f,
                                    indent='\t',
                                    newl='\n',
                                    addindent='\t',
                                    encoding='utf-8')
                f.close()
示例#34
0
class VulneraNetXMLReportGenerator(ReportGenerator):
    """
    This class generates a report with the method printToFile(fileName) which contains
    the information of all the vulnerabilities notified to this object through the
    method add_vulnerability(category,level,url,parameter,info).
    The format of the file is XML and it has the following structure:
    <report type="security">
        <generatedBy id="Wapiti 3.0.2"/>
            <bugTypeList>
                <bugType name="SQL Injection">
                    <bugList/>

    <report>
        <vulnerabilityTypeList>
            <vulnerabilityType name="SQL Injection">
                <vulnerabilityList>
                    <vulnerability level="3">
                        <url>http://www.a.com</url>
                        <parameters>id=23</parameters>
                        <info>SQL Injection</info>
                    </vulnerability>
                </vulnerabilityList>
            </vulnerabilityType>
        </vulnerabilityTypeList>
    </report>
    """
    def __init__(self):
        super().__init__()
        self._timestamp = datetime.datetime.now()
        self._xml_doc = Document()
        self._vulnerability_type_list = None

    def set_report_info(self, target, scope, date, version):
        super().set_report_info(target, scope, date, version)
        report = self._xml_doc.createElement("Report")

        report.setAttribute("generatedBy", version)
        report.setAttribute("generationDate", self._timestamp.isoformat())
        self._vulnerability_type_list = self._xml_doc.createElement(
            "VulnerabilityTypeList")
        report.appendChild(self._vulnerability_type_list)

        self._xml_doc.appendChild(report)

    def _add_to_vulnerability_type_list(self, vulnerability_type):
        self._vulnerability_type_list.appendChild(vulnerability_type)

    def add_vulnerability_type(self,
                               name,
                               description="",
                               solution="",
                               references=None):
        """
        This method adds a vulnerability type, it can be invoked to include in the
        report the type.
        The types are not stored previously, they are added when the method
        add_vulnerability(category,level,url,parameter,info) is invoked
        and if there is no vulnerability of a type, this type will not be presented
        in the report
        """
        vulnerability_type = self._xml_doc.createElement("VulnerabilityType")
        vulnerability_type.appendChild(
            self._xml_doc.createElement("VulnerabilityList"))

        vuln_title_node = self._xml_doc.createElement("Title")
        vuln_title_node.appendChild(self._xml_doc.createTextNode(name))
        vulnerability_type.appendChild(vuln_title_node)

        self._add_to_vulnerability_type_list(vulnerability_type)
        if description != "":
            description_node = self._xml_doc.createElement("Description")
            description_node.appendChild(
                self._xml_doc.createCDATASection(description))
            vulnerability_type.appendChild(description_node)
        if solution != "":
            solution_node = self._xml_doc.createElement("Solution")
            solution_node.appendChild(
                self._xml_doc.createCDATASection(solution))
            vulnerability_type.appendChild(solution_node)
        if references != "":
            references_node = self._xml_doc.createElement("References")
            for ref in references:
                reference_node = self._xml_doc.createElement("Reference")
                name_node = self._xml_doc.createElement("name")
                url_node = self._xml_doc.createElement("url")
                name_node.appendChild(self._xml_doc.createTextNode(ref))
                url_node.appendChild(
                    self._xml_doc.createTextNode(references[ref]))
                reference_node.appendChild(name_node)
                reference_node.appendChild(url_node)
                references_node.appendChild(reference_node)
            vulnerability_type.appendChild(references_node)
        return vulnerability_type

    def _add_to_vulnerability_list(self, category, vulnerability):
        vulnerability_type = None
        for node in self._vulnerability_type_list.childNodes:
            title_node = node.getElementsByTagName("Title")
            if (title_node.length >= 1 and title_node[0].childNodes.length == 1
                    and title_node[0].childNodes[0].wholeText == category):
                vulnerability_type = node
                break
        if vulnerability_type is None:
            vulnerability_type = self.add_vulnerability_type(category)
        vulnerability_type.childNodes[0].appendChild(vulnerability)

    def add_vulnerability(self,
                          category=None,
                          level=0,
                          request=None,
                          parameter="",
                          info=""):
        """
        Store the information about the vulnerability to be printed later.
        The method printToFile(fileName) can be used to save in a file the
        vulnerabilities notified through the current method.
        """

        peer = None

        vulnerability = self._xml_doc.createElement("Vulnerability")

        if level == 1:
            st_level = "Low"
        elif level == 2:
            st_level = "Moderate"
        else:
            st_level = "Important"

        level_node = self._xml_doc.createElement("Severity")
        level_node.appendChild(self._xml_doc.createTextNode(st_level))
        vulnerability.appendChild(level_node)

        ts_node = self._xml_doc.createElement("DetectionDate")
        # tsNode.appendChild(self.__xmlDoc.createTextNode(ts.isoformat()))
        vulnerability.appendChild(ts_node)

        ##
        url_detail_node = self._xml_doc.createElement("URLDetail")
        vulnerability.appendChild(url_detail_node)

        url_node = self._xml_doc.createElement("URL")
        url_node.appendChild(self._xml_doc.createTextNode(request.url))
        url_detail_node.appendChild(url_node)

        if peer is not None:
            peer_node = self._xml_doc.createElement("Peer")
            if is_peer_tuple(peer):
                addr_node = self._xml_doc.createElement("Addr")
                addr_node.appendChild(self._xml_doc.createTextNode(peer[0]))
                peer_node.appendChild(addr_node)

                port_node = self._xml_doc.createElement("Port")
                port_node.appendChild(
                    self._xml_doc.createTextNode(str(peer[1])))
                peer_node.appendChild(port_node)
            else:
                addr_node = self._xml_doc.createElement("Addr")
                addr_node.appendChild(self._xml_doc.createTextNode(str(peer)))
                peer_node.appendChild(addr_node)
            url_detail_node.appendChild(peer_node)

        parameter_node = self._xml_doc.createElement("Parameter")
        parameter_node.appendChild(self._xml_doc.createTextNode(parameter))
        url_detail_node.appendChild(parameter_node)

        ##

        info_node = self._xml_doc.createElement("Info")
        info = info.replace("\n", "<br />")
        info_node.appendChild(self._xml_doc.createTextNode(info))
        url_detail_node.appendChild(info_node)

        self._add_to_vulnerability_list(category, vulnerability)

    def generate_report(self, output_path):
        """
        Create a xml file with a report of the vulnerabilities which have been logged with
        the method add_vulnerability(category,level,url,parameter,info)
        """
        with open(output_path, "w") as fd:
            self._xml_doc.writexml(fd, addindent="   ", newl="\n")
示例#35
0
class XMLReportGenerator(ReportGenerator):
    """
    This class generates a report with the method printToFile(fileName) which contains
    the information of all the vulnerabilities notified to this object through the
    method add_vulnerability(vulnerabilityTypeName,level,url,parameter,info).
    The format of the file is XML and it has the following structure:
    <report type="security">
        <generatedBy id="Wapiti 3.0.4"/>
        <vulnerabilityTypeList>
            <vulnerabilityType name="SQL Injection">

        <vulnerabilityTypeList>
            <vulnerabilityType name="SQL Injection">
                <vulnerabilityList>
                    <vulnerability level="3">
                        <url>http://www.a.com</url>
                        <parameters>id=23</parameters>
                        <info>SQL Injection</info>
                    </vulnerability>
                </vulnerabilityList>
            </vulnerabilityType>
        </vulnerabilityTypeList>
    </report>
    """

    def __init__(self):
        super().__init__()
        self._xml_doc = Document()
        self._flaw_types = {}

        self._vulns = {}
        self._anomalies = {}
        self._additionals = {}

    # Vulnerabilities
    def add_vulnerability_type(self, name, description="", solution="", references=None):
        if name not in self._flaw_types:
            self._flaw_types[name] = {
                "desc": description,
                "sol": solution,
                "ref": references
            }
        if name not in self._vulns:
            self._vulns[name] = []

    def add_vulnerability(self, category=None, level=0, request=None, parameter="", info=""):
        """
        Store the information about the vulnerability to be printed later.
        The method printToFile(fileName) can be used to save in a file the
        vulnerabilities notified through the current method.
        """

        vuln_dict = {
            "method": request.method,
            "path": request.file_path,
            "info": info,
            "level": level,
            "parameter": parameter,
            "http_request": request.http_repr(left_margin=""),
            "curl_command": request.curl_repr,
        }

        if category not in self._vulns:
            self._vulns[category] = []
        self._vulns[category].append(vuln_dict)

    # Anomalies
    def add_anomaly_type(self, name, description="", solution="", references=None):
        if name not in self._flaw_types:
            self._flaw_types[name] = {
                "desc": description,
                "sol": solution,
                "ref": references
            }
        if name not in self._anomalies:
            self._anomalies[name] = []

    def add_anomaly(self, category=None, level=0, request=None, parameter="", info=""):
        """
        Store the information about the vulnerability to be printed later.
        The method printToFile(fileName) can be used to save in a file the
        vulnerabilities notified through the current method.
        """

        anom_dict = {
            "method": request.method,
            "path": request.file_path,
            "info": info,
            "level": level,
            "parameter": parameter,
            "http_request": request.http_repr(left_margin=""),
            "curl_command": request.curl_repr,
        }
        if category not in self._anomalies:
            self._anomalies[category] = []
        self._anomalies[category].append(anom_dict)

    # Additionals
    def add_additional_type(self, name, description="", solution="", references=None):
        """
        This method adds an addtional type, it can be invoked to include in the
        report the type.
        """
        if name not in self._flaw_types:
            self._flaw_types[name] = {
                "desc": description,
                "sol": solution,
                "ref": references
            }
        if name not in self._additionals:
            self._additionals[name] = []

    def add_additional(self, category=None, level=0, request=None, parameter="", info=""):
        """
        Store the information about the addtional to be printed later.
        The method printToFile(fileName) can be used to save in a file the
        addtionals notified through the current method.
        """

        addition_dict = {
            "method": request.method,
            "path": request.file_path,
            "info": info,
            "level": level,
            "parameter": parameter,
            "http_request": request.http_repr(left_margin=""),
            "curl_command": request.curl_repr,
        }
        if category not in self._additionals:
            self._additionals[category] = []
        self._additionals[category].append(addition_dict)

    def generate_report(self, output_path):
        """
        Create a xml file with a report of the vulnerabilities which have been logged with
        the method add_vulnerability(vulnerabilityTypeName,level,url,parameter,info)
        """

        report = self._xml_doc.createElement("report")
        report.setAttribute("type", "security")
        self._xml_doc.appendChild(report)

        # Add report infos
        report_infos = self._xml_doc.createElement("report_infos")
        generator_name = self._xml_doc.createElement("info")
        generator_name.setAttribute("name", "generatorName")
        generator_name.appendChild(self._xml_doc.createTextNode("wapiti"))
        report_infos.appendChild(generator_name)

        generator_version = self._xml_doc.createElement("info")
        generator_version.setAttribute("name", "generatorVersion")
        generator_version.appendChild(self._xml_doc.createTextNode(self._infos["version"]))
        report_infos.appendChild(generator_version)

        scope = self._xml_doc.createElement("info")
        scope.setAttribute("name", "scope")
        scope.appendChild(self._xml_doc.createTextNode(self._infos["scope"]))
        report_infos.appendChild(scope)

        date_of_scan = self._xml_doc.createElement("info")
        date_of_scan.setAttribute("name", "dateOfScan")
        date_of_scan.appendChild(self._xml_doc.createTextNode(self._infos["date"]))
        report_infos.appendChild(date_of_scan)

        target = self._xml_doc.createElement("info")
        target.setAttribute("name", "target")
        target.appendChild(self._xml_doc.createTextNode(self._infos["target"]))
        report_infos.appendChild(target)

        report.appendChild(report_infos)

        vulnerabilities = self._xml_doc.createElement("vulnerabilities")
        anomalies = self._xml_doc.createElement("anomalies")
        additionals = self._xml_doc.createElement("additionals")

        # Loop on each flaw classification
        for flaw_type in self._flaw_types:
            container = None
            classification = ""
            flaw_dict = {}
            if flaw_type in self._vulns:
                container = vulnerabilities
                classification = "vulnerability"
                flaw_dict = self._vulns
            elif flaw_type in self._anomalies:
                container = anomalies
                classification = "anomaly"
                flaw_dict = self._anomalies
            elif flaw_type in self._additionals:
                container = additionals
                classification = "additional"
                flaw_dict = self._additionals

            # Child nodes with a description of the flaw type
            flaw_type_node = self._xml_doc.createElement(classification)
            flaw_type_node.setAttribute("name", flaw_type)
            flaw_type_desc = self._xml_doc.createElement("description")
            flaw_type_desc.appendChild(self._xml_doc.createCDATASection(self._flaw_types[flaw_type]["desc"]))
            flaw_type_node.appendChild(flaw_type_desc)
            flaw_type_solution = self._xml_doc.createElement("solution")
            flaw_type_solution.appendChild(self._xml_doc.createCDATASection(self._flaw_types[flaw_type]["sol"]))
            flaw_type_node.appendChild(flaw_type_solution)

            flaw_type_references = self._xml_doc.createElement("references")
            for ref in self._flaw_types[flaw_type]["ref"]:
                reference_node = self._xml_doc.createElement("reference")
                title_node = self._xml_doc.createElement("title")
                url_node = self._xml_doc.createElement("url")
                title_node.appendChild(self._xml_doc.createTextNode(ref))
                url = self._flaw_types[flaw_type]["ref"][ref]
                url_node.appendChild(self._xml_doc.createTextNode(url))
                reference_node.appendChild(title_node)
                reference_node.appendChild(url_node)
                flaw_type_references.appendChild(reference_node)
            flaw_type_node.appendChild(flaw_type_references)

            # And child nodes with each flaw of the current type
            entries_node = self._xml_doc.createElement("entries")
            for flaw in flaw_dict[flaw_type]:
                entry_node = self._xml_doc.createElement("entry")
                method_node = self._xml_doc.createElement("method")
                method_node.appendChild(self._xml_doc.createTextNode(flaw["method"]))
                entry_node.appendChild(method_node)
                path_node = self._xml_doc.createElement("path")
                path_node.appendChild(self._xml_doc.createTextNode(flaw["path"]))
                entry_node.appendChild(path_node)
                level_node = self._xml_doc.createElement("level")
                level_node.appendChild(self._xml_doc.createTextNode(str(flaw["level"])))
                entry_node.appendChild(level_node)
                parameter_node = self._xml_doc.createElement("parameter")
                parameter_node.appendChild(self._xml_doc.createTextNode(flaw["parameter"]))
                entry_node.appendChild(parameter_node)
                info_node = self._xml_doc.createElement("info")
                info_node.appendChild(self._xml_doc.createTextNode(flaw["info"]))
                entry_node.appendChild(info_node)
                http_request_node = self._xml_doc.createElement("http_request")
                http_request_node.appendChild(self._xml_doc.createCDATASection(flaw["http_request"]))
                entry_node.appendChild(http_request_node)
                curl_command_node = self._xml_doc.createElement("curl_command")
                curl_command_node.appendChild(self._xml_doc.createCDATASection(flaw["curl_command"]))
                entry_node.appendChild(curl_command_node)
                entries_node.appendChild(entry_node)
            flaw_type_node.appendChild(entries_node)
            container.appendChild(flaw_type_node)
        report.appendChild(vulnerabilities)
        report.appendChild(anomalies)
        report.appendChild(additionals)

        with open(output_path, "w", errors="ignore") as xml_report_file:
            self._xml_doc.writexml(xml_report_file, addindent="   ", newl="\n")
示例#36
0
def CopyBinaries(out_dir, out_project_dir, src_package, shared, use_lzma):
    # Copy jar files to libs.
    libs_dir = os.path.join(out_project_dir, 'libs')
    if not os.path.exists(libs_dir):
        os.mkdir(libs_dir)

    if shared:
        libs_to_copy = ['xwalk_core_library_java_app_part.jar']
    elif src_package:
        libs_to_copy = [
            'jsr_305_javalib.jar',
        ]
    else:
        libs_to_copy = [
            'xwalk_core_library_java.jar',
        ]

    for lib in libs_to_copy:
        source_file = os.path.join(out_dir, 'lib.java', lib)
        target_file = os.path.join(libs_dir, lib)
        shutil.copyfile(source_file, target_file)

    if shared:
        return

    print 'Copying binaries...'
    # Copy assets.
    res_raw_dir = os.path.join(out_project_dir, 'res', 'raw')
    res_value_dir = os.path.join(out_project_dir, 'res', 'values')
    if not os.path.exists(res_raw_dir):
        os.mkdir(res_raw_dir)
    if not os.path.exists(res_value_dir):
        os.mkdir(res_value_dir)

    paks_to_copy = [
        'icudtl.dat',
        # Please refer to XWALK-3516, disable v8 use external startup data,
        # reopen it if needed later.
        # 'natives_blob.bin',
        # 'snapshot_blob.bin',
        'xwalk.pak',
    ]

    pak_list_xml = Document()
    resources_node = pak_list_xml.createElement('resources')
    string_array_node = pak_list_xml.createElement('string-array')
    string_array_node.setAttribute('name', 'xwalk_resources_list')
    pak_list_xml.appendChild(resources_node)
    resources_node.appendChild(string_array_node)
    for pak in paks_to_copy:
        source_file = os.path.join(out_dir, pak)
        target_file = os.path.join(res_raw_dir, pak)
        shutil.copyfile(source_file, target_file)
        item_node = pak_list_xml.createElement('item')
        item_node.appendChild(pak_list_xml.createTextNode(pak))
        string_array_node.appendChild(item_node)
    pak_list_file = open(
        os.path.join(res_value_dir, 'xwalk_resources_list.xml'), 'w')
    pak_list_xml.writexml(pak_list_file, newl='\n', encoding='utf-8')
    pak_list_file.close()

    # Copy native libraries.
    source_dir = os.path.join(out_dir, XWALK_CORE_SHELL_APK, 'libs')
    distutils.dir_util.copy_tree(source_dir, libs_dir)

    # NOTE: Gradle doesn't accept '-', use '_' instead.
    if use_lzma:
        for arch in ['x86', 'armeabi_v7a']:
            arch_dir = os.path.join(libs_dir, arch)
            lib = os.path.join(arch_dir, 'libxwalkcore.so.lzma')
            if os.path.isfile(lib):
                shutil.move(
                    lib, os.path.join(res_raw_dir, "libxwalkcore.so." + arch))
    else:
        shutil.rmtree(os.path.join(res_raw_dir, "libxwalkcore.so.*"),
                      ignore_errors=True)
示例#37
0
class T(basecanvas.T):
    def __init__(self, fname):
        basecanvas.T.__init__(self)
        self.__out_fname = fname
        self.__xmin, self.__xmax, self.__ymin, self.__ymax = 0, 0, 0, 0
        self.__doc = Document()
        self.__doc.appendChild(
            self.__doc.createComment('Created by PyChart ' + version.version +
                                     ' ' + version.copyright))
        self.__svg = self.__doc.createElement('svg')  # the svg doc
        self.__doc.appendChild(self.__svg)
        self.__defs = self.__doc.createElement('defs')  # for clip paths
        self.__svg.appendChild(self.__defs)
        self.__currElt = self.__svg
        self.gsave()  # create top-level group for dflt styles
        self._updateStyle(
            font_family=theme.default_font_family,
            font_size=theme.default_font_size,
            font_style='normal',
            font_weight='normal',
            font_stretch='normal',
            fill='none',
            stroke='rgb(0,0,0)',  #SVG dflt none, PS dflt blk
            stroke_width=theme.default_line_width,
            stroke_linejoin='miter',
            stroke_linecap='butt',
            stroke_dasharray='none')

    def _updateStyle(self, **addstyledict):
        elt = _protectCurrentChildren(self.__currElt)

        # fetch the current styles for this node
        mystyledict = _parseStyleStr(elt.getAttribute('style'))

        # concat all parent style strings to get dflt styles for this node
        parent, s = elt.parentNode, ''
        while parent.nodeType != Document.nodeType:
            # prepend parent str so later keys will override earlier ones
            s = parent.getAttribute('style') + s
            parent = parent.parentNode
        dfltstyledict = _parseStyleStr(s)

        # Do some pre-processing on the caller-supplied add'l styles
        # Convert '_' to '-' so caller can specify style tags as python
        # variable names, eg. stroke_width => stroke-width.
        # Also convert all RHS values to strs
        for key in addstyledict.keys():
            k = re.sub('_', '-', key)
            addstyledict[k] = str(addstyledict[key])  # all vals => strs
            if (k != key): del addstyledict[key]

        for k in addstyledict.keys():
            if (mystyledict.has_key(k) or  # need to overwrite it
                (not dfltstyledict.has_key(k)) or  # need to set it
                    dfltstyledict[k] !=
                    addstyledict[k]):  # need to override it
                mystyledict[k] = addstyledict[k]

        s = _makeStyleStr(mystyledict)
        if s: elt.setAttribute('style', s)

        self.__currElt = elt

    ####################################################################
    # methods below define the pychart backend device API

    # First are a set of methods to start, construct and finalize a path

    def newpath(self):  # Start a new path
        if (self.__currElt.nodeName != 'g'):
            raise OverflowError, "No containing group for newpath"
        # Just insert a new 'path' element into the document
        p = self.__doc.createElement('path')
        self.__currElt.appendChild(p)
        self.__currElt = p

    # This set of methods add data to an existing path element,
    # simply add to the 'd' (data) attribute of the path elt

    def moveto(self, x, y):  #
        if (self.__currElt.nodeName != 'path'):
            raise OverflowError, "No path for moveto"
        d = ' '.join([self.__currElt.getAttribute('d'), 'M', ` x `, ` -y
                      `]).strip()
        self.__currElt.setAttribute('d', d)

    def lineto(self, x, y):
        if (self.__currElt.nodeName != 'path'):
            raise OverflowError, "No path for lineto"
        d = ' '.join([self.__currElt.getAttribute('d'), 'L', ` x `, ` -y
                      `]).strip()
        self.__currElt.setAttribute('d', d)

    def path_arc(self, x, y, radius, ratio, start_angle, end_angle):
        # mimic PS 'arc' given radius, yr/xr (=eccentricity), start and
        # end angles.  PS arc draws from CP (if exists) to arc start,
        # then draws arc in counterclockwise dir from start to end
        # SVG provides an arc command that draws a segment of an
        # ellipse (but not a full circle) given these args:
        # A xr yr rotate majorArcFlag counterclockwiseFlag xe ye
        # We don't use rotate(=0) and flipped axes => all arcs are clockwise

        if (self.__currElt.nodeName != 'path'):
            raise OverflowError, "No path for path_arc"

        self.comment('x=%g, y=%g, r=%g, :=%g, %g-%g' %
                     (x, y, radius, ratio, start_angle, end_angle))

        xs = x + radius * math.cos(2 * math.pi / 360. * start_angle)
        ys = y + ratio * radius * math.sin(2 * math.pi / 360. * start_angle)
        xe = x + radius * math.cos(2 * math.pi / 360. * end_angle)
        ye = y + ratio * radius * math.sin(2 * math.pi / 360. * end_angle)
        if (end_angle < start_angle):  # make end bigger than start
            while end_angle <= start_angle:  # '<=' so 360->0 becomes 360->720
                end_angle += 360
        full_circ = (end_angle - start_angle >= 360)  # draw a full circle?

        d = self.__currElt.getAttribute('d')
        d += ' %s %g %g' % (d and 'L' or 'M', xs, -ys
                            )  # draw from CP, if exists
        if (radius > 0):  # skip, eg. 0-radius 'rounded' corners which blowup
            if (full_circ):
                # If we're drawing a full circle, move to the end coord
                # and draw half a circle to the reflected xe,ye
                d += ' M %g %g A %g %g 0 1 0 %g %g' % (
                    xe, -ye, radius, radius * ratio, 2 * x - xe, -(2 * y - ye))
            # Draw arc from the CP (either reflected xe,ye for full circle else
            # xs,ys) to the end coord - note with full_circ the
            # 'bigArcFlag' value is moot, with exactly 180deg left to draw
            d += ' A %g %g 0 %d 0 %g %g' % (
                radius, radius * ratio, end_angle - start_angle > 180, xe, -ye)
        self.__currElt.setAttribute('d', d.strip())

    def curveto(self, x1, y1, x2, y2, x3, y3):
        # Equivalent of PostScript's x1 y1 x2 y2 x3 y3 curveto which
        # draws a cubic bezier curve from curr pt to x3,y3 with ctrl points
        # x1,y1, and x2,y2
        # In SVG this is just d='[M x0 y0] C x1 y1 x2 y2 x3 y3'
        #! I can't find an example of this being used to test it
        if (self.__currElt.nodeNode != 'path'):
            raise OverflowError, "No path for curveto"
        d = ' '.join([
            self.__currElt.getAttribute('d'),
            'C',
            ` x1 `,
            ` -y1 `,
            ` x2 `,
            ` -y2 `,
            ` x3 `,
            ` -y3 `,
        ]).strip()
        self.__currElt.setAttribute('d', d)

    def closepath(self):  # close back to start of path
        if (self.__currElt.nodeName != 'path'):
            raise OverflowError, "No path for closepath"
        d = ' '.join([self.__currElt.getAttribute('d'), 'Z']).strip()
        self.__currElt.setAttribute('d', d)

    # Next we have three methods for finalizing a path element,
    # either fill it, clip to it, or draw it (stroke)
    # canvas.polygon() can generate fill/clip cmds with
    # no corresponding path so just ignore them
    def stroke(self):
        if (self.__currElt.nodeName != 'path'):
            self.comment('No path - ignoring stroke')
            return
        self._updateStyle(fill='none')
        self.__currElt = self.__currElt.parentNode

    def fill(self):
        if (self.__currElt.nodeName != 'path'):
            self.comment('No path - ignoring fill')
            return
        self._updateStyle(stroke='none')
        self.__currElt = self.__currElt.parentNode

    def clip_sub(self):
        if (self.__currElt.nodeName != 'path'):
            self.comment('No path - ignoring clip')
            return

        # remove the current path from the tree ...
        p = self.__currElt
        self.__currElt = p.parentNode
        self.__currElt.removeChild(p)

        # ... add it to a clipPath elt in the defs section
        clip = self.__doc.createElement('clipPath')
        clipid = 'clip' + ` len(self.__defs.childNodes) `
        clip.setAttribute('id', clipid)
        clip.appendChild(p)
        self.__defs.appendChild(clip)

        # ... update the local style to point to it
        self._updateStyle(clip_path='url(#%s)' % clipid)

    # The text_xxx routines specify the start/end and contents of text
    def text_begin(self):
        if (self.__currElt.nodeName != 'g'):
            raise ValueError, "No group for text block"
        t = self.__doc.createElement('text')
        self.__currElt.appendChild(t)
        self.__currElt = t

    def text_moveto(self, x, y, angle):
        if (self.__currElt.nodeName != 'text'):
            raise ValueError, "No text for moveto"
        self.__currElt.setAttribute('x', ` x `)
        self.__currElt.setAttribute('y', ` -y `)
        if (angle):
            self.__currElt.setAttribute('transform',
                                        'rotate(%g,%g,%g)' % (-angle, x, -y))

    def text_show(self, font_name, size, color, str):
        if (self.__currElt.nodeName != 'text'):
            raise ValueError, "No text for show"

        # PyChart constructs a postscript font name, for example:
        #
        # Helvetica Helvetica-Bold Helvetica-Oblique Helvetica-BoldOblique
        # Helvetica-Narrow Times-Roman Times-Italic
        # Symbol Palatino-Roman Bookman-Demi Courier AvantGarde-Book
        #
        # We need to deconstruct this to get the font-family (the
        # piece before the '-'), and other characteristics.
        # Note that 'Courier' seems to correspond to SVGs 'CourierNew'
        # and that the SVG Symbol font is Unicode where the ascii text
        # 'Symbol' doesn't create greek characters like 'Sigma ...' -
        # should really pass a unicode string, or provide translation
        #
        # SVG defines:
        # font-style = normal (aka roman) | italic | oblique
        # font-weight = normal | bold (aka demi?)
        # font-stretch = normal | wider | narrower | ultra-condensed |
        #	extra-condensed | condensed | semi-condensed |
        #	semi-expanded | expanded | extra-expanded | ultra-expanded
        # ('narrow' seems to correspond to 'condensed')

        m = re.match(r'([^-]*)(-.*)?', font_name)
        font_name, modifiers = m.groups()
        if font_name == 'Courier': font_name = 'CourierNew'
        font_style = font_weight = font_stretch = 'normal'
        if modifiers:
            if re.search('Italic', modifiers): font_style = 'italic'
            elif re.search('Oblique', modifiers): font_style = 'oblique'
            if re.search('Bold|Demi', modifiers): font_weight = 'bold'
            if re.search('Narrow', modifiers): font_stretch = 'condensed'
        #! translate ascii symbol font chars -> unicode (see www.unicode.org)
        #! http://www.unicode.org/Public/MAPPINGS/VENDORS/ADOBE/symbol.txt
        #! but xml Text element writes unicode chars as '?' to XML file...
        str = re.sub(r'\\([()])', r'\1', str)  # unescape brackets
        self._updateStyle(fill=_svgcolor(color),
                          stroke='none',
                          font_family=font_name,
                          font_size=size,
                          font_style=font_style,
                          font_weight=font_weight,
                          font_stretch=font_stretch)
        self.__currElt.appendChild(self.__doc.createTextNode(str))

    def text_end(self):
        if (self.__currElt.nodeName != 'text'):
            raise ValueError, "No text for close"
        self.__currElt = self.__currElt.parentNode

    # Three methods that change the local style of elements
    # If applied to a group, they persist until the next grestore,
    # If applied within a path element, they only affect that path -
    # although this may not in general correspond to (say) PostScript
    # behavior, it appears to correspond to reflect mode of use of this API
    def set_fill_color(self, color):
        self._updateStyle(fill=_svgcolor(color))

    def set_stroke_color(self, color):
        self._updateStyle(stroke=_svgcolor(color))

    def set_line_style(self, style):  # see line_style.py
        linecap = {0: 'butt', 1: 'round', 2: 'square'}
        linejoin = {0: 'miter', 1: 'round', 2: 'bevel'}
        if style.dash: dash = ','.join(map(str, style.dash))
        else: dash = 'none'
        self._updateStyle(stroke_width=style.width,
                          stroke=_svgcolor(style.color),
                          stroke_linecap=linecap[style.cap_style],
                          stroke_linejoin=linejoin[style.join_style],
                          stroke_dasharray=dash)

    # gsave & grestore respectively push & pop a new context to hold
    # new style and transform parameters.  push/pop transformation are
    # similar but explicitly specify a coordinate transform at the
    # same time
    def gsave(self):
        if (self.__currElt.nodeName not in ['g', 'svg']):
            raise ValueError, "No group for gsave"
        g = self.__doc.createElement('g')
        self.__currElt.appendChild(g)
        self.__currElt = g

    def grestore(self):
        if (self.__currElt.nodeName != 'g'):
            raise ValueError, "No group for grestore"
        # first pop off any auto-generated groups (see protectCurrentChildren)
        while (self.__currElt.hasAttribute('auto')):
            self.__currElt.removeAttribute('auto')
            self.__currElt = self.__currElt.parentNode
        # then pop off the original caller-generated group
        self.__currElt = self.__currElt.parentNode

    def push_transformation(self, baseloc, scale, angle, in_text=0):
        #? in_text arg appears to always be ignored

        # In some cases this gets called after newpath, with
        # corresonding pop_transformation called after the path is
        # finalized so we check specifically for that, and generate
        # an enclosing group to hold the incomplete path element
        # We could add the transform directly to the path element
        # (like we do with line-style etc) but that makes it harder
        # to handle the closing 'pop' and might lead to inconsitency
        # with PostScript if the closing pop doesn't come right after
        # the path element

        elt = self.__currElt
        if elt.nodeName == 'g':
            elt = None
        elif (elt.nodeName == 'path' and not elt.hasAttribute('d')):
            g = elt.parentNode
            g.removeChild(elt)
            self.__currElt = g
        else:
            raise ValueError, "Illegal placement of push_transformation"

        t = ''
        if baseloc:
            t += 'translate(%g,%g) ' % (baseloc[0], -baseloc[1])
        if angle:
            t += 'rotate(%g) ' % -angle
        if scale:
            t += 'scale(%g,%g) ' % tuple(scale)

        self.gsave()
        self.__currElt.setAttribute('transform', t.strip())
        if elt:  # elt has incomplete 'path' or None
            self.__currElt.appendChild(elt)
            self.__currElt = elt

    def pop_transformation(self, in_text=0):  #? in_text unused?
        self.grestore()

    # If verbose, add comments to the output stream (helps debugging)
    def comment(self, str):
        if _comment_p:
            self.__currElt.appendChild(self.__doc.createComment(str))

    # The verbatim method is currently not supported - presumably with
    # the SVG backend the user would require access to the DOM since
    # we're not directly outputting plain text here
    def verbatim(self, str):
        self.__currElt.appendChild(
            self.__doc.createComment('verbatim not implemented: ' + str))

    # The close() method finalizes the SVG document and flattens the
    # DOM document to XML text to the specified file (or stdout)
    def close(self):
        basecanvas.T.close(self)
        self.grestore()  # matching the gsave in __init__
        if (self.__currElt.nodeName != 'svg'):
            raise ValueError, "Incomplete document at close!"

        # Don't bother to output an empty document - this can happen
        # when we get close()d immediately by theme reinit
        if (len(self.__svg.childNodes[-1].childNodes) == 0):
            return

        fp, need_close = self.open_output(self.__out_fname)
        bbox = theme.adjust_bounding_box(
            [self.__xmin, self.__ymin, self.__xmax, self.__ymax])
        self.__svg.setAttribute(
            'viewBox', '%g %g %g %g' %
            (xscale(bbox[0]), -yscale(bbox[3]), xscale(bbox[2]) -
             xscale(bbox[0]), yscale(bbox[3]) - yscale(bbox[1])))
        self.__doc.writexml(fp, '', '  ', '\n')
        if need_close:
            fp.close()
示例#38
0
    #
    # Flows and Routes ########
    #

    # TODO: can we make flows and routes conversion switchable by option ?
    # parse flows
    flow_d = parse_flows(XMLDOC)
    # parse routes
    routes_by_start_d = parse_routes(XMLDOC, edge_list, is_verbinder_d)
    # validate relative flows
    validate_rel_flow(routes_by_start_d)
    # computes the probability for each route
    set_probability(routes_by_start_d, flow_d)

    #
    # XML Handling ##########
    #

    # create dom document and define routes + flows
    result_doc = Document()
    root = result_doc.createElement("routes")
    result_doc.appendChild(root)

    result_doc = set_v_types(veh_comp_d, result_doc, root, speed_d)
    result_doc = set_route_distributions(result_doc, routes_by_start_d, root)
    result_doc = set_flows(routes_by_start_d, flow_d, result_doc, root)
    # write the data into a .rou.xml file
    with open("%s.rou.xml" % args.output_file, "w") as ofh:
        result_doc.writexml(ofh, addindent='    ', newl='\n')
        ofh.close()
    def create_follow_scenario(self, data_dic, gf, path, xodr_path, osgb_path):
        """
        生成场景文件
        :param data_dic: 预读取的数据
        :param gf: GeneralFunc实例
        :param path: 输出路径
        :param xodr_path: 地图文件
        :param osgb_path: 地图数据库文件
        :return: None
        """
        same_lane = list()
        for idx, val in data_dic['objects_range'].items():
            if self.filter_y_range(val[1]):
                same_lane.append(idx)
        obj, min_val = 0.0, 1000
        for idx in same_lane:
            temp = self.follow_objects(data_dic['objects_range'][idx][0])
            if min_val > temp:
                min_val = temp
                obj = idx
        entity = [
            'Ego',
        ]
        x_speed = data_dic['objects_speed'][obj][0]
        for i, j in enumerate(x_speed):
            if j == 0:
                x_speed[i] = x_speed[i - 1] + 0.2
        filter_x_list = {'Ego': gf.draw_filter(data_dic['ego_speed'], '', '')}
        init_speed = {'Ego': filter_x_list['Ego'][0]}
        init_position = {'Ego': ['1.4', '-7.2', '0', '0', '0', '0']}
        if obj != 0.0:
            entity.append('Target' + str(obj))
            filter_x_list['Target' + str(obj)] = gf.draw_filter(
                x_speed, '', '')
            init_x, init_y, sx = gf.cal_init(
                filter_x_list['Ego'], filter_x_list['Target' + str(obj)],
                data_dic['objects_range'][obj][0],
                data_dic['objects_range'][obj][1])
            init_position['Target' + str(obj)] = [
                str(1.4 + 40),
                str(-7.2 + init_y), '0', '0', '0', '0'
            ]
            init_speed['Target' + str(obj)] = sx
        kwargs = dict()
        kwargs['out_path'] = path
        kwargs['GF'] = gf
        kwargs['xodr'] = xodr_path
        kwargs['osgb'] = osgb_path
        kwargs['entity'] = entity
        kwargs['speed'] = filter_x_list
        kwargs['init_speed'] = init_speed
        kwargs['init_pos'] = init_position

        doc = Document()
        GF = kwargs['GF']

        # root node
        root = doc.createElement('OpenSCENARIO')
        doc.appendChild(root)

        # FileHeader
        header = GF.create_header(doc, 'cut in scenario')
        root.appendChild(header)

        # ParameterDeclaration
        pdcl = GF.create_parameter(doc)
        root.appendChild(pdcl)

        # Catalogs
        catalogs = GF.create_catalogs(doc)
        root.appendChild(catalogs)

        # RoadNetworks
        # '../Runtime/Tools/RodDistro_6710_Rod4.6.0/DefaultProject/Odr/city_quick_3.xodr'
        # '../Runtime/Tools/RodDistro_6710_Rod4.6.0/DefaultProject/Database/city_quick_3.opt.osgb'
        rnode = GF.create_road_network(doc, kwargs['xodr'], kwargs['osgb'])
        root.appendChild(rnode)

        # Entities
        entity_list = kwargs['entity']
        enode = doc.createElement('Entities')
        for e in entity_list:
            obj_node = GF.create_entities(doc, e)
            enode.appendChild(obj_node)
        root.appendChild(enode)

        # StoryBoard
        snode = doc.createElement('Storyboard')
        # Init
        init_node = doc.createElement('Init')
        actions_node = doc.createElement('Actions')
        for i in entity_list:
            pr_node = GF.create_init_story(doc, kwargs['init_pos'][i],
                                           kwargs['init_speed'][i], i)
            actions_node.appendChild(pr_node)
        init_node.appendChild(actions_node)
        snode.appendChild(init_node)
        # Story
        story_node = doc.createElement('Story')
        story_node.setAttribute('name', 'CutInStory')
        story_node.setAttribute('owner', '')
        act_node = doc.createElement('Act')
        act_node.setAttribute('name', 'CutInAct')
        for key, value in kwargs['speed'].items():
            # Sequence
            seq_node = doc.createElement('Sequence')
            seq_node.setAttribute('name', key + 'Sequence')
            seq_node.setAttribute('numberOfExecutions', '1')
            actors_node = doc.createElement('Actors')
            entity_node = doc.createElement('Entity')
            entity_node.setAttribute('name', key)
            actors_node.appendChild(entity_node)
            seq_node.appendChild(actors_node)
            maneuver_node = doc.createElement('Maneuver')
            maneuver_node.setAttribute('name', key + 'Maneuver')
            for index, val in enumerate(value):
                event_node = GF.create_event(doc, index, key, val)
                maneuver_node.appendChild(event_node)

            seq_node.appendChild(maneuver_node)
            act_node.appendChild(seq_node)
            # Conditions Sequence
        conditions_node = GF.create_conditions_node(
            doc, {
                'value': '0',
                'rule': 'greater_than',
                'owner': '',
                'root_name': 'Conditions'
            })
        act_node.appendChild(conditions_node)
        story_node.appendChild(act_node)
        snode.appendChild(story_node)

        end_condition_node = doc.createElement('EndConditions')
        snode.appendChild(end_condition_node)
        root.appendChild(snode)

        with open(kwargs['out_path'], 'w') as f:
            doc.writexml(f, indent="\n", addindent="\t", encoding='utf-8')
示例#40
0
def makexml(txtPath, xmlPath, picPath):  # txt所在文件夹路径,xml文件保存路径,图片所在文件夹路径
    """此函数用于将yolo格式txt标注文件转换为voc格式xml标注文件

    在自己的标注图片文件夹下建三个子文件夹,分别命名为picture、txt、xml
    """
    dic = {
        '0': "LP",  # 创建字典用来对类型进行转换
        '1': "LZ",  # 此处的字典要与自己的classes.txt文件中的类对应,且顺序要一致
        '2': "SC",
        '3': "MH",
        '4': "ML",
        '5': "MD",
        '6': "WR",
        '7': 'QT'
    }
    files = os.listdir(txtPath)
    for i, name in enumerate(files):
        xmlBuilder = Document()
        annotation = xmlBuilder.createElement("annotation")  # 创建annotation标签
        xmlBuilder.appendChild(annotation)
        txtFile = open(txtPath + name)
        txtList = txtFile.readlines()
        img = cv2.imread(picPath + name[0:-4] + ".jpg")
        Pheight, Pwidth, Pdepth = img.shape

        folder = xmlBuilder.createElement("folder")  # folder标签
        foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")
        folder.appendChild(foldercontent)
        annotation.appendChild(folder)  # folder标签结束

        filename = xmlBuilder.createElement("filename")  # filename标签
        filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")
        filename.appendChild(filenamecontent)
        annotation.appendChild(filename)  # filename标签结束

        size = xmlBuilder.createElement("size")  # size标签
        width = xmlBuilder.createElement("width")  # size子标签width
        widthcontent = xmlBuilder.createTextNode(str(Pwidth))
        width.appendChild(widthcontent)
        size.appendChild(width)  # size子标签width结束

        height = xmlBuilder.createElement("height")  # size子标签height
        heightcontent = xmlBuilder.createTextNode(str(Pheight))
        height.appendChild(heightcontent)
        size.appendChild(height)  # size子标签height结束

        depth = xmlBuilder.createElement("depth")  # size子标签depth
        depthcontent = xmlBuilder.createTextNode(str(Pdepth))
        depth.appendChild(depthcontent)
        size.appendChild(depth)  # size子标签depth结束

        annotation.appendChild(size)  # size标签结束

        for j in txtList:
            oneline = j.strip().split(" ")
            object = xmlBuilder.createElement("object")  # object 标签
            picname = xmlBuilder.createElement("name")  # name标签
            namecontent = xmlBuilder.createTextNode(dic[oneline[0]])
            picname.appendChild(namecontent)
            object.appendChild(picname)  # name标签结束

            pose = xmlBuilder.createElement("pose")  # pose标签
            posecontent = xmlBuilder.createTextNode("Unspecified")
            pose.appendChild(posecontent)
            object.appendChild(pose)  # pose标签结束

            truncated = xmlBuilder.createElement("truncated")  # truncated标签
            truncatedContent = xmlBuilder.createTextNode("0")
            truncated.appendChild(truncatedContent)
            object.appendChild(truncated)  # truncated标签结束

            difficult = xmlBuilder.createElement("difficult")  # difficult标签
            difficultcontent = xmlBuilder.createTextNode("0")
            difficult.appendChild(difficultcontent)
            object.appendChild(difficult)  # difficult标签结束

            bndbox = xmlBuilder.createElement("bndbox")  # bndbox标签
            xmin = xmlBuilder.createElement("xmin")  # xmin标签
            mathData = int(((float(oneline[1])) * Pwidth + 1) -
                           (float(oneline[3])) * 0.5 * Pwidth)
            xminContent = xmlBuilder.createTextNode(str(mathData))
            xmin.appendChild(xminContent)
            bndbox.appendChild(xmin)  # xmin标签结束

            ymin = xmlBuilder.createElement("ymin")  # ymin标签
            mathData = int(((float(oneline[2])) * Pheight + 1) -
                           (float(oneline[4])) * 0.5 * Pheight)
            yminContent = xmlBuilder.createTextNode(str(mathData))
            ymin.appendChild(yminContent)
            bndbox.appendChild(ymin)  # ymin标签结束

            xmax = xmlBuilder.createElement("xmax")  # xmax标签
            mathData = int(((float(oneline[1])) * Pwidth + 1) +
                           (float(oneline[3])) * 0.5 * Pwidth)
            xmaxContent = xmlBuilder.createTextNode(str(mathData))
            xmax.appendChild(xmaxContent)
            bndbox.appendChild(xmax)  # xmax标签结束

            ymax = xmlBuilder.createElement("ymax")  # ymax标签
            mathData = int(((float(oneline[2])) * Pheight + 1) +
                           (float(oneline[4])) * 0.5 * Pheight)
            ymaxContent = xmlBuilder.createTextNode(str(mathData))
            ymax.appendChild(ymaxContent)
            bndbox.appendChild(ymax)  # ymax标签结束

            object.appendChild(bndbox)  # bndbox标签结束

            annotation.appendChild(object)  # object标签结束

        f = open(xmlPath + name[0:-4] + ".xml", 'w')
        xmlBuilder.writexml(f,
                            indent='\t',
                            newl='\n',
                            addindent='\t',
                            encoding='utf-8')
        f.close()
示例#41
0
def gen_car_and_carplate_independently(dic):
    img_name = dic['img_name']
    height = dic['height']
    width = dic['width']
    channel = dic['channel']
    xmin = dic['xmin']
    ymin = dic['ymin']
    xmax = dic['xmax']
    ymax = dic['ymax']

    # 创建dom文档
    doc = Document()
    # 创建根节点
    annotation = doc.createElement('annotation')
    # 根节点插入dom树
    doc.appendChild(annotation)
    # folder
    folder = doc.createElement('folder')
    annotation.appendChild(folder)
    folder.appendChild(doc.createTextNode('AOLP_LE'))
    # filename
    filename = doc.createElement('filename')
    annotation.appendChild(filename)
    filename.appendChild(doc.createTextNode(img_name))
    # source
    source = doc.createElement('source')
    annotation.appendChild(source)
    database_ = doc.createElement('database')
    database_.appendChild(doc.createTextNode('The AOLP Law Enforcement'))
    source.appendChild(database_)
    # 创建size节点
    size = doc.createElement('size')
    annotation.appendChild(size)
    width_ = doc.createElement('width')
    width_.appendChild(doc.createTextNode(str(width)))
    height_ = doc.createElement('height')
    height_.appendChild(doc.createTextNode(str(height)))
    depth_ = doc.createElement('depth')
    depth_.appendChild(doc.createTextNode(str(channel)))
    size.appendChild(width_)
    size.appendChild(height_)
    size.appendChild(depth_)
    # segmentation
    segmented = doc.createElement('segmented')
    annotation.appendChild(segmented)
    segmented.appendChild(doc.createTextNode(str(0)))

    # 如果标注出现错误,比如xmin大于xmax,直接跳过,并且不保存xml
    need_save = True
    for i in range(len(xmin)):
        if xmin[i] > xmax[i]:
            print(img_name)
            xmin[i], xmax[i] = xmax[i], xmin[i]
        #     need_save = False
        #     continue
        if ymin[i] > ymax[i]:
            print(img_name)
            ymin[i], ymax[i] = ymax[i], ymin[i]
        #     need_save = False
        #     continue

        # 限制不超过图片上下界
        carplate_xmin = ex.limit_in_bounds(xmin[i], 1, width)
        carplate_ymin = ex.limit_in_bounds(ymin[i], 1, height)
        carplate_xmax = ex.limit_in_bounds(xmax[i], 1, width)
        carplate_ymax = ex.limit_in_bounds(ymax[i], 1, height)

        # carplate
        object = doc.createElement('object')
        annotation.appendChild(object)
        name = doc.createElement('name')
        name.appendChild(doc.createTextNode("carplate"))
        object.appendChild(name)
        # pose
        pose_ = doc.createElement('pose')
        pose_.appendChild(doc.createTextNode('Unspecified'))
        object.appendChild(pose_)
        # truncated
        truncated_ = doc.createElement('truncated')
        truncated_.appendChild(doc.createTextNode(str(0)))
        object.appendChild(truncated_)
        # difficult
        difficult_ = doc.createElement('difficult')
        difficult_.appendChild(doc.createTextNode(str(0)))
        object.appendChild(difficult_)
        # the bndbox
        bndbox = doc.createElement('bndbox')
        object.appendChild(bndbox)
        xmin_ = doc.createElement('xmin')
        xmin_.appendChild(doc.createTextNode(str(carplate_xmin)))
        bndbox.appendChild(xmin_)
        ymin_ = doc.createElement('ymin')
        ymin_.appendChild(doc.createTextNode(str(carplate_ymin)))
        bndbox.appendChild(ymin_)
        xmax_ = doc.createElement('xmax')
        xmax_.appendChild(doc.createTextNode(str(carplate_xmax)))
        bndbox.appendChild(xmax_)
        ymax_ = doc.createElement('ymax')
        ymax_.appendChild(doc.createTextNode(str(carplate_ymax)))
        bndbox.appendChild(ymax_)

    if need_save:
        fp = open(os.path.join(target_anno,
                               str(dic['img_index']) + '.xml'), 'w')
        doc.writexml(fp,
                     indent='\t',
                     addindent='\t',
                     newl='\n',
                     encoding="utf-8")
示例#42
0
def main():
    """ This function converts a style gallery to XML"""
    style_file_path = arcpy.GetParameterAsText(0)
    export_name_path = arcpy.GetParameterAsText(1)
    classes_to_export = arcpy.GetParameter(2)

    arcpy.AddMessage("Start Processing...")

    style_gallery = CreateObject(
        ArcGisModules.module_framework.StyleGallery,
        interface=ArcGisModules.module_display.IStyleGallery)

    storage = change_interface(
        style_gallery, ArcGisModules.module_display.IStyleGalleryStorage)
    style_file_service = CheckIfStyleFileIsAlreadyInStorageService()

    if storage.DefaultStylePath in style_file_path:
        style_gallery_name = style_file_path.replace(storage.DefaultStylePath,
                                                     "")
    else:
        style_gallery_name = style_file_path
    style_file_service.check_style_file(storage, style_gallery_name)

    if not style_file_service.style_file_exists_in_storage:
        storage.AddFile(style_file_path)

    qgis_style_xml = codecs.open(export_name_path, "w", encoding="utf-8")

    xml_document = Document()

    root_element = xml_document.createElement("qgis_style")
    root_element.setAttribute("version", "2")
    xml_document.appendChild(root_element)

    for class_to_export in classes_to_export:
        if class_to_export in [
                u'Marker Symbols', u'Fill Symbols', u'Line Symbols'
        ]:
            SymbolCreator(xml_document).create_symbols(style_gallery,
                                                       style_gallery_name,
                                                       class_to_export)
        elif class_to_export == u'Color Ramps':
            ColorRampCreator(xml_document).create_colorramps(
                style_gallery, style_gallery_name)
        elif class_to_export == u'Text Symbols':
            TextFormatCreator(xml_document).create_text_formats(
                style_gallery, style_gallery_name)
        elif class_to_export == u'Labels':
            LabelSettingsCreator(xml_document).create_label_settings(
                style_gallery, style_gallery_name)

    if not style_file_service.style_file_exists_in_storage:
        storage.RemoveFile(style_file_path)

    try:
        xml_document.writexml(qgis_style_xml,
                              indent="  ",
                              addindent="  ",
                              newl="\n",
                              encoding="UTF-8")
        arcpy.AddMessage("Style File saved!")
    finally:
        qgis_style_xml.close()
示例#43
0
def generate_xml(image_info, tag_list, out_dir=None):
    """
    :param out_dir:
    :info: 输出xml文件
    :param image_info:
    :param tag_list:
    :return:
    """
    img_name = image_info['file_name']
    img_h = str(image_info['height'])
    img_w = str(image_info['width'])
    img_path = image_info['path']

    folder = img_path.split('\\')[-2]

    doc = Document()
    orderpack = doc.createElement("annotation")
    doc.appendChild(orderpack)

    objectfolder = doc.createElement("folder")
    objectcontenttext = doc.createTextNode(folder)
    objectfolder.appendChild(objectcontenttext)
    orderpack.appendChild(objectfolder)

    objectfilename = doc.createElement("filename")
    objectcontenttext = doc.createTextNode(img_name)
    objectfilename.appendChild(objectcontenttext)
    orderpack.appendChild(objectfilename)

    objectpath = doc.createElement("path")
    objectcontenttext = doc.createTextNode(img_path)
    objectpath.appendChild(objectcontenttext)
    orderpack.appendChild(objectpath)

    objectsource = doc.createElement("source")
    orderpack.appendChild(objectsource)

    objectdatabase = doc.createElement("database")
    objectdatabasetext = doc.createTextNode('Unknown')
    objectdatabase.appendChild(objectdatabasetext)
    objectsource.appendChild(objectdatabase)

    objectsize = doc.createElement("size")
    orderpack.appendChild(objectsize)

    objectwidth = doc.createElement("width")
    objectwidthtext = doc.createTextNode(img_w)
    objectwidth.appendChild(objectwidthtext)
    objectsize.appendChild(objectwidth)

    objectheight = doc.createElement("height")
    objectheighttext = doc.createTextNode(img_h)
    objectheight.appendChild(objectheighttext)
    objectsize.appendChild(objectheight)

    objectdepth = doc.createElement("depth")
    objectdepthtext = doc.createTextNode('3')
    objectdepth.appendChild(objectdepthtext)
    objectsize.appendChild(objectdepth)

    objectcontent = doc.createElement("segmented")
    objectcontenttext = doc.createTextNode('0')
    objectcontent.appendChild(objectcontenttext)
    orderpack.appendChild(objectcontent)

    for tag in tag_list:
        bbox = tag.bbox
        category = tag.name
        xmin = bbox[0]
        ymin = bbox[1]
        xmax = bbox[2]
        ymax = bbox[3]

        xmin = str(int(xmin))
        ymin = str(int(ymin))
        xmax = str(int(xmax))
        ymax = str(int(ymax))

        objectobject = doc.createElement("object")
        orderpack.appendChild(objectobject)

        objectname = doc.createElement("name")
        objectcontenttext = doc.createTextNode(str(category))
        objectname.appendChild(objectcontenttext)
        objectobject.appendChild(objectname)

        objectpose = doc.createElement("pose")
        objectcontenttext = doc.createTextNode('Unspecified')
        objectpose.appendChild(objectcontenttext)
        objectobject.appendChild(objectpose)

        objecttruncated = doc.createElement("truncated")
        objectcontenttext = doc.createTextNode('0')
        objecttruncated.appendChild(objectcontenttext)
        objectobject.appendChild(objecttruncated)

        objectdifficult = doc.createElement("difficult")
        objectcontenttext = doc.createTextNode('0')
        objectdifficult.appendChild(objectcontenttext)
        objectobject.appendChild(objectdifficult)

        objectbndbox = doc.createElement("bndbox")
        objectobject.appendChild(objectbndbox)

        objectxmin = doc.createElement("xmin")
        objectcontenttext = doc.createTextNode(xmin)
        objectxmin.appendChild(objectcontenttext)
        objectbndbox.appendChild(objectxmin)

        objectymin = doc.createElement("ymin")
        objectcontenttext = doc.createTextNode(ymin)
        objectymin.appendChild(objectcontenttext)
        objectbndbox.appendChild(objectymin)

        objectxmax = doc.createElement("xmax")
        objectcontenttext = doc.createTextNode(xmax)
        objectxmax.appendChild(objectcontenttext)
        objectbndbox.appendChild(objectxmax)

        objectymax = doc.createElement("ymax")
        objectcontenttext = doc.createTextNode(ymax)
        objectymax.appendChild(objectcontenttext)
        objectbndbox.appendChild(objectymax)

    if not out_dir:
        xml_file = os.path.splitext(img_path)[0] + '.xml'
    else:
        xml_file = os.path.join(out_dir, os.path.splitext(img_name)[0] + '.xml')
    f = open(xml_file, 'w')
    doc.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')
    f.close()
def gen_car_and_carplate_independently(dic):
    num_car = 0
    num_truck = 0
    num_pedestrian = 0

    img_name = dic['img_name']
    height = dic['height']
    width = dic['width']
    channel = dic['channel']
    objs = dic['objs']

    # 创建dom文档
    doc = Document()
    # 创建根节点
    annotation = doc.createElement('annotation')
    # 根节点插入dom树
    doc.appendChild(annotation)
    # folder
    folder = doc.createElement('folder')
    annotation.appendChild(folder)
    folder.appendChild(doc.createTextNode('Udacity'))
    # filename
    filename = doc.createElement('filename')
    annotation.appendChild(filename)
    filename.appendChild(doc.createTextNode(img_name))
    # source
    source = doc.createElement('source')
    annotation.appendChild(source)
    database_ = doc.createElement('database')
    database_.appendChild(doc.createTextNode('Udacity objects'))
    source.appendChild(database_)
    # 创建size节点
    size = doc.createElement('size')
    annotation.appendChild(size)
    width_ = doc.createElement('width')
    width_.appendChild(doc.createTextNode(str(width)))
    height_ = doc.createElement('height')
    height_.appendChild(doc.createTextNode(str(height)))
    depth_ = doc.createElement('depth')
    depth_.appendChild(doc.createTextNode(str(channel)))
    size.appendChild(width_)
    size.appendChild(height_)
    size.appendChild(depth_)
    # segmentation
    segmented = doc.createElement('segmented')
    annotation.appendChild(segmented)
    segmented.appendChild(doc.createTextNode(str(0)))

    for obj in objs:
        obj_class = obj['Label']
        xmin = round(float(obj['xmin']))
        xmax = round(float(obj['xmax']))
        ymin = round(float(obj['ymin']))
        ymax = round(float(obj['ymax']))

        if need_car and obj_class == 'Car':
            num_car += 1
            global Car_num
            Car_num += 1
            # 限制不超过图片上下界
            car_xmin = ex.limit_in_bounds(xmin, 1, width)
            car_ymin = ex.limit_in_bounds(ymin, 1, height)
            car_xmax = ex.limit_in_bounds(xmax, 1, width)
            car_ymax = ex.limit_in_bounds(ymax, 1, height)

            # car
            object = doc.createElement('object')
            annotation.appendChild(object)
            name = doc.createElement('name')
            name.appendChild(doc.createTextNode('car'))
            object.appendChild(name)
            # pose
            pose_ = doc.createElement('pose')
            pose_.appendChild(doc.createTextNode('Unspecified'))
            object.appendChild(pose_)
            # occluded
            occluded_ = doc.createElement('occluded')
            occluded_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(occluded_)
            # truncated
            truncated_ = doc.createElement('truncated')
            truncated_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(truncated_)
            # difficult
            difficult_ = doc.createElement('difficult')
            difficult_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(difficult_)
            # the bndbox
            bndbox = doc.createElement('bndbox')
            object.appendChild(bndbox)
            xmin_ = doc.createElement('xmin')
            xmin_.appendChild(doc.createTextNode(str(car_xmin)))
            bndbox.appendChild(xmin_)
            ymin_ = doc.createElement('ymin')
            ymin_.appendChild(doc.createTextNode(str(car_ymin)))
            bndbox.appendChild(ymin_)
            xmax_ = doc.createElement('xmax')
            xmax_.appendChild(doc.createTextNode(str(car_xmax)))
            bndbox.appendChild(xmax_)
            ymax_ = doc.createElement('ymax')
            ymax_.appendChild(doc.createTextNode(str(car_ymax)))
            bndbox.appendChild(ymax_)

        if need_truck and obj_class == 'Truck':
            num_truck += 1
            global Truck_num
            Truck_num += 1
            # 限制不超过图片上下界
            truck_xmin = ex.limit_in_bounds(xmin, 1, width)
            truck_ymin = ex.limit_in_bounds(ymin, 1, height)
            truck_xmax = ex.limit_in_bounds(xmax, 1, width)
            truck_ymax = ex.limit_in_bounds(ymax, 1, height)

            # truck
            object = doc.createElement('object')
            annotation.appendChild(object)
            name = doc.createElement('name')
            name.appendChild(doc.createTextNode("truck"))
            object.appendChild(name)
            # pose
            pose_ = doc.createElement('pose')
            pose_.appendChild(doc.createTextNode('Unspecified'))
            object.appendChild(pose_)
            # occluded
            occluded_ = doc.createElement('occluded')
            occluded_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(occluded_)
            # truncated
            truncated_ = doc.createElement('truncated')
            truncated_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(truncated_)
            # difficult
            difficult_ = doc.createElement('difficult')
            difficult_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(difficult_)
            # the bndbox
            bndbox = doc.createElement('bndbox')
            object.appendChild(bndbox)
            xmin_ = doc.createElement('xmin')
            xmin_.appendChild(doc.createTextNode(str(truck_xmin)))
            bndbox.appendChild(xmin_)
            ymin_ = doc.createElement('ymin')
            ymin_.appendChild(doc.createTextNode(str(truck_ymin)))
            bndbox.appendChild(ymin_)
            xmax_ = doc.createElement('xmax')
            xmax_.appendChild(doc.createTextNode(str(truck_xmax)))
            bndbox.appendChild(xmax_)
            ymax_ = doc.createElement('ymax')
            ymax_.appendChild(doc.createTextNode(str(truck_ymax)))
            bndbox.appendChild(ymax_)

        if need_pedestrian and obj_class == 'Pedestrian':
            num_pedestrian += 1
            global Pedestrian_num
            Pedestrian_num += 1
            # 限制不超过图片上下界
            pedestrian_xmin = ex.limit_in_bounds(xmin, 1, width)
            pedestrian_ymin = ex.limit_in_bounds(ymin, 1, height)
            pedestrian_xmax = ex.limit_in_bounds(xmax, 1, width)
            pedestrian_ymax = ex.limit_in_bounds(ymax, 1, height)

            # pedestrian
            object = doc.createElement('object')
            annotation.appendChild(object)
            name = doc.createElement('name')
            name.appendChild(doc.createTextNode("pedestrian"))
            object.appendChild(name)
            # pose
            pose_ = doc.createElement('pose')
            pose_.appendChild(doc.createTextNode('Unspecified'))
            object.appendChild(pose_)
            # occluded
            occluded_ = doc.createElement('occluded')
            occluded_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(occluded_)
            # truncated
            truncated_ = doc.createElement('truncated')
            truncated_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(truncated_)
            # difficult
            difficult_ = doc.createElement('difficult')
            difficult_.appendChild(doc.createTextNode(str(0)))
            object.appendChild(difficult_)
            # the bndbox
            bndbox = doc.createElement('bndbox')
            object.appendChild(bndbox)
            xmin_ = doc.createElement('xmin')
            xmin_.appendChild(doc.createTextNode(str(pedestrian_xmin)))
            bndbox.appendChild(xmin_)
            ymin_ = doc.createElement('ymin')
            ymin_.appendChild(doc.createTextNode(str(pedestrian_ymin)))
            bndbox.appendChild(ymin_)
            xmax_ = doc.createElement('xmax')
            xmax_.appendChild(doc.createTextNode(str(pedestrian_xmax)))
            bndbox.appendChild(xmax_)
            ymax_ = doc.createElement('ymax')
            ymax_.appendChild(doc.createTextNode(str(pedestrian_ymax)))
            bndbox.appendChild(ymax_)

    # 如果车辆,卡车或行人数量大于0,才写入文件 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    if (need_car and num_car > 0) or (need_truck and num_truck > 0) or (need_pedestrian and num_pedestrian > 0):
        fp = open(os.path.join(target_dir, img_name.split('.')[0] + '.xml'), 'w')
        doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
示例#45
0
def CopyBinaries(out_dir, out_project_dir, src_package):
    """cp out/Release/<pak> out/Release/xwalk_core_library/res/raw/<pak>
     cp out/Release/lib.java/<lib> out/Release/xwalk_core_library/libs/<lib>
     cp out/Release/xwalk_core_shell_apk/libs/*
        out/Release/xwalk_core_library/libs
  """

    print 'Copying binaries...'
    # Copy assets.
    res_raw_dir = os.path.join(out_project_dir, 'res', 'raw')
    res_value_dir = os.path.join(out_project_dir, 'res', 'values')
    if not os.path.exists(res_raw_dir):
        os.mkdir(res_raw_dir)
    if not os.path.exists(res_value_dir):
        os.mkdir(res_value_dir)

    paks_to_copy = [
        'icudtl.dat',
        'xwalk.pak',
    ]

    pak_list_xml = Document()
    resources_node = pak_list_xml.createElement('resources')
    string_array_node = pak_list_xml.createElement('string-array')
    string_array_node.setAttribute('name', 'xwalk_resources_list')
    pak_list_xml.appendChild(resources_node)
    resources_node.appendChild(string_array_node)
    for pak in paks_to_copy:
        source_file = os.path.join(out_dir, pak)
        target_file = os.path.join(res_raw_dir, pak)
        shutil.copyfile(source_file, target_file)
        item_node = pak_list_xml.createElement('item')
        item_node.appendChild(pak_list_xml.createTextNode(pak))
        string_array_node.appendChild(item_node)
    pak_list_file = open(
        os.path.join(res_value_dir, 'xwalk_resources_list.xml'), 'w')
    pak_list_xml.writexml(pak_list_file, newl='\n', encoding='utf-8')
    pak_list_file.close()

    libs_dir = os.path.join(out_project_dir, 'libs')
    if not os.path.exists(libs_dir):
        os.mkdir(libs_dir)

    # Copy jar files to libs.
    if src_package:
        libs_to_copy = [
            'eyesfree_java.jar',
            'jsr_305_javalib.jar',
        ]
    else:
        libs_to_copy = [
            'xwalk_core_library_java_app_part.jar',
            'xwalk_core_library_java_library_part.jar',
        ]

    for lib in libs_to_copy:
        source_file = os.path.join(out_dir, 'lib.java', lib)
        target_file = os.path.join(libs_dir, lib)
        shutil.copyfile(source_file, target_file)

    # Copy native libraries.
    source_dir = os.path.join(out_dir, XWALK_CORE_SHELL_APK, 'libs')
    target_dir = libs_dir
    distutils.dir_util.copy_tree(source_dir, target_dir)
示例#46
0
class XMLReportGenerator(ReportGenerator):
    """
    This class generates a report with the method printToFile(fileName) which contains
    the information of all the vulnerabilities notified to this object through the
    method add_vulnerability(vulnerabilityTypeName,level,url,parameter,info).
    The format of the file is XML and it has the following structure:
    <report type="security">
        <generatedBy id="Wapiti X.X.X"/>
        <vulnerabilityTypeList>
            <vulnerabilityType name="SQL Injection">

        <vulnerabilityTypeList>
            <vulnerabilityType name="SQL Injection">
                <vulnerabilityList>
                    <vulnerability level="3">
                        <url>http://www.a.com</url>
                        <parameters>id=23</parameters>
                        <info>SQL Injection</info>
                    </vulnerability>
                </vulnerabilityList>
            </vulnerabilityType>
        </vulnerabilityTypeList>
    </report>
    """
    def __init__(self):
        super().__init__()
        self._xml_doc = Document()
        self._flaw_types = {}

        self._vulns = {}
        self._anomalies = {}
        self._additionals = {}

    # Vulnerabilities
    def add_vulnerability_type(self,
                               name,
                               description="",
                               solution="",
                               references=None,
                               wstg=None):
        if name not in self._flaw_types:
            self._flaw_types[name] = {
                "desc": description,
                "sol": solution,
                "ref": references,
                "wstg": wstg
            }
        if name not in self._vulns:
            self._vulns[name] = []

    def add_vulnerability(self,
                          module: str,
                          category=None,
                          level=0,
                          request=None,
                          parameter="",
                          info="",
                          wstg=None):
        """
        Store the information about the vulnerability to be printed later.
        The method printToFile(fileName) can be used to save in a file the
        vulnerabilities notified through the current method.
        """

        vuln_dict = {
            "method": request.method,
            "path": request.file_path,
            "info": info,
            "level": level,
            "parameter": parameter,
            "referer": request.referer,
            "module": module,
            "http_request": request.http_repr(left_margin=""),
            "curl_command": request.curl_repr,
            "wstg": wstg
        }

        if category not in self._vulns:
            self._vulns[category] = []
        self._vulns[category].append(vuln_dict)

    # Anomalies
    def add_anomaly_type(self,
                         name,
                         description="",
                         solution="",
                         references=None,
                         wstg=None):
        if name not in self._flaw_types:
            self._flaw_types[name] = {
                "desc": description,
                "sol": solution,
                "ref": references,
                "wstg": wstg
            }
        if name not in self._anomalies:
            self._anomalies[name] = []

    def add_anomaly(self,
                    module: str,
                    category=None,
                    level=0,
                    request=None,
                    parameter="",
                    info="",
                    wstg=None):
        """
        Store the information about the vulnerability to be printed later.
        The method printToFile(fileName) can be used to save in a file the
        vulnerabilities notified through the current method.
        """

        anom_dict = {
            "method": request.method,
            "path": request.file_path,
            "info": info,
            "level": level,
            "parameter": parameter,
            "referer": request.referer,
            "module": module,
            "http_request": request.http_repr(left_margin=""),
            "curl_command": request.curl_repr,
            "wstg": wstg
        }
        if category not in self._anomalies:
            self._anomalies[category] = []
        self._anomalies[category].append(anom_dict)

    # Additionals
    def add_additional_type(self,
                            name,
                            description="",
                            solution="",
                            references=None,
                            wstg=None):
        """
        This method adds an addtional type, it can be invoked to include in the
        report the type.
        """
        if name not in self._flaw_types:
            self._flaw_types[name] = {
                "desc": description,
                "sol": solution,
                "ref": references,
                "wstg": wstg
            }
        if name not in self._additionals:
            self._additionals[name] = []

    def add_additional(self,
                       module: str,
                       category=None,
                       level=0,
                       request=None,
                       parameter="",
                       info="",
                       wstg=None):
        """
        Store the information about the addtional to be printed later.
        The method printToFile(fileName) can be used to save in a file the
        addtionals notified through the current method.
        """

        addition_dict = {
            "method": request.method,
            "path": request.file_path,
            "info": info,
            "level": level,
            "parameter": parameter,
            "referer": request.referer,
            "module": module,
            "http_request": request.http_repr(left_margin=""),
            "curl_command": request.curl_repr,
            "wstg": wstg
        }
        if category not in self._additionals:
            self._additionals[category] = []
        self._additionals[category].append(addition_dict)

    def generate_report(self, output_path):
        """
        Create a xml file with a report of the vulnerabilities which have been logged with
        the method add_vulnerability(vulnerabilityTypeName,level,url,parameter,info)
        """

        report = self._xml_doc.createElement("report")
        report.setAttribute("type", "security")
        self._xml_doc.appendChild(report)

        # Add report infos
        report_infos = self._create_info_section()
        report.appendChild(report_infos)

        vulnerabilities = self._xml_doc.createElement("vulnerabilities")
        anomalies = self._xml_doc.createElement("anomalies")
        additionals = self._xml_doc.createElement("additionals")

        # Loop on each flaw classification
        for flaw_type_name, flaw_type in self._flaw_types.items():
            container = None
            classification = ""
            flaw_dict = {}
            if flaw_type_name in self._vulns:
                container = vulnerabilities
                classification = "vulnerability"
                flaw_dict = self._vulns
            elif flaw_type_name in self._anomalies:
                container = anomalies
                classification = "anomaly"
                flaw_dict = self._anomalies
            elif flaw_type_name in self._additionals:
                container = additionals
                classification = "additional"
                flaw_dict = self._additionals

            # Child nodes with a description of the flaw type
            flaw_type_node = self._xml_doc.createElement(classification)
            flaw_type_node.setAttribute("name", flaw_type_name)
            flaw_type_desc = self._xml_doc.createElement("description")
            flaw_type_desc.appendChild(
                self._xml_doc.createCDATASection(flaw_type["desc"]))
            flaw_type_node.appendChild(flaw_type_desc)
            flaw_type_solution = self._xml_doc.createElement("solution")
            flaw_type_solution.appendChild(
                self._xml_doc.createCDATASection(flaw_type["sol"]))
            flaw_type_node.appendChild(flaw_type_solution)

            flaw_type_references = self._xml_doc.createElement("references")
            for ref in flaw_type["ref"]:
                reference_node = self._xml_doc.createElement("reference")
                title_node = self._xml_doc.createElement("title")
                url_node = self._xml_doc.createElement("url")
                title_node.appendChild(self._xml_doc.createTextNode(ref))
                url = flaw_type["ref"][ref]
                url_node.appendChild(self._xml_doc.createTextNode(url))
                wstg_node = self._xml_doc.createElement("wstg")
                for wstg_code in flaw_type["wstg"] or []:
                    wstg_code_node = self._xml_doc.createElement("code")
                    wstg_code_node.appendChild(
                        self._xml_doc.createTextNode(wstg_code))
                    wstg_node.appendChild(wstg_code_node)
                reference_node.appendChild(title_node)
                reference_node.appendChild(url_node)
                reference_node.appendChild(wstg_node)
                flaw_type_references.appendChild(reference_node)
            flaw_type_node.appendChild(flaw_type_references)

            # And child nodes with each flaw of the current type
            entries_node = self._xml_doc.createElement("entries")
            for flaw in flaw_dict[flaw_type_name]:
                entry_node = self._xml_doc.createElement("entry")
                method_node = self._xml_doc.createElement("method")
                method_node.appendChild(
                    self._xml_doc.createTextNode(flaw["method"]))
                entry_node.appendChild(method_node)
                path_node = self._xml_doc.createElement("path")
                path_node.appendChild(
                    self._xml_doc.createTextNode(flaw["path"]))
                entry_node.appendChild(path_node)
                level_node = self._xml_doc.createElement("level")
                level_node.appendChild(
                    self._xml_doc.createTextNode(str(flaw["level"])))
                entry_node.appendChild(level_node)
                parameter_node = self._xml_doc.createElement("parameter")
                parameter_node.appendChild(
                    self._xml_doc.createTextNode(flaw["parameter"]))
                entry_node.appendChild(parameter_node)
                info_node = self._xml_doc.createElement("info")
                info_node.appendChild(
                    self._xml_doc.createTextNode(flaw["info"]))
                entry_node.appendChild(info_node)
                referer_node = self._xml_doc.createElement("referer")
                referer_node.appendChild(
                    self._xml_doc.createTextNode(flaw["referer"]))
                entry_node.appendChild(referer_node)
                module_node = self._xml_doc.createElement("module")
                module_node.appendChild(
                    self._xml_doc.createTextNode(flaw["module"]))
                entry_node.appendChild(module_node)
                http_request_node = self._xml_doc.createElement("http_request")
                http_request_node.appendChild(
                    self._xml_doc.createCDATASection(flaw["http_request"]))
                entry_node.appendChild(http_request_node)
                curl_command_node = self._xml_doc.createElement("curl_command")
                curl_command_node.appendChild(
                    self._xml_doc.createCDATASection(flaw["curl_command"]))
                entry_node.appendChild(curl_command_node)
                wstg_node = self._xml_doc.createElement("wstg")
                for wstg_code in flaw["wstg"] or []:
                    wstg_code_node = self._xml_doc.createElement("code")
                    wstg_code_node.appendChild(
                        self._xml_doc.createTextNode(wstg_code))
                    wstg_node.appendChild(wstg_code_node)
                entry_node.appendChild(wstg_node)
                entries_node.appendChild(entry_node)
            flaw_type_node.appendChild(entries_node)
            container.appendChild(flaw_type_node)
        report.appendChild(vulnerabilities)
        report.appendChild(anomalies)
        report.appendChild(additionals)

        with open(output_path, "w", errors="ignore",
                  encoding='utf-8') as xml_report_file:
            self._xml_doc.writexml(xml_report_file, addindent="   ", newl="\n")

    def _create_info_section(self) -> Element:
        """
        Write the authentication section explaining what method, fields, url were used and also if it has been
        successful
        """
        report_infos = self._xml_doc.createElement("report_infos")
        generator_name = self._xml_doc.createElement("info")
        generator_name.setAttribute("name", "generatorName")
        generator_name.appendChild(self._xml_doc.createTextNode("wapiti"))
        report_infos.appendChild(generator_name)

        generator_version = self._xml_doc.createElement("info")
        generator_version.setAttribute("name", "generatorVersion")
        generator_version.appendChild(
            self._xml_doc.createTextNode(self._infos["version"]))
        report_infos.appendChild(generator_version)

        scope = self._xml_doc.createElement("info")
        scope.setAttribute("name", "scope")
        scope.appendChild(self._xml_doc.createTextNode(self._infos["scope"]))
        report_infos.appendChild(scope)

        date_of_scan = self._xml_doc.createElement("info")
        date_of_scan.setAttribute("name", "dateOfScan")
        date_of_scan.appendChild(
            self._xml_doc.createTextNode(self._infos["date"]))
        report_infos.appendChild(date_of_scan)

        target = self._xml_doc.createElement("info")
        target.setAttribute("name", "target")
        target.appendChild(self._xml_doc.createTextNode(self._infos["target"]))
        report_infos.appendChild(target)

        target = self._xml_doc.createElement("info")
        target.setAttribute("name", "crawledPages")
        target.appendChild(
            self._xml_doc.createTextNode(str(self._infos["crawled_pages"])))
        report_infos.appendChild(target)

        auth_node = self._xml_doc.createElement("info")
        auth_node.setAttribute("name", "auth")

        if self._infos.get("auth") is not None:
            auth_dict = self._infos["auth"]
            is_logged_in = "true" if auth_dict["logged_in"] is True else "false"

            auth_method_node = self._xml_doc.createElement("method")
            auth_method_node.appendChild(
                self._xml_doc.createTextNode(auth_dict["method"]))
            auth_node.appendChild(auth_method_node)
            auth_url_node = self._xml_doc.createElement("url")
            auth_url_node.appendChild(
                self._xml_doc.createTextNode(auth_dict["url"]))
            auth_node.appendChild(auth_url_node)
            auth_logged_in_node = self._xml_doc.createElement("logged_in")
            auth_logged_in_node.appendChild(
                self._xml_doc.createTextNode(is_logged_in))
            auth_node.appendChild(auth_logged_in_node)

            form_node = self._xml_doc.createElement("form")
            if auth_dict.get("form") is not None and len(
                    auth_dict["form"]) > 0:
                auth_form_dict = auth_dict["form"]

                form_login_field_node = self._xml_doc.createElement(
                    "login_field")
                form_login_field_node.appendChild(
                    self._xml_doc.createTextNode(
                        auth_form_dict["login_field"]))
                form_node.appendChild(form_login_field_node)
                form_password_field_node = self._xml_doc.createElement(
                    "password_field")
                form_password_field_node.appendChild(
                    self._xml_doc.createTextNode(
                        auth_form_dict["password_field"]))
                form_node.appendChild(form_password_field_node)
                auth_node.appendChild(form_node)
            else:
                form_node.setAttributeNS(
                    "http://www.w3.org/2001/XMLSchema-instance", "xsi:nil",
                    "true")
        else:
            auth_node.setAttributeNS(
                "http://www.w3.org/2001/XMLSchema-instance", "xsi:nil", "true")
        report_infos.appendChild(auth_node)
        return report_infos
示例#47
0
    def writeXacro(self):

        # Create document
        doc = Document()

        # Create comment
        comment = doc.createComment(
            'WARNING: This file was auto-generated by csv2xacro_node.py. It should not be edited by hand.'
        )
        doc.appendChild(comment)

        # Create root label
        root = doc.createElement('robot')
        root.setAttribute('name', 'duckietown')
        root.setAttribute('xmlns:xacro', 'http://www.ros.org/wiki/xacro')
        doc.appendChild(root)

        # Create Parameters comment
        comment = doc.createComment('Parameters')
        root.appendChild(comment)

        # Create tile width
        tempChild = doc.createElement('xacro:property')
        tempChild.setAttribute('name', 'tile_width')
        tempChild.setAttribute('value', str(self.tile_width))
        root.appendChild(tempChild)

        # Create tag offsets
        tempChild = doc.createElement('xacro:property')
        tempChild.setAttribute('name', 'pos_0')
        tempChild.setAttribute(
            'value', '%s %s' % (str(self.tag_offset), str(self.tag_curb)))
        root.appendChild(tempChild)

        tempChild = doc.createElement('xacro:property')
        tempChild.setAttribute('name', 'pos_1')
        tempChild.setAttribute(
            'value', '%s %s' % (str(self.tag_curb), str(self.tag_offset)))
        root.appendChild(tempChild)

        tempChild = doc.createElement('xacro:property')
        tempChild.setAttribute('name', 'pos_2')
        tempChild.setAttribute(
            'value', '%s %s' % (str(-self.tag_curb), str(self.tag_offset)))
        root.appendChild(tempChild)

        tempChild = doc.createElement('xacro:property')
        tempChild.setAttribute('name', 'pos_3')
        tempChild.setAttribute(
            'value', '%s %s' % (str(-self.tag_offset), str(self.tag_curb)))
        root.appendChild(tempChild)

        tempChild = doc.createElement('xacro:property')
        tempChild.setAttribute('name', 'pos_4')
        tempChild.setAttribute(
            'value', '%s %s' % (str(-self.tag_offset), str(-self.tag_curb)))
        root.appendChild(tempChild)

        tempChild = doc.createElement('xacro:property')
        tempChild.setAttribute('name', 'pos_5')
        tempChild.setAttribute(
            'value', '%s %s' % (str(-self.tag_curb), str(-self.tag_offset)))
        root.appendChild(tempChild)

        tempChild = doc.createElement('xacro:property')
        tempChild.setAttribute('name', 'pos_6')
        tempChild.setAttribute(
            'value', '%s %s' % (str(self.tag_curb), str(-self.tag_offset)))
        root.appendChild(tempChild)

        tempChild = doc.createElement('xacro:property')
        tempChild.setAttribute('name', 'pos_7')
        tempChild.setAttribute(
            'value', '%s %s' % (str(self.tag_offset), str(-self.tag_curb)))
        root.appendChild(tempChild)

        # Create comment
        comment = doc.createComment('Include the tile and tag macros')
        root.appendChild(comment)

        # Create file name
        tempChild = doc.createElement('xacro:include')
        tempChild.setAttribute(
            'filename',
            '$(find duckietown_description)/urdf/macros.urdf.xacro')
        root.appendChild(tempChild)

        # Create comment
        comment = doc.createComment(
            'The world frame is at the lower left corner of duckietown')
        root.appendChild(comment)

        # Create link
        tempChild = doc.createElement('link')
        tempChild.setAttribute('name', 'world')
        root.appendChild(tempChild)

        # Create comment
        comment = doc.createComment('Describe the tiles')
        root.appendChild(comment)

        # Create tiles
        header = True
        x_max = 0
        y_max = 0
        for row in self.tile_csv:
            if header or row[0].isspace():
                header = False
                continue
            x = row[0].strip('" ')
            if int(x) > x_max:
                x_max = int(x)
            y = row[1].strip('" ')
            if int(y) > y_max:
                y_max = int(y)
            tempChild = doc.createElement('xacro:tile')
            tempChild.setAttribute('x', row[0].strip('" '))
            tempChild.setAttribute('y', row[1].strip('" '))
            tempChild.setAttribute('type', row[2].strip('" '))
            tempChild.setAttribute('rotation', row[3].strip('" '))
            root.appendChild(tempChild)

        # Check that the map has the right number of tiles
        if not ((x_max + 1) *
                (y_max + 1) == root.getElementsByTagName('xacro:tile').length):
            raise Exception("Map should be exactly rectangular.")

        # Create comment
        comment = doc.createComment('Empty border tiles')
        root.appendChild(comment)

        # Create empty border tiles
        for x in range(x_max + 2):
            tempChild = doc.createElement('xacro:tile')
            tempChild.setAttribute('x', str(x))
            tempChild.setAttribute('y', str(y_max + 1))
            tempChild.setAttribute('visible', 'false')
            root.appendChild(tempChild)

        for y in range(y_max + 1):
            tempChild = doc.createElement('xacro:tile')
            tempChild.setAttribute('x', str(x_max + 1))
            tempChild.setAttribute('y', str(y))
            tempChild.setAttribute('visible', 'false')
            root.appendChild(tempChild)

        # Create comment
        comment = doc.createComment('Describe the tags')
        root.appendChild(comment)

        # Create tags
        header = True
        for row in self.tag_csv:
            if header or row[0].isspace():
                header = False
                continue
            tempChild = doc.createElement('xacro:tag')
            tempChild.setAttribute('id', row[0].strip('" '))
            tempChild.setAttribute('x', row[1].strip('" '))
            tempChild.setAttribute('y', row[2].strip('" '))
            tempChild.setAttribute('pos', "${pos_" + row[3].strip('" ') + "}")
            tempChild.setAttribute('rotation', row[4].strip('" '))
            root.appendChild(tempChild)

        # Write to file
        doc.writexml(self.map_xml, indent='    ', addindent='    ', newl='\n')

        # Close file
        self.map_xml.close()
        self.tile_csv_file.close()
        self.tag_csv_file.close()
示例#48
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
from xml.dom.minidom import Document

encoding = "utf-8"

if __name__ == "__main__":
    doc = Document()
    root = doc.createElement("root")
    doc.appendChild(root)
    startdir = sys.argv[1]
    rootdir = os.path.dirname(startdir)
    for fpathe, dirs, files in os.walk(startdir):
        for f in files:
            if f.lower().endswith(".plist"):
                element = doc.createElement("item")
                element.setAttribute(
                    "name", os.path.relpath(os.path.join(fpathe, f), rootdir))
                root.appendChild(element)

    file = open(sys.argv[2], "wb")
    doc.writexml(file, addindent='\t', newl='\n')
    file.close()
示例#49
0
    if field_attr.getAttribute("IsKeyword") == "1":
        CurrentSelect.setAttribute(field_attr.tagName, "")

# create data element
CutChartData = doc.createElement('CutChartData')
CutChart.appendChild(CutChartData)
for row in range(data_start_row - 1, cut_chart_sheet.nrows):
    record = doc.createElement("Record")
    CutChartData.appendChild(record)
    for name in field_name:
        cell_value = cut_chart_sheet.cell_value(row, column[name])
        if data_type[name] == "TEXT":
            record.setAttribute(name, "%s" % cell_value)
        elif data_type[name] == "FLOAT":
            record.setAttribute(
                name, display_format[name] %
                (float(cell_value) * scale[name] + offset[name]))
        elif data_type[name] == "INT":
            record.setAttribute(
                name, "%d" % (int(cell_value) * scale[name] + offset[name]))
        elif data_type[name] == "BOOL":
            record.setAttribute(name, "%s" % cell_value)

xml_file = codecs.open(sys.argv[2], 'w', 'utf-8')
doc.writexml(xml_file,
             indent='',
             newl='\r\n',
             addindent='  ',
             encoding='utf-8')
xml_file.close()
示例#50
0
                        objectPOINT_Y = doc.createElement("LocationY")
                        objectPOINT_Ytext = doc.createTextNode(str(row[2]))
                        objectPOINT_Y.appendChild(objectPOINT_Ytext)
                        objectE.appendChild(objectPOINT_Y)

                        objectLocationZ = doc.createElement("LocationZ")
                        objectLocationZtext = doc.createTextNode(str(row[3]))
                        objectLocationZ.appendChild(objectLocationZtext)
                        objectE.appendChild(objectLocationZ)

                        objectMatrix3 = doc.createElement("Matrix3")
                        objectMatrix3text = doc.createTextNode(str(row[4]))
                        objectMatrix3.appendChild(objectMatrix3text)
                        objectE.appendChild(objectMatrix3)

                        ModelPointClass.appendChild(objectE)

                    f = codecs.open(newXmlPath, 'w', 'gbk')
                    doc.writexml(f,
                                 indent='\t',
                                 newl='\n',
                                 addindent='\t',
                                 encoding='gb2312')
                    f.close()
                    arcpy.AddMessage(newXmlPath + ' finish')
    arcpy.AddMessage('all the files complete!!!')

except arcpy.ExecuteError:
    arcpy.GetMessages()
示例#51
0
        for k, v in self.children.items():
            child = doc.createElement(k)
            node.appendChild(child)
            v.toXml(child)

root = Trie()
with open('lexicon.txt', 'r') as f:
    for line in f:
        if len(line) == 0:
            continue
        line = line.split(' ')
        word = line[0]
        cnt = int(line[1])
        cur = root
        cur.cnt_total += cnt
        for ch in word:
            if ch not in cur.children:
                cur.children[ch] = Trie()
            cur = cur.children[ch]
            cur.cnt_total += cnt
        cur.isWord = True
        cur.cnt_as_word += cnt
root.setProb(root.cnt_total)
root.validate()
doc = Document()
root_xml = doc.createElement('root')
doc.appendChild(root_xml)
root.toXml(root_xml)
with open(os.path.join('..', 'tree.xml'),'w',encoding='UTF-8') as fh:
    doc.writexml(fh, indent='', addindent='\t', newl='\n', encoding='UTF-8')
示例#52
0
def main():
    
    keepBeforeRecord=False
    restartEmul=True
    reinstallApp=True
    test=False    ####for debug
    paral=False
    train=False   ####half and half
    #model.save("keras_mode.h5")
    
    
    #port='5554'
    #cmd="/home/yu/adt-bundle-linux-x86_64-20140702/sdk/platform-tools/adb -s emulator-"+port+" logcat -d"
    #logText=commands.getstatusoutput(cmd)
    #################
    
    batch_size = 30
    init_step=5
    ############inital
    apkList=[]
    
    
    for folderName in os.listdir("/SPACE/reforce-project/dataset/train/unfinished"):
        
            sourceCodePath="/SPACE/reforce-project/dataset/train/unfinished/"+folderName+"/bin/"
            source="/SPACE/reforce-project/dataset/train/unfinished/"+folderName
            destination="/SPACE/reforce-project/dataset/train/finished/"+folderName
            
            apkFile,coverageEm,packageName,activityName=ENV.compileApkEmma(sourceCodePath, commands, os)
        
            apkList.append((apkFile,coverageEm,packageName,activityName,sourceCodePath, source, destination))  
    
    
    port='5554'
    #d = Device('emulator-'+port)
    #time.sleep(4)
    address='.'
    '''
    packageName="bander.notepad"
    activityName="bander.notepad.NoteList"
    apkName="notepad_debug.apk"
    '''
    ##############agent
    
    

    
    ##############
    apkCount=0
    
    #agent = DQNAgent.DQNAgentClass( wordEmbeddings, neighborLen)############need to modification

    matrixDict={}

    
    for apkItem in apkList:
        if not train:#test
            shutil.copyfile("./model/qDict-half.pkl", "./model/qDict.pkl")
        
        
        crashID=0
        eventNum=[0]
        
        
        importantButton=[]
        
        
        for iteTime in range(0,3):
            countReward=0
            #psutil.virtual_memory()
            '''
            outputFile = open("./testRecords/trainRecord"+str(iteTime)+".txt", 'a')
            outputFile.write("memory: "+str(psutil.virtual_memory())+"\n")
            outputFile.write("#####################"+"\n")
            outputFile.close()
            '''
            gc.collect()
            '''
            outputFile = open("./testRecords/trainRecord"+str(iteTime)+".txt", 'a')
            outputFile.write("memory: "+str(psutil.virtual_memory())+"\n")
            outputFile.write("#####################"+"\n")
            #outputFile.write(str(objgraph.show_growth())+"\n")
            

            outputFile.close()
            '''
                        
            agent = DQNAgent.DQNAgentClass(matrixDict)############need to modification

            
        
            recordMiddleList=[]
            #simiarMap={}
            
        
        
        
            agent.memory={}###### brent's suggestion
            ############################3
            
            
            resultId=0
            lastResultId=-1000#### check the same page
            ############generate a record root in every apk
            recordDoc=Document()
            recordRoot=recordDoc.createElement("recdrodRoot")
            
            
            ###test
            if keepBeforeRecord:
                doc=minidom.parse(address+'/middleResults/record.xml')
                recordRoot=doc.documentElement
            ###test end
            
            doc_write =Document()
            doc_write.appendChild(recordRoot)  
            with codecs.open(address+"/middleResults/record.xml","wb","utf-8") as out:
                doc_write.writexml(out)
            out.close()
            
            if restartEmul:
                ENV.restartAndroidEumlator(port,commands) #        for the text close
            
            '''
            outputFile = open("./testRecords/trainRecord"+str(iteTime)+".txt", 'a')
            outputFile.write("memory: "+str(psutil.virtual_memory())+"\n")
            outputFile.write("#####################"+"\n")
            outputFile.close()
            '''
            
            cmd="/home/yu/adt-bundle-linux-x86_64-20140702/sdk/platform-tools/adb shell dumpsys window displays | grep 'init'"
            tupleLine=commands.getstatusoutput(cmd)
            while(True):
            
                try:
                
                    heightAndWeight=tupleLine[1].strip().split(" ")[0].split("=")[1]
                    height=int(heightAndWeight.split("x")[1])
                    weight=int(heightAndWeight.split("x")[0])
                    
                    
                    break
                except:
                    
                    print("waitForEmu")
                    continue
            
            
            ####################install apk
            
            
            
            apkName=apkItem[0]
            coverageEm=apkItem[1]
            packageName=apkItem[2]
            activityName=apkItem[3]
            sourceCodePath=apkItem[4]
            source=apkItem[5]
            destination=apkItem[6]
            
            recordNameCrash=source.rsplit("/",1)[1]

        
            d = Device('emulator-'+port)
            if reinstallApp:
                cmd="/home/yu/adt-bundle-linux-x86_64-20140702/sdk/platform-tools/adb -s emulator-"+port+" uninstall "+packageName
                commands.getstatusoutput(cmd) 
            
                cmd="/home/yu/adt-bundle-linux-x86_64-20140702/sdk/platform-tools/adb -s emulator-"+port+" install "+apkName
                commands.getstatusoutput(cmd) 
            
        
            specChar=False
            
            actionIndex=None
            feature=None
            lastFeature=None
            lastActionIndex=None
            appCloseTag=False
            lastFeatureTuple=None
            crashTag=False
            logHistory=""
            
            
            ######################################coverage data
            lineCoverage=0
            methodCoverage=0
            classCoverage=0
            
            coverageData=[lineCoverage,methodCoverage,classCoverage]#class is not the activity coverage
            
            
            ####################clean sdcard
            ENV.cleanSd(commands, port)
            
            
            start = time.time()
            
            apkCount+=1
            
            lastEventInRecord=None
            lastSimilarEleList=[]
            restartTag=False
            
            finishTag=[1]
            
            crashedIntentStr="init"
            
            coverageRecord=[]
            classNameSet=set()
            
            actionIndex=None
            
            for iterCount in range(10000):
                              
                print("eventNum")
                print(eventNum[0])
                
                
                coverageRecord.append(coverageData[0])
                
                
                
                print(matrixDict)
                #menuTag=False
                #adb -s emulator-5556 shell rm -r sdcard/*
                print("apkCount:"+str(apkCount))
                
                
                ####################3every apk only train 2 hours
                end=time.time()
                if end-start>1200:#300:#1200:
                    break
                
                
                print("iterCount:"+str(iterCount))
                print("currentReward"+str(countReward))
                
                if not iterCount==0:#it is the first time
                    ##################clean
                    cmd="/home/yu/adt-bundle-linux-x86_64-20140702/sdk/platform-tools/adb -s emulator-"+port+" logcat -c"
                    commands.getstatusoutput(cmd)
                    #ENV.step(actionIndex, d, feature, commands, address, port, apkName, packageName, activityName,specChar, eventNum)
                    '''
                    try:
                    '''
                    #run
                    '''
                    if acitonIndex=="contextual":
                        print("bingo")
                    '''
                    
                    if not actionIndex=="contextual":                        
                        ENV.step(actionIndex, d, feature, commands, address, port, apkName, packageName, activityName,specChar, eventNum, height,weight)
                    else:
                        cmd="python /home/yu/workspace2/rf-android/trigger/tester.py -s emulator-5554 -f "+apkName+" -p broadcast " + "-c "+crashedIntentStr
                        intent=commands.getstatusoutput(cmd)
                        eventNum[0]=eventNum[0]+1
                    '''
                    except:
                        d.press.back()
                        eventNum[0]=eventNum[0]+1
                        print("except to click back")
                    '''
                    #send a random intent
                    
                    #################check crash
                    cmd="/home/yu/adt-bundle-linux-x86_64-20140702/sdk/platform-tools/adb -s emulator-"+port+" logcat -d"
                    logText=commands.getstatusoutput(cmd)
                    logText=logText[1]
                    
                    if "FATAL" in logText or "fatal" in logText:
                        '''
                        if intentIter and selectedStr:
                           crashedIntentStr+=selectedStr+"***"
                        '''
                    
                    
                        #logText=
                        crashID+=1
                        ENV.writeCrash(logText, recordNameCrash ,crashID, eventNum)
                    
                        logBoolean,newLogAdd, specOut=ENV.checkBefore(logText,logHistory)
                        if specOut==True and specChar==False:
                            specChar=True
                        if not logBoolean:
                        
                            crashTag=True
                            #ENV.writeCrash(logText,iterCount)
                            logHistory+=newLogAdd
                            eventNum[0]=eventNum[0]+1
                            className=ENV.restartApp(port,commands, packageName, activityName)
                            print("catch a crash and restart app")
                            
                        else:
                            crashTag=False
                            className=ENV.restartApp(port,commands, packageName, activityName)
                            eventNum[0]=eventNum[0]+1
                            print("catch a crash and restart app")
                    else:
                        crashTag=False
                    
                
                
                
                cmd="/home/yu/adt-bundle-linux-x86_64-20140702/sdk/platform-tools/adb -s emulator-"+port+" shell dumpsys window windows | grep -E 'mFocusedApp'"
                tupleLine=commands.getstatusoutput(cmd)
                className=str(tupleLine).split(" ")[-2]
                
                if not packageName in tupleLine[1] and not "com.android" in tupleLine[1] or "com.android.launcher" in tupleLine[1]:

                    appCloseTag=True
                    className=ENV.restartApp(port,commands, packageName, activityName)
                    restartTag=True
                    eventNum[0]=eventNum[0]+1

                else:
                    appCloseTag=False
                    
                
                root_Result, notEmpty, loading=ENV.dumpTrans(d, address, minidom, os, Trans, packageName, className, commands, port)
                            
                
                if notEmpty==False:
                    if loading==1:
                        for iterTimes in range(0,20):
                            if notEmpty==False:####may be dump at a middle page
                                time.sleep(0.4)
                                root_Result, notEmpty, loading=ENV.dumpTrans(d, address, minidom, os, Trans, packageName, className, commands, port)
                                print("dumped a empty and loading page, so repeat dump"+str(iterTimes))
                            else:
                                break
                    else:                            
                        #permitOtherApp="anypackageisok"
                        #otherRoot_Result, notEmpty, loading=ENV.dumpTrans(d, address, minidom, os, Trans, permitOtherApp, className, commands, port)
                        
                        
                        #ENV.expOtherApp(d,height,weight)####just random find one to click
                        #eventNum[0]=eventNum[0]+10
                        for iterTimes in range(0,5):
                            
                            cmd="/home/yu/adt-bundle-linux-x86_64-20140702/sdk/platform-tools/adb -s emulator-"+port+" shell dumpsys window windows | grep -E 'mFocusedApp'"
                            tupleLine=commands.getstatusoutput(cmd)
                            
                            if not packageName in tupleLine[1]:
                                print(iterTimes)
                                d.press.back()
                                d.wait.update()
                                time.sleep(0.5)
                                eventNum[0]=eventNum[0]+1
                            else:
                                className=str(tupleLine).split(" ")[-2]
                                break
                            
                            if iterTimes==4:
                                print("restart")
                                className=ENV.restartApp(port,commands, packageName, activityName)
                                eventNum[0]=eventNum[0]+1

                                restartTag=True
                                break
                        root_Result, notEmpty, loading=ENV.dumpTrans(d, address, minidom, os, Trans, packageName, className, commands, port)
    
                ##########################remove
                os.remove(address+'/middleResults/result.xml')                
                
                #######################compare similarity
                sameEvent, similarEventList, sameResultListMiddle,similarResultListMiddle=Similar.getSimilar(recordRoot,root_Result)
                
                ######################extract feature
                feature=Feature.FeatureClass(root_Result,sameResultListMiddle, similarResultListMiddle, sameEvent, similarEventList)
                
                
                
                #featureStepNum=len(feature.runableList)
                stepNum=feature.wholeNum##remove restart menu back
                
                if stepNum<=1:
                    icstState="<1"
                elif stepNum<=3:
                    icstState="<3"
                elif stepNum<=8:
                    icstState="<8"
                elif stepNum<=15:
                    icstState="<15"
                else:
                    icstState=">15"    
                
                
                featureTuple=icstState#########generate feature for machine learning input
                
                ######################run model
                #actionIndex, timeCost=agent.actMatrix(featureTuple, iterCount, test)
                actionIndex, timeCost=agent.act(featureTuple, iterCount, test)
                #actionIndex=np.argmax(action)
                #actionIndex=3
                print("actionIndex: "+str(actionIndex))
                
                '''
                if actionIndex=="contextual":
                    print("bigo")
                '''
                ##########################recordRoot
                
                
                ########################add page ID
                resultId+=1
                
                ##################run
                #className=ENV.step(actionIndex, d, feature, commands, address, port)
                
                #lastEventInRecord, lastSimilarEleList, lastResultId, transferChanged, resultEventRecord=Similar.addToRecordRoot(recordRoot, root_Result,resultId,feature,featureStepNum,actionIndex,sameEvent, similarEventList, lastEventInRecord, restartTag, lastSimilarEleList)
                ############################record into file
                '''
                doc_write =Document()
                doc_write.appendChild(recordRoot)  
                with codecs.open(address+"/middleResults/record.xml","wb","utf-8") as out:
                    doc_write.writexml(out)
                out.close()
                '''
                ##################memorize and update model, this part can be implemented parallel with run
                if iterCount==0:#it is the first time
                    lastFeature=feature
                    lastActionIndex=actionIndex 
                    
                else:
                    ####################updateLast FeatureTuple
                    
                    ENV.computeRewardCoverage(commands, port, sourceCodePath, coverageData)
                    cmd="/home/yu/adt-bundle-linux-x86_64-20140702/sdk/platform-tools/adb -s emulator-"+port+" shell dumpsys window windows | grep -E 'mFocusedApp'"
                    tupleLine=commands.getstatusoutput(cmd)
                    className=str(tupleLine).split(" ")[-2]
                    
                    if not className in classNameSet:
                        classNameSet.add(className)
                        reward=1
                    else:
                        reward=0
                    
                    countReward+=reward
                    
                    #agent.memorize(lastFeatureTuple, lastActionIndex, reward, featureTuple)
                        
                        
                    testLastTuple=(lastFeatureTuple, lastActionIndex, reward, featureTuple)
                    #################train DQN

                    if iterCount > 0:
                        agent.replay(testLastTuple)
                                                
                        
                        
                    
                lastFeature=feature
                lastFeatureTuple=featureTuple
                lastActionIndex=actionIndex
                #lastResultEventRecord=resultEventRecord
                #lastFeatureStepNum=featureStepNum

            
            #np.save("./model/memory.npy",agent.memory)
            #pickle.dump( agent.memory, open( "./model/memory.p", "wb" ) )
            # favorite_color = pickle.load( open( "save.p", "rb" ) )
            
            agent.save("./model/qDict.pkl")
            
            recordName=source.rsplit("/",1)[1]
            shutil.move("./emmaOutput.txt", "./trainResultICST/"+recordName+"_coverage_"+str(iteTime)+".txt")
            shutil.move("./middleResults/record.xml", "./trainResultICST/"+recordName+"_record_"+str(iteTime)+".xml")
            
            shutil.move("./coverage.ec", "./trainResultICST/"+recordName+"_emma_"+str(iteTime)+".ec")

            
            outputFile = open("./experimentRecord.txt", 'a')
            outputFile.write("\n")
            outputFile.write("coverage:"+str(coverageData[0])+"\n")
            outputFile.write("testCoverage:"+str(countReward)+"\n")
            outputFile.write("iterate:"+str(iteTime)+"\n")
            outputFile.close()
            #for covData in coverageRecord:
            
            outputFile = open("/SPACE/reforce-project/dataset/train/resultICST/"+recordName+"_coverage_"+str(iteTime)+".txt", 'a')
            
            for covData in coverageRecord:
                outputFile.write(str(covData)+"\n")
            outputFile.close()  
            
            
            outputFile = open("./trainResultICST/"+recordName+"_eventNum_"+str(iteTime)+".txt", 'a')
            outputFile.write(str(str(eventNum[0]))+"\n")
            outputFile.close()
            
            
            agent.save("/SPACE/reforce-project/dataset/train/resultICST/"+recordName+"_model_"+str(iteTime)+".pkl")            
            #agent.clean()
            
            
            #del agent
            #gc.collect()
            
        dest = shutil.move(source, destination)#  for the test
示例#53
0
def put_msndata2(client, cfg):
    data = get_bzd(client, "hioki", cfg, 1, "putbzd2")
    if data is None:
        return

    doc = Document()
    env = doc.createElement("env:Envelope")
    doc.appendChild(env)
    env.setAttribute("xmlns:env",
                     "http://pdps.in.audi.vwg/legacy_schema/20.7.3/envelope")
    env.setAttribute(
        "xmlns:com",
        "http://pdps.in.audi.vwg/legacy_schema/20.7.3-2018.01/common")
    env.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
    env.setAttribute(
        "xsi:schemaLocation",
        "http://pdps.in.audi.vwg/legacy_schema/20.7.3-2018.01/envelope envelope.xsd"
    )

    header = doc.createElement("Header")
    env.appendChild(header)

    sender = doc.createElement("Sender")
    header.appendChild(sender)

    location = doc.createElement("Location")
    sender.appendChild(location)

    sender_assembly_cycle = get_item(data, u"assembly_cycle")
    location.setAttribute("assembly_cycle", sender_assembly_cycle)
    location.setAttribute("assembly_line", "MES Battery Assembly Line")
    location.setAttribute("assembly_subline", "1")
    location.setAttribute("plant", "C4")
    location.setAttribute("plant_segment", "Battery")

    device = doc.createElement("Device")
    sender.appendChild(device)
    sender_hostname = get_item(data, u"hostname")
    device.setAttribute("hostname", sender_hostname)
    sender_manufacturer = get_item(data, u"manufacturer")
    device.setAttribute("manufacturer", sender_manufacturer)
    sender_type = get_item(data, u"type")
    device.setAttribute("type", sender_type)
    sender_opmode = get_item(data, u"opmode")
    device.setAttribute("opmode", sender_opmode)

    telegraminfo = doc.createElement("Telegraminfo")
    header.appendChild(telegraminfo)
    sender_timestamp = get_item(data, u"opmode")
    telegraminfo.setAttribute("timestamp", sender_timestamp)
    sender_datatype = get_item(data, u"opmode")
    telegraminfo.setAttribute("datatype", sender_datatype)

    communicationtype = doc.createElement("Communicationtype")
    header.appendChild(communicationtype)
    communicationtype.setAttribute("type", "SEND_DATA")

    body = doc.createElement("Body")
    env.appendChild(body)

    vechicledata = doc.createElement("VehicleData")
    body.appendChild(vechicledata)

    cid = get_item(data, u"psn")
    sss = get_bodyident(cid)
    print(sss)
    bodyident = doc.createElement("BodyIdent")
    vechicledata.appendChild(bodyident)

    bodyident.setAttribute("plant", get_item(data, "plant"))
    bodyident.setAttribute("checkdigit", get_item(data, "checkdigit"))
    bodyident.setAttribute("productionyear", get_item(data, "productionyear"))
    bodyident.setAttribute("id", get_item(data, "bodyidentid"))

    datadata = doc.createElement("Data")
    body.appendChild(datadata)

    moduledata = doc.createElement("ModuleData")
    datadata.appendChild(moduledata)
    sta = get_item(data, u"esn")
    for i in range(20):
        if i == 0:
            modul = doc.createElement("Module")
            moduledata.appendChild(modul)
            modul.setAttribute("status", "AV")
            modul.setAttribute("code", "KGG")
            msn = get_item(data, "psn")
            if msn is None:
                print("msn is None! msn:", msn)
                cfg["putbzd2"] = get_item(data, u"time")
                return
            if len(msn) > 1:
                modul.setAttribute("serialnumber", msn)
            else:
                print("msn is empty! msn:", msn)
                cfg["putbzd2"] = get_item(data, u"time")
                return
        else:
            result = get_bzd(client, "hioki", cfg, 0, "putbzd2")
            if result is None:
                return
            stat = get_item(result, u"esn")
            if sta == stat:
                msn = get_item(data, "psn")
                if len(msn) < 1:
                    break
                modul = doc.createElement("Module")
                moduledata.appendChild(modul)
                modul.setAttribute("status", "AV")
                modul.setAttribute("code", "KGG")
                if len(msn) > 1:
                    modul.setAttribute("serialnumber", msn)
                    cfg["putbzd2"] = get_item(result, u"time")
                else:
                    print("msn is empty! msn:", msn)
                    cfg["putbzd2"] = get_item(result, u"time")
                    break
            else:
                break

    xml = open("msn2.xml", "w")
    doc.writexml(xml, indent='\t', newl='\n', addindent='\t', encoding='gbk')
    xml.close()
    with open("msn2.xml", "rt") as xml:
        line = xml.read()
        print(line)
示例#54
0
def main():
    #文件夹路径
    folderpath = 'C:\\Users\\Administrator.SKY-20120726UJY\\Desktop\\1 20 unclear\\inside'

    for (root, dirs, files) in os.walk(folderpath):
        for file in files:
            filetype = file.split(".")
            #读取每一个xml文件
            if filetype[1] == 'csv':

                #对每个文件涂色
                filepath = os.path.join(root, file)
                #print(filepath)
                try:
                    data_frame = pd.read_csv(filepath, encoding='utf-8')
                except:
                    data_frame = pd.read_csv(filepath, encoding='gbk')

                c = 0
                final_result = data_frame["标注结果"].tolist()
                material_id = data_frame["文件名称"].tolist()

                filepath = os.path.join(root, file)
                prop = None
                if "inside" in root:
                    prop = "inside"
                elif "outside" in root:
                    if "backview" in root:
                        prop = "outside_backview"
                    elif "sideview" in root:
                        prop = "outside_sideview"
                    else:
                        print("File Type Not Found")
                #根据xml文件名称选相应图片

                for mi, fi in zip(final_result, material_id):
                    mi = json.loads(mi)
                    fi = str(fi)
                    point_list = []

                    doc = Document()
                    dataRoot = doc.createElement("xml_file")
                    doc.appendChild(dataRoot)
                    unpack(fi, mi, dataRoot, doc)
                    xmlpath = root[:-3] + "csv\\" + str(fi) + '.xml'
                    xmlFile = open(xmlpath, 'w', encoding='utf-8')
                    doc.writexml(xmlFile,
                                 indent='',
                                 addindent='\t',
                                 newl='\n',
                                 encoding='utf-8')
                    #xmlFile.write(doc.toprettyxml(indent = '\t'))
                    xmlFile.close()
                    c = c + 1
                    try:
                        photopath = root[:-3] + "JPEGImages\\" + str(
                            fi) + ".jpg"
                    except:
                        photopath = root[:-3] + "JPEGImages\\" + str(
                            fi) + ".png"

                    #进行涂色
                    print(photopath)
                    print(xmlpath)
                    tuse(xmlpath, photopath, prop)
def writeDocument(sourcePath,targetPath,xmlFileName):

    if sourcePath == None or targetPath == None:
        return False
    ## Warn user if capabilities are not correct, exit if not valid layers
    errs = False
    if dlaService.validateSourceUrl(sourcePath) == False:
        dla.addError("Errors in Source Service Capabilities, exiting without writing the output file")
        errs = True
    if dlaService.validateTargetUrl(targetPath) == False:
        dla.addError("Errors in Target Service Capabilities, exiting without writing the output file")
        errs = True
    try:
        desc = arcpy.Describe(sourcePath)
    except:
        dla.addError("Unable to Describe the source dataset, exiting")
        errs = True
    try:
        descT = arcpy.Describe(targetPath)
    except:
        dla.addError("Unable to Describe the target dataset, exiting")
        errs = True

    if errs:
        return False

    xmlDoc = Document()
    root = xmlDoc.createElement('SourceTargetMatrix')
    xmlDoc.appendChild(root)
    root.setAttribute("version",'1.1')
    root.setAttribute("xmlns:esri",'http://www.esri.com')
    
    dataset = xmlDoc.createElement("Datasets")
    root.appendChild(dataset)
    prj = dla.getProject()
    if prj == None:
        prj = ''
    else:
        prj = prj.filePath
    setSourceTarget(dataset,xmlDoc,"Project",dla.dropXmlFolder(xmlFileName,prj))
    setSourceTarget(dataset,xmlDoc,"Source",dla.dropXmlFolder(xmlFileName,sourcePath))
    setSourceTarget(dataset,xmlDoc,"Target",dla.dropXmlFolder(xmlFileName,targetPath))

    setSpatialReference(dataset,xmlDoc,desc,"Source")
    setSpatialReference(dataset,xmlDoc,descT,"Target")

    setSourceTarget(dataset,xmlDoc,"ReplaceBy","")

    fieldroot = xmlDoc.createElement("Fields")
    root.appendChild(fieldroot)

    fields = getFields(descT)
    sourceFields = getFields(desc)
    #sourceNames = [field.name[field.name.rfind(".")+1:] for field in sourceFields] ***
    sourceNames = [field.name for field in sourceFields]
    upperNames = [nm.upper() for nm in sourceNames]

    #try:
    for field in fields:

        fNode = xmlDoc.createElement("Field")
        fieldroot.appendChild(fNode)
        fieldName = field.name #[field.name.rfind(".")+1:] ***
        matchSourceFields(xmlDoc,fNode,field,fieldName,sourceNames,upperNames)

    # write the source field values
    setSourceFields(root,xmlDoc,sourceFields)
    setTargetFields(root,xmlDoc,fields)

    # add data to the document
    if len(sourceNames) > 0:
        writeDataSample(xmlDoc,root,sourceNames,sourcePath,10)
    # write it out
    xmlDoc.writexml( open(xmlFileName, 'wt', encoding='utf-8'),indent="  ",addindent="  ",newl='\n')
    xmlDoc.unlink()
    return True
示例#56
0
def gen_car_and_carplate_independently(dic):
    img_name = dic['img_name']
    height = dic['height']
    width = dic['width']
    channel = dic['channel']
    xmin = dic['xmin']
    ymin = dic['ymin']
    xmax = dic['xmax']
    ymax = dic['ymax']
    carplate_x_top_left = dic['carplate_x_top_left']
    carplate_x_top_right = dic['carplate_x_top_right']
    carplate_x_bottom_right = dic['carplate_x_bottom_right']
    carplate_x_bottom_left = dic['carplate_x_bottom_left']
    carplate_y_top_left = dic['carplate_y_top_left']
    carplate_y_top_right = dic['carplate_y_top_right']
    carplate_y_bottom_right = dic['carplate_y_bottom_right']
    carplate_y_bottom_left = dic['carplate_y_bottom_left']

    # 创建dom文档
    doc = Document()
    # 创建根节点
    annotation = doc.createElement('annotation')
    # 根节点插入dom树
    doc.appendChild(annotation)
    # folder
    folder = doc.createElement('folder')
    annotation.appendChild(folder)
    folder.appendChild(doc.createTextNode('Cars'))
    # filename
    filename = doc.createElement('filename')
    annotation.appendChild(filename)
    filename.appendChild(doc.createTextNode(img_name))
    # source
    source = doc.createElement('source')
    annotation.appendChild(source)
    database_ = doc.createElement('database')
    database_.appendChild(doc.createTextNode('Cars car carplate'))
    source.appendChild(database_)
    # 创建size节点
    size = doc.createElement('size')
    annotation.appendChild(size)
    width_ = doc.createElement('width')
    width_.appendChild(doc.createTextNode(str(width)))
    height_ = doc.createElement('height')
    height_.appendChild(doc.createTextNode(str(height)))
    depth_ = doc.createElement('depth')
    depth_.appendChild(doc.createTextNode(str(channel)))
    size.appendChild(width_)
    size.appendChild(height_)
    size.appendChild(depth_)
    # segmentation
    segmented = doc.createElement('segmented')
    annotation.appendChild(segmented)
    segmented.appendChild(doc.createTextNode(str(0)))

    if need_car:
        # 限制不超过图片上下界
        car_xmin = ex.limit_in_bounds(xmin, 1, width)
        car_ymin = ex.limit_in_bounds(ymin, 1, height)
        car_xmax = ex.limit_in_bounds(xmax, 1, width)
        car_ymax = ex.limit_in_bounds(ymax, 1, height)

        # car
        object = doc.createElement('object')
        annotation.appendChild(object)
        name = doc.createElement('name')
        name.appendChild(doc.createTextNode('car'))
        object.appendChild(name)
        # pose
        pose_ = doc.createElement('pose')
        pose_.appendChild(doc.createTextNode('Unspecified'))
        object.appendChild(pose_)
        # truncated
        truncated_ = doc.createElement('truncated')
        truncated_.appendChild(doc.createTextNode(str(0)))
        object.appendChild(truncated_)
        # difficult
        difficult_ = doc.createElement('difficult')
        difficult_.appendChild(doc.createTextNode(str(0)))
        object.appendChild(difficult_)
        # the bndbox
        bndbox = doc.createElement('bndbox')
        object.appendChild(bndbox)
        xmin_ = doc.createElement('xmin')
        xmin_.appendChild(doc.createTextNode(str(car_xmin)))
        bndbox.appendChild(xmin_)
        ymin_ = doc.createElement('ymin')
        ymin_.appendChild(doc.createTextNode(str(car_ymin)))
        bndbox.appendChild(ymin_)
        xmax_ = doc.createElement('xmax')
        xmax_.appendChild(doc.createTextNode(str(car_xmax)))
        bndbox.appendChild(xmax_)
        ymax_ = doc.createElement('ymax')
        ymax_.appendChild(doc.createTextNode(str(car_ymax)))
        bndbox.appendChild(ymax_)

    if need_carplate:
        # 限制不超过图片上下界
        carplate_x_top_left = ex.limit_in_bounds(carplate_x_top_left, 1, width)
        carplate_y_top_left = ex.limit_in_bounds(carplate_y_top_left, 1,
                                                 height)
        carplate_x_top_right = ex.limit_in_bounds(carplate_x_top_right, 1,
                                                  width)
        carplate_y_top_right = ex.limit_in_bounds(carplate_y_top_right, 1,
                                                  height)
        carplate_x_bottom_right = ex.limit_in_bounds(carplate_x_bottom_right,
                                                     1, width)
        carplate_y_bottom_right = ex.limit_in_bounds(carplate_y_bottom_right,
                                                     1, height)
        carplate_x_bottom_left = ex.limit_in_bounds(carplate_x_bottom_left, 1,
                                                    width)
        carplate_y_bottom_left = ex.limit_in_bounds(carplate_y_bottom_left, 1,
                                                    height)

        carplate_xmin = min(carplate_x_top_left, carplate_x_bottom_left)
        carplate_ymin = min(carplate_y_top_left, carplate_y_top_right)
        carplate_xmax = max(carplate_x_bottom_right, carplate_x_top_right)
        carplate_ymax = max(carplate_y_bottom_right, carplate_y_bottom_left)

        # carplate
        object = doc.createElement('object')
        annotation.appendChild(object)
        name = doc.createElement('name')
        name.appendChild(doc.createTextNode("carplate"))
        object.appendChild(name)
        # pose
        pose_ = doc.createElement('pose')
        pose_.appendChild(doc.createTextNode('Unspecified'))
        object.appendChild(pose_)
        # truncated
        truncated_ = doc.createElement('truncated')
        truncated_.appendChild(doc.createTextNode(str(0)))
        object.appendChild(truncated_)
        # difficult
        difficult_ = doc.createElement('difficult')
        difficult_.appendChild(doc.createTextNode(str(0)))
        object.appendChild(difficult_)
        # the bndbox
        bndbox = doc.createElement('bndbox')
        object.appendChild(bndbox)
        xmin_ = doc.createElement('xmin')
        xmin_.appendChild(doc.createTextNode(str(carplate_xmin)))
        bndbox.appendChild(xmin_)
        ymin_ = doc.createElement('ymin')
        ymin_.appendChild(doc.createTextNode(str(carplate_ymin)))
        bndbox.appendChild(ymin_)
        xmax_ = doc.createElement('xmax')
        xmax_.appendChild(doc.createTextNode(str(carplate_xmax)))
        bndbox.appendChild(xmax_)
        ymax_ = doc.createElement('ymax')
        ymax_.appendChild(doc.createTextNode(str(carplate_ymax)))
        bndbox.appendChild(ymax_)

        x_top_left_ = doc.createElement('x_top_left')
        x_top_left_.appendChild(doc.createTextNode(str(carplate_x_top_left)))
        bndbox.appendChild(x_top_left_)
        y_top_left_ = doc.createElement('y_top_left')
        y_top_left_.appendChild(doc.createTextNode(str(carplate_y_top_left)))
        bndbox.appendChild(y_top_left_)
        x_top_right_ = doc.createElement('x_top_right')
        x_top_right_.appendChild(doc.createTextNode(str(carplate_x_top_right)))
        bndbox.appendChild(x_top_right_)
        y_top_right_ = doc.createElement('y_top_right')
        y_top_right_.appendChild(doc.createTextNode(str(carplate_y_top_right)))
        bndbox.appendChild(y_top_right_)
        x_bottom_right_ = doc.createElement('x_bottom_right')
        x_bottom_right_.appendChild(
            doc.createTextNode(str(carplate_x_bottom_right)))
        bndbox.appendChild(x_bottom_right_)
        y_bottom_right_ = doc.createElement('y_bottom_right')
        y_bottom_right_.appendChild(
            doc.createTextNode(str(carplate_y_bottom_right)))
        bndbox.appendChild(y_bottom_right_)
        x_bottom_left_ = doc.createElement('x_bottom_left')
        x_bottom_left_.appendChild(
            doc.createTextNode(str(carplate_x_bottom_left)))
        bndbox.appendChild(x_bottom_left_)
        y_bottom_left_ = doc.createElement('y_bottom_left')
        y_bottom_left_.appendChild(
            doc.createTextNode(str(carplate_y_bottom_left)))
        bndbox.appendChild(y_bottom_left_)

    fp = open(
        os.path.join(target_dir,
                     str(dic['img_index']).zfill(5) + '.xml'), 'w')
    doc.writexml(fp, indent='\t', addindent='\t', newl='\n', encoding="utf-8")
示例#57
0
                servicepackText = doc.createTextNode(servicePack)
                servicepackElement.appendChild(servicepackText)

    # Get the XML old file content to a local variable
    mssqlXml = open(paths.MSSQL_XML, "r")
    oldMssqlXml = mssqlXml.read()
    oldMssqlXmlSignatures = oldMssqlXml.count("<signature>")
    oldMssqlXmlList = oldMssqlXml.splitlines(1)
    mssqlXml.close()

    # Backup the XML old file
    shutil.copy(paths.MSSQL_XML, "%s.bak" % paths.MSSQL_XML)

    # Save our newly created XML to the signatures file
    mssqlXml = open(paths.MSSQL_XML, "w")
    doc.writexml(writer=mssqlXml, addindent="    ", newl="\n")
    mssqlXml.close()

    # Get the XML new file content to a local variable
    mssqlXml = open(paths.MSSQL_XML, "r")
    newMssqlXml = mssqlXml.read()
    newMssqlXmlSignatures = newMssqlXml.count("<signature>")
    newMssqlXmlList = newMssqlXml.splitlines(1)
    mssqlXml.close()

    # If the new XML versions file differs from the old one it probably
    # means that we have got new Microsoft SQL Server versions
    if oldMssqlXmlSignatures != newMssqlXmlSignatures:
        infoMsg = "Microsoft SQL Server XML versions file updated successfully. "

        if oldMssqlXmlSignatures < newMssqlXmlSignatures:
示例#58
0
    def main(self):
        json = self.get_page()
        results = self.parse_page(json)
        os.system('cls')
        print(json.get('data')['updateInfo'])
        x_line = '-' * 155
        print(
            f"今日总票房: {json.get('data')['totalBox']} {json.get('data')['totalBoxUnit']}",
            end=f'\n{x_line}\n')
        print('电影名称',
              '综合票房(万)',
              '票房占比',
              '场均上座率',
              '场均人次',
              '平均票价',
              '排片场次',
              '排片占比',
              '累积总票房',
              '上映天数',
              sep='\t',
              end=f'\n{x_line}\n')
        res = ""
        boxDict = []
        for result in results:
            print(result['movieName'][:7].ljust(8),
                  result['boxInfo'][:8].rjust(8),
                  result['boxRate'][:8].rjust(8),
                  result['avgSeatView'][:8].rjust(8),
                  result['avgShowView'][:8].rjust(8),
                  result['avgViewBox'][:8].rjust(8),
                  result['showInfo'][:8].rjust(8),
                  result['showRate'][:8].rjust(8),
                  result['sumBoxInfo'][:8].rjust(8),
                  result['releaseInfo'][:8],
                  sep='\t',
                  end='\n\n')

            res = '{"movieName":"' + result['movieName'] + '","boxInfo":"' + result[
                'boxInfo'] + '","boxRate":"' + result[
                    'boxRate'] + '","avgSeatView":"' + result[
                        'avgSeatView'] + '","avgShowView":"' + result[
                            'avgShowView'] + '","avgViewBox":"' + result[
                                'avgViewBox'] + '","showInfo":"' + result[
                                    'showInfo'] + '","showRate":"' + result[
                                        'showRate'] + '","sumBoxInfo":"' + result[
                                            'sumBoxInfo'] + '","releaseInfo":"' + result[
                                                'releaseInfo'] + '"}'
            #print(res)

            ress = {}
            ress['movieName'] = result['movieName']
            ress['boxInfo'] = result['boxInfo']
            ress['boxRate'] = result['boxRate']
            ress['avgSeatView'] = result['avgSeatView']
            ress['avgShowView'] = result['avgShowView']
            ress['avgViewBox'] = result['avgViewBox']
            ress['showInfo'] = result['showInfo']
            ress['showRate'] = result['showRate']
            ress['sumBoxInfo'] = result['sumBoxInfo']
            ress['releaseInfo'] = result['releaseInfo']
            #ress = {'movieName': result['movieName'], 'boxInfo': result['boxInfo'], 'boxRate': result['boxRate'], 'avgSeatView': result['avgSeatView'], 'avgShowView': result['avgShowView'], 'avgViewBox': result['avgViewBox'], 'showInfo': result['showInfo'], 'showRate': result['showRate'], 'sumBoxInfo': result['sumBoxInfo'], 'releaseInfo': result['releaseInfo']}
            #print(ress)
            #writeInfoToXml(ress, 'E:\\大三\\3\\应用集成原理与工具\\Pythontest1\\box.xml')
            boxDict.append(ress)

            # 创建dom文档
            doc = Document()

            # 创建根节点
            boxlist = doc.createElement('boxlist')
            # 根节点插入dom树
            doc.appendChild(boxlist)

            # 依次将box中的每一组元素提取出来,创建对应节点并插入dom树
            for v in boxDict:
                # 分离
                (movieName, boxInfo, boxRate, avgSeatView, avgShowView,
                 avgViewBox, showInfo, showRate, sumBoxInfo,
                 releaseInfo) = (v['movieName'], v['boxInfo'], v['boxRate'],
                                 v['avgSeatView'], v['avgShowView'],
                                 v['avgViewBox'], v['showInfo'], v['showRate'],
                                 v['sumBoxInfo'], v['releaseInfo'])
                # (index, image, title, actor, time, score) = (v[0], v[1], v[2], v[3], v[4], v[5])
                boxes = doc.createElement('boxes')
                boxlist.appendChild(boxes)

                # 将排名插入<boxes>中
                # 创建节点<movieName>
                movieNamee = doc.createElement('movieName')
                # 创建<index>下的文本节点
                movieName_text = doc.createTextNode(movieName)
                # 将文本节点插入到<index>下
                movieNamee.appendChild(movieName_text)
                # 将<index>插入到父节点<scores>下
                boxes.appendChild(movieNamee)

                # 将boxInfo插入<boxes>中,处理同上
                boxInfoo = doc.createElement('boxInfo')
                boxInfo_text = doc.createTextNode(boxInfo)
                boxInfoo.appendChild(boxInfo_text)
                boxes.appendChild(boxInfoo)

                boxRatee = doc.createElement('boxRate')
                boxRate_text = doc.createTextNode(boxRate)
                boxRatee.appendChild(boxRate_text)
                boxes.appendChild(boxRatee)

                avgSeatVieww = doc.createElement('avgSeatView')
                avgSeatView_text = doc.createTextNode(avgSeatView)
                avgSeatVieww.appendChild(avgSeatView_text)
                boxes.appendChild(avgSeatVieww)

                avgShowVieww = doc.createElement('avgShowView')
                avgShowView_text = doc.createTextNode(avgShowView)
                avgShowVieww.appendChild(avgShowView_text)
                boxes.appendChild(avgShowVieww)

                avgViewBoxx = doc.createElement('avgViewBox')
                avgViewBox_text = doc.createTextNode(avgViewBox)
                avgViewBoxx.appendChild(avgViewBox_text)
                boxes.appendChild(avgViewBoxx)

                showInfoo = doc.createElement('showInfo')
                showInfo_text = doc.createTextNode(showInfo)
                showInfoo.appendChild(showInfo_text)
                boxes.appendChild(showInfoo)

                showRatee = doc.createElement('showRate')
                showRate_text = doc.createTextNode(showRate)
                showRatee.appendChild(showRate_text)
                boxes.appendChild(showRatee)

                sumBoxInfoo = doc.createElement('avgShowView')
                sumBoxInfo_text = doc.createTextNode(sumBoxInfo)
                sumBoxInfoo.appendChild(sumBoxInfo_text)
                boxes.appendChild(sumBoxInfoo)

                releaseInfoo = doc.createElement('avgViewBox')
                releaseInfo_text = doc.createTextNode(releaseInfo)
                releaseInfoo.appendChild(releaseInfo_text)
                boxes.appendChild(releaseInfoo)

            try:
                with open('box.xml', 'w', encoding='UTF-8') as fh:
                    doc.writexml(fh,
                                 indent='',
                                 addindent='\t',
                                 newl='\n',
                                 encoding='UTF-8')
                    print('写入')
            except Exception as err:
                print('错误信息:{0}'.format(err))
示例#59
0
                workPathTemp = workPath + cols[0]
                shutil.rmtree(workPathTemp, True)
                os.mkdir(workPathTemp)
            else:
                workPathTemp = workPath
            print rowIndex
            for index in range(1, N):
                if (verbose):
                    print "\t%s = %s" % (header[index], cols[index])
                parameter = doc.createElement("parameter")
                # can acces two ways,  by attribute or ID (both have use the same string
                parameter.setAttribute("name", header[index])
                parameter.setAttribute("id", str(header[index]))
                parameter.setIdAttribute("id")
                parameter.setAttribute("value", cols[index])
                top.appendChild(parameter)
            if (individualTestDir == True):
                f = open(workPathTemp + "//parameters.xml", 'w')
                fExe.write(executableName + " " + workPathTemp +
                           " parameters.xml\n")
            else:
                f = open(workPathTemp + "//" + cols[0] + ".xml", 'w')
                fExe.write(executableName + " " + workPathTemp + " " +
                           cols[0] + ".xml\n")
            doc.writexml(f, addindent="\t", newl="\n")
            f.close()

        rowIndex = rowIndex + 1

    fExe.close()
    # Write Text
    nodeText = doc.createTextNode(value[1])
    tempChild.appendChild(nodeText)

    # Create Element
    subjectRegisted = doc.createElement('subject-registed')
    studentinfo.appendChild(subjectRegisted)

    # Create Element
    tempChild = doc.createElement('subject-name')
    subjectRegisted.appendChild(tempChild)

    # Write Text
    nodeText = doc.createTextNode(value[2])
    tempChild.appendChild(nodeText)

    # Create Element
    tempChild = doc.createElement('subject-ID')
    subjectRegisted.appendChild(tempChild)

    # Write Text
    nodeText = doc.createTextNode(value[3])
    tempChild.appendChild(nodeText)

doc.writexml(open('output.xml', 'w'), indent="  ", addindent=" ", newl='\n')

doc.unlink()

print 'Write file complete!'