コード例 #1
0
def export_robot_element(element):
    """
    Browse the 'elements' list of robot instance
    and export the ones related to the keyword 'element' given as a parameter

    :param : element, string in ['Transmission','Gazebo','material']

    The output file is <ROBOT>_<element>.xacro
    """
    global robot, OUTPUT
    doc = Document()
    root = doc.createElement("robot")
    doc.appendChild(root)
    root.setAttribute("xmlns:xacro","http://www.ros.org/wiki/xacro")
    for i in robot.elements:
        try:
            if element == 'Transmission':
                if i.name.find(element) != -1:
                    root.appendChild(i.to_xml(doc))
            elif element == 'Gazebo':
                if i.reference != None:
                    root.appendChild(i.to_xml(doc))
                elif i.plugins != []:
                    root.appendChild(i.to_xml(doc))
            elif element == 'material':
                if type(i) == naoqi_tools.urdf.Material:
                    root.appendChild(i.to_xml(doc))
        except AttributeError:
            pass
    filename = OUTPUT[0:OUTPUT.rfind('.')] + '_' + str(element) + '.xacro'
    print('exporting ' + element + ' xacro')
    write_comments_in_xacro(doc, filename)
コード例 #2
0
 def generate_reports(self, test_runner):
     "Generates the XML reports to a given XMLTestRunner object."
     from xml.dom.minidom import Document
     all_results = self._get_info_by_testcase()
     
     if isinstance(test_runner.output, basestring) and not \
         os.path.exists(test_runner.output):
         os.makedirs(test_runner.output)
     
     for suite, tests in all_results.items():
         doc = Document()
         
         # Build the XML file
         testsuite = _XMLTestResult._report_testsuite(suite, tests, doc)
         for test in tests:
             _XMLTestResult._report_testcase(suite, test, testsuite, doc)
         _XMLTestResult._report_output(test_runner, testsuite, doc)
         xml_content = doc.toprettyxml(indent='\t', encoding="utf-8")
         
         if type(test_runner.output) in (str, unicode):
             report_file = file('%s%sTEST-%s.xml' % \
                 (test_runner.output, os.sep, suite), 'w')
             try:
                 report_file.write(xml_content)
             finally:
                 report_file.close()
         else:
             # Assume that test_runner.output is a stream
             test_runner.output.write(xml_content)
コード例 #3
0
ファイル: feed.py プロジェクト: Distrotech/docutils
    def generate_document(self):
        doc = Document()
        Element = doc.createElement
        Text = doc.createTextNode

        rss = doc.appendChild(Element('rss'))
        rss.setAttribute('version', '2.0')

        channel = rss.appendChild(Element('channel'))
        for key in ('title', 'description', 'link'):
            value = getattr(self, key)
            channel.appendChild(Element(key)).appendChild(Text(value))
        date = format_rss_date(self._last_update or datetime.utcnow())
        channel.appendChild(Element('pubDate')).appendChild(Text(date))

        for item in self.items:
            d = Element('item')
            for key in ('title', 'author', 'link', 'description'):
                d.appendChild(Element(key)).appendChild(Text(item[key]))
            pub_date = format_rss_date(item['pub_date'])
            d.appendChild(Element('pubDate')).appendChild(Text(pub_date))
            d.appendChild(Element('guid')).appendChild(Text(item['link']))
            channel.appendChild(d)

        return doc
コード例 #4
0
ファイル: xunit.py プロジェクト: EIChaoYang/avocado
 def _render(self, result):
     document = Document()
     testsuite = document.createElement('testsuite')
     testsuite.setAttribute('name', 'avocado')
     testsuite.setAttribute('tests', self._escape_attr(result.tests_total))
     testsuite.setAttribute('errors', self._escape_attr(result.errors + result.interrupted))
     testsuite.setAttribute('failures', self._escape_attr(result.failed))
     testsuite.setAttribute('skipped', self._escape_attr(result.skipped + result.cancelled))
     testsuite.setAttribute('time', self._escape_attr(result.tests_total_time))
     testsuite.setAttribute('timestamp', self._escape_attr(datetime.datetime.now()))
     document.appendChild(testsuite)
     for test in result.tests:
         testcase = self._create_testcase_element(document, test)
         status = test.get('status', 'ERROR')
         if status in ('PASS', 'WARN'):
             pass
         elif status == 'SKIP':
             testcase.appendChild(Element('skipped'))
         elif status == 'FAIL':
             element, system_out = self._create_failure_or_error(document,
                                                                 test,
                                                                 'failure')
             testcase.appendChild(element)
             testcase.appendChild(system_out)
         elif status == 'CANCEL':
             testcase.appendChild(Element('skipped'))
         else:
             element, system_out = self._create_failure_or_error(document,
                                                                 test,
                                                                 'error')
             testcase.appendChild(element)
             testcase.appendChild(system_out)
         testsuite.appendChild(testcase)
     return document.toprettyxml(encoding='UTF-8')
コード例 #5
0
    def storeToFile(self, suffix=""):
        if not self.dataToStore:
            return

        doc = Document()
        results = doc.createElement("results")
        doc.appendChild(results)
        for a in self.dataToStore:
            item = doc.createElement("item")
            for k, d in a.items():
                tag = doc.createElement(k)
                if k == "author":
                    tag2 = doc.createElement("name")
                    data = doc.createTextNode(d)
                    tag2.appendChild(data)
                    tag.appendChild(tag2)
                else:
                    data = doc.createTextNode(" ".join(d.split()))
                    tag.appendChild(data)
                item.appendChild(tag)
            results.appendChild(item)

        timestamp = time.strftime("%H%M%S_")
        f = open("output/" + timestamp + self.board + "_" + self.section + "_" + str(suffix) + ".xml", "w")
        f.write(doc.toprettyxml(indent="    ", encoding="utf-8"))
        f.close()
        self.dataToStore = []
コード例 #6
0
ファイル: xml.py プロジェクト: disqus/mule
 def generate_reports(self, test_runner):
     "Generates the XML reports to a given XMLTestRunner object."
     from xml.dom.minidom import Document
     all_results = self._get_info_by_testcase()
     
     if type(test_runner.output) == str and not \
         os.path.exists(test_runner.output):
         os.makedirs(test_runner.output)
     
     for suite, tests in all_results.items():
         doc = Document()
         
         # Build the XML file
         testsuite = _XMLTestResult._report_testsuite(suite, tests, doc)
         for test in tests:
             _XMLTestResult._report_testcase(suite, test, testsuite, doc)
         _XMLTestResult._report_output(suite, tests, test_runner, testsuite, doc)
         xml_content = doc.toprettyxml(indent='\t')
         
         if type(test_runner.output) is str:
             report_file = open(os.path.join(test_runner.output, '%s.xml' % (suite,)), 'w')
             try:
                 report_file.write(xml_content)
             finally:
                 report_file.close()
         else:
             # Assume that test_runner.output is a stream
             test_runner.output.write(xml_content)
コード例 #7
0
	def send_server_status(self, status):
		doc = Document()
		rootNode = doc.createElement('server')
		rootNode.setAttribute("status", status)
		doc.appendChild(rootNode)
		response = self.send_packet("/server/status", doc)
		if response is False:
			Logger.warn("SMRequest::send_server_status Unable to send packet")
			return False
		
		document = self.get_response_xml(response)
		if document is None:
			Logger.warn("SMRequest::send_server_status response not XML")
			return False
		
		rootNode = document.documentElement
		
		if rootNode.nodeName != "server":
			Logger.error("SMRequest::send_server_status response not valid %s"%(rootNode.toxml()))
			return False
		
		if not rootNode.hasAttribute("name") or rootNode.getAttribute("name") != self.name:
			Logger.error("SMRequest::send_server_status response invalid name")
			return False
		
		if not rootNode.hasAttribute("status") or rootNode.getAttribute("status") != status:
			Logger.error("SMRequest::send_server_status response invalid status")
			return False
		
		return True
コード例 #8
0
    def testTaxes7(self):
        doc = Document()
        parent_node = doc.createElement('parent_node')
        doc.appendChild(parent_node)
        data = {
                'default-tax-table': {
                        'tax-rules': [
                            {
                                'shipping-taxed': True,
                                'rate': 0.175,
                                'tax-area': {
                                    'postal-area': [
                                        {'country-code': 'DE'},
                                        {'country-code': 'ES'},
                                        {'country-code': 'GB'},
                                    ],
                                 },
                            },
                        ]
                    },
        }
        self.gc._taxes(doc, parent_node, data)
        xml1 = "<parent_node><tax-tables>\
<default-tax-table><tax-rules><default-tax-rule>\
<shipping-taxed>true</shipping-taxed><rate>0.175</rate>\
<tax-areas><postal-area><country-code>DE</country-code>\
</postal-area><postal-area><country-code>ES</country-code>\
</postal-area><postal-area><country-code>GB</country-code>\
</postal-area></tax-areas></default-tax-rule></tax-rules>\
</default-tax-table></tax-tables></parent_node>"
        doc_good = parseString(xml1)
        self.assertEquals(doc.toxml(), doc_good.toxml())
コード例 #9
0
    def testShippingExclude(self):
        doc = Document()
        parent_node = doc.createElement('parent_node')
        doc.appendChild(parent_node)
        data = {
            'us-state-area': ['AK','HI'],
            'us-zip-area': ['90210', '04005', '04092'],
            'us-country-area': 'CONTINENTAL_48',
            'world-area': True,
            'postal-area': [{
                'country-code': 'US',
                'postal-code-pattern': ['94043', '90211'],
                },
            ],
        }
        self.gc._shipping_allowed_excluded(doc, parent_node, data)
        xml1 = "<parent_node><us-state-area><state>AK</state></us-state-area>\
<us-state-area><state>HI</state></us-state-area>\
<us-zip-area><zip-pattern>90210</zip-pattern></us-zip-area>\
<us-zip-area><zip-pattern>04005</zip-pattern></us-zip-area>\
<us-zip-area><zip-pattern>04092</zip-pattern></us-zip-area>\
<us-country-area country-area='CONTINENTAL_48'/>\
<world-area/>\
<postal-area><country-code>US</country-code>\
<postal-code-pattern>94043</postal-code-pattern>\
<postal-code-pattern>90211</postal-code-pattern></postal-area>\
</parent_node>"
        doc_good = parseString(xml1)
        self.assertEquals(doc.toxml(), doc_good.toxml())
コード例 #10
0
    def testTaxes2(self):
        doc = Document()
        parent_node = doc.createElement('parent_node')
        doc.appendChild(parent_node)
        data = {
                'default-tax-table': {
                    'tax-rules': [
                            {
                                'shipping-taxed': True,
                                'rate': 0.06,
                                'tax-area': {
                                    'us-state-area': ['CT'],
                                 }
                            },
                            {
                                'rate': 0.05,
                                'tax-area': {
                                    'us-state-area': ['MD'],
                                 }
                            }
                        ]
                    }
        }
        self.gc._taxes(doc, parent_node, data)
        xml1 = "<parent_node><tax-tables><default-tax-table><tax-rules>\
<default-tax-rule><shipping-taxed>true</shipping-taxed><rate>0.06</rate>\
<tax-area><us-state-area><state>CT</state></us-state-area></tax-area>\
</default-tax-rule><default-tax-rule><shipping-taxed>false</shipping-taxed>\
<rate>0.05</rate><tax-area><us-state-area><state>MD</state></us-state-area>\
</tax-area></default-tax-rule></tax-rules></default-tax-table></tax-tables>\
</parent_node>"
        doc_good = parseString(xml1)
        self.assertEquals(doc.toxml(), doc_good.toxml())
コード例 #11
0
    def testTaxes3(self):
        doc = Document()
        parent_node = doc.createElement('parent_node')
        doc.appendChild(parent_node)
        data = {
                'default-tax-table': {
                        'tax-rules': [
                            {
                                'shipping-taxed': False,
                                'rate': 0.08375,
                                'tax-area': {
                                    'us-zip-area': ['100*'],
                                 }
                            },
                            {
                                'shipping-taxed': True,
                                'rate': 0.04,
                                'tax-area': {
                                    'us-state-area': ['NY'],
                                 }
                            }
                        ]
                    }
        }
        self.gc._taxes(doc, parent_node, data)
        xml1 = "<parent_node><tax-tables><default-tax-table>\
<tax-rules><default-tax-rule><shipping-taxed>false</shipping-taxed>\
<rate>0.08375</rate><tax-area><us-zip-area><zip-pattern>100*</zip-pattern>\
</us-zip-area></tax-area></default-tax-rule>\
<default-tax-rule><shipping-taxed>true</shipping-taxed>\
<rate>0.04</rate><tax-area><us-state-area><state>NY</state></us-state-area>\
</tax-area></default-tax-rule>\
</tax-rules></default-tax-table></tax-tables></parent_node>"
        doc_good = parseString(xml1)
        self.assertEquals(doc.toxml(), doc_good.toxml())
コード例 #12
0
ファイル: OvfLibvirt.py プロジェクト: Awingu/open-ovf
def parallalElement(deviceType, path, port):
    """
    Creates a character device element, a <parallel> element, as a child
    of L{device element<devicesElement>}, that specifies the parallel character
    device to be used.

    @param deviceType: device type
    @type deviceType: String

    @param path: source path
    @type path: String

    @param port: port number
    @type port: String

    @return: <parallel> element
    @rtype: DOM Element
    """
    document = Document()
    deviceElem = document.createElement('parallel')
    deviceElem.setAttribute('type', deviceType)
    portElem = document.createElement('source')
    portElem.setAttribute('path', path)
    portElem = document.createElement('target')
    portElem.setAttribute('port', port)
    deviceElem.appendChild(portElem)
    return deviceElem
コード例 #13
0
ファイル: converter.py プロジェクト: jordanorc/iaae
def questions_to_aiml(questions):
    punctuation = "\"`~!@#$%^&()-_=+[{]}\|;:',<.>/?"
    _puncStripRE = re.compile("[" + re.escape(punctuation) + "]")

    # Create the minidom document
    doc = Document()
    
    # Create the <aiml> base element
    aiml = doc.createElement("aiml")
    doc.appendChild(aiml)
    

    for question, answer in questions:        
        patterns = (re.sub(_puncStripRE, "", question), re.sub(_puncStripRE, "*", question))
        
        for p in patterns:
            category = doc.createElement("category") 
            pattern = doc.createElement("pattern") 
            pattern.appendChild(doc.createTextNode(p.upper()))  
            
            template = doc.createElement("template") 
            template.appendChild(doc.createTextNode(answer))
        
            category.appendChild(pattern)
            category.appendChild(template)
    
            aiml.appendChild(category)

    # Print our newly created XML
    return doc.toxml()
コード例 #14
0
ファイル: OvfLibvirt.py プロジェクト: Awingu/open-ovf
def graphicsElement(graphicsType, listen=None, port=None):
    """
    Creates a <graphics> element, a child of L{device element<devicesElement>},
    that specifies the graphics framebuffer device, either on the host or
    through a VNC server.

    @param graphicsType: frame buffer type: sdl or vnc
    @type graphicsType: String

    @param listen: IP address for VNC server
    @type listen: String

    @param port: port for VNC server
    @type port: String

    @return: <graphics> element
    @rtype: DOM Element
    """
    document = Document()
    elem = document.createElement('graphics')
    elem.setAttribute('type', graphicsType)
    if(listen != None):
        elem.setAttribute('listen', listen)
    if(port != None):
        elem.setAttribute('port', port)
    return elem
コード例 #15
0
ファイル: OvfLibvirt.py プロジェクト: Awingu/open-ovf
def consoleElement(deviceType, port):
    """
    Creates a character device element, a <console> element, as a child
    of L{device element<devicesElement>}, that specifies the console character
    device to be used. Examples from libvirt.org:

    Domain Logfile: This disables all input on the character device, and
    sends output into the virtual machine's logfile.
        - type: stdio
        - port: 1

    @param deviceType: device type
    @type deviceType: String

    @param port: port number
    @type port: String

    @return: <console> element
    @rtype: String
    """
    document = Document()
    deviceElem = document.createElement('console')
    deviceElem.setAttribute('type', deviceType)
    portElem = document.createElement('target')
    portElem.setAttribute('port', port)
    deviceElem.appendChild(portElem)
    return deviceElem
コード例 #16
0
ファイル: xmlrunner.py プロジェクト: FrancescAlted/cython
    def generate_reports(self, test_runner):
        "Generates the XML reports to a given XMLTestRunner object."
        from xml.dom.minidom import Document
        all_results = self._get_info_by_testcase()

        if type(test_runner.output) == str and not \
            os.path.exists(test_runner.output):
            os.makedirs(test_runner.output)

        for suite, tests in all_results.items():
            doc = Document()

            # Build the XML file
            testsuite = _XMLTestResult._report_testsuite(suite, tests, doc)
            stdout, stderr = [], []
            for test in tests:
                _XMLTestResult._report_testcase(suite, test, testsuite, doc)
                if test.stdout:
                    stdout.extend(['*****************', test.get_description(), test.stdout])
                if test.stderr:
                    stderr.extend(['*****************', test.get_description(), test.stderr])
            _XMLTestResult._report_output(test_runner, testsuite, doc,
                                          '\n'.join(stdout), '\n'.join(stderr))
            xml_content = doc.toprettyxml(indent='\t')

            if type(test_runner.output) is str:
                report_file = open('%s%sTEST-%s.xml' % \
                    (test_runner.output, os.sep, suite), 'w')
                try:
                    report_file.write(xml_content)
                finally:
                    report_file.close()
            else:
                # Assume that test_runner.output is a stream
                test_runner.output.write(xml_content)
コード例 #17
0
ファイル: OvfLibvirt.py プロジェクト: Awingu/open-ovf
def devicesElement(*devices):
    """
    Creates a <devices> element, a child of L{domain element
    <domainElement>}, is the parent element for all devices.

    Devices:
    ========

    -L{<Emulator<emulatorElement>}
    -L{<Disk<diskElement>}
    -L{<Network<networkElement>}
    -L{<Input<inputElement>}
    -L{<Graphics<graphicsElement>}

    @param devices: tuple of Libvirt devices
    @type devices: DOM Element tuple

    @return: <devices> element
    @rtype: DOM Element
    """
    document = Document()
    deviceElem = document.createElement('devices')
    addDevice(deviceElem, devices)

    return deviceElem
コード例 #18
0
ファイル: xmlParser.py プロジェクト: noba3/KoTos
 def formXml(self):
     """Form the XML to be written to RssFeeds.xml"""
     #create the document
     doc = Document()
     #create root element
     rssfeedsTag = doc.createElement('rssfeeds')
     doc.appendChild(rssfeedsTag)
     #create comments
     c1Tag = doc.createComment('RSS feeds. To have multiple feeds, just add a feed to the set. You can also have multiple sets.')
     c2Tag = doc.createComment('To use different sets in your skin, each must be called from skin with a unique id.')
     rssfeedsTag.appendChild(c1Tag)
     rssfeedsTag.appendChild(c2Tag)
     for setNum in sorted(self.feedsList.keys()):
         #create sets
         setTag = doc.createElement('set')
         #create attributes
         setTag.setAttribute('id', self.feedsList[setNum]['attrs']['id'])
         #only write rtl tags if they've been explicitly set
         if 'rtl' in self.feedsList[setNum]['attrs']:
             setTag.setAttribute('rtl', self.feedsList[setNum]['attrs']['rtl'])
         rssfeedsTag.appendChild(setTag)
         #create feed elements
         for feed in self.feedsList[setNum]['feedslist']:
             feedTag = doc.createElement('feed')
             feedTag.setAttribute('updateinterval', feed['updateinterval'])
             feedUrl = doc.createTextNode(feed['url'])
             feedTag.appendChild(feedUrl)
             setTag.appendChild(feedTag)
     return doc.toprettyxml(indent = '  ', encoding = 'UTF-8')
コード例 #19
0
 def createGeneralUnknownResponse(self, message):
     doc = Document()
     root_response = doc.createElement('Response')
     root_response.setAttribute("creationSuccessful", str(False))
     root_response.setAttribute("reason", message)
     doc.appendChild(root_response)
     return doc.toxml(encoding='utf-8')
コード例 #20
0
def testAttributeRepr():
    dom = Document()
    el = dom.appendChild(dom.createElement(u"abc"))
    node = el.setAttribute("abc", "def")
    confirm(str(node) == repr(node))
    dom.unlink()
    confirm(len(Node.allnodes) == 0)
コード例 #21
0
ファイル: MusicXML.py プロジェクト: anzev/mingus
def _note2musicxml(note):
    doc = Document()
    note_node = doc.createElement("note")
    if note==None:
        #note is a rest
        rest = doc.createElement("rest")
        note_node.appendChild(rest)
    else:
        #add pitch info
        pitch = doc.createElement("pitch")
        step = doc.createElement("step")
        step.appendChild(doc.createTextNode(note.name[:1]))
        pitch.appendChild(step)
        octave = doc.createElement("octave")
        octave.appendChild(doc.createTextNode(str(note.octave)))
        pitch.appendChild(octave)
        #check for alterations
        count = 0
        for i in note.name[1:]:
            if   i=="b": count-=1
            elif i=="#": count +=1
        if count != 0:
            alter = doc.createElement("alter")
            alter.appendChild(doc.createTextNode(str(count)))
            pitch.appendChild(alter)
            
        note_node.appendChild(pitch)

    return note_node
コード例 #22
0
ファイル: ovd-client.py プロジェクト: skdong/nfs-ovd
    def do_call_exit(self):
        if d.sessionProperties["persistent"]:
            mode = "suspend"
        else:
            mode = "logout"

        document = Document()
        rootNode = document.createElement("logout")
        rootNode.setAttribute("mode", mode)
        document.appendChild(rootNode)

        url = "%s/logout.php" % (self.base_url)
        request = urllib2.Request(url)
        request.add_header("Content-type", "text/xml; charset=UTF-8")
        request.add_data(document.toxml())

        try:
            url = self.urlOpener.open(request)

        except urllib2.HTTPError, exc:
            if exc.code == 500:
                Logger.warn("Service failurek")
                return False

            Logger.debug("HTTP request return code %d (%s)" % (exc.code, exc.msg))
            Logger.debug(" * return: %s" % (str(exc.read())))
            return False
コード例 #23
0
ファイル: testeml.py プロジェクト: ebegoli/EMLPy
def test_handle_repr_set(self):
    ''' '''
    emotionml = Document()
    repset_name = 'dimension-set'
    repset = Representation(name='fear', representation='dimension')
    emotionml = handle_repr_set(repset_name, repset, emotionml)
    print emotionml.toprettyxml()
    def __init__(self, mlist, addPic, featurelist):
		#def __init__(self, mlist):
		Document.__init__(self)
		self.mlist=mlist
		self.addPic=addPic
		self.featurelist=featurelist
		self.generateXML()
コード例 #25
0
	def __init__(self, data=None):
		Document.__init__(self)

		if data is not None:
			self.__dict__ = parseString(data).__dict__

		self.annotations = { }
コード例 #26
0
ファイル: generate_urdf.py プロジェクト: cottsay/romeo_robot
def exportXacroRobot(robot):
    global output
    global name
    doc = Document()
    root = doc.createElement("robot")
    doc.appendChild(root)
    root.setAttribute("xmlns:xacro","http://www.ros.org/wiki/xacro")
    root.setAttribute("name",robot.name)
    if name == 'nao':
        root.appendChild(ur.short(doc,'xacro:include','filename',output[output.rfind('/')+1:output.find('.')]+ 'Transmission.xacro'))
        root.appendChild(ur.short(doc,'xacro:include','filename',output[output.rfind('/')+1:output.find('.')]+ 'Gazebo.xacro'))    
        root.appendChild(ur.short(doc,'xacro:include','filename',output[output.rfind('/')+1:output.find('.')]+ 'hands.xacro'))
    includeVisualCollision(robot)
    for i in dicXacro.keys():
        print "exporting " +str(dicXacro[i]) + ' xacro' 
        exportXacroRobotChain(robot,i)
        if output.find('/') != -1:
            filenamerobot = output[output.rfind('/')+1:output.find('.')]+ i + '.xacro'
        else : 
            filenamerobot = output[0:output.find('.')]+ i + str('.xacro')
        root.appendChild(ur.short(doc,'xacro:include','filename',filenamerobot))
## Transmission elements not available from Aldebaran libraries yet
#    exportXacroRobotElementList(robot,'Transmission')
## Plugin not implemented yet : neither mimic joints, camera or ros_control
#    exportXacroRobotElementList(robot,'Gazebo')
    root.appendChild(ur.short(doc,'xacro:include','filename',output[output.rfind('/')+1:output.find('.')]+ 'sensors.xacro'))
    exportSensorsXacro(robot,output[output.rfind('/')+1:output.find('.')]+ 'sensors.xacro')   
    root.appendChild(ur.short(doc,'xacro:include','filename',output[output.rfind('/')+1:output.find('.')]+ 'visual_collisions.xacro'))

    filename = output[0:output.find('.')]+ 'robot' + str('.xacro')
    print filename
    writeCommentedXacro(doc,filename)
コード例 #27
0
ファイル: pythonbits.py プロジェクト: kylev/Pythonbits
class pythonbits_config:
	"""Class for holding pythonbits config strings. read() or create_dom() must be called before first use. Access strings through obj.strings[key]"""
	def __init__(self):
		self.strings={}
		self.file=tempdir()+"config.xml"
		if not os.path.exists(self.file):
			update_url = "https://github.com/Ichabond/Pythonbits/raw/master/config.xml"
			opener = _MyOpener()
			nconf = opener.open(update_url)
			if (nconf.info()["Status"]=="200 OK"):
				open(tempdir()+"config.xml", "w").write(nconf.read())
			else:
				__logerror("Cannot update config file.")
			
		
	def __del__(self):
		self.file.close()

	def read(self, file=0):
		if(file==0):
			file=self.file
		self.xml = parse(file)
		self.load_strings()

	def write(self, file=0):
		if(file==0):
			file=self.file
		location = self.file.name
		file.close()
		file = open(location, "w")
		file.write(self.xml.toprettyxml())
		file.close()
		self.file = open(location, "r")

	def set_location(self, location):
		try:
			self.file = open(location, "r")
		except IOError:
			self.file = open(location, "w")

	def load_strings(self):
		for node in self.xml.getElementsByTagName("string"):
			self.strings[node.getAttribute("name")]=node.firstChild.data.replace('\n','').replace('\t','')

	def add_string(self, name, data):
		container = self.xml.getElementsByTagName("pythonbits")[0]
		stringtag = self.xml.createElement("string")
		stringtag.setAttribute("name", name)
		stringtag.appendChild(self.xml.createTextNode(data))
		container.appendChild(stringtag)
		self.load_strings()
	def del_string(self, name):
		del self.strings[name]
		###Horrible hack. Write real code.
		self.create_dom()
		for (name, entry) in self.strings.items():
			self.add_string(name, entry)
	def create_dom(self):
		self.xml = Document()
		self.xml.appendChild(self.xml.createElement("pythonbits"))
コード例 #28
0
ファイル: __init__.py プロジェクト: ubuntunux/Blender
def createDoc(path):
    file = open(path, "w")
    doc = Document()
    wml = doc.createElement("data")
    doc.appendChild(wml)
    file.write(doc.toprettyxml(indent=" "))
    file.close()            
コード例 #29
0
ファイル: generate_urdf.py プロジェクト: cottsay/romeo_robot
def exportXacroRobotElementList(robot,element):
    global output
    doc = Document()
    root = doc.createElement("robot")
    doc.appendChild(root)
    root.setAttribute("xmlns:xacro","http://www.ros.org/wiki/xacro")
    print 'create element file'
    for i in robot.elements:
        try:
            if element == 'Transmission':
                if i.name.find(element) != -1:
                    root.appendChild(i.to_xml(doc))
            elif element == 'Gazebo':
                if i.reference != None:
                    root.appendChild(i.to_xml(doc))
                elif i.plugins != []:
                    root.appendChild(i.to_xml(doc))
            elif element == 'material':
                if type(i) == romeo_description.urdf.Material:
                    root.appendChild(i.to_xml(doc))
        except AttributeError:
            pass
    filename = output[0:output.find('.')]+ str(element) + '.xacro'
    print filename
    writeCommentedXacro(doc, filename)   
コード例 #30
0
def _testElementReprAndStrUnicode():
    dom = Document()
    el = dom.appendChild(dom.createElement(u"abc"))
    string1 = repr(el)
    string2 = str(el)
    confirm(string1 == string2)
    dom.unlink()
コード例 #31
0
ファイル: _xmlParser.py プロジェクト: vaginessa/ADEL
def sms_messages_to_xml(xml_dir, sms_list):
    _adel_log.log(
        "############  XML OUTPUT GENERATION -> SMS MESSAGES  ############ \n",
        2)
    # Create the minidom document
    doc = Document()
    xml = doc.createElement("SMS_Messages")
    doc.appendChild(xml)
    for i in range(0, len(sms_list)):
        # Create the <SMS_Message> element
        sms_message = doc.createElement("SMS_Message")
        xml.appendChild(sms_message)
        id = doc.createElement("id")
        sms_message.appendChild(id)
        id_text = doc.createTextNode(sms_list[i][0])
        id.appendChild(id_text)
        thread_id = doc.createElement("thread_id")
        sms_message.appendChild(thread_id)
        thread_id_text = doc.createTextNode(sms_list[i][1])
        thread_id.appendChild(thread_id_text)
        number = doc.createElement("number")
        sms_message.appendChild(number)
        number_text = doc.createTextNode(sms_list[i][2])
        number.appendChild(number_text)
        person = doc.createElement("person")
        sms_message.appendChild(person)
        person_text = doc.createTextNode(sms_list[i][3])
        person.appendChild(person_text)
        date = doc.createElement("date")
        sms_message.appendChild(date)
        date_text = doc.createTextNode(sms_list[i][4])
        date.appendChild(date_text)
        read = doc.createElement("read")
        sms_message.appendChild(read)
        read_text = doc.createTextNode(sms_list[i][5])
        read.appendChild(read_text)
        type = doc.createElement("type")
        sms_message.appendChild(type)
        type_text = doc.createTextNode(sms_list[i][6])
        type.appendChild(type_text)
        subject = doc.createElement("subject")
        sms_message.appendChild(subject)
        subject_text = doc.createTextNode(sms_list[i][7])
        subject.appendChild(subject_text)
        body = doc.createElement("body")
        sms_message.appendChild(body)
        body_text = doc.createTextNode(sms_list[i][8])
        body.appendChild(body_text)
    # Print our newly created XML files to Log
    _adel_log.log(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")), 3)
    # create xml file
    xml_sms_messages = open(xml_dir + "/sms_Messages.xml", "a+")
    xml_sms_messages.write(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")))
    xml_sms_messages.close()
    _adel_log.log("xmlParser:          ----> sms_Messages.xml created!", 4)
コード例 #32
0
ファイル: _xmlParser.py プロジェクト: vaginessa/ADEL
def twitter_to_xml(xml_dir, twitter_list, tweet_list):
    _adel_log.log(
        "############  XML OUTPUT GENERATION -> TWITTER ENTRIES  ############ \n",
        2)
    # Create the minidom document
    doc = Document()
    twitter_entries = doc.createElement("Twitter_Entries")
    doc.appendChild(twitter_entries)
    ## Entry generated is User_ID, User_Name, Real_Name, description, location (if given), profile_created, updated, followers, friends
    USER_ID = 0
    USER_NAME = 1
    REAL_NAME = 2
    DESCRIPTION = 3
    LOCATION = 4
    PROFILE_CREATED = 5
    UPDATED = 6
    FOLLOWERS = 7
    FRIENDS = 8

    for i in range(0, len(twitter_list)):
        if i == 0:
            user_entry_node = doc.createElement("Twitter_Account_Owner")
        else:
            user_entry_node = doc.createElement("User_Entry")
        twitter_entries.appendChild(user_entry_node)
        user_id_node = doc.createElement("User_id")
        user_entry_node.appendChild(user_id_node)
        user_id_node_text = doc.createTextNode(twitter_list[i][USER_ID])
        user_id_node.appendChild(user_id_node_text)
        user_name_node = doc.createElement("User_Name")
        user_entry_node.appendChild(user_name_node)
        user_name_node_text = doc.createTextNode(twitter_list[i][USER_NAME])
        user_name_node.appendChild(user_name_node_text)
        real_name_node = doc.createElement("Real_Name")
        user_entry_node.appendChild(real_name_node)
        real_name_node_text = doc.createTextNode(twitter_list[i][REAL_NAME])
        real_name_node.appendChild(real_name_node_text)
        description_node = doc.createElement("Description")
        user_entry_node.appendChild(description_node)
        description_node_text = doc.createTextNode(
            twitter_list[i][DESCRIPTION])
        description_node.appendChild(description_node_text)
        location_node = doc.createElement("Location")
        user_entry_node.appendChild(location_node)
        location_node_text = doc.createTextNode(twitter_list[i][LOCATION])
        location_node.appendChild(location_node_text)
        profile_created_node = doc.createElement("Profile_created")
        user_entry_node.appendChild(profile_created_node)
        profile_created_node_text = doc.createTextNode(
            twitter_list[i][PROFILE_CREATED])
        profile_created_node.appendChild(profile_created_node_text)
        updated_node = doc.createElement("Updated")
        user_entry_node.appendChild(updated_node)
        updated_note_text = doc.createTextNode(twitter_list[i][UPDATED])
        updated_node.appendChild(updated_note_text)
        followers_node = doc.createElement("Followers")
        user_entry_node.appendChild(followers_node)
        followers_node_text = doc.createTextNode(twitter_list[i][FOLLOWERS])
        followers_node.appendChild(followers_node_text)
        friends_node = doc.createElement("Friends")
        user_entry_node.appendChild(friends_node)
        friends_node_text = doc.createTextNode(twitter_list[i][FRIENDS])
        friends_node.appendChild(friends_node_text)
        user_id = int(twitter_list[i][USER_ID])
        #print user_id
        if user_id in tweet_list:
            tweets = tweet_list[user_id]
            tweets_node = doc.createElement("Tweets")
            user_entry_node.appendChild(tweets_node)
            for j in range(0, len(tweets)):
                tweet_node = doc.createElement("Tweet")
                tweets_node.appendChild(tweet_node)
                data_node = doc.createElement("Tweet_created")
                tweet_node.appendChild(data_node)
                data_node_text = doc.createTextNode(tweets[j][3])
                data_node.appendChild(data_node_text)
                message_node = doc.createElement("Message")
                tweet_node.appendChild(message_node)
                message_node_text = doc.createTextNode(tweets[j][0])
                message_node.appendChild(message_node_text)
                source_node = doc.createElement("Source")
                tweet_node.appendChild(source_node)
                source_node_text = doc.createTextNode(tweets[j][1])
                source_node.appendChild(source_node_text)
                source_url_node = doc.createElement("Source_Url")
                tweet_node.appendChild(source_url_node)
                source_url_node_text = doc.createTextNode(tweets[j][2])
                source_url_node.appendChild(source_url_node_text)
    # Print our newly created XML files to Log
    _adel_log.log(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")), 3)
    # Create xml file
    twitter_xml_name = "twitter_" + twitter_list[0][USER_ID] + ".xml"
    xml_twitter = open(xml_dir + "/" + twitter_xml_name, "a+")
    xml_twitter.write(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")))
    xml_twitter.close()
    _adel_log.log(
        "xmlParser:          ----> " + twitter_xml_name + " created!", 4)
コード例 #33
0
ファイル: _xmlParser.py プロジェクト: vaginessa/ADEL
def contacts_to_xml(xml_dir, contactsList):
    _adel_log.log(
        "############  XML OUTPUT GENERATION -> CONTACTS  ############ \n", 2)
    # Create the minidom document
    doc = Document()
    xml = doc.createElement("contacts")
    doc.appendChild(xml)
    for i in range(0, len(contactsList)):
        # Create the <contact> element
        contact = doc.createElement("contact")
        xml.appendChild(contact)
        id = doc.createElement("id")
        contact.appendChild(id)
        id_text = doc.createTextNode(contactsList[i][0])
        id.appendChild(id_text)
        photo_id = doc.createElement("photo_id")
        contact.appendChild(photo_id)
        photo_id_text = doc.createTextNode(contactsList[i][1])
        photo_id.appendChild(photo_id_text)
        times_contacted = doc.createElement("times_contacted")
        contact.appendChild(times_contacted)
        times_contacted_text = doc.createTextNode(contactsList[i][2])
        times_contacted.appendChild(times_contacted_text)
        last_time_contacted = doc.createElement("last_time_contacted")
        contact.appendChild(last_time_contacted)
        last_time_contacted_text = doc.createTextNode(contactsList[i][3])
        last_time_contacted.appendChild(last_time_contacted_text)
        starred = doc.createElement("starred")
        contact.appendChild(starred)
        starred_text = doc.createTextNode(contactsList[i][4])
        starred.appendChild(starred_text)
        number = doc.createElement("number")
        contact.appendChild(number)
        number_text = doc.createTextNode(contactsList[i][5])
        number.appendChild(number_text)
        display_name = doc.createElement("display_name")
        contact.appendChild(display_name)
        display_name_text = doc.createTextNode(contactsList[i][6])
        display_name.appendChild(display_name_text)
        lastname = doc.createElement("lastname")
        contact.appendChild(lastname)
        lastname_text = doc.createTextNode(contactsList[i][7])
        lastname.appendChild(lastname_text)
        firstname = doc.createElement("firstname")
        contact.appendChild(firstname)
        firstname_text = doc.createTextNode(contactsList[i][8])
        firstname.appendChild(firstname_text)
        company = doc.createElement("company")
        contact.appendChild(company)
        company_text = doc.createTextNode(contactsList[i][9])
        company.appendChild(company_text)
        email = doc.createElement("email")
        contact.appendChild(email)
        email_text = doc.createTextNode(contactsList[i][10])
        email.appendChild(email_text)
        url = doc.createElement("url")
        contact.appendChild(url)
        url_text = doc.createTextNode(contactsList[i][11])
        url.appendChild(url_text)
        address = doc.createElement("address")
        contact.appendChild(address)
        address_text = doc.createTextNode(contactsList[i][12])
        address.appendChild(address_text)
    # Print our newly created XML files to Log
    _adel_log.log(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")), 3)
    # create xml file
    xml_contacts = open(xml_dir + "/contacts.xml", "a+")
    xml_contacts.write(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")))
    xml_contacts.close()
    _adel_log.log("xmlParser:          ----> contacts.xml created!", 4)
コード例 #34
0
ファイル: _xmlParser.py プロジェクト: vaginessa/ADEL
def facebook_to_xml(xml_dir, UserDict, FriendsList, ConnList):
    _adel_log.log(
        "############  XML OUTPUT GENERATION -> FACEBOOK ENTRIES  ############ \n",
        2)
    # Create the minidom document
    doc = Document()
    fb_entries = doc.createElement("Facebook_Entries")
    doc.appendChild(fb_entries)

    # UserDict is a dictionary structure containing the FB_user informations. Following keys are available:
    # uid -> Facebook User ID
    # secret      \
    # access token \
    # session_key --> these three seem to be hash values for cryptographic purpose
    # first_name
    # last_name -> as given in the account
    # name -> screen name for friendslists (?)
    # username -> loginname for FB account
    # machine_id -> another hash to pinpoint used device (?)
    # pic_square -> Link to account picture
    # Entry generated for FriendsList is User_ID, Last_Name, First_Name,
    # Birthday, E-mail (if given)

    USER_ID = 0
    NAME = 1
    BIRTHDAY = 2
    E_MAIL = 3
    for i in range(0, len(FriendsList)):
        user_entry_node = doc.createElement("Friends_Entry")
        fb_entries.appendChild(user_entry_node)
        user_id_node = doc.createElement("Facebook_User_id")
        user_entry_node.appendChild(user_id_node)
        user_id_note_text = doc.createTextNode(FriendsList[i][USER_ID])
        user_id_node.appendChild(user_id_note_text)
        user_name_node = doc.createElement("Friends_Name")
        user_entry_node.appendChild(user_name_node)
        user_name_node_text = doc.createTextNode(FriendsList[i][NAME])
        user_name_node.appendChild(user_name_node_text)
        birthday_note = doc.createElement("Birthday")
        user_entry_node.appendChild(birthday_note)
        birthday_note_text = doc.createTextNode(FriendsList[i][BIRTHDAY])
        birthday_note.appendChild(birthday_note_text)
        email_node = doc.createElement("E-Mail_Adress")
        user_entry_node.appendChild(email_node)
        email_node_text = doc.createTextNode(FriendsList[i][E_MAIL])
        email_node.appendChild(email_node_text)
        """
        user_id = int(TwitterList[i][USER_ID])
        #print user_id             
        if user_id in TweetList:
            tweets = TweetList[user_id]
            tweetsNode = doc.createElement("Tweets")
            user_entry_node.appendChild(tweetsNode)
            for j in range (0,len(tweets)):
                tweetNode = doc.createElement("Tweet")
                tweetsNode.appendChild(tweetNode)
                dateNode = doc.createElement("Tweet_created")
                tweetNode.appendChild(dateNode)
                dateNode_text = doc.createTextNode(tweets[j][3])
                dateNode.appendChild(dateNode_text)
                messageNode = doc.createElement("Message")
                tweetNode.appendChild(messageNode)
                messageNode_text = doc.createTextNode(str(tweets[j][0]))
                messageNode.appendChild(messageNode_text)
                sourceNode = doc.createElement("Source")
                tweetNode.appendChild(sourceNode)
                sourceNode_text = doc.createTextNode(tweets[j][1])
                sourceNode.appendChild(sourceNode_text)
                sourceUrlNode = doc.createElement("Source_Url")
                tweetNode.appendChild(sourceUrlNode)
                sourceUrlNode_text = doc.createTextNode(tweets[j][2])
                sourceUrlNode.appendChild(sourceUrlNode_text) 
        """
    # Print our newly created XML files to Log
    _adel_log.log(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")), 3)
    # Create xml file
    xml_fb = open(xml_dir + "/facebook.xml", "a+")
    xml_fb.write(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")))
    xml_fb.close()
    _adel_log.log("xmlParser:          ----> facebook.xml created!", 4)
コード例 #35
0
ファイル: _xmlParser.py プロジェクト: vaginessa/ADEL
def call_log_to_xml(xml_dir, callLogList):
    _adel_log.log(
        "############  XML OUTPUT GENERATION -> CALL LOG ENTRIES  ############ \n",
        2)
    # Create the minidom document
    doc = Document()
    xml = doc.createElement("Call_Log_Entries")
    doc.appendChild(xml)
    for i in range(0, len(callLogList)):
        # Create the <Call_Log_Entry> element
        call_log_entry = doc.createElement("Call_Log_Entry")
        xml.appendChild(call_log_entry)
        id = doc.createElement("id")
        call_log_entry.appendChild(id)
        id_text = doc.createTextNode(callLogList[i][0])
        id.appendChild(id_text)
        number = doc.createElement("number")
        call_log_entry.appendChild(number)
        number_text = doc.createTextNode(callLogList[i][1])
        number.appendChild(number_text)
        date = doc.createElement("date")
        call_log_entry.appendChild(date)
        date_text = doc.createTextNode(callLogList[i][2])
        date.appendChild(date_text)
        duration = doc.createElement("duration")
        call_log_entry.appendChild(duration)
        duration_text = doc.createTextNode(callLogList[i][3])
        duration.appendChild(duration_text)
        type = doc.createElement("type")
        call_log_entry.appendChild(type)
        type_text = doc.createTextNode(callLogList[i][4])
        type.appendChild(type_text)
        name = doc.createElement("name")
        call_log_entry.appendChild(name)
        name_text = doc.createTextNode(callLogList[i][5])
        name.appendChild(name_text)
    # Print our newly created XML files to Log
    _adel_log.log(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")), 3)
    # create xml file
    xml_callLogs = open(xml_dir + "/call_logs.xml", "a+")
    xml_callLogs.write(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")))
    xml_callLogs.close()
    _adel_log.log("xmlParser:          ----> call_logs.xml created!", 4)
コード例 #36
0
ファイル: _xmlParser.py プロジェクト: vaginessa/ADEL
def calendar_to_xml(xml_dir, calendar_list):
    _adel_log.log(
        "############  XML OUTPUT GENERATION -> CALENDAR ENTRIES  ############ \n",
        2)
    # Create the minidom document
    doc = Document()
    xml = doc.createElement("Calendar_Entries")
    doc.appendChild(xml)
    for i in range(0, len(calendar_list)):
        # Create the <Calendar_Entry> element
        calendar_entry = doc.createElement("Calendar_Entry")
        xml.appendChild(calendar_entry)
        id = doc.createElement("id")
        calendar_entry.appendChild(id)
        id_text = doc.createTextNode(calendar_list[i][0])
        id.appendChild(id_text)
        calendarName = doc.createElement("calendarName")
        calendar_entry.appendChild(calendarName)
        calendarName_text = doc.createTextNode(calendar_list[i][1])
        calendarName.appendChild(calendarName_text)
        title = doc.createElement("title")
        calendar_entry.appendChild(title)
        title_text = doc.createTextNode(calendar_list[i][2])
        title.appendChild(title_text)
        eventLocation = doc.createElement("eventLocation")
        calendar_entry.appendChild(eventLocation)
        event_location_text = doc.createTextNode(calendar_list[i][3])
        eventLocation.appendChild(event_location_text)
        description = doc.createElement("description")
        calendar_entry.appendChild(description)
        description_text = doc.createTextNode(calendar_list[i][4])
        description.appendChild(description_text)
        all_day = doc.createElement("all_day")
        calendar_entry.appendChild(all_day)
        allDay_text = doc.createTextNode(calendar_list[i][5])
        all_day.appendChild(allDay_text)
        start = doc.createElement("start")
        calendar_entry.appendChild(start)
        start_text = doc.createTextNode(calendar_list[i][6])
        start.appendChild(start_text)
        end = doc.createElement("end")
        calendar_entry.appendChild(end)
        end_text = doc.createTextNode(calendar_list[i][7])
        end.appendChild(end_text)
        has_alarm = doc.createElement("has_alarm")
        calendar_entry.appendChild(has_alarm)
        has_alarm_text = doc.createTextNode(calendar_list[i][8])
        has_alarm.appendChild(has_alarm_text)
    # Print our newly created XML files to Log
    _adel_log.log(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")), 3)
    # create xml file
    xml_calendar = open(xml_dir + "/calendar.xml", "a+")
    xml_calendar.write(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")))
    xml_calendar.close()
    _adel_log.log("xmlParser:          ----> calendar.xml created!", 4)
コード例 #37
0
ファイル: _xmlParser.py プロジェクト: vaginessa/ADEL
def smartphone_info_to_xml(xml_dir, smartphone_infolist):
    _adel_log.log(
        "############  XML OUTPUT GENERATION -> SMARTPHONE INFOS  ############ \n",
        2)
    # Create the minidom document
    doc = Document()
    info = doc.createElement("smartphone_info")
    doc.appendChild(info)
    account_name = doc.createElement("account_name")
    info.appendChild(account_name)
    account_name_text = doc.createTextNode(smartphone_infolist[0])
    account_name.appendChild(account_name_text)
    account_type = doc.createElement("account_type")
    info.appendChild(account_type)
    account_type_text = doc.createTextNode(smartphone_infolist[1])
    account_type.appendChild(account_type_text)
    imsi = doc.createElement("imsi")
    info.appendChild(imsi)
    imsi_text = doc.createTextNode(smartphone_infolist[2])
    imsi.appendChild(imsi_text)
    android_id = doc.createElement("android_id")
    info.appendChild(android_id)
    android_id_text = doc.createTextNode(smartphone_infolist[3])
    android_id.appendChild(android_id_text)
    handheld_id = doc.createElement("handheld_id")
    info.appendChild(handheld_id)
    handheld_id_text = doc.createTextNode(smartphone_infolist[4])
    handheld_id.appendChild(handheld_id_text)
    model = doc.createElement("model")
    info.appendChild(model)
    model_text = doc.createTextNode(smartphone_infolist[5])
    model.appendChild(model_text)
    android_version = doc.createElement("android_version")
    info.appendChild(android_version)
    android_version_text = doc.createTextNode(smartphone_infolist[6])
    android_version.appendChild(android_version_text)
    # Print our newly created XML files to Log
    _adel_log.log(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")), 3)
    # create xml file
    xml_info = open(xml_dir + "/info.xml", "a+")
    xml_info.write(
        make_pretty_xml(doc.toprettyxml(indent="  ", encoding="UTF-8")))
    xml_info.close()
    _adel_log.log("xmlParser:          ----> info.xml created!", 4)
コード例 #38
0
from copy import deepcopy
from xml.dom.minidom import Document


def copy_table_after(table, paragraph):
    tbl, p = table._tbl, paragraph._p
    new_tbl = deepcopy(tbl)
    p.addnext(new_tbl)


def replaceText(document, search, replace):
    for table in document.tables:
        for row in table.rows:
            for paragraph in row.cells:
                if search in paragraph.text:
                    paragraph.text = replace


name = 'док.docx'
document = Document(name)
template = document.tables[0]
replaceText(document, '<<VALUE_TO_FIND>>', 'New value')
paragraph = document.add_paragraph()
copy_table_after(template, paragraph)
# pip install C:\path\to\downloaded\file\lxml-4.3.5-cp38-cp38-win32.whl
# pip install C:\Users\dbukreev\Downloads\lxml-4.4.2-cp38-cp38-win_amd64.whl   pip install lxml==3.6.0

コード例 #39
0
ファイル: writer_xml.py プロジェクト: raydrawc/genCfgs
class XmlWriter(Writer):
    # 文件后缀
    def suffix(self):
        return ".xml"

    # 注释开始
    def comment_start(self):
        return "<!--"

    # 注释结束
    def comment_end(self):
        return "-->"

    # 创建根元素
    def root_element(self):
        root = self.doc.createElement(self.base_name)
        return root

    # dict类型转换为xml
    def dict_to_xml(self, root, value):
        # 需要对key排序,不然每次导出的xml字段是乱的,对版本管理不友好
        for k in sorted(value):
            v = value[k]
            sub_root = self.doc.createElement(k)

            self.to_xml(sub_root, v)
            root.appendChild(sub_root)

    # list类型转换为xml
    def list_to_xml(self, root, value):
        for k, v in enumerate(value):
            # xml中并不支持array,用item来命名,外加一个index属性
            sub_root = self.doc.createElement("item")
            sub_root.setAttribute("index", str(k))

            self.to_xml(sub_root, v)
            root.appendChild(sub_root)

    # tuple类型转换为xml
    def tuple_to_xml(self, root, value):
        valueList = list(value)
        for k, v in enumerate(valueList):
            # xml中并不支持array,用item来命名,外加一个index属性
            sub_root = self.doc.createElement("item")
            sub_root.setAttribute("index", str(k))

            self.to_xml(sub_root, v)
            root.appendChild(sub_root)

    # 转换为xml节点
    def to_xml(self, root, value):
        sub_node = None
        val_type_str = None
        val_type = type(value)
        if int == val_type:
            # python3中没有Long类型,int64也用int表示
            val_type_str = "int64"
            sub_node = self.doc.createTextNode(str(value))
        elif long == val_type:
            val_type_str = "int64"
            sub_node = self.doc.createTextNode(str(value))
        elif float == val_type:
            val_type_str = "number"
            # 去除带小数时的小数点,100.0 ==>> 100
            if long(value) == float(value):
                sub_node = self.doc.createTextNode(str(long(value)))
            else:
                sub_node = self.doc.createTextNode(str(value))
        elif str == val_type or unicode == val_type:
            val_type_str = "string"
            sub_node = self.doc.createTextNode(value)
        elif tuple == val_type:
            self.tuple_to_xml(root, value)
        elif dict == val_type:
            self.dict_to_xml(root, value)
        elif list == val_type:
            self.list_to_xml(root, value)
        else:
            raise Exception("invalid type", val_type)

        # 类型为dict或者list的,没有这个type属性
        if val_type_str: root.setAttribute("type", val_type_str)
        if sub_node: root.appendChild(sub_node)

    # 文件内容
    def context(self, ctx):
        # 创建DOM文档对象
        self.doc = Document()
        root = self.root_element()

        self.to_xml(root, ctx)
        self.doc.appendChild(root)

        return self.comment() + self.doc.toprettyxml(indent="   ")
コード例 #40
0
def csomag_XML_elem(cs_felm, cs_azon, cs_nev, cs_pontsz, cs_gen, cs_hat,
                    cs_tom, cs_MD5, genfolder):
    """ XML minidom-document létrehozása a csomag adataival és a csomag_log rekord kezelése"""
    doc = Document()

    # csomag elem létrehozása
    csomag = doc.createElement('csomag')
    csomag.setAttribute("azonosito", '%s' % str(cs_azon))
    csomag.setAttribute("nev", '%s' % cs_nev)
    csomag.setAttribute("pontszam", '%s' % str(cs_pontsz))
    doc.appendChild(csomag)

    # a csomag gyermek elemeinek létrehozása
    generalas = doc.createElement('generalas')
    generalas_tartalom = doc.createTextNode('%s' % cs_gen)
    generalas.appendChild(generalas_tartalom)
    csomag.appendChild(generalas)

    hatarido = doc.createElement('hatarido')
    hatarido_tartalom = doc.createTextNode('%s' % cs_hat)
    hatarido.appendChild(hatarido_tartalom)
    csomag.appendChild(hatarido)

    muvelet = doc.createElement('muvelet')
    csomag.appendChild(muvelet)

    hash = doc.createElement('hash')
    csomag.appendChild(hash)

    # a művelet gyermek elemeinek létrehozása
    tomorites = doc.createElement('tomorites')
    tomorites_tartalom = doc.createTextNode('%s' % cs_tom)
    tomorites.appendChild(tomorites_tartalom)
    muvelet.appendChild(tomorites)

    # a művelet gyermek elemeinek létrehozása
    md5szerver = doc.createElement('MD5_szerver')
    md5szerver_tartalom = doc.createTextNode('%s' % cs_MD5)
    md5szerver.appendChild(md5szerver_tartalom)
    hash.appendChild(md5szerver)

    fname = genfolder + '/' + cs_felm + '/' + cs_nev + '.xml'
    # XML kiírása
    doc.writexml(open(fname, 'w'), indent="  ", addindent="  ", newl='\n')
    doc.unlink()

    if os.path.isfile(fname):
        queryLogId = QtSql.QSqlQuery()
        siker = queryLogId.exec_(
            '''SELECT package_id FROM csomag_log WHERE csomag_azon = %s;''' %
            (cs_azon))
        if siker:
            if queryLogId.size() == 1:
                queryLogId.next()
                return queryLogId.value(0), 2
            elif queryLogId.size() > 1:
                QtGui.QMessageBox.warning(
                    None, u"Csomag-log",
                    u"Csomag-log rekord lekérdezési hiba.")
                return 0, 0
        else:
            if queryLogId.lastError().type() != 0:
                QtGui.QMessageBox.warning(
                    None, u"Hiba:",
                    u"Hiba típus: %s" % str(queryLogId.lastError().text()))
                return 0, 0

        queryLog = QtSql.QSqlQuery()
        siker = queryLog.exec_(
            '''INSERT INTO csomag_log(felmero,csomag_azon,csomag_nev,cs_pontszam,cs_generalas,cs_hatarido,m_tomorites,hash_md5_szerver) 
            VALUES('%s',%s,'%s',%s,'%s','%s','%s','%s') RETURNING package_id ;'''
            % (cs_felm, cs_azon, cs_nev, cs_pontsz, cs_gen, cs_hat, cs_tom,
               cs_MD5))
        if siker:
            if queryLog.size() == 1:
                queryLog.next()
                return queryLog.value(0), 1
            else:
                QtGui.QMessageBox.warning(
                    None, u"Csomag-log", u"Csomag-log rekord rögzítési hiba.")
                return 0, 0

        else:
            if queryLog.lastError().type() != 0:
                QtGui.QMessageBox.warning(
                    None, u"Hiba:",
                    u"Hiba típus: %s" % str(queryLog.lastError().text()))
                return 0, 0

    else:
        print "No XML - No PackMan!"
        return 0, 0
コード例 #41
0
def create_RIC_SIC_rename_file(oldRic, oldSic, srcPath, exlfile):
    """ Create src flie dynamically to rename both RIC and SIC:
        Argument :oldRic: old ric name 
             oldSic: old sic name
             srcPath: path for src file and exl file  (e.g. /ChangeSicRic.src)
             exlfile: get full path of exlfile in local from fmscmd (e.g. /nasmf_a.exl)
        return : New Ric and Sic name
        
    """

    if os.path.exists(srcPath):
        os.remove(srcPath)

    srcDoc = Document()
    src = srcDoc.createElement('SRC')  #set root element
    src.setAttribute('xmlns', 'SrcSchema')
    src.setAttribute('xmlns:it', 'DbIssueTypeSchema')
    src.setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance")
    src.setAttribute('xsi:schemaLocation',
                     'DbFieldsSchema AllFields.xsd SrcSchema SrcSchema.xsd')
    srcDoc.appendChild(src)

    date = srcDoc.createElement('date')
    date_txt = srcDoc.createTextNode(
        datetime.datetime.now().strftime("%Y-%m-%d"))
    date.appendChild(date_txt)
    src.appendChild(date)

    action = srcDoc.createElement('action')
    action_txt = srcDoc.createTextNode('BOTH')
    action.appendChild(action_txt)
    src.appendChild(action)

    dom = xml.dom.minidom.parse(exlfile)
    root = dom.documentElement
    #exlFileName,exchangeName,
    exlFileName = root.getElementsByTagName('name')[0].firstChild.data
    iteratorlist = dom.getElementsByTagName('it:EXCHANGE')
    exchangeName = iteratorlist[0].firstChild.data

    exlFile = srcDoc.createElement('exlFile')
    exlFile_txt = srcDoc.createTextNode(exlFileName)
    exlFile.appendChild(exlFile_txt)
    src.appendChild(exlFile)

    exchange = srcDoc.createElement('exchange')
    exchange_txt = srcDoc.createTextNode(exchangeName)
    exchange.appendChild(exchange_txt)
    src.appendChild(exchange)

    kNode = srcDoc.createElement('k')
    src.appendChild(kNode)

    bothNode = srcDoc.createElement('both')
    kNode.appendChild(bothNode)

    orNode = srcDoc.createElement('or')
    orNode_txt = srcDoc.createTextNode(oldRic)
    orNode.appendChild(orNode_txt)
    bothNode.appendChild(orNode)

    newRic = ('TEST' + oldRic +
              datetime.datetime.now().strftime("%Y%m%d%H%M%S"))[0:32]
    nrNode = srcDoc.createElement('nr')
    nrNode_txt = srcDoc.createTextNode(newRic)
    nrNode.appendChild(nrNode_txt)
    bothNode.appendChild(nrNode)

    osNode = srcDoc.createElement('os')
    osNode_txt = srcDoc.createTextNode(oldSic[2:len(oldSic)])
    osNode.appendChild(osNode_txt)
    bothNode.appendChild(osNode)

    newSic = oldSic[2:len(oldSic)] + 'TestSIC'
    nsNode = srcDoc.createElement('ns')
    nsNode_txt = srcDoc.createTextNode(newSic)
    nsNode.appendChild(nsNode_txt)
    bothNode.appendChild(nsNode)

    fileHandle = open(srcPath, 'w')
    srcDoc.writexml(fileHandle,
                    indent='\t',
                    addindent='\t',
                    newl='\n',
                    encoding="utf-8")
    return newRic, newSic
コード例 #42
0
def GetXMLSepa(dictDonnees):
    """ Génération du fichier XML SEPA """
    doc = Document()

    # Variables principales
    type_remise = dictDonnees["type_remise"]
    remise_nom = dictDonnees["remise_nom"]
    remise_date_heure = dictDonnees["remise_date_heure"]
    remise_nbre = dictDonnees["remise_nbre"]
    remise_montant = dictDonnees["remise_montant"]

    creancier_nom = dictDonnees["creancier_nom"]
    creancier_rue = dictDonnees["creancier_rue"]
    creancier_cp = dictDonnees["creancier_cp"]
    creancier_ville = dictDonnees["creancier_ville"]
    creancier_pays = dictDonnees["creancier_pays"]
    creancier_siret = dictDonnees["creancier_siret"]

    listeLots = dictDonnees["lots"]

    # Génération du document XML
    racine = doc.createElement("Document")
    racine.setAttribute("xmlns:xsi",
                        "http://www.w3.org/2001/XMLSchema-instance")
    racine.setAttribute("xmlns",
                        "urn:iso:std:iso:20022:tech:xsd:pain.008.001.02")
    doc.appendChild(racine)

    # CstmrDrctDbtInitn
    CstmrDrctDbtInitn = doc.createElement("CstmrDrctDbtInitn")
    racine.appendChild(CstmrDrctDbtInitn)

    # ----------------------------------------------------------- NIVEAU MESSAGE ------------------------------------------------------------------------------

    # ------------- Caractéristiques générales du prélèvement -------------------

    # GrpHdr
    GrpHdr = doc.createElement("GrpHdr")
    CstmrDrctDbtInitn.appendChild(GrpHdr)

    # MsgId
    MsgId = doc.createElement("MsgId")
    GrpHdr.appendChild(MsgId)
    MsgId.appendChild(doc.createTextNode(remise_nom))

    # CreDtTm
    CreDtTm = doc.createElement("CreDtTm")
    GrpHdr.appendChild(CreDtTm)
    CreDtTm.appendChild(doc.createTextNode(remise_date_heure))

    # NbOfTxs
    NbOfTxs = doc.createElement("NbOfTxs")
    GrpHdr.appendChild(NbOfTxs)
    NbOfTxs.appendChild(doc.createTextNode(remise_nbre))

    # CtrlSum
    CtrlSum = doc.createElement("CtrlSum")
    GrpHdr.appendChild(CtrlSum)
    CtrlSum.appendChild(doc.createTextNode(remise_montant))

    # ------------- Créantier (organisateur) -------------------

    # InitgPty
    InitgPty = doc.createElement("InitgPty")
    GrpHdr.appendChild(InitgPty)

    # Nm
    Nm = doc.createElement("Nm")
    InitgPty.appendChild(Nm)
    Nm.appendChild(doc.createTextNode(creancier_nom[:70]))

    # Id
    Id = doc.createElement("Id")
    InitgPty.appendChild(Id)

    # OrgId
    OrgId = doc.createElement("OrgId")
    Id.appendChild(OrgId)

    # Othr
    Othr = doc.createElement("Othr")
    OrgId.appendChild(Othr)

    # Id
    Id = doc.createElement("Id")
    Othr.appendChild(Id)
    Id.appendChild(doc.createTextNode(creancier_siret))

    # SchmeNm
    SchmeNm = doc.createElement("SchmeNm")
    Othr.appendChild(SchmeNm)

    # Prtry
    Prtry = doc.createElement("Prtry")
    SchmeNm.appendChild(Prtry)
    Prtry.appendChild(doc.createTextNode("SIRET"))

    # ----------------------------------------------------------- NIVEAU LOT ------------------------------------------------------------------------------

    for dictLot in listeLots:

        lot_nom = dictLot["lot_nom"]
        lot_nbre = dictLot["lot_nbre"]
        lot_montant = dictLot["lot_montant"]
        lot_date = dictLot["lot_date"]
        lot_iban = dictLot["lot_iban"]
        lot_bic = dictLot["lot_bic"]
        lot_ics = dictLot["lot_ics"]
        lot_dft_titulaire = dictLot["dft_titulaire"]
        lot_dft_iban = dictLot["dft_iban"]
        lot_motif = dictLot["motif"]
        lot_sequence = dictLot["lot_sequence"]
        listeTransactions = dictLot["transactions"]

        # PmtInf
        PmtInf = doc.createElement("PmtInf")
        CstmrDrctDbtInitn.appendChild(PmtInf)

        # PmtInfId
        PmtInfId = doc.createElement("PmtInfId")
        PmtInf.appendChild(PmtInfId)
        PmtInfId.appendChild(doc.createTextNode(lot_nom))

        # PmtMtd
        PmtMtd = doc.createElement("PmtMtd")
        PmtInf.appendChild(PmtMtd)
        PmtMtd.appendChild(doc.createTextNode("DD"))

        # NbOfTxs
        NbOfTxs = doc.createElement("NbOfTxs")
        PmtInf.appendChild(NbOfTxs)
        NbOfTxs.appendChild(doc.createTextNode(lot_nbre))

        # CtrlSum
        CtrlSum = doc.createElement("CtrlSum")
        PmtInf.appendChild(CtrlSum)
        CtrlSum.appendChild(doc.createTextNode(lot_montant))

        # PmtTpInf
        PmtTpInf = doc.createElement("PmtTpInf")
        PmtInf.appendChild(PmtTpInf)

        # SvcLvl
        SvcLvl = doc.createElement("SvcLvl")
        PmtTpInf.appendChild(SvcLvl)

        # Cd
        Cd = doc.createElement("Cd")
        SvcLvl.appendChild(Cd)
        Cd.appendChild(doc.createTextNode("SEPA"))

        # LclInstrm
        LclInstrm = doc.createElement("LclInstrm")
        PmtTpInf.appendChild(LclInstrm)

        # Cd
        Cd = doc.createElement("Cd")
        LclInstrm.appendChild(Cd)
        Cd.appendChild(doc.createTextNode("CORE"))

        # SeqTp
        SeqTp = doc.createElement("SeqTp")
        PmtTpInf.appendChild(SeqTp)
        SeqTp.appendChild(doc.createTextNode(lot_sequence))

        # ReqdColltnDt
        ReqdColltnDt = doc.createElement("ReqdColltnDt")
        PmtInf.appendChild(ReqdColltnDt)
        ReqdColltnDt.appendChild(doc.createTextNode(lot_date))

        # Cdtr
        Cdtr = doc.createElement("Cdtr")
        PmtInf.appendChild(Cdtr)

        if type_remise == "prive":
            # Cdtr
            Nm = doc.createElement("Nm")
            Cdtr.appendChild(Nm)
            Nm.appendChild(doc.createTextNode(creancier_nom))

        if type_remise == "public_dft":
            perception = dictDonnees["perception"]

            # Cdtr
            Nm = doc.createElement("Nm")
            Cdtr.appendChild(Nm)
            Nm.appendChild(doc.createTextNode(perception["nom"]))

            # PstlAdr
            PstlAdr = doc.createElement("PstlAdr")
            Cdtr.appendChild(PstlAdr)

            # Ctry
            Ctry = doc.createElement("Ctry")
            PstlAdr.appendChild(Ctry)
            Ctry.appendChild(doc.createTextNode("FR"))

            # AdrLine
            AdrLine = doc.createElement("AdrLine")
            PstlAdr.appendChild(AdrLine)
            AdrLine.appendChild(doc.createTextNode(perception["rue_resid"]))

            # AdrLine
            AdrLine = doc.createElement("AdrLine")
            PstlAdr.appendChild(AdrLine)
            AdrLine.appendChild(
                doc.createTextNode(
                    u"%s %s" %
                    (perception["cp_resid"], perception["ville_resid"])))

        # CdtrAcct
        CdtrAcct = doc.createElement("CdtrAcct")
        PmtInf.appendChild(CdtrAcct)

        # Id
        Id = doc.createElement("Id")
        CdtrAcct.appendChild(Id)

        # IBAN
        IBAN = doc.createElement("IBAN")
        Id.appendChild(IBAN)
        IBAN.appendChild(doc.createTextNode(lot_iban))

        # CdtrAgt
        CdtrAgt = doc.createElement("CdtrAgt")
        PmtInf.appendChild(CdtrAgt)

        # FinInstnId
        FinInstnId = doc.createElement("FinInstnId")
        CdtrAgt.appendChild(FinInstnId)

        # BIC
        BIC = doc.createElement("BIC")
        FinInstnId.appendChild(BIC)
        BIC.appendChild(doc.createTextNode(lot_bic))

        if type_remise == "public_dft":

            # UltmtCdtr
            UltmtCdtr = doc.createElement("UltmtCdtr")
            PmtInf.appendChild(UltmtCdtr)

            # Nm
            Nm = doc.createElement("Nm")
            UltmtCdtr.appendChild(Nm)
            Nm.appendChild(doc.createTextNode(lot_dft_titulaire))

            # Id
            Id = doc.createElement("Id")
            UltmtCdtr.appendChild(Id)

            # OrgId
            OrgId = doc.createElement("OrgId")
            Id.appendChild(OrgId)

            # Othr
            Othr = doc.createElement("Othr")
            OrgId.appendChild(Othr)

            # Id
            Id = doc.createElement("Id")
            Othr.appendChild(Id)
            Id.appendChild(doc.createTextNode(lot_dft_iban))

        # CdtrSchmeId
        CdtrSchmeId = doc.createElement("CdtrSchmeId")
        PmtInf.appendChild(CdtrSchmeId)

        # Id
        Id = doc.createElement("Id")
        CdtrSchmeId.appendChild(Id)

        # PrvtId
        PrvtId = doc.createElement("PrvtId")
        Id.appendChild(PrvtId)

        # Othr
        Othr = doc.createElement("Othr")
        PrvtId.appendChild(Othr)

        # Id
        Id = doc.createElement("Id")
        Othr.appendChild(Id)
        Id.appendChild(doc.createTextNode(lot_ics))

        # SchmeNm
        SchmeNm = doc.createElement("SchmeNm")
        Othr.appendChild(SchmeNm)

        # Prtry
        Prtry = doc.createElement("Prtry")
        SchmeNm.appendChild(Prtry)
        Prtry.appendChild(doc.createTextNode("SEPA"))

        # ----------------------------------------------------------- NIVEAU TRANSACTION ------------------------------------------------------------------------------

        for dictTransaction in listeTransactions:

            transaction_id = dictTransaction["transaction_id"]
            transaction_montant = dictTransaction["transaction_montant"]
            transaction_mandat_id = dictTransaction["transaction_mandat_id"]
            transaction_mandat_date = dictTransaction[
                "transaction_mandat_date"]
            transaction_bic = dictTransaction["transaction_bic"]
            transaction_debiteur = dictTransaction["transaction_debiteur"]
            transaction_iban = dictTransaction["transaction_iban"]

            # DrctDbtTxInf
            DrctDbtTxInf = doc.createElement("DrctDbtTxInf")
            PmtInf.appendChild(DrctDbtTxInf)

            # PmtId
            PmtId = doc.createElement("PmtId")
            DrctDbtTxInf.appendChild(PmtId)

            # EndToEndId
            EndToEndId = doc.createElement("EndToEndId")
            PmtId.appendChild(EndToEndId)
            EndToEndId.appendChild(doc.createTextNode(transaction_id))

            # InstdAmt
            InstdAmt = doc.createElement("InstdAmt")
            DrctDbtTxInf.appendChild(InstdAmt)
            InstdAmt.appendChild(doc.createTextNode(transaction_montant))
            InstdAmt.setAttribute("Ccy", "EUR")

            # DrctDbtTx
            DrctDbtTx = doc.createElement("DrctDbtTx")
            DrctDbtTxInf.appendChild(DrctDbtTx)

            # MndtRltdInf
            MndtRltdInf = doc.createElement("MndtRltdInf")
            DrctDbtTx.appendChild(MndtRltdInf)

            # MndtId
            MndtId = doc.createElement("MndtId")
            MndtRltdInf.appendChild(MndtId)
            MndtId.appendChild(doc.createTextNode(transaction_mandat_id))

            # DtOfSgntr
            DtOfSgntr = doc.createElement("DtOfSgntr")
            MndtRltdInf.appendChild(DtOfSgntr)
            DtOfSgntr.appendChild(doc.createTextNode(transaction_mandat_date))

            # DbtrAgt
            DbtrAgt = doc.createElement("DbtrAgt")
            DrctDbtTxInf.appendChild(DbtrAgt)

            # FinInstnId
            FinInstnId = doc.createElement("FinInstnId")
            DbtrAgt.appendChild(FinInstnId)

            # Dbtr
            BIC = doc.createElement("BIC")
            FinInstnId.appendChild(BIC)
            BIC.appendChild(doc.createTextNode(transaction_bic))

            # Dbtr
            Dbtr = doc.createElement("Dbtr")
            DrctDbtTxInf.appendChild(Dbtr)

            # Nm
            Nm = doc.createElement("Nm")
            Dbtr.appendChild(Nm)
            Nm.appendChild(doc.createTextNode(transaction_debiteur[:70]))

            # DbtrAcct
            DbtrAcct = doc.createElement("DbtrAcct")
            DrctDbtTxInf.appendChild(DbtrAcct)

            # Id
            Id = doc.createElement("Id")
            DbtrAcct.appendChild(Id)

            # IBAN
            IBAN = doc.createElement("IBAN")
            Id.appendChild(IBAN)
            IBAN.appendChild(doc.createTextNode(transaction_iban))

            if type_remise == "public_dft":

                # RmtInf
                RmtInf = doc.createElement("RmtInf")
                DrctDbtTxInf.appendChild(RmtInf)

                # Ustrd
                Ustrd = doc.createElement("Ustrd")
                RmtInf.appendChild(Ustrd)
                Ustrd.appendChild(doc.createTextNode(lot_motif))

    return doc
コード例 #43
0
def CombineType12():
    # 表头字典查找表,用于列表生成对应图谱
    dict111={}
    xlsfile0 = r'C:\Users\Administrator\Desktop\大脑解剖2.0.1_0文档.xlsx'
    book = xlrd.open_workbook(xlsfile0)
    sheet0 = book.sheet_by_name('图谱编号')
    for i in range(1,sheet0.nrows):
        dict111[sheet0.cell_value(i,0)]=sheet0.cell_value(i,1)
    print(len(dict111))
    dict222={}
    xlsfile0 = r'C:\Users\Administrator\Desktop\大脑解剖2.0.1_0文档.xlsx'
    book = xlrd.open_workbook(xlsfile0)
    sheet0 = book.sheet_by_name('图谱编号')
    for i in range(1,sheet0.nrows):
        dict222[sheet0.cell_value(i,0)]=sheet0.cell_value(i,1)
    print(len(dict222))

    doc = Document()
    SignInfoList = doc.createElement('AtlasList')
    doc.appendChild(SignInfoList)
    xlsfile = r'C:\Users\Administrator\Desktop\大脑解剖2.0.1_0文档.xlsx'
    book = xlrd.open_workbook(xlsfile)
    sheet1 = book.sheet_by_name('Book02文档')
    count = sheet1.nrows
    atlasArray = []
    # 图谱编号对应  type2 类型数组
    nameType2Dict={}
    for i in range(1,count):
        if(sheet1.cell_value(i,0) not in atlasArray):
            atlasArray.append(sheet1.cell_value(i,0))
    print(atlasArray)
    for j in atlasArray:
        t2=type2(j)
        t2.namelist=[]
        for i in range(1, count):
            if(sheet1.cell_value(i,0) == j):
                t2.namelist.append(sheet1.cell_value(i,1))
        nameType2Dict[j]=t2
    print(len(nameType2Dict))
    mainkeys=nameType2Dict.keys()
    # t=nameType2Dict.get('24')
    # print(t.namelist)
    # create 111
    combine=dict(dict111,**dict222)
    print(dict111)
    for i in dict111.keys():
        atlas = doc.createElement('Atlas')
        atlas.setAttribute('AtlasId', i)
        atlas.setAttribute('Chinese', dict111.get(i))
        atlas.setAttribute('DingziName',i)
        atlas.setAttribute('version', '0')
        atlas.setAttribute('PictureName',i)
        atlas.setAttribute('minDis', '0')
        atlas.setAttribute('maxDis', '0')
        atlas.setAttribute('signPositionX', '0')
        atlas.setAttribute('signPositionY', '0')
        atlas.setAttribute('signDistance', '0')
        atlas.setAttribute('signSize', '20')
        atlas.setAttribute('cameraBackXRot', '0')
        atlas.setAttribute('cameraBackYRot', '0')
        atlas.setAttribute('cameraBackZRot', '0')
        atlas.setAttribute('R', '255')
        atlas.setAttribute('G', '0')
        atlas.setAttribute('B', '255')
        atlas.setAttribute('fieldOfView', '10')
        SignInfoList.appendChild(atlas)
        # print(i)
        if(mainkeys.__contains__(i)):
            #解析对应type2 模型p
            t2=nameType2Dict.get(i)
            for j in t2.namelist :
                SignElement = doc.createElement('model')
                SignElement.setAttribute('ModelName', j)
                SignElement.setAttribute('IsTranslucent', '0')
                atlas.appendChild(SignElement)

    doc.appendChild(SignInfoList)
    f=open(r'C:\Users\Administrator\Desktop\AtlasList.xml','w',encoding='utf-8')
    doc.writexml(f,addindent='  ',newl='\n',encoding='utf-8')
コード例 #44
0
class TowerEngineSceneExporter(bpy.types.Operator, ExportHelper):
    bl_idname = "export_tes.tes"
    bl_label = "Export"
    bl_options = {'PRESET'}

    filename_ext = ".tes"

    limit_export_prop = BoolProperty(name="Limit to selection",
                                     description="Export selection only",
                                     default=True)

    include_mat_prop = BoolProperty(
        name="Include Materials in Scene file",
        description=
        "Keep materials globally in Scene file instead in every Mesh file that uses a specific material.",
        default=True)

    pack_tex_prop = BoolProperty(
        name="Pack Textures in Files",
        description=
        "Include Texture Images directly into .tem/.tes files instead of copying and linking the URL",
        default=False)

    save_cubemaps_prop = BoolProperty(
        name="Save Cube Maps",
        description="Include Cube Maps of the current scene",
        default=True)

    def execute(self, context):
        self.directory = os.path.dirname(self.filepath)
        self.file_basename = os.path.basename(self.filepath)

        self.__collect_data()
        self.__save()
        return {'FINISHED'}

    def __collect_data(self):
        # collect objects
        self.objects = dict()

        for obj in bpy.data.objects:
            if (self.limit_export_prop == True and obj.select == False):
                continue

            self.objects[obj.name] = obj

        # collect meshes
        self.meshes = dict()
        for obj in self.objects.values():
            if obj.type == "MESH" and not obj.towerengine.disable_mesh:
                mesh = obj.data
                self.meshes[mesh.name] = mesh

        # collect materials
        if self.include_mat_prop:
            self.materials = dict()
            self.mesh_exclude_materials = set()
            for mesh in self.meshes.values():
                for material in mesh.materials:
                    if material == None:
                        continue
                    self.materials[material.name] = material
                    self.mesh_exclude_materials.add(material.name)

        # collect cubemaps
        self.cubemaps = dict()
        self.scene_cubemap = None
        if self.save_cubemaps_prop:
            for texture_slot in bpy.context.scene.world.texture_slots.values():
                if texture_slot == None:
                    continue

                tex = texture_slot.texture

                if tex.type != "ENVIRONMENT_MAP":
                    continue

                if tex.image == None:
                    continue

                self.cubemaps[tex.name] = tex

                if texture_slot.use_map_horizon:
                    self.scene_cubemap = tex.name

        return

    def __save(self):
        self.__save_assets()

        self.xml_doc = Document()
        self.xml_root = self.xml_doc.createElement("tscene")
        self.xml_doc.appendChild(self.xml_root)

        self.__save_assets_xml()
        self.__save_objects_xml()
        self.__save_scene_xml()

        scene_file = open(self.filepath, 'w')
        scene_file.write(self.xml_doc.toprettyxml(indent="  "))
        scene_file.close()

    def __save_assets_xml(self):
        assets_node = self.xml_doc.createElement("assets")

        if self.include_mat_prop:
            for material_name in self.materials.keys():
                material_node = export_material.create_node(
                    self.xml_doc, self.materials[material_name],
                    self.directory,
                    os.path.join(self.assets_subdir,
                                 "materials"), self.pack_tex_prop)
                assets_node.appendChild(material_node)

        for mesh_name in self.meshes.keys():
            mesh_node = self.xml_doc.createElement("mesh")
            mesh_node.setAttribute("name", mesh_name)

            data_node = self.xml_doc.createElement("data")
            data_node.setAttribute("file", self.saved_mesh_files[mesh_name])
            mesh_node.appendChild(data_node)

            assets_node.appendChild(mesh_node)

        for cubemap_name in self.cubemaps.keys():
            cubemap_node = self.xml_doc.createElement("cubemap")
            cubemap_node.setAttribute("name", cubemap_name)

            data_node = self.xml_doc.createElement("data")
            data_node.setAttribute("file",
                                   self.saved_cubemap_files[cubemap_name])
            cubemap_node.appendChild(data_node)

            assets_node.appendChild(cubemap_node)

        self.xml_root.appendChild(assets_node)

    def __save_objects_xml(self):
        objects_node = self.xml_doc.createElement("objects")

        object_save_functions = {
            "MESH": self.__save_mesh_object_xml,
            "LAMP": self.__save_lamp_object_xml,
            "EMPTY": self.__save_empty_object_xml
        }

        for obj_name, obj in self.objects.items():
            obj_node = None

            try:
                obj_node = object_save_functions[obj.type](obj)
            except KeyError:
                print("Exporting objects of type \"" + obj.type +
                      "\" is not supported")
                continue

            if obj_node is not None:
                obj_node.setAttribute("name", obj_name)
                if hasattr(obj, "towerengine"):
                    obj_node.setAttribute("tag", obj.towerengine.tag)

                    for attribute in obj.towerengine.attributes:
                        attr_node = self.xml_doc.createElement("attribute")
                        attr_node.setAttribute("name", attribute.name)
                        attr_node.setAttribute("value", attribute.value)
                        obj_node.appendChild(attr_node)

                objects_node.appendChild(obj_node)

        self.xml_root.appendChild(objects_node)

    def __save_scene_xml(self):
        scene_node = self.xml_doc.createElement("scene")

        if self.scene_cubemap is not None:
            scene_node.setAttribute("sky_cubemap", self.scene_cubemap)

        self.xml_root.appendChild(scene_node)

    def __save_mesh_object_xml(self, obj):
        if obj.towerengine.disable_mesh:
            return None

        obj_node = self.xml_doc.createElement("mesh")

        obj_node.appendChild(self.__create_transform_node_xml(
            obj.matrix_world))

        mesh_asset_node = self.xml_doc.createElement("mesh_asset")
        mesh_asset_node.setAttribute("asset", obj.data.name)
        obj_node.appendChild(mesh_asset_node)

        if obj.rigid_body:
            obj_node.appendChild(self.__save_rigid_body_xml(obj))

        return obj_node

    def __save_rigid_body_xml(self, obj):
        rigid_body = obj.rigid_body

        rigid_body_node = self.xml_doc.createElement("rigid_body")

        collision_shape_node = self.__save_collision_shape_xml(obj)
        rigid_body_node.appendChild(collision_shape_node)

        mass = rigid_body.mass
        if rigid_body.type == "PASSIVE":
            mass = 0.0
        rigid_body_node.setAttribute("mass", str(mass))

        group = 0
        for i in range(0, 20):
            if rigid_body.collision_groups[i]:
                group |= (1 << i)
        rigid_body_node.setAttribute("group", str(group))

        return rigid_body_node

    def __save_collision_shape_xml(self, obj, compound_child=False):
        rigid_body = obj.rigid_body

        if obj.towerengine.compound_shape and not compound_child:
            collision_shape_node = self.xml_doc.createElement(
                "collision_shape")
            collision_shape_node.setAttribute("type", "compound")
            for child in obj.children:
                child_node = self.__save_collision_shape_xml(child, True)
                if child_node is not None:
                    collision_shape_node.appendChild(child_node)

        elif rigid_body.collision_shape == "MESH" and not compound_child:
            collision_shape_node = self.xml_doc.createElement(
                "collision_shape")
            collision_shape_node.setAttribute("type", "mesh")

        elif rigid_body.collision_shape == "CONVEX_HULL" and not compound_child:
            collision_shape_node = self.xml_doc.createElement(
                "collision_shape")
            collision_shape_node.setAttribute("type", "convex")

        elif rigid_body.collision_shape == "BOX":
            collision_shape_node = self.xml_doc.createElement(
                "collision_shape")
            collision_shape_node.setAttribute("type", "box")

            bounds = utils.bound_box_minmax(obj.bound_box)
            half_extents_node = self.xml_doc.createElement("half_extents")
            half_extents_node.setAttribute(
                "x", str((bounds[1][0] - bounds[0][0]) * 0.5))
            half_extents_node.setAttribute(
                "y", str((bounds[1][2] - bounds[0][2]) * 0.5))
            half_extents_node.setAttribute(
                "z", str((bounds[1][1] - bounds[0][1]) * 0.5))
            collision_shape_node.appendChild(half_extents_node)

        elif not compound_child:
            collision_shape_node = self.xml_doc.createElement(
                "collision_shape")
            print("Collision Shape type " + rigid_body.collision_shape +
                  " is currently not supported. Using mesh instead.")
            collision_shape_node.setAttribute("type", "mesh")

        else:
            return None

        if compound_child:
            collision_shape_node.appendChild(
                self.__create_transform_node_xml(obj.matrix_local))

        return collision_shape_node

    def __save_lamp_object_xml(self, obj):
        obj_node = None
        lamp = obj.data
        matrix = obj.matrix_world

        if lamp.type == "POINT":
            obj_node = self.xml_doc.createElement("point_light")

            obj_node.appendChild(
                self.__create_vector_node_xml("position", matrix[0][3],
                                              matrix[2][3], -matrix[1][3]))

            distance_node = self.xml_doc.createElement("distance")
            distance_node.setAttribute("v", str(lamp.distance))
            obj_node.appendChild(distance_node)

            color_node = self.__create_color_node_xml(
                "color", lamp.color[0] * lamp.energy,
                lamp.color[1] * lamp.energy, lamp.color[2] * lamp.energy)
            obj_node.appendChild(color_node)

        elif lamp.type == "SUN":
            obj_node = self.xml_doc.createElement("dir_light")

            direction = matrix.to_3x3() * Vector((0.0, 0.0, 1.0))
            direction.normalize()
            obj_node.appendChild(
                self.__create_vector_node_xml("direction", direction.x,
                                              direction.z, -direction.y))

            color_node = self.__create_color_node_xml(
                "color", lamp.color[0] * lamp.energy,
                lamp.color[1] * lamp.energy, lamp.color[2] * lamp.energy)
            obj_node.appendChild(color_node)

        else:
            print(
                "Lamp Object " + obj.name +
                " could not be exported. Only POINT and SUN lamps are supported."
            )

        return obj_node

    def __save_empty_object_xml(self, obj):
        name = "empty"
        if obj.towerengine.object_type == "REFLECTION_PROBE":
            name = "reflection_probe"
        obj_node = self.xml_doc.createElement(name)
        obj_node.appendChild(self.__create_transform_node_xml(
            obj.matrix_world))
        obj_node.appendChild(self.__create_delta_transform_node_xml(obj))
        return obj_node

    def __create_transform_node_xml(self, matrix):
        transform_node = self.xml_doc.createElement("transform")
        transform_node.appendChild(
            self.__create_vector_node_xml("position", matrix[0][3],
                                          matrix[2][3], -matrix[1][3]))
        transform_node.appendChild(
            self.__create_vector_node_xml("basis_x", matrix[0][0],
                                          matrix[0][2], -matrix[0][1]))
        transform_node.appendChild(
            self.__create_vector_node_xml("basis_y", matrix[2][0],
                                          matrix[2][2], -matrix[2][1]))
        transform_node.appendChild(
            self.__create_vector_node_xml("basis_z", -matrix[1][0],
                                          -matrix[1][2], matrix[1][1]))
        return transform_node

    def __create_delta_transform_node_xml(self, obj):
        node = self.xml_doc.createElement("delta_transform")
        node.appendChild(
            self.__create_vector_node_xml("position", obj.delta_location.x,
                                          obj.delta_location.z,
                                          -obj.delta_location.y))
        node.appendChild(
            self.__create_vector_node_xml("rotation",
                                          obj.delta_rotation_euler.x,
                                          obj.delta_rotation_euler.z,
                                          -obj.delta_rotation_euler.y))
        node.appendChild(
            self.__create_vector_node_xml("scale", obj.delta_scale.x,
                                          obj.delta_scale.z,
                                          -obj.delta_scale.y))
        return node

    def __create_vector_node_xml(self, name, x, y, z):
        node = self.xml_doc.createElement(name)
        node.setAttribute("x", str(x))
        node.setAttribute("y", str(y))
        node.setAttribute("z", str(z))
        return node

    def __create_color_node_xml(self, name, r, g, b):
        node = self.xml_doc.createElement(name)
        node.setAttribute("r", str(r))
        node.setAttribute("g", str(g))
        node.setAttribute("b", str(b))
        return node

    def __save_assets(self):
        self.assets_subdir = self.file_basename + "_assets"
        self.assets_dir = os.path.join(self.directory, self.assets_subdir)

        try:
            os.makedirs(self.assets_dir)
        except OSError as e:
            print(str(e))

        self.__save_meshes()
        self.__save_cubemaps()

    def __save_meshes(self):
        self.saved_mesh_files = dict()

        if self.include_mat_prop != True:
            self.mesh_exclude_materials = None

        for mesh_name, mesh in self.meshes.items():
            export_mesh = ExportMesh()
            export_mesh.add_mesh(mesh)

            mesh_file = os.path.join(self.assets_dir, mesh_name + ".tem")

            export_mesh.save(mesh_file, self.pack_tex_prop,
                             self.mesh_exclude_materials)

            self.saved_mesh_files[mesh_name] = os.path.relpath(
                mesh_file, self.directory)

    def __save_cubemaps(self):
        self.saved_cubemap_files = dict()

        for cubemap_name, tex in self.cubemaps.items():
            original_file = tex.image.filepath
            file_extension = original_file.split(".")[-1]
            tex_file = os.path.join(
                self.assets_dir,
                "cubemap_" + cubemap_name + "." + file_extension)
            try:
                shutil.copy(bpy.path.abspath(original_file), tex_file)
            except OSError as e:
                print(str(e))
            except IOError as e:
                print(str(e))
            self.saved_cubemap_files[cubemap_name] = os.path.relpath(
                tex_file, self.directory)
コード例 #45
0
ファイル: txt2xml.py プロジェクト: Li-by/my-opencv
def main():
    wrong_list = []
    for walk in os.walk(labels):
        # os.walk遍历文件夹所有的文件
        for each in walk[2]:
            print(each)
            fidin=open(walk[0] + '/'+ each,'r')
            objIndex = 0
            for data in islice(fidin, 1, None):        
                objIndex += 1
                data=data.strip('\n')
                datas = data.split(' ')
                # 审核机制
#                if 5 != len(datas):
#                    print 'bounding box information error:{}'.format(datas[0])
#                    # 检验一行,是否为5个元素,不是则报错
#                    continue
                if 5 != len(datas):
                    wrong_list.append(each)
                    # 检验一行,是否为5个元素,不是则跳过
                    pass
                
                pictureName = each.replace('txt', 'jpg')
                imageFile = imgpath + pictureName
                print(imageFile)
                img = cv2.imread(imageFile)
                imgSize = img.shape
                if 1 == objIndex:
                    xmlName = each.replace('.txt', '.xml')
                    f = open(xmlpath_new + xmlName, "w")
                    doc = Document()
                    annotation = doc.createElement('annotation')
                    doc.appendChild(annotation)
                    
                    folder = doc.createElement('folder')
                    folder.appendChild(doc.createTextNode(foldername))
                    annotation.appendChild(folder)
                    
                    filename = doc.createElement('filename')
                    filename.appendChild(doc.createTextNode(pictureName))
                    annotation.appendChild(filename)
                    
                    source = doc.createElement('source')                
                    database = doc.createElement('database')
                    database.appendChild(doc.createTextNode('My Database'))
                    source.appendChild(database)
                    source_annotation = doc.createElement('annotation')
                    source_annotation.appendChild(doc.createTextNode(foldername))
                    source.appendChild(source_annotation)
                    image = doc.createElement('image')
                    image.appendChild(doc.createTextNode('flickr'))
                    source.appendChild(image)
                    flickrid = doc.createElement('flickrid')
                    flickrid.appendChild(doc.createTextNode('NULL'))
                    source.appendChild(flickrid)
                    annotation.appendChild(source)
                    
                    owner = doc.createElement('owner')
                    flickrid = doc.createElement('flickrid')
                    flickrid.appendChild(doc.createTextNode('NULL'))
                    owner.appendChild(flickrid)
                    name = doc.createElement('name')
                    name.appendChild(doc.createTextNode('matt'))
                    owner.appendChild(name)
                    annotation.appendChild(owner)
                    
                    size = doc.createElement('size')
                    width = doc.createElement('width')
                    width.appendChild(doc.createTextNode(str(imgSize[1])))
                    size.appendChild(width)
                    height = doc.createElement('height')
                    height.appendChild(doc.createTextNode(str(imgSize[0])))
                    size.appendChild(height)
                    depth = doc.createElement('depth')
                    depth.appendChild(doc.createTextNode(str(imgSize[2])))
                    size.appendChild(depth)
                    annotation.appendChild(size)
                    
                    segmented = doc.createElement('segmented')
                    segmented.appendChild(doc.createTextNode(str(0)))
                    annotation.appendChild(segmented)
                    try:
                        annotation.appendChild(insertObject(doc, datas))
                    except:
                        wrong_list.append(each)

                else:
                    try:
                        annotation.appendChild(insertObject(doc, datas))
                    except:
                        wrong_list.append(each)
            try:
                f.write(doc.toprettyxml(indent = '    '))
                f.close()
                fidin.close()
            except:
                pass
    return wrong_list
コード例 #46
0
def CreateAtlasListxmlType2():
    dict={}
    xlsfile0 = r'C:\Users\Administrator\Desktop\大脑解剖2.0.1_0文档.xlsx'
    book = xlrd.open_workbook(xlsfile0)
    sheet0 = book.sheet_by_name('图谱编号')
    for i in range(1,sheet0.nrows):
        dict[sheet0.cell_value(i,0)]=sheet0.cell_value(i,1)

    # print(len(dict))
    doc=Document()
    SignInfoList=doc.createElement('AtlasList')
    doc.appendChild(SignInfoList)

    xlsfile=r'C:\Users\Administrator\Desktop\大脑解剖2.0.1_0文档.xlsx'
    book =xlrd.open_workbook(xlsfile)
    sheet1=book.sheet_by_name('Book02文档')
    count =sheet1.nrows
    colume=sheet1.ncols
    atlasArray=[]
    # 总行数
    # print(count)
    for i in range(1,count):
        if(sheet1.cell_value(i,0) not in atlasArray):
            atlasArray.append(sheet1.cell_value(i, 0))  #不存在图谱  新建节点元素
            atlas=doc.createElement('Atlas')
            temp=str(sheet1.cell_value(i ,0))
            atlas.setAttribute('AtlasId',temp)
            atlas.setAttribute('Chinese', dict.get(temp))
            atlas.setAttribute('DingziName', temp)
            atlas.setAttribute('version', '0')
            atlas.setAttribute('PictureName',temp)
            atlas.setAttribute('minDis', '0')
            atlas.setAttribute('maxDis', '0')
            atlas.setAttribute('signPositionX', '0')
            atlas.setAttribute('signPositionY', '0')
            atlas.setAttribute('signDistance', '0')
            atlas.setAttribute('signSize', '20')
            atlas.setAttribute('cameraBackXRot', '0')
            atlas.setAttribute('cameraBackYRot', '0')
            atlas.setAttribute('cameraBackZRot', '0')
            atlas.setAttribute('R', '255')
            atlas.setAttribute('G', '0')
            atlas.setAttribute('B', '255')
            atlas.setAttribute('fieldOfView', '2')
            SignInfoList.appendChild(atlas)
        SignElement = doc.createElement('model')
        # SignElement.setAttribute('AtlasId', sheet1.cell_value(i, 0))
        SignElement.setAttribute('ModelName', sheet1.cell_value(i, 1))
        SignElement.setAttribute('IsTranslucent', str(sheet1.cell_value(i, 2)))
        atlas.appendChild(SignElement)
    doc.appendChild(SignInfoList)
    f=open(r'C:\Users\Administrator\Desktop\AtlasList.xml','w',encoding='utf-8')
    doc.writexml(f,addindent='  ',newl='\n',encoding='utf-8')
コード例 #47
0
ファイル: kitti2voc.py プロジェクト: busyboxs/KITTI-progress
def generate_xml(name, lines, img_size=(370, 1224, 3),
                 class_sets=('pedestrian', 'cyclist'),
                 doncareothers=True):
    """
    Write annotations into voc xml format.
    Examples:
        In: 0000001.txt
            cls        truncated    occlusion   angle   boxes                         3d annotation...
            Pedestrian 0.00         0           -0.20   712.40 143.00 810.73 307.92   1.89 0.48 1.20 1.84 1.47 8.41 0.01
        Out: 0000001.xml
            <annotation>
                <folder>VOC2007</folder>
                <filename>000001.jpg</filename>
                <source>
                ...
                <object>
                    <name>Pedestrian</name>
                    <pose>Left</pose>
                    <truncated>1</truncated>
                    <difficult>0</difficult>
                    <bndbox>
                        <xmin>x1</xmin>
                        <ymin>y1</ymin>
                        <xmax>x2</xmax>
                        <ymax>y2</ymax>
                    </bndbox>
                </object>
            </annotation>
    :param name: stem name of an image, example: 0000001
    :param lines: lines in kitti annotation txt
    :param img_size: [height, width, channle]
    :param class_sets: ('Pedestrian', 'Car', 'Cyclist')
    :return:
    """

    doc = Document()

    def append_xml_node_attr(child, parent=None, text=None):
        ele = doc.createElement(child)
        if text is not None:
            text_node = doc.createTextNode(text)
            ele.appendChild(text_node)
        parent = doc if parent is None else parent
        parent.appendChild(ele)
        return ele

    img_name = name + '.jpg'

    # create header
    annotation = append_xml_node_attr('annotation')
    append_xml_node_attr('folder', parent=annotation, text='KITTI')
    append_xml_node_attr('filename', parent=annotation, text=img_name)
    source = append_xml_node_attr('source', parent=annotation)
    append_xml_node_attr('database', parent=source, text='KITTI')
    append_xml_node_attr('annotation', parent=source, text='KITTI')
    append_xml_node_attr('image', parent=source, text='KITTI')
    append_xml_node_attr('flickrid', parent=source, text='000000')
    owner = append_xml_node_attr('owner', parent=annotation)
    append_xml_node_attr('url', parent=owner, text='http://www.cvlibs.net/datasets/kitti/index.php')
    size = append_xml_node_attr('size', annotation)
    append_xml_node_attr('width', size, str(img_size[1]))
    append_xml_node_attr('height', size, str(img_size[0]))
    append_xml_node_attr('depth', size, str(img_size[2]))
    append_xml_node_attr('segmented', parent=annotation, text='0')

    # create objects
    objs = []
    for line in lines:
        splitted_line = line.strip().lower().split()
        cls = splitted_line[0].lower()
        if cls in ['van', 'truck']:  # add van and truck class to car
            cls = 'car'
        if cls in ['person_sitting']:
            cls = 'pedestrian'
        if not doncareothers and cls not in class_sets:
            continue
        cls = 'dontcare' if cls not in class_sets else cls
        obj = append_xml_node_attr('object', parent=annotation)
        occlusion = int(float(splitted_line[2]))  # 0,1,2,3
        x1, y1, x2, y2 = int(float(splitted_line[4]) + 1), int(float(splitted_line[5]) + 1), \
                         int(float(splitted_line[6]) + 1), int(float(splitted_line[7]) + 1)
        truncation = float(splitted_line[1])  # 0~1 float
        difficult = 1 if _is_hard(cls, truncation, occlusion, x1, y1, x2, y2) else 0
        truncted = 0 if truncation < 0.5 else 1

        append_xml_node_attr('name', parent=obj, text=cls)
        append_xml_node_attr('pose', parent=obj, text='Left')
        append_xml_node_attr('truncated', parent=obj, text=str(truncted))
        append_xml_node_attr('difficult', parent=obj, text=str(int(difficult)))
        bb = append_xml_node_attr('bndbox', parent=obj)
        append_xml_node_attr('xmin', parent=bb, text=str(x1))
        append_xml_node_attr('ymin', parent=bb, text=str(y1))
        append_xml_node_attr('xmax', parent=bb, text=str(x2))
        append_xml_node_attr('ymax', parent=bb, text=str(y2))

        o = {'class': cls, 'box': np.asarray([x1, y1, x2, y2], dtype=float), \
             'truncation': truncation, 'difficult': difficult, 'occlusion': occlusion}
        objs.append(o)

    return doc, objs
コード例 #48
0
def CreateAtlasElement(atlasID):
    dict111={}
    xlsfile0 = r'C:\Users\Administrator\Desktop\运动系统2.0.1图钉bookmarktype02.xlsx'
    book = xlrd.open_workbook(xlsfile0)
    sheet0 = book.sheet_by_name('111')
    for i in range(1,sheet0.nrows):
        dict111[sheet0.cell_value(i,0)]=sheet0.cell_value(i,1)
    # dict222={}
    # xlsfile0 = r'C:\Users\Administrator\Desktop\运动系统2.0.1图钉bookmarktype02.xlsx'
    # book = xlrd.open_workbook(xlsfile0)
    # sheet0 = book.sheet_by_name('222')
    # for i in range(1,sheet0.nrows):
    #     dict222[sheet0.cell_value(i,0)]=sheet0.cell_value(i,1)
    doc = Document()
    SignInfoList = doc.createElement('AtlasList')
    doc.appendChild(SignInfoList)
    xlsfile = r'C:\Users\Administrator\Desktop\运动系统2.0.1图钉bookmarktype02.xlsx'
    book = xlrd.open_workbook(xlsfile)
    sheet1 = book.sheet_by_name('type2')
    count = sheet1.nrows
    atlasArray = []
    nameType2Dict={}
    for i in range(1,count):
        if(sheet1.cell_value(i,0) not in atlasArray):
            atlasArray.append(sheet1.cell_value(i,0))
        for j in atlasArray:
            t2=type2(j)
            t2.namelist=[]
            for i in range(1, count):
                if(sheet1.cell_value(i,0) == j):
                    t2.namelist.append(sheet1.cell_value(i,1))
            nameType2Dict[j]=t2
    mainkeys=nameType2Dict.keys()

    atlas = doc.createElement('Atlas')
    atlas.setAttribute('AtlasId', atlasID)
    atlas.setAttribute('Chinese', dict111.get(atlasID))
    atlas.setAttribute('DingziName',atlasID)
    atlas.setAttribute('version', '0')
    atlas.setAttribute('PictureName',atlasID)
    atlas.setAttribute('minDis', '0')
    atlas.setAttribute('maxDis', '0')
    atlas.setAttribute('signPositionX', '0')
    atlas.setAttribute('signPositionY', '0')
    atlas.setAttribute('signDistance', '0')
    atlas.setAttribute('signSize', '20')
    atlas.setAttribute('cameraBackXRot', '0')
    atlas.setAttribute('cameraBackYRot', '0')
    atlas.setAttribute('cameraBackZRot', '0')
    atlas.setAttribute('R', '255')
    atlas.setAttribute('G', '0')
    atlas.setAttribute('B', '255')
    atlas.setAttribute('fieldOfView', '2')
    SignInfoList.appendChild(atlas)
    # print(i)
    #解析对应type2 模型p
    t2=nameType2Dict.get(atlasID)
    if(len(t2.namelist)>0):
        for j in t2.namelist :
            SignElement = doc.createElement('model')
            SignElement.setAttribute('ModelName', j)
            SignElement.setAttribute('IsTranslucent', '0')
            atlas.appendChild(SignElement)

    doc.appendChild(SignInfoList)
    f=open(r'C:\Users\Administrator\Desktop\atlaselement.xml','w',encoding='utf-8')
    doc.writexml(f,addindent='  ',newl='\n',encoding='utf-8')
コード例 #49
0
def generate_xml(name, split_lines, img_size, class_ind):
    doc = Document()  # 创建DOM文档对象

    annotation = doc.createElement('annotation')
    doc.appendChild(annotation)

    title = doc.createElement('folder')
    title_text = doc.createTextNode('CarRecognization')
    title.appendChild(title_text)
    annotation.appendChild(title)

    img_name = name + '.jpg'

    title = doc.createElement('filename')
    title_text = doc.createTextNode(img_name)
    title.appendChild(title_text)
    annotation.appendChild(title)

    source = doc.createElement('source')
    annotation.appendChild(source)

    title = doc.createElement('database')
    title_text = doc.createTextNode('The Car Database')
    title.appendChild(title_text)
    source.appendChild(title)

    title = doc.createElement('annotation')
    title_text = doc.createTextNode('CarRecognization')
    title.appendChild(title_text)
    source.appendChild(title)

    size = doc.createElement('size')
    annotation.appendChild(size)

    title = doc.createElement('width')
    title_text = doc.createTextNode(str(img_size[1]))
    title.appendChild(title_text)
    size.appendChild(title)

    title = doc.createElement('height')
    title_text = doc.createTextNode(str(img_size[0]))
    title.appendChild(title_text)
    size.appendChild(title)

    title = doc.createElement('depth')
    title_text = doc.createTextNode(str(img_size[2]))
    title.appendChild(title_text)
    size.appendChild(title)

    for split_line in split_lines:
        line = split_line.strip().split()
        if line[0] in class_ind:
            object = doc.createElement('object')
            annotation.appendChild(object)

            title = doc.createElement('name')
            title_text = doc.createTextNode(line[0])
            title.appendChild(title_text)
            object.appendChild(title)

            bndbox = doc.createElement('bndbox')
            object.appendChild(bndbox)
            title = doc.createElement('xmin')
            title_text = doc.createTextNode(str(int(float(line[1]))))
            title.appendChild(title_text)
            bndbox.appendChild(title)
            title = doc.createElement('xmax')
            title_text = doc.createTextNode(str(int(float(line[2]))))
            title.appendChild(title_text)
            bndbox.appendChild(title)
            title = doc.createElement('ymin')
            title_text = doc.createTextNode(str(int(float(line[3]))))
            title.appendChild(title_text)
            bndbox.appendChild(title)
            title = doc.createElement('ymax')
            title_text = doc.createTextNode(str(int(float(line[4]))))
            title.appendChild(title_text)
            bndbox.appendChild(title)

    # 将DOM对象doc写入文件
    f = open(
        '/home/maozezhong/Desktop/交通卡口车辆信息精准识别/car-recoganization-competition/darknet/scripts/VOCdevkit/VOC2018/Annotation/'
        + name + '.xml', 'w')
    f.write(doc.toprettyxml(indent=''))
    f.close()
コード例 #50
0
    def get_ur(self, withhold_dns=False):
        '''
        Returns the JobRecord in CAR format. See
        https://twiki.cern.ch/twiki/bin/view/EMI/ComputeAccounting

        Namespace information is written only once per record, by dbunloader.
        '''
        record_id = self.get_field('MachineName') + ' ' + self.get_field(
            'LocalJobId') + ' ' + str(self.get_field('EndTime'))

        # Create the document directly
        doc = Document()
        ur = doc.createElement('urf:UsageRecord')

        rec_id = doc.createElement('urf:RecordIdentity')
        rec_id.setAttribute('urf:createTime',
                            datetime.now().strftime('%Y-%m-%dT%H:%M:%S'))
        rec_id.setAttribute('urf:recordId', record_id)
        ur.appendChild(rec_id)

        job_id = doc.createElement('urf:JobIdentity')
        local_job_id = doc.createElement('urf:LocalJobId')
        text = doc.createTextNode(self.get_field('LocalJobId'))
        local_job_id.appendChild(text)
        job_id.appendChild(local_job_id)
        ur.appendChild(job_id)

        user_id = doc.createElement('urf:UserIdentity')

        if self.get_field('GlobalUserName') is not None:
            if withhold_dns:
                dn = WITHHELD_DN
            else:
                dn = self.get_field('GlobalUserName')
            global_user_name = doc.createElement('urf:GlobalUserName')
            global_user_name.appendChild(doc.createTextNode(dn))
            global_user_name.setAttribute('urf:type', 'opensslCompat')
            user_id.appendChild(global_user_name)

        if self.get_field('VO') is not None:
            group = doc.createElement('urf:Group')
            group.appendChild(doc.createTextNode(self.get_field('VO')))
            user_id.appendChild(group)

        if self.get_field('FQAN') is not None:
            fqan = doc.createElement('urf:GroupAttribute')
            fqan.setAttribute('urf:type', 'FQAN')
            fqan.appendChild(doc.createTextNode(self.get_field('FQAN')))
            user_id.appendChild(fqan)

        if self.get_field('VOGroup') is not None:
            vogroup = doc.createElement('urf:GroupAttribute')
            vogroup.setAttribute('urf:type', 'vo-group')
            vogroup.appendChild(doc.createTextNode(self.get_field('VOGroup')))
            user_id.appendChild(vogroup)

        if self.get_field('VORole') is not None:
            vorole = doc.createElement('urf:GroupAttribute')
            vorole.setAttribute('urf:type', 'vo-role')
            vorole.appendChild(doc.createTextNode(self.get_field('VORole')))
            user_id.appendChild(vorole)

        if self.get_field('LocalUserId') is not None:
            local_user_id = doc.createElement('urf:LocalUserId')
            local_user_id.appendChild(
                doc.createTextNode(self.get_field('LocalUserId')))
            user_id.appendChild(local_user_id)

        ur.appendChild(user_id)

        status = doc.createElement('urf:Status')
        status.appendChild(doc.createTextNode('completed'))
        ur.appendChild(status)

        infra = doc.createElement('urf:Infrastructure')
        infra.setAttribute('urf:type', self.get_field('InfrastructureType'))
        ur.appendChild(infra)

        wall = doc.createElement('urf:WallDuration')
        wall.appendChild(
            doc.createTextNode('PT' + str(self.get_field('WallDuration')) +
                               'S'))
        ur.appendChild(wall)

        cpu = doc.createElement('urf:CpuDuration')
        cpu.setAttribute('urf:usageType', 'all')
        cpu.appendChild(
            doc.createTextNode('PT' + str(self.get_field('CpuDuration')) +
                               'S'))
        ur.appendChild(cpu)

        service_level = doc.createElement('urf:ServiceLevel')
        service_level.setAttribute('urf:type',
                                   self.get_field('ServiceLevelType'))
        service_level.appendChild(
            doc.createTextNode(str(self.get_field('ServiceLevel'))))
        ur.appendChild(service_level)

        if self.get_field('MemoryReal') > 0:
            pmem = doc.createElement('urf:Memory')
            pmem.setAttribute('urf:type', 'Physical')
            pmem.setAttribute('urf:storageUnit', 'KB')
            pmem.appendChild(
                doc.createTextNode(str(self.get_field('MemoryReal'))))
            ur.appendChild(pmem)

        if self.get_field('MemoryVirtual') > 0:
            vmem = doc.createElement('urf:Memory')
            vmem.setAttribute('urf:type', 'Shared')
            vmem.setAttribute('urf:storageUnit', 'KB')
            vmem.appendChild(
                doc.createTextNode(str(self.get_field('MemoryVirtual'))))
            ur.appendChild(vmem)

        if self.get_field('NodeCount') > 0:
            ncount = doc.createElement('urf:NodeCount')
            ncount.appendChild(
                doc.createTextNode(str(self.get_field('NodeCount'))))
            ur.appendChild(ncount)

        if self.get_field('Processors') > 0:
            procs = doc.createElement('urf:Processors')
            procs.appendChild(
                doc.createTextNode(str(self.get_field('Processors'))))
            ur.appendChild(procs)

        end = doc.createElement('urf:EndTime')
        end_text = time.strftime('%Y-%m-%dT%H:%M:%SZ',
                                 self.get_field('EndTime').timetuple())
        end.appendChild(doc.createTextNode(end_text))
        ur.appendChild(end)

        start = doc.createElement('urf:StartTime')
        start_text = time.strftime('%Y-%m-%dT%H:%M:%SZ',
                                   self.get_field('StartTime').timetuple())
        start.appendChild(doc.createTextNode(start_text))
        ur.appendChild(start)

        machine = doc.createElement('urf:MachineName')
        machine.appendChild(doc.createTextNode(self.get_field('MachineName')))
        ur.appendChild(machine)

        if self.get_field('SubmitHost') is not None:
            subhost = doc.createElement('urf:SubmitHost')
            subhost.appendChild(
                doc.createTextNode(self.get_field('SubmitHost')))
            ur.appendChild(subhost)

        queue = doc.createElement('urf:Queue')
        queue.appendChild(doc.createTextNode(str(self.get_field('Queue'))))
        ur.appendChild(queue)

        site = doc.createElement('urf:Site')
        site.appendChild(doc.createTextNode(self.get_field('Site')))
        ur.appendChild(site)

        doc.appendChild(ur)
        # We don't want the XML declaration, because the whole XML
        # document will be assembled by another part of the program.
        return doc.documentElement.toxml()
コード例 #51
0
def generate_xml(img_name, img_size, bbox, annotation_path):
    print("writing to {}.xml".format(img_name[:-4]))
    doc = Document()

    annotation = doc.createElement('annotation')
    doc.appendChild(annotation)

    title = doc.createElement('folder')
    title_text = doc.createTextNode('trees')
    title.appendChild(title_text)
    annotation.appendChild(title)

    title = doc.createElement('filename')
    title_text = doc.createTextNode(img_name)
    title.appendChild(title_text)
    annotation.appendChild(title)

    source = doc.createElement('source')
    annotation.appendChild(source)

    title = doc.createElement('database')
    title_text = doc.createTextNode('RGB-D trees db')
    title.appendChild(title_text)
    source.appendChild(title)

    title = doc.createElement('annotation')
    title_text = doc.createTextNode('RGB-D trees annotation')
    title.appendChild(title_text)
    source.appendChild(title)

    size = doc.createElement('size')
    annotation.appendChild(size)

    title = doc.createElement('width')
    title_text = doc.createTextNode(str(img_size[0]))
    title.appendChild(title_text)
    size.appendChild(title)

    title = doc.createElement('height')
    title_text = doc.createTextNode(str(img_size[1]))
    title.appendChild(title_text)
    size.appendChild(title)

    title = doc.createElement('depth')
    title_text = doc.createTextNode(str(img_size[2]))
    title.appendChild(title_text)
    size.appendChild(title)

    for xmin, ymin, xmax, ymax in bbox:
        object = doc.createElement('object')
        annotation.appendChild(object)

        title = doc.createElement('name')
        title_text = doc.createTextNode('tree')
        title.appendChild(title_text)
        object.appendChild(title)

        bndbox = doc.createElement('bndbox')
        object.appendChild(bndbox)

        title = doc.createElement('xmin')
        title_text = doc.createTextNode(str(xmin))
        title.appendChild(title_text)
        bndbox.appendChild(title)
        title = doc.createElement('ymin')
        title_text = doc.createTextNode(str(ymin))
        title.appendChild(title_text)
        bndbox.appendChild(title)
        title = doc.createElement('xmax')
        title_text = doc.createTextNode(str(xmax))
        title.appendChild(title_text)
        bndbox.appendChild(title)
        title = doc.createElement('ymax')
        title_text = doc.createTextNode(str(ymax))
        title.appendChild(title_text)
        bndbox.appendChild(title)

    f = open(annotation_path + img_name[:-4] + '.xml', 'w')

    f.write(doc.toprettyxml(indent='\t'))
    f.close()
コード例 #52
0
class KMLFromCSVPoints(object):
    def __init__(self, csv_points, output_file=None, icon=None):
        """class initializer"""
        self.d = Document()
        self.csv = csv_points
        self.initial_position = (38.35, -99.1, 3862426)
        self.date_format_ge = '%Y-%m-%d'  # Google Earth date format
        self.date_range = (datetime.now(), datetime.now() + timedelta(days=1))
        self.icon = icon
        self.find_initial_position()
        self.get_date_range()

        # if no output file path, add '.kml' to input file path
        if output_file:
            self.output_file = output_file
        else:
            self.output_file = self.csv.input_file[:-3] + 'kml'

    def find_initial_position(self):
        """Find the Geometric Center of all the lat/lon points.
        Also find the Altitude needed to view all the points.
        This will be used to initially center the Google Earth view.
        """
        # find the lat/lon of all points
        lats = []
        lons = []
        for r in self.csv.rows:
            lats.append(float(r[self.csv.lat_col]))
            lons.append(float(r[self.csv.lon_col]))

        # find the (unweighted) center of the collection of points
        mid_lat = (max(lats) + min(lats)) / 2.0
        mid_lon = (max(lons) + min(lons)) / 2.0

        # use empirical formula to calculate the altitude of the initial view
        max_diff = max(max(lats) - min(lats), max(lons) - min(lons))
        alt = int(1000.0 * (125.5 * max_diff - 14.0))
        alt = max(10000, alt)  # set a lower limit of 10km
        alt = min(alt, 10000000)  # set an upper limit of 10,000km

        self.initial_position = (mid_lat, mid_lon, alt)

    def get_date_range(self):
        """Produce master date range for all rows in the CSV."""
        # if not date column in file, skip finding a range
        if self.csv.date_time_col == -1:
            return

        # get all dates from file
        dates = []
        for r in self.csv.rows:
            d = r[self.csv.date_time_col][:8]
            d = datetime.strptime(d, self.csv.date_format_in)
            dates.append(d)

        # find max and min dates
        min_d = min(dates)
        max_d = max(dates)

        self.date_range = (min_d, max_d + timedelta(days=1))

    def google_start_date(self):
        """Helper method to get begin date in Google Earth format."""
        return self.date_range[0].strftime(self.date_format_ge)

    def google_end_date(self):
        """Helper method to get end date in Google Earth format."""
        return self.date_range[1].strftime(self.date_format_ge)

    def create_kml(self):
        """Create lowest-level tags: kml & Document. Add styles.
        Then loop through the CSV rows and add placemarks.
        """
        base = self.d.createElement('kml')
        base.setAttribute('xmlns', 'http://earth.google.com/kml/2.2')
        self.d.appendChild(base)
        doc = self.d.createElement('Document')
        base.appendChild(doc)

        self.add_master_timespan(doc)
        self.add_initial_lookat(doc)
        self.add_style_map(doc)
        self.add_styles(doc)

        for row in self.csv.rows:
            self.add_single_placemark(doc, row)

    def add_element(self, *args):
        """simple helper method to reduce the amount of code duplication
        while adding final 1 (or 2)-deep elements that contain text
        """
        if len(args) == 3:
            new_node = self.d.createElement(args[1])
            text_node = self.d.createTextNode(args[2])
            new_node.appendChild(text_node)
            args[0].appendChild(new_node)
        elif len(args) == 4:
            new_master_node = self.d.createElement(args[1])
            new_node = self.d.createElement(args[2])
            text_node = self.d.createTextNode(args[3])
            new_node.appendChild(text_node)
            new_master_node.appendChild(new_node)
            args[0].appendChild(new_master_node)
        else:
            exit('The add_element method requires 3 or 4 arguments.')

    def add_master_timespan(self, doc):
        """The standard KML TimeSpan reference can be found at:
        https://developers.google.com/kml/documentation/kmlreference#timespan
        """
        master_timespan = self.d.createElement('TimeSpan')
        doc.appendChild(master_timespan)

        self.add_element(master_timespan, 'begin', self.google_start_date())
        self.add_element(master_timespan, 'end', self.google_end_date())

    def add_initial_lookat(self, doc):
        """The LookAt sets where the screen is initally centered.
        The standard KML LookAt reference can be found at:
        https://developers.google.com/kml/documentation/kmlreference#lookat
        """
        lookat = self.d.createElement('LookAt')
        doc.appendChild(lookat)

        # create local timespan
        lookat_timespan = self.d.createElement('TimeSpan')
        lookat.appendChild(lookat_timespan)

        self.add_element(lookat_timespan, 'begin', self.google_start_date())
        self.add_element(lookat_timespan, 'end', self.google_end_date())

        # create lat/lon tags
        self.add_element(lookat, 'report_latitude',
                         str(self.initial_position[0]))
        self.add_element(lookat, 'report_longitude',
                         str(self.initial_position[1]))
        self.add_element(lookat, 'altitude', str(self.initial_position[2]))
        self.add_element(lookat, 'altitudeMode', 'relativeToGround')

    def add_style_map(self, doc):
        """The standard KML StyleMap reference can be found at:
        https://developers.google.com/kml/documentation/kmlreference#stylemap
        """
        style_map = self.d.createElement('StyleMap')
        style_map.setAttribute('id', 'point')
        doc.appendChild(style_map)

        p1 = self.d.createElement('Pair')
        self.add_element(p1, 'key', 'normal')
        self.add_element(p1, 'styleUrl', '#point_unfocused')
        style_map.appendChild(p1)

        p2 = self.d.createElement('Pair')
        self.add_element(p2, 'key', 'highlight')
        self.add_element(p2, 'styleUrl', '#point_focused')
        style_map.appendChild(p2)

    def add_styles(self, doc):
        """The standard KML Style reference can be found at:
        https://developers.google.com/kml/documentation/kmlreference#style
        """
        self.add_focused_style(doc)
        self.add_unfocused_style(doc)

    def add_focused_style(self, doc):
        """create Point Focused Style"""
        style_foc = self.d.createElement('Style')
        style_foc.setAttribute('id', 'point_focused')

        self.add_element(style_foc, 'LabelStyle', 'scale', '1.0')

        icon_style = self.d.createElement('IconStyle')
        self.add_element(icon_style, 'scale', '1.0')
        self.add_element(icon_style, 'heading', '0.0')

        icon = self.d.createElement('Icon')
        self.add_element(icon, 'href', self.icon)
        self.add_element(icon, 'refreshInterval', '0.0')
        self.add_element(icon, 'viewRefreshTime', '0.0')
        self.add_element(icon, 'viewBoundScale', '0.0')
        icon_style.appendChild(icon)
        style_foc.appendChild(icon_style)
        doc.appendChild(style_foc)

    def add_unfocused_style(self, doc):
        """create Point Unfocused Style"""
        style_unfoc = self.d.createElement('Style')
        style_unfoc.setAttribute('id', 'point_unfocused')

        self.add_element(style_unfoc, 'LabelStyle', 'scale', '0.0')

        icon_style = self.d.createElement('IconStyle')
        self.add_element(icon_style, 'scale', '1.0')
        self.add_element(icon_style, 'heading', '0.0')

        icon = self.d.createElement('Icon')
        self.add_element(icon, 'href', self.icon)
        self.add_element(icon, 'refreshInterval', '0.0')
        self.add_element(icon, 'viewRefreshTime', '0.0')
        self.add_element(icon, 'viewBoundScale', '0.0')
        icon_style.appendChild(icon)
        style_unfoc.appendChild(icon_style)
        doc.appendChild(style_unfoc)

    def add_single_placemark(self, doc, csv_row):
        """Create a single Placemark (one for each point).
        The standard KML Placemark reference can be found at:
        https://developers.google.com/kml/documentation/kmlreference#placemark
        """
        # get date info
        if self.csv.date_time_col == -1:
            start = datetime.now()
            end = datetime.now() + timedelta(days=1)
        else:
            date = csv_row[self.csv.date_time_col][:8]
            start = datetime.strptime(date, self.csv.date_format_in)
            end = start + timedelta(days=1)

        date_start = start.strftime(self.date_format_ge)
        date_end = end.strftime(self.date_format_ge)

        # get location info
        lat = str(csv_row[self.csv.lat_col])
        lon = str(csv_row[self.csv.lon_col])
        alt = '0'

        # create placemark
        placemark = self.d.createElement('Placemark')
        doc.appendChild(placemark)

        self.add_element(placemark, 'name', csv_row[0] + ' on ' + date_start)

        # fill description with HTML table of all the CSV columns
        desc = self.d.createElement('description')
        table = self.add_html_table(csv_row)
        desc.appendChild(table)
        placemark.appendChild(desc)

        point_timespan = self.d.createElement('TimeSpan')
        placemark.appendChild(point_timespan)
        self.add_element(point_timespan, 'begin', date_start)
        self.add_element(point_timespan, 'end', date_end)

        self.add_element(placemark, 'styleUrl', '#point')

        coord = lon + ',' + lat + ',' + alt
        self.add_element(placemark, 'Point', 'coordinates', coord)

    def add_html_table(self, csv_row):
        """add HTML table to description"""
        table = self.d.createElement('table')
        table.appendChild(self.add_table_header())

        # loop over all point attributes (CSV columns)
        col_num = 0
        for col in csv_row:
            table.appendChild(self.add_table_row(self.csv.header[col_num],
                                                 col))
            col_num += 1

        return table

    def add_table_row(self, key, value):
        """Add an HTML row for each CSV column."""
        new_row = self.d.createElement('tr')

        self.add_element(new_row, 'td', key)
        self.add_element(new_row, 'td', value)

        return new_row

    def add_table_header(self):
        """Add a Bold HTML header to the placemark header."""
        new_row = self.d.createElement('tr')

        self.add_element(new_row, 'td', 'b', 'Field')
        self.add_element(new_row, 'td', 'b', 'Value')

        return new_row

    def write_kml(self):
        """Write out the final KML file."""
        f = open(self.output_file, 'w')
        #f.write(self.d.toxml(encoding='utf-8'))
        f.write(self.d.toxml())
        f.close()
コード例 #53
0
ファイル: MainView.py プロジェクト: zhishang80/code_saturne
    def SaveState(self):
        """
        """
        newdoc = Document()

        root = newdoc.createElement('root')
        newdoc.appendChild(root)

        newnode = newdoc.createElement('timeRefresh')
        newnode.setAttribute("value", str(self.timeRefresh))
        root.appendChild(newnode)
        newnode = newdoc.createElement('subplotNumber')
        newnode.setAttribute("value", str(self.subplotNumber))
        root.appendChild(newnode)

        for (name, fle, status, subplot_id, probes_number) in self.fileList:
            newnode = newdoc.createElement('file')
            newnode.setAttribute("name", name)
            root.appendChild(newnode)

            for itt in self.listFileProbes[name]:
                node = newdoc.createElement('probe')
                node.setAttribute("id", str(itt.index))
                node.setAttribute("name", itt.name)
                node.setAttribute("status", itt.status)
                node.setAttribute("subplot_id", str(itt.subplot_id))
                newnode.appendChild(node)

        name = os.path.join(self.caseName, '.trackcvg.state')
        ficIn = open(name, 'w')

        newdoc.writexml(ficIn, indent="  ", addindent="  ", newl='\n')

        newdoc.unlink()
        ficIn.close()
コード例 #54
0
ファイル: io_nori.py プロジェクト: hchoi405/nori
class NoriWriter:
    def __init__(self, context, filepath):
        self.context = context
        self.filepath = filepath
        self.working_dir = os.path.dirname(self.filepath)

    def create_xml_element(self, name, attr):
        el = self.doc.createElement(name)
        for k, v in attr.items():
            el.setAttribute(k, v)
        return el

    def create_xml_entry(self, t, name, value):
        return self.create_xml_element(t, {"name": name, "value": value})

    def create_xml_transform(self, mat, el=None):
        transform = self.create_xml_element("transform", {"name": "toWorld"})
        if (el):
            transform.appendChild(el)
        value = ""
        for j in range(4):
            for i in range(4):
                value += str(mat[j][i]) + ","
        transform.appendChild(
            self.create_xml_element("matrix", {"value": value[:-1]}))
        return transform

    def create_xml_mesh_entry(self, filename):
        meshElement = self.create_xml_element("mesh", {"type": "obj"})
        meshElement.appendChild(
            self.create_xml_element("string", {
                "name": "filename",
                "value": "meshes/" + filename
            }))
        return meshElement

    def write(self):
        """Main method to write the blender scene into Nori format"""

        n_samples = 32
        # create xml document
        self.doc = Document()
        self.scene = self.doc.createElement("scene")
        self.doc.appendChild(self.scene)

        # 1) write integrator configuration
        self.scene.appendChild(
            self.create_xml_element("integrator", {"type": "normals"}))

        # 2) write sampler
        sampler = self.create_xml_element("sampler", {"type": "independent"})
        sampler.appendChild(
            self.create_xml_element("integer", {
                "name": "sampleCount",
                "value": str(n_samples)
            }))
        self.scene.appendChild(sampler)

        # 3) export one camera
        cameras = [
            cam for cam in self.context.scene.objects
            if cam.type in {'CAMERA'}
        ]
        if (len(cameras) == 0):
            print("WARN: No camera to export")
        else:
            if (len(cameras) > 1):
                print(
                    "WARN: Does not handle multiple camera, only export the active one"
                )
            self.scene.appendChild(self.write_camera(
                self.context.scene.camera))  # export the active one

        # 4) export all meshes
        if not os.path.exists(self.working_dir + "/meshes"):
            os.makedirs(self.working_dir + "/meshes")

        meshes = [
            obj for obj in self.context.scene.objects
            if obj.type in {'MESH', 'FONT', 'SURFACE', 'META'}
        ]
        print(meshes)
        for mesh in meshes:
            self.write_mesh(mesh)

        # 6) write the xml file
        self.doc.writexml(open(self.filepath, "w"), "", "\t", "\n")

    def write_camera(self, cam):
        """convert the selected camera (cam) into xml format"""
        camera = self.create_xml_element("camera", {"type": "perspective"})
        camera.appendChild(
            self.create_xml_entry("float", "fov",
                                  str(cam.data.angle * 180 / math.pi)))
        camera.appendChild(
            self.create_xml_entry("float", "nearClip",
                                  str(cam.data.clip_start)))
        camera.appendChild(
            self.create_xml_entry("float", "farClip", str(cam.data.clip_end)))
        percent = self.context.scene.render.resolution_percentage / 100.0
        camera.appendChild(
            self.create_xml_entry(
                "integer", "width",
                str(int(self.context.scene.render.resolution_x * percent))))
        camera.appendChild(
            self.create_xml_entry(
                "integer", "height",
                str(int(self.context.scene.render.resolution_y * percent))))

        mat = cam.matrix_world

        # Conversion to Y-up coordinate system
        coord_transf = bpy_extras.io_utils.axis_conversion(from_forward='Y',
                                                           from_up='Z',
                                                           to_forward='-Z',
                                                           to_up='Y').to_4x4()
        mat = coord_transf @ mat
        pos = mat.translation
        # Nori's camera needs this these coordinates to be flipped
        m = Matrix([[-1, 0, 0, 0], [0, 1, 0, 0], [0, 0, -1, 0], [0, 0, 0, 0]])
        t = mat.to_3x3() @ m.to_3x3()
        mat = Matrix([[t[0][0], t[0][1], t[0][2], pos[0]],
                      [t[1][0], t[1][1], t[1][2], pos[1]],
                      [t[2][0], t[2][1], t[2][2], pos[2]], [0, 0, 0, 1]])
        trans = self.create_xml_transform(mat)
        camera.appendChild(trans)
        return camera

    def write_mesh(self, mesh):
        viewport_selection = self.context.selected_objects
        bpy.ops.object.select_all(action='DESELECT')

        obj_name = mesh.name + ".obj"
        obj_path = os.path.join(self.working_dir, 'meshes', obj_name)
        mesh.select_set(True)
        bpy.ops.export_scene.obj(filepath=obj_path,
                                 check_existing=False,
                                 use_selection=True,
                                 use_edges=False,
                                 use_smooth_groups=False,
                                 use_materials=False,
                                 use_triangles=True,
                                 use_mesh_modifiers=True)
        mesh.select_set(False)

        # Add the corresponding entry to the xml
        mesh_element = self.create_xml_mesh_entry(obj_name)
        # We currently just export a default material, a more complex material conversion
        # could be implemented following: http://tinyurl.com/nnhxwuh
        bsdf_element = self.create_xml_element("bsdf", {"type": "diffuse"})
        bsdf_element.appendChild(
            self.create_xml_entry("color", "albedo", "0.75,0.75,0.75"))
        mesh_element.appendChild(bsdf_element)
        self.scene.appendChild(mesh_element)

        for ob in viewport_selection:
            ob.select_set(True)
コード例 #55
0
def generate_xml(name, split_lines, img_size, class_ind):
    doc = Document()  #创建DOM文档对象

    annotation = doc.createElement('annotation')
    doc.appendChild(annotation)

    title = doc.createElement('folder')
    title_text = doc.createTextNode('KITTI')
    title.appendChild(title_text)
    annotation.appendChild(title)

    img_name = name + '.png'

    title = doc.createElement('filename')
    title_text = doc.createTextNode(img_name)
    title.appendChild(title_text)
    annotation.appendChild(title)

    source = doc.createElement('source')
    annotation.appendChild(source)

    title = doc.createElement('database')
    title_text = doc.createTextNode('The KITTI Database')
    title.appendChild(title_text)
    source.appendChild(title)

    title = doc.createElement('annotation')
    title_text = doc.createTextNode('KITTI')
    title.appendChild(title_text)
    source.appendChild(title)

    size = doc.createElement('size')
    annotation.appendChild(size)

    title = doc.createElement('width')
    title_text = doc.createTextNode(str(img_size[1]))
    title.appendChild(title_text)
    size.appendChild(title)

    title = doc.createElement('height')
    title_text = doc.createTextNode(str(img_size[0]))
    title.appendChild(title_text)
    size.appendChild(title)

    title = doc.createElement('depth')
    title_text = doc.createTextNode(str(img_size[2]))
    title.appendChild(title_text)
    size.appendChild(title)

    for split_line in split_lines:
        line = split_line.strip().split()
        if line[0] in class_ind:
            object = doc.createElement('object')
            annotation.appendChild(object)

            title = doc.createElement('name')
            title_text = doc.createTextNode(line[0])
            title.appendChild(title_text)
            object.appendChild(title)

            bndbox = doc.createElement('bndbox')
            object.appendChild(bndbox)
            title = doc.createElement('xmin')
            title_text = doc.createTextNode(str(int(float(line[4]))))
            title.appendChild(title_text)
            bndbox.appendChild(title)
            title = doc.createElement('ymin')
            title_text = doc.createTextNode(str(int(float(line[5]))))
            title.appendChild(title_text)
            bndbox.appendChild(title)
            title = doc.createElement('xmax')
            title_text = doc.createTextNode(str(int(float(line[6]))))
            title.appendChild(title_text)
            bndbox.appendChild(title)
            title = doc.createElement('ymax')
            title_text = doc.createTextNode(str(int(float(line[7]))))
            title.appendChild(title_text)
            bndbox.appendChild(title)


########### 将DOM对象doc写入文件
    f = open('label/' + name + '.xml', 'w')
    f.write(doc.toprettyxml(indent=''))
    f.close()
コード例 #56
0
ファイル: generate.py プロジェクト: betawins/ci_cd
def main():
  # Boundaries
  max_events = 25 # <strike>Don't use a value beyond 40, this will cause a segfault</strike> 
  max_period = 250000000
  min_period = 50000000
  max_rep    = 1000
  
  max_fid    = 0xf
  max_gid    = 0xfff - 3 # -2 (PPS=0xfff, LATCH=0xffe, SPECIAL=0xffd)
  max_evtno  = 0xfff
  max_sid    = 0xfff
  max_bpid   = 0x3fff
  max_res_id = 0x3ff # Unused
  max_par    = 0xffffffffffffffff
  max_tef    = 0x0 # Always zero (0xffffffff planned)
  max_res    = 0xffffffff # Unused
  
  # Generate parameters
  events   = rnd.randint(1, max_events)
  period   = rnd.randint(min_period, max_period)
  rep      = rnd.randint(1, max_rep)
  max_offs = period - 1
  offs_lst = []
  
  
  # Create random messages
  rnd_fid = []
  rnd_gid = []
  rnd_evtno = []
  rnd_sid = []
  rnd_bpid = []
  rnd_par = []
  rnd_tef = []
  rnd_offs = []
  for x in range(0, events):
    rnd_fid.append(rnd.randint(0, max_fid))
    rnd_gid.append(rnd.randint(0, max_gid))
    rnd_evtno.append(rnd.randint(0, max_evtno))
    rnd_sid.append(rnd.randint(0, max_sid))
    rnd_bpid.append(rnd.randint(0, max_bpid))
    rnd_par.append(rnd.randint(0, max_par))
    rnd_tef.append(rnd.randint(0, max_tef))
    rnd_offs.append(rnd.randint(0, max_offs))
  # Sort offsets
  rnd_offs.sort()
    
  # Output parameters
  print "Events:      " + str(events)
  print "Repetitions: " + str(rep)
  print "Period:      " + str(period) + "ns"
  print "Duration:    " + str((period*rep)) + "ns"
  print "Rate~:       " + str((period/events)) + "ns"
  print "Frequency~:  " + str((1.0/(period/events))*1000000000) + "Hz"
  print ""
  
  # Prepare schedule
  doc = Document()
  page = doc.createElement('page')
  doc.appendChild(page)
  
  meta = doc.createElement('meta')
  page.appendChild(meta)
  
  p = doc.createElement('startplan')
  text = doc.createTextNode("A")
  p.appendChild(text)
  meta.appendChild(p)
  
  p = doc.createElement('altplan')
  text = doc.createTextNode("B")
  p.appendChild(text)
  meta.appendChild(p)
  
  plan = doc.createElement('plan')
  page.appendChild(plan)
  
  metap = doc.createElement('meta')

  p = doc.createElement('starttime')
  text = doc.createTextNode("___STARTTIME___")
  p.appendChild(text)
  metap.appendChild(p)

  p = doc.createElement('lastjump')
  text = doc.createTextNode("idle")
  p.appendChild(text)
  metap.appendChild(p)

  plan.appendChild(metap)
  
  chain = doc.createElement('chain')
  plan.appendChild(chain)
  
  metax = doc.createElement('meta')
  
  p = doc.createElement('rep')
  text = doc.createTextNode(str(rep))
  p.appendChild(text)
  metax.appendChild(p)
  
  p = doc.createElement('period')
  text = doc.createTextNode(str(period))
  p.appendChild(text)
  metax.appendChild(p)
  
  p = doc.createElement('branchpoint')
  text = doc.createTextNode("yes")
  p.appendChild(text)
  metax.appendChild(p)
  
  chain.appendChild(metax)
  
  # Print generated random messages
  for x in range(0, events):
    msg = doc.createElement('msg')
  
    inner_id = doc.createElement('id')
    
    p = doc.createElement('FID')
    text = doc.createTextNode(str(rnd_fid[x]))
    p.appendChild(text)
    inner_id.appendChild(p)
    
    p = doc.createElement('GID')
    text = doc.createTextNode(str(rnd_gid[x]))
    p.appendChild(text)
    inner_id.appendChild(p)
    
    p = doc.createElement('EVTNO')
    text = doc.createTextNode(str(rnd_evtno[x]))
    p.appendChild(text)
    inner_id.appendChild(p)
    
    p = doc.createElement('SID')
    text = doc.createTextNode(str(rnd_sid[x]))
    p.appendChild(text)
    inner_id.appendChild(p)
    
    p = doc.createElement('BPID')
    text = doc.createTextNode(str(rnd_bpid[x]))
    p.appendChild(text)
    inner_id.appendChild(p)
    
    msg.appendChild(inner_id)
    
    chain.appendChild(msg)
    
    p = doc.createElement('par')
    text = doc.createTextNode(str(format(rnd_par[x], '#018x')))
    p.appendChild(text)
    msg.appendChild(p)
    
    p = doc.createElement('tef')
    text = doc.createTextNode(str(rnd_tef[x]))
    p.appendChild(text)
    msg.appendChild(p)
    
    p = doc.createElement('offs')
    text = doc.createTextNode(str(rnd_offs[x]))
    p.appendChild(text)
    msg.appendChild(p)
    
  # Save events to XML file
  f = open("log/schedule.xml", "w")
  try:
    f.write(doc.toprettyxml(indent="  "))
  finally:
    f.close()
  
  # Done
  return 0
コード例 #57
0
ファイル: card.py プロジェクト: mobilpay/python
    def encrypt(self, x509_file_path):
        if self._signature is None or self._orderId is None or isinstance(
                self._invoice, Invoice) is not True:
            raise Exception(
                "One or more mandatory properties are invalid!: " +
                str(self._signature) + ":" + str(self._orderId),
                self.ERROR_PREPARE_MANDATORY_PROPERTIES_UNSET)

        xml_doc = Document()

        order = xml_doc.createElement("order")

        order.setAttribute("type", self._type)
        order.setAttribute("id", self._orderId)

        order.setAttribute("timestamp", f"{datetime.now():%Y%m%d%H%M%S}")

        signature = xml_doc.createElement("signature")
        signature_text = xml_doc.createTextNode(self._signature)
        signature.appendChild(signature_text)

        invoice = self._invoice.create_xml_element(xml_doc)

        order.appendChild(signature)
        order.appendChild(invoice)

        if self._objRequestParams is not None and len(
                self._objRequestParams) > 0:
            params = xml_doc.createElement("params")
            for p in self._objRequestParams:
                param = xml_doc.createElement("param")
                name = xml_doc.createElement("name")
                value = xml_doc.createElement("value")
                name_text = xml_doc.createTextNode(p.strip())
                value_text = xml_doc.createCDATASection(
                    self._objRequestParams.get(p))
                name.appendChild(name_text)
                value.appendChild(value_text)
                param.appendChild(name)
                param.appendChild(value)
                params.appendChild(param)

            xml_doc.appendChild(params)

        if self._returnUrl is not None and len(self._returnUrl) > 0:
            url = xml_doc.createElement("url")
            return_url = xml_doc.createElement("return")
            return_url_text = xml_doc.createTextNode(self._returnUrl)
            return_url.appendChild(return_url_text)
            url.appendChild(return_url)
            if self._confirmUrl is not None and len(self._confirmUrl) > 0:
                confirm_url = xml_doc.createElement("confirm")
                confirm_url_text = xml_doc.createTextNode(self._confirmUrl)
                confirm_url.appendChild(confirm_url_text)
                url.appendChild(confirm_url)

            order.appendChild(url)

        xml_doc.appendChild(order)
        self._xmlDoc = xml_doc
        super()._encrypt(x509_file_path)
コード例 #58
0
def save_to_xml(save_path, im_height, im_width, objects_axis, label_name):
    im_depth = 0
    object_num = len(objects_axis)
    doc = Document()

    annotation = doc.createElement('annotation')
    doc.appendChild(annotation)

    folder = doc.createElement('folder')
    folder_name = doc.createTextNode('VOC2007')
    folder.appendChild(folder_name)
    annotation.appendChild(folder)

    filename = doc.createElement('filename')
    filename_name = doc.createTextNode('000024.jpg')
    filename.appendChild(filename_name)
    annotation.appendChild(filename)

    source = doc.createElement('source')
    annotation.appendChild(source)

    database = doc.createElement('database')
    database.appendChild(doc.createTextNode('The VOC2007 Database'))
    source.appendChild(database)

    annotation_s = doc.createElement('annotation')
    annotation_s.appendChild(doc.createTextNode('PASCAL VOC2007'))
    source.appendChild(annotation_s)

    image = doc.createElement('image')
    image.appendChild(doc.createTextNode('flickr'))
    source.appendChild(image)

    flickrid = doc.createElement('flickrid')
    flickrid.appendChild(doc.createTextNode('322409915'))
    source.appendChild(flickrid)

    owner = doc.createElement('owner')
    annotation.appendChild(owner)

    flickrid_o = doc.createElement('flickrid')
    flickrid_o.appendChild(doc.createTextNode('knautia'))
    owner.appendChild(flickrid_o)

    name_o = doc.createElement('name')
    name_o.appendChild(doc.createTextNode('yang'))
    owner.appendChild(name_o)

    size = doc.createElement('size')
    annotation.appendChild(size)
    width = doc.createElement('width')
    width.appendChild(doc.createTextNode(str(im_width)))
    height = doc.createElement('height')
    height.appendChild(doc.createTextNode(str(im_height)))
    depth = doc.createElement('depth')
    depth.appendChild(doc.createTextNode(str(im_depth)))
    size.appendChild(width)
    size.appendChild(height)
    size.appendChild(depth)
    segmented = doc.createElement('segmented')
    segmented.appendChild(doc.createTextNode('0'))
    annotation.appendChild(segmented)
    for i in range(object_num):
        objects = doc.createElement('object')
        annotation.appendChild(objects)
        object_name = doc.createElement('name')
        object_name.appendChild(
            doc.createTextNode(label_name[int(objects_axis[i][-1])]))
        objects.appendChild(object_name)
        pose = doc.createElement('pose')
        pose.appendChild(doc.createTextNode('Unspecified'))
        objects.appendChild(pose)
        truncated = doc.createElement('truncated')
        truncated.appendChild(doc.createTextNode('1'))
        objects.appendChild(truncated)
        difficult = doc.createElement('difficult')
        difficult.appendChild(doc.createTextNode('0'))
        objects.appendChild(difficult)
        bndbox = doc.createElement('bndbox')
        objects.appendChild(bndbox)

        x0 = doc.createElement('x0')
        x0.appendChild(doc.createTextNode(str((objects_axis[i][0]))))
        bndbox.appendChild(x0)
        y0 = doc.createElement('y0')
        y0.appendChild(doc.createTextNode(str((objects_axis[i][1]))))
        bndbox.appendChild(y0)

        x1 = doc.createElement('x1')
        x1.appendChild(doc.createTextNode(str((objects_axis[i][2]))))
        bndbox.appendChild(x1)
        y1 = doc.createElement('y1')
        y1.appendChild(doc.createTextNode(str((objects_axis[i][3]))))
        bndbox.appendChild(y1)

        x2 = doc.createElement('x2')
        x2.appendChild(doc.createTextNode(str((objects_axis[i][4]))))
        bndbox.appendChild(x2)
        y2 = doc.createElement('y2')
        y2.appendChild(doc.createTextNode(str((objects_axis[i][5]))))
        bndbox.appendChild(y2)

        x3 = doc.createElement('x3')
        x3.appendChild(doc.createTextNode(str((objects_axis[i][6]))))
        bndbox.appendChild(x3)
        y3 = doc.createElement('y3')
        y3.appendChild(doc.createTextNode(str((objects_axis[i][7]))))
        bndbox.appendChild(y3)

    f = open(save_path, 'w')
    f.write(doc.toprettyxml(indent=''))
    f.close()
コード例 #59
0
ファイル: script.py プロジェクト: HerveMignot/books-macosx
    queryString = 'ti="' + title + '"'

    if (authors != ""):
        queryString = queryString + ' and au="' + authors + '"'

    queryString = queryString.replace('ti="" and ', '')

conn = zoom.Connection('z3950.loc.gov', 7090)
conn.databaseName = 'VOYAGER'
conn.preferredRecordSyntax = 'USMARC'

sys.stderr.write("<!-- " + queryString + "-->\n")

query = zoom.Query('CCL', str(queryString))

doc = Document()
root = doc.createElement("importedData")
doc.appendChild(root)
collection = doc.createElement("List")
collection.setAttribute("name", "Library of Congress Import")
root.appendChild(collection)

res = conn.search(query)

count = 0

for r in res:
    m = MARC(MARC=r.data)

    bookElement = doc.createElement("Book")
コード例 #60
0
def _process_content_objects(doc):
    cnt = 0
    results = []

    # connect to uitdb
    response = _connect_to_uitdb()
    #authenticate user
    userkey = _get_userkey(response.read())

    if response.getcode() == 200 and userkey:
        # Parse the input XML document
        try:
            if doc.getElementsByTagName("events"):
                item_type = 'event'
            elif doc.getElementsByTagName("actors"):
                item_type = 'actor'
            elif doc.getElementsByTagName("productions"):
                item_type = 'production'

            content_objects = doc.getElementsByTagName(item_type)

            for co in content_objects:
                # Number of event in input XML
                cnt += 1

                # Create the output XML document
                doc = Document()
                cdbxml = doc.createElement("cdbxml")
                cdbxml.setAttribute(
                    "xmlns",
                    "http://www.cultuurdatabank.com/XMLSchema/CdbXSD/3.1/FINAL"
                )
                doc.appendChild(cdbxml)
                list_element = doc.createElement(item_type + 's')
                cdbxml.appendChild(list_element)
                list_element.appendChild(co)
                xml_output = doc.toxml().encode('utf-8')

                # Send the XML to the Entry API and measure duration
                params = urllib.urlencode({'key': userkey})
                headers = {
                    "Content-type": "text/xml",
                    "Content-Length": len(xml_output)
                }

                conn = httplib.HTTPConnection(SERVER)
                conn.request("POST", "/api/v1/" + item_type + "?" + params, "",
                             headers)
                conn.send(xml_output)
                starttime = time.time()
                r2 = conn.getresponse()
                endtime = time.time()

                duration = round((endtime - starttime) * 1000)
                data = r2.read()
                conn.close()

                # Print the response analyses
                result = {}
                result['item_nr'] = str(cnt)
                result['item_type'] = item_type
                result['item_externalid'] = co.getAttribute('externalid')
                result['http_response'] = str(r2.status) + ' ' + str(r2.reason)
                result['duration_ms'] = str(duration)
                result['api_response'] = data

                if args.verbosity:
                    for key, value in result.items():
                        print key, ':', str(value)
                    print '-----------------------'

                results.append(result)

        except Exception, e:
            print "Error occured while parsing XML: %s" % e
            sys.exit(1)

        # Create report in current working directory
        if args.save_report:
            try:
                doc = Document()
                report = doc.createElement("report")
                doc.appendChild(report)
                for result in results:
                    item = doc.createElement("item")
                    item.setAttribute('type', result['item_type'])
                    item.setAttribute('nr', result['item_nr'])
                    item.setAttribute('external_id', result['item_externalid'])
                    item.setAttribute('http_response', result['http_response'])
                    item.setAttribute('duration_ms', result['duration_ms'])
                    report.appendChild(item)
                xml_output = doc.toxml().encode('utf-8')
                path = os.getcwd() + '/report_' + datetime.datetime.now(
                ).strftime("%Y%m%d%H%M") + '.xml'
                file = open(path, 'w')
                file.write(xml_output)
                file.close()
                print "File created: '%s'" % path
            except Exception, e:
                print "Creating document closed with error: %s" % e