Пример #1
0
def project_save1(tree):
    f = open('resar.xml', 'w')
    w = XMLWriter(f)
    html = w.start("data")
    w.element("empresa", '"' + tree.GetItemText(tree.GetRootItem(), 0) + '"')
    w.start('items')

    def printChildren(tree, treeItem):
        subItem = tree.GetFirstChild(treeItem)[0]
        name = tree.GetItemText(treeItem, 1)
        index = tree.GetItemText(treeItem, 0)
        try:
            parent = tree.GetItemText(tree.GetItemParent(treeItem), 1)
        except Exception:
            parent = 'root'

        if not subItem.IsOk():
            w.element("item", nombre=name, indice=index, parent=parent)

        else:
            w.element("item", nombre=name, indice=index, parent='root')

        while subItem.IsOk():
            printChildren(tree, subItem)
            subItem = tree.GetNextSibling(subItem)

    printChildren(tree, tree.GetRootItem())
   
    w.end('items')
    w.close(html)
    f.close()
Пример #2
0
    def persist(self):
        # Create an xml 
        outs = cStringIO.StringIO()
        w = XMLWriter(outs)
        root = w.start("parsedlog", submitter=self.__submitter)
        #self.parser.asXml(w)
        self.loot.asXml(w)
        self.attendance.asXml(w)
        self.kills.asXml(w)
        w.close(root)

        # Stuff the xml into an http post
        headers = {"Content-type": "application/xml",
                   "Accept": "text/plain"}

        #conn = httplib.HTTPConnection('beauty.quonic.net')
        #conn.request("POST", "/~erik/eqlogdb/submit.psp", outs.getvalue())
        conn = httplib.HTTPConnection('beauty.quonic.net')
        conn.request("POST", "/~erik/eqlogdb/submit.psp", outs.getvalue())
        response = conn.getresponse()
        if response.status == 200:
                self.loot.clear()
                self.attendance.clear()
                self.kills.clear()
        print response.read()
        conn.close()
Пример #3
0
 def asXml(self):
     outs = cStringIO.StringIO()
     w = XMLWriter(outs)
     root = w.start("parsedlog", submitter=self.myname)
     self.fights.asXml(w)
     self.loot.asXml(w)
     self.attendance.asXml(w)
     self.kills.asXml(w)
     w.close(root)
     return str(outs.getvalue())
Пример #4
0
def main():
    dimension = sys.argv[1]
    min_count = int(sys.argv[2])
    engine_response = _json_post('%s/ws/query' % (ENGINE_URL), ENGINE_QUERY % {'dimension':dimension, 'min_count': min_count})
    facets = engine_response['facets'][dimension]['childIds']
    w = XMLWriter(sys.stdout)
    w.start("dimension", {'id': dimension, 'type': 'tree'})
    for facet in facets:
        w.start('element', {'id': facet, '_count':str(engine_response['facets'][dimension]['data'][facet]['count'])})
        w.end('element')
    w.end('dimension')
Пример #5
0
def tagcloudxml(request):
    xml = cStringIO.StringIO()
    w = XMLWriter(xml)
    tags = w.start("tags")
    for tag in tagcloud():
        w.element("a",
            text=tag['tag'],
            attrs={'href': "http://www.bhamtechevents.org/browse_events/%s".format(tag['tag']),
                   'title': "%s topics".format(tag["count"]), 'rel': "tag",
                   'style': "font-size: %s".format(tag["size"])}
        )
    w.end()
    w.close(tags)
    w.flush()
    return HttpResponse(xml.read())
Пример #6
0
    def __init__(self, filename):
        BaseReporter.__init__(self)

        from cStringIO import StringIO
        self._sio = StringIO()
        from elementtree.SimpleXMLWriter import XMLWriter
        self.writer = XMLWriter(self._sio, "utf-8")
        self.filename = filename

        self.test_starts = {}
def main():
    genre_objects = json.load(sys.stdin)
    registry = {}
    for obj in genre_objects:
        registry[obj['id']] = obj

    _validate_parent_child_relationships(registry)

    top_level = [obj for obj in genre_objects if len(obj['_parent_ids']) == 0]

    touched = set()
    paths = set()

    w = XMLWriter(sys.stdout)
    w.start("dimension", {'id':'genre', 'type':'tree'})
    for obj in top_level:
        process_genre(w, obj, '', registry, touched, paths)
    w.end('dimension')

    _assert_all_visited(registry, touched)
Пример #8
0
    def __init__(self):
        BaseReporter.__init__(self)

        from cStringIO import StringIO
        self._sio = StringIO()
        try:
            from elementtree.SimpleXMLWriter import XMLWriter
        except ImportError:
            from testoob.compatibility.SimpleXMLWriter import XMLWriter
        self.writer = XMLWriter(self._sio, "utf-8")

        self.test_starts = {}
Пример #9
0
    def __call__(self,options):
        """Make .tex file for JOB"""
        ret,jlist,base,jobspecfile,order,jjspec = e4t.cli.select_jobs_from_jobfile(options.jobfile)

        def xmloutjobs(w,name,*args,**kw):
            (datareqs,processors,outputs) = e4t.cli.get_elem_configs(*args,**kw)
            cfg = e4t.cli._read_cfgfile(kw,'OUTPUT',name)
            _cfgs = (kw, cfg)
            if cfg:
                # pprint(cfg)
                cfg = udict(cfg)
                cfg.remove('^__.*')
                cfg = ldict([ (k.replace(' ','_'),v) for k,v in cfg.items()])
                w.start("job",name=name,**ldict(cfg))
                w.end("job")

        formats = options.options.xget('FORMAT','XML')            

        from elementtree.SimpleXMLWriter import XMLWriter
        from StringIO import StringIO
        myxml = StringIO()
        w = XMLWriter(myxml)

        for part,jobs in jlist.items():        
            w.start('index', name=part )
            
            for j,k in jobs:
                if k['kind'] in ('table','figure'):
                    xmloutjobs(w,j,**k)

            w.end('index')

            ret = myxml.getvalue()

            xmls = dom.parseString(ret) # or xml.dom.minidom.parseString(xml_string)

            pp = Postprocessor(options=options)
            pp.output(False,xmls.toprettyxml(),'%s.xml'% part)
Пример #10
0
def main(sysargs):
    # settings
    args = EasyArgs(sysargs)
    cfg = EasyConfig(args.config, group="trainer")
    max_clicks = args.clicks or cfg.default_clicks
    window_name = "EagleEye Trainer"
    
    if "help" in args:
        usage()
        return 0
    
    # grab marks from args
    if len(args) > 5:
        mark_in = args[4]
        mark_out = args[5]
        
        # test integer-ness
        try: int(mark_in) and int(mark_out)
        except: 
            usage()
            return 1
        
    elif len(args) > 3:
        ret, mark_in, mark_out = marker_tool(args[1], cfg.buffer_size, window_name)
        if not ret:
            print "Not processing - exiting."
            return 1
    else:
        usage()
        return 1
    
    ## clicking time!
    cropped_total = mark_out - mark_in
    print "video cropped at:", mark_in, "to", mark_out, "- ({} frames)".format(cropped_total)
    
    # clicking function
    def on_mouse(event, x, y, flags, params):
        # left click to mark
        if event == cv2.EVENT_LBUTTONDOWN:
            params['pos'] = (x, y)
            params['status'] = Status.record
            
        # right click to skip
        elif event == cv2.EVENT_RBUTTONDOWN:
            params['status'] = Status.skip
    
    # working variables
    params = {'status': Status.wait, 'pos': None}
    write_xml = False
    textstatus = ""
    dataQuality = 0   # 0 = good, >0 = bad/potentially bad
    
    # default right side (buttonside)
    if cfg.dual_mode:
        lens = Theta.Right
        trainer_points = {Theta.Right:{}, Theta.Left:{}}
    else: # both sides otherwise
        lens = Theta.Both
        trainer_points = {Theta.Both:{}}
    
    print "Minimum reflectors: {} | Ignore Negative xyz: {}".format(cfg.min_reflectors, cfg.check_negatives)
        
    # load video (again)
    in_vid = BuffSplitCap(args[1], crop=(0,0,0,0), rotate=BuffSplitCap.r0, buff_max=cfg.buffer_size)
    in_vid.restrict(mark_in, mark_out)
    
    # load csv (with appropriate ratio)
    in_csv = Memset(args[2])
    in_csv.restrict()
    in_csv.setRatio(cropped_total)
    
    # test for marker data
    if len(in_csv.row()) < 10:
        print "This CSV contains no marker data!\nAborting."
        return 1
    
    # status
    print ""
    print "Writing to:", args[3]
    print "Number of clicks at:", max_clicks
    print ""
    
    cv2.namedWindow(window_name)
    cv2.setMouseCallback(window_name, on_mouse, params)
    
    # grab clicks (Process 2)
    while in_vid.isOpened():
        frame = in_vid.frame(side=lens)
        
        sys.stdout.write(in_vid.status() + " | Clicks {} / {}\r".format(
                                                len(trainer_points[lens]), max_clicks))
        sys.stdout.flush()
        
        # prepare CSV data, click data
        tx = float(in_csv.row()[2])
        ty = float(in_csv.row()[3])
        tz = float(in_csv.row()[4])
        rx = float(in_csv.row()[5])
        ry = float(in_csv.row()[6])
        rz = float(in_csv.row()[7])
        
        # data quality status
        visible = int(in_csv.row()[9])
        max_visible = int(in_csv.row()[8])
        
        # status text to write
        textrow = "VICON - x: {:.4f} y: {:.4f} z: {:.4f} | rx: {:.4f} ry: {:.4f} rz: {:.4f}".format(tx, ty, tz, rx, ry, rz)
        textquality = "Visible: {} , Max Visible: {}".format(visible, max_visible)
        textstatus = "{} | {}/{} clicks".format(in_vid.status(), len(trainer_points[lens]), max_clicks)
        
        if lens == Theta.Left:
            textstatus += " - back side"
        elif lens == Theta.Right:
            textstatus += " - button side"
        #else none, no lens split
        
        # if data is qualified bad, reduce timeout by one
        if dataQuality > 0:
            dataQuality -= 1
        
        if dataQuality == 0:
            dataStatus = " - Good data!!"
            dataStatus_colour = (0, 255, 0) # green
        else:
            dataStatus = " - Potentially bad data (wait {})".format(dataQuality)
            dataStatus_colour = (0, 255, 255) # yellow
        
        # Data tests
        # values must be above 0 and minimum reflectors
        if (cfg.check_negatives and (tx <= 0 or ty <= 0 or tz <= 0)) \
                or visible < cfg.min_reflectors:
            dataStatus = " - Bad data!!"
            dataStatus_colour = (0, 0, 255) # red
            
            if cfg.ignore_baddata:
                dataStatus += " Ignored."
                dataQuality = 1 + cfg.quality_delay
        
        # draw the trainer dot (if applicable)
        if in_vid.at() in trainer_points[lens]:
            cv2.circle(frame, trainer_points[lens][in_vid.at()][0], 1, cfg.dot_colour, 2)
            cv2.circle(frame, trainer_points[lens][in_vid.at()][0], 15, cfg.dot_colour, 1)
        
        # draw text and show
        displayText(frame, textrow, top=True)
        displayText(frame, textquality)
        displayText(frame, textstatus)
        displayText(frame, dataStatus, endl=True, colour=dataStatus_colour)
        
        cv2.imshow(window_name, frame)
        
        # pause for input
        while params['status'] == Status.wait:
            key = cv2.waitKey(10)
            if key == Key.esc:
                params['status'] = Status.stop
            elif key == Key.enter:
                write_xml = True
                params['status'] = Status.stop
            elif key == Key.right:
                params['status'] = Status.skip
            elif key == Key.left:
                params['status'] = Status.back
            elif key == Key.backspace:
                params['status'] = Status.remove
            elif Key.char(key, '1') and cfg.dual_mode:
                params['status'] = Status.still
                lens = Theta.Left
            elif Key.char(key, '2') and cfg.dual_mode:
                params['status'] = Status.still
                lens = Theta.Right
            
        # catch exit status
        if params['status'] == Status.stop:
            print "\nprocess aborted!"
            break
        
        # write data
        if params['status'] == Status.record \
                and len(trainer_points[lens]) != max_clicks: # TODO: does this disable recording clicks on the last frame
                
            if dataQuality == 0:
                trainer_points[lens][in_vid.at()] = (params['pos'], in_csv.row()[2:5], in_csv.row()[8:10])
            params['status'] = Status.skip
        
        # or remove it
        elif params['status'] == Status.remove \
                and in_vid.at() in trainer_points[lens]:
            del trainer_points[lens][in_vid.at()]
            print "\nremoved dot"
        
        # load next csv frame
        if params['status'] == Status.skip:
            if in_vid.next():
                in_csv.next()
            else:
                write_xml = True
                print "\nend of video: {}/{}".format(in_vid.at() -1, mark_out -1)
                break
        
        # or load previous csv frame
        elif params['status'] == Status.back:
            if in_vid.back():
                in_csv.back()
        
        # reset status
        params['status'] = Status.wait
    
    # clean up
    cv2.destroyAllWindows()
    
    
    ## write xml
    if write_xml:
        out_xml = XMLWriter(args[3])
        out_xml.declaration()
        doc = out_xml.start("TrainingSet")
        
        # source information
        out_xml.start("video", mark_in=str(mark_in), mark_out=str(mark_out))
        out_xml.data(os.path.basename(args[1]))
        out_xml.end()
        out_xml.element("csv", os.path.basename(args[2]))
        
        # training point data
        for lens in trainer_points:
            if lens == Theta.Right:
                out_xml.start("buttonside", points=str(len(trainer_points[lens])))
            elif lens == Theta.Left:
                out_xml.start("backside", points=str(len(trainer_points[lens])))
            else: # non dualmode
                out_xml.start("frames", points=str(len(trainer_points[lens])))
            
            for i in trainer_points[lens]:
                pos, row, markers = trainer_points[lens][i]
                x, y = pos
                
                out_xml.start("frame", num=str(i))
                out_xml.element("plane", 
                                x=str(x), 
                                y=str(y))
                out_xml.element("vicon", 
                                x=str(row[0]), y=str(row[1]), z=str(row[2]))
                out_xml.element("visibility", 
                                visibleMax=str(markers[0]), 
                                visible=str(markers[1]))
                out_xml.end()
                
            out_xml.end() # frames
        
        # clean up
        out_xml.close(doc)
        
        print "Data was written."
    else:
        print "No data was written"
    
    print "\nDone."
    return 0
def createMetadata(item_ID):
	# get number of files
	images = os.listdir("/processing/"+item_ID+"/images")
	leaf_count = len(images) - 1 #accounts for /thumbs directory in there		

	# get dimensions of cover and create cover image
	cover_path = Template('/processing/$item_ID/images/$item_ID').substitute(item_ID=item_ID) + "00001.jpg"
	im = Image.open(cover_path)
	width, height = im.size #returns tuple

	# generate cover thumbnail
	max_cover_width = 200
	max_cover_height = 200
	im.thumbnail((max_cover_width, max_cover_height), Image.ANTIALIAS)				
	im.save(Template('/processing/$item_ID/$item_ID').substitute(item_ID=item_ID) + "_cover.jpg")
	print "Cover created for",item_ID	

	# write to xml
	fhand = Template("/processing/$item_ID/$item_ID$suffix.xml").substitute(item_ID=item_ID, suffix="_metadata")
	w = XMLWriter(fhand, "utf-8")

	metadata = w.start("add")
	w.start("doc")
	w.element("field", Template("meta:$item_ID").substitute(item_ID=item_ID), name="id") 
	w.element("field", Template("$item_ID").substitute(item_ID=item_ID), name="ItemID") #no underscore for solr index in "ItemID"
	#creats overall ratio - height / width
	w.element("field", str(height), name="pheight")
	w.element("field", str(width), name="pwidth")
	w.element("field", str(leaf_count), name="leafs")
	w.element("field", Template("$item_ID").substitute(item_ID=item_ID), name="item_title") #how will we generate this? ItemID for now... 
	w.end() #closes <doc>

	w.close(metadata)
Пример #12
0
 def __init__(self, filename, encoding="utf-8", x=XmlFormat):
     self.xmlfile = open(filename, "w")
     self.writer = XMLWriter(self.xmlfile, encoding)
     self.x = x
     self.doc = self.writer.start(self.x.HEAD)
Пример #13
0
class TaraxuDjangoWriter:
    def __init__(self, filename, encoding="utf-8", x=XmlFormat):
        self.xmlfile = open(filename, "w")
        self.writer = XMLWriter(self.xmlfile, encoding)
        self.x = x
        self.doc = self.writer.start(self.x.HEAD)

    def close(self):
        self.writer.close(self.doc)
        self.xmlfile.close()

    def start_task(self, task):
        # detect what type of task this is and remember it for encapsulated elements
        self.ttype = type(task).__name__
        # start task and remember it so that we can close
        self.task = self.writer.start(
            self.x.TASK[self.ttype], id=task.corpus.custom_id, sourcelanguage=task.corpus.language.name
        )

    def end_task(self, task):
        self.writer.close(self.task)

    def add_result(self, result):
        label = self.x.ITEM[self.ttype]

        # pythonic way to get to the particular type of the result
        try:
            result = result.rankingresult
        except:
            pass
        else:
            self.writer.start(
                label,
                id=result.item.source_sentence.custom_id.strip(),
                duration=str(result.duration),
                user=result.user.username,
            )
            ranks = _RankingRank.objects.filter(result=result)
            self.writer.element(self.x.SOURCE, result.item.source_sentence.text)
            for rank in ranks:
                self.writer.element(
                    self.x.TRANSLATION,
                    rank.translation.text,
                    system=rank.translation.document.translateddocument.translation_system.name,
                    rank=str(rank.rank),
                )
            self.writer.end()

        try:
            result = result.selectandposteditresult
        except:
            pass
        else:
            self.writer.start(
                label,
                id=result.item.source_sentence.custom_id.strip(),
                duration=str(result.duration),
                user=result.user.username,
            )
            self.writer.element(self.x.SOURCE, result.item.source_sentence.text)

            try:
                translation = Translation.objects.get(
                    source_sentence=result.item.source_sentence,
                    document__translateddocument__translation_system=result.system,
                )
                self.writer.element(self.x.TRANSLATION, translation.text)
            except:
                pass
            try:
                self.writer.element(
                    self.x.POSTEDIT, result.sentence, system=result.system.name, fromscratch=str(result.fromScratch)
                )
            except:
                self.writer.element(self.x.POSTEDIT, result.sentence, system="?", fromscratch=str(result.fromScratch))
            self.writer.end()
Пример #14
0
def write_graph_xml(g, output_file='transit_graph.xml'):
	from elementtree.SimpleXMLWriter import XMLWriter
	#w = XMLWriter(sys.stdout)#output_file)

	w = XMLWriter(output_file)

	data = w.start("data")

	gx = w.start("gtfs")

	sx = w.start("stops")
	for s in g.stops:
		w.element("stop", 
			id=str(s),
			name=str(g.stops[s].name),
			desc=str(g.stops[s].desc),
			lat=str(g.stops[s].lat),
			lng=str(g.stops[s].lng),
			url=str(g.stops[s].url),
			location_type=str(g.stops[s].location_type),
			parent_station=str(g.stops[s].parent_station)
		)
	w.end() # stops


	w.start("stop_times")
	for st in g.stop_times:
		w.element("stop_time",
			id=str(st),
			stop_id=str(g.stop_times[st].stop_id),
			trip_id=str(g.stop_times[st].trip_id),
			stop_sequence=str(g.stop_times[st].stop_sequence),
			pickup_type=str(g.stop_times[st].pickup_type),
			dropoff_type=str(g.stop_times[st].dropoff_type),
			arrival_time_s=str(g.stop_times[st].arrival_time_s),
			departure_time_s=str(g.stop_times[st].departure_time_s),
			arrival_time=str(g.stop_times[st].arrival_time),
			departure_time=str(g.stop_times[st].departure_time)
		)
	w.end() # stoptimes

	w.start("trips")
	for t in g.trips:
		w.element("trip",
			id=str(g.trips[t].id),
			route_id=str(g.trips[t].route_id),
			service_id=str(g.trips[t].service_id),
			trip_headsign=str(g.trips[t].trip_headsign),
			block_id=str(g.trips[t].block_id)
		)
	w.end() # trips

	w.start("routes")
	for r in g.routes:
		w.element("route",
			id=str(g.routes[r].id),
			short_name=str(g.routes[r].short_name),
			long_name=str(g.routes[r].long_name),
			desc=str(g.routes[r].desc),
			type=str(g.routes[r].type)
		)
	w.end() # routes


	w.end() # gtfs


	w.start("graph")

	# write nodes
	w.start("nodes")
	for n in g:
		w.element("node",
			id=str(n),
			stop_id=str(g[n].stop_id),
			stop_time_id=str(g[n].stop_time_id),
			arrival_time=str(g[n].arrival_time),
			departure_time=str(g[n].departure_time)
		)

	w.end() # nodes


	# write arcs
	w.start("arcs")
	for n in g:
		w.start("node", id=str(n))

		for arc in g[n]:
			w.element("arc", 
				id=str(arc), 
				weight=str(g[n][arc]), 
				type=str(g[n][arc].type)
			)

		w.end() # arc-node

	w.end() # arcs


	w.end() # graph

	w.close(data)
Пример #15
0
def writeXML(frames, path, args):
    out_xml = XMLWriter(path)
    out_xml.declaration()
    doc = out_xml.start("AnnotationEvaluation")
    
    # source information
    out_xml.element("video", mark_in=str(args[4]), mark_out=str(args[5]))
    out_xml.element("mapper", os.path.basename(args[1]))
    out_xml.element("annotation", os.path.basename(args[2]))
    out_xml.element("comparison", mean_err=str(calMean(frames)))
    # compared points
    out_xml.start("frames", total=str((args[5]-1) - (args[4]+1)+1), compared=str(len(frames)))
    for f in frames.keys():
        out_xml.start("frame", num=str(f))
        for key in frames[f]:
            out_xml.start("object", lens=Theta.name(frames[f][key]["lens"]), name=key, err=str(frames[f][key]["err"]))
            out_xml.element("annotatedCentroid", x=str(frames[f][key]["ann_x"]), y=str(frames[f][key]["ann_y"]))
            out_xml.element("mappedCentroid", x=str(frames[f][key]["map_x"]), y=str(frames[f][key]["map_y"]))
            out_xml.end() # object
        out_xml.end() # frames
    
    # clean up
    out_xml.close(doc)
#Write datastreams manifest to XML
	#example datastream chunk
	'''
	<foxml:datastream CONTROL_GROUP="M" ID="ORIGINAL" STATE="A">
	    <foxml:datastreamVersion ID="ORIGINAL.0" MIMETYPE="image/jpeg" LABEL="Page#">
	        <foxml:contentLocation TYPE="URL">
	            <xsl:attribute name="REF">http://image.url/Sketches_and_scraps0000#.jpg</xsl:attribute>
	        </foxml:contentLocation>           
	    </foxml:datastreamVersion>
	</foxml:datastream>
	'''

fhand = item_path + "/" + item_ID + "_FOXML.xml"

w = XMLWriter(fhand, encoding="utf-8")
w.declaration()

#namespace dictionary
namespace={}
namespace['xmlns:foxml'] = "info:fedora/fedora-system:def/foxml#"
namespace['xmlns:xsi']="http://www.w3.org/2001/XMLSchema-instance"

manifest = w.start("ebook_item", namespace)

# iterate through parent collections and create <collection> elements for each
for collection_name in collections:
	w.element("collection", Template("$collection_name").substitute(collection_name=collection_name))

w.element("PID_prefix", Template("$PID_prefix").substitute(PID_prefix=PID_prefix))
w.element("book_title", Template("$item_ID").substitute(item_ID=item_ID))
    else:
        return 'N/A'

urls = get_news_urls()

for u in urls:
    title = get_articles_title(u)
    article_content = get_articles_content(u)
    author = get_subnews_author(u)
    subnews = get_subnews_titles(u)
    subnews_content = get_subnews_content(u)
    date = get_articles_dates(u)
    iso = date.split(' ')[0]

    filename = 'nl_' + iso + '_' +  title.replace(' ','_').lower() + '.xml'
    w = XMLWriter(filename, encoding='utf-8')
    html = w.start("broadcast")

    content = ' '.join(article_content)

    w.element("title", title)
    w.element("meta", name="author", value=author)
    w.element("meta", name="published", value=date)
    w.element("meta", name="corpus", value=filename)
    w.element("meta", name="source", value=u)
    w.element("meta", name="generator", value="https://github.com/caio1982/Nuntii-Latini")
    
    w.start("headline")
    w.element("title", title)
    w.element("content", content)
    w.end("headline")
Пример #18
0
def intWriter(path, buttonside=None, backside=None):
    try:
        status = ""
        print 'Generating Intrinsic Parameters to:', path, '...'
        with open(path, 'w') as int_xml:
            w = XMLWriter(int_xml)
            w.declaration()
            
            # Camera Intrinsic (Root)
            root = w.start('dual_intrinsic')
            num_sides = range(0, 2)
            #num_sides = range(0, 1) if buttonside is None or backside is None else range(0, 2)
            for i in num_sides:
                w.start("Buttonside" if i == 0 else "Backside")

                if i == 0 and buttonside[0].size > 0 and buttonside[1].size > 0:
                    status += 'Buttonside'
                    camMat = buttonside[0]
                    distCoe = buttonside[1]
                    calibError = buttonside[2]
                elif i == 1 and backside[0].size > 0 and backside[1].size > 0:
                    if status == "":    status += 'Backside'
                    else:   status += ' & Backside'
                    camMat = backside[0]
                    distCoe = backside[1]
                    calibError = backside[2]
                else:
                    w.end()
                    continue
                
                
                # Camera Matrix
                w.element('CamMat',
                         fx=str(camMat[0][0]), fy=str(camMat[1][1]),
                         cx=str(camMat[0][2]), cy=str(camMat[1][2]))
                
                # Distortion Coefficients
                if (len(distCoe[0]) == 8): # 8 coefficients Rational Model, k4 k5 k6 enabled
                    w.element('DistCoe', 
                                k1=str(distCoe[0][0]), k2=str(distCoe[0][1]),
                                p1=str(distCoe[0][2]), p2=str(distCoe[0][3]),
                                k3=str(distCoe[0][4]), k4=str(distCoe[0][5]),
                                k5=str(distCoe[0][6]), k6=str(distCoe[0][7]))
                elif (len(distCoe[0]) == 12): # 12 coefficients Prism Model, c1, c2, c3, c4 enabled, new in OpenCV 3.0.0
                    w.element('DistCoe', 
                                k1=str(distCoe[0][0]), k2=str(distCoe[0][1]),
                                p1=str(distCoe[0][2]), p2=str(distCoe[0][3]),
                                k3=str(distCoe[0][4]), k4=str(distCoe[0][5]),
                                k5=str(distCoe[0][6]), k6=str(distCoe[0][7]),
                                c1=str(distCoe[0][8]), c2=str(distCoe[0][9]),
                                c3=str(distCoe[0][10]),c4=str(distCoe[0][11]))
                else:
                    w.element('DistCoe', 
                                k1=str(distCoe[0][0]), k2=str(distCoe[0][1]),
                                p1=str(distCoe[0][2]), p2=str(distCoe[0][3]),
                                k3=str(distCoe[0][4]))
                
                # Error values
                if len(calibError) > 0:
                    w.element('Error', 
                                rms=str(calibError['rms']), 
                                total=str(calibError['tot_err']), 
                                arth=str(calibError['arth_err']))
                
                w.end() #buttonside/backside
                
            w.close(root)
        print status, 'Intrinsic calibration has been generated successfully.'
        
    except Exception as e:
        # keep it bubbling up, to catch in main()
        raise Exception("{}: {}\n'ERROR: occurred in writing intrinsic XML file.'".format(type(e), e.message))
Пример #19
0
class MusicXMLWriter:
    song = None  #Core.MusicData.Song
    writer = None  #elementtree.SimpleXMLWriter.XMLWriter
    currentBeat = 0
    divisions = 2  #Hmm
    currentMeasureIndex = -1

    def __init__(self, song):
        self.song = song

    def write(self, fileName):
        file = open(fileName, "w")
        file.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
        self.writer = XMLWriter(file)

        structure = self.writer.start("score-partwise", {"version": "3.0"})

        self.writer.start("part-list")
        self.writer.start("score-part", {"id": "P1"})
        self.writer.element("part-name", "Music")
        self.writer.end("score-part")
        self.writer.end("part-list")
        self.writer.start("part", {"id": "P1"})

        for index, measure in enumerate(self.song.get_measures()):
            #raw_input()
            self.currentMeasureIndex = index
            self.currentBeat = 0

            self.writer.start("measure", {"number": str(index + 1)})
            self.writer.start("attributes")
            self.writer.element("divisions", str(self.divisions))

            if (index == 0):
                self.writer.start("key")
                self.writer.element(
                    "fifths",
                    str(KeySignature.key_sig_values[self.song.key.value]))
                self.writer.element("mode", "major")
                self.writer.end("key")
                self.writer.start("time")
                self.writer.element("beats", str(measure.duration))
                self.writer.element("beat-type", "4")
                self.writer.end("time")
                self.writer.element("staves", "2")
                self.writer.start("clef", {"number": "1"})
                self.writer.element("sign", "G")
                self.writer.element("line", "2")
                self.writer.end("clef")
                self.writer.start("clef", {"number": "2"})
                self.writer.element("sign", "F")
                self.writer.element("line", "4")
                self.writer.end("clef")

            self.writer.end("attributes")

            self.writeChordSymbol(measure.chords[0])

            for note in measure._notes:
                if note.duration == 1.5 and self.currentBeat % 1 == 0.5:
                    splitNotes = self.splitDottedHalf(note)
                    #for x in splitNotes:
                    #print str(x.pitch)+", "+str(x.duration)
                    self.writeNoteXML(splitNotes[0], 1, "start")
                    self.writeNoteXML(splitNotes[1], 1, "stop")
                else:
                    if len(measure.harmonies) == 1:
                        if self.currentBeat == measure.harmonies[0][0]:
                            #print "harmony found: "+str(index+1)
                            self.writeNoteXML([note, measure.harmonies[0][1]],
                                              1)
                        else:
                            self.writeNoteXML(note, 1)
                    else:
                        self.writeNoteXML(note, 1)

            self.writer.start("backup")
            self.writer.element("duration",
                                str(measure.duration * self.divisions))
            self.writer.end("backup")
            ### OLD
            #c = measure.chords[0].get_random_voicing(measure.duration)
            #self.writeNoteXML(c,2)
            ### NEW
            newThing = make_chord_measure(measure.chords[0], measure.duration)
            for note_tuple in newThing:
                self.writeNoteXML(note_tuple, 2)
            ####

            self.writer.end("measure")
        self.writer.end("part")
        self.writer.end(structure)

    def writeNoteXML(self, note_s, staffNumber, tied=None):
        """
        This can output XML for either a note or a chord.
        """
        if isinstance(note_s, Note):
            note_s = [note_s]
        elif not isinstance(note_s, list):
            raise ValueError("WTF IS GOING ON")

        for i, n in enumerate(note_s):
            self.writer.start("note")
            self.writer.start("pitch")
            self.writer.element("step", n.pitch.letter[:1])
            if (n.pitch.sharp_or_flat != 0):
                self.writer.element("alter", str(n.pitch.sharp_or_flat))
            if n.pitch.letter == 'Cb':
                self.writer.element("octave", str(n.pitch.octave + 1))
            else:
                self.writer.element("octave", str(n.pitch.octave))
            self.writer.end("pitch")
            self.writer.element("duration", str(n.duration * self.divisions))

            if (tied == "start" or n.tie == "start"):
                self.writer.start("notations")
                self.writer.element("tied", None, {"type": "start"})
                self.writer.end("notations")
            elif (tied == "stop" or n.tie == "stop"):
                self.writer.start("notations")
                self.writer.element("tied", None, {"type": "stop"})
                self.writer.end("notations")
            nextNote = self.song.get_measure_at_index(
                self.currentMeasureIndex).get_note_at_beat(self.currentBeat +
                                                           n.duration)
            last_note_measure_change = int(
                math.floor((self.currentBeat - 0.25) / 4))
            last_beat = (self.song.beats_per_measure + self.currentBeat -
                         0.25) % self.song.beats_per_measure
            lastNote = self.song.get_measure_at_index(
                last_note_measure_change).get_note_at_beat(
                    last_beat)  #I think this works...
            #The conditionals below are only self detecting for eighth notes, if parameter "beam" is None
            if n is None:
                brk = 0
            """
            if n.type == "eighth" and self.currentBeat % 1 == 0 and nextNote is not None and tied is None:
                if nextNote.type == "eighth":
                    self.writer.element("beam","begin")
            elif n.type == "eighth" and self.currentBeat % 1 == 0.5 and lastNote.type == "eighth"  and tied is None: #lastNote NULL???
                self.writer.element("beam","end")
            """
            self.writer.element("type", n.type)
            if (n.dot):
                self.writer.element("dot")

            self.writer.element("staff", str(staffNumber))
            if (i > 0):
                self.writer.element("chord")
            self.writer.end("note")
        self.currentBeat += note_s[0].duration

    def writeChordSymbol(self, chord):
        self.writer.start("harmony")
        self.writer.start("root")
        root = self.song.key.scale[chord.step - 1]
        self.writer.element("root-step", root[0])
        if (len(root) > 1):
            alter = 1 if root[1] == '#' else -1
            self.writer.element("root-alter", str(alter))
        self.writer.end("root")
        typeText = "" if chord.chord_type == "maj" else "m"
        self.writer.element("kind", "none", {"text": typeText})
        self.writer.end("harmony")

    def splitDottedHalf(self, note):
        firstEighth = Note(Pitch(note.pitch.value, self.song.key), 0.5)
        secondQuarter = Note(Pitch(note.pitch.value, self.song.key),
                             note.duration - 0.5)
        return [firstEighth, secondQuarter]
Пример #20
0
class OldHTMLReporter(BaseReporter):
    "Direct HTML reporting. Deprecated in favor of XSLT reporting"
    def __init__(self, filename):
        BaseReporter.__init__(self)

        from cStringIO import StringIO
        self._sio = StringIO()
        from elementtree.SimpleXMLWriter import XMLWriter
        self.writer = XMLWriter(self._sio, "utf-8")
        self.filename = filename

        self.test_starts = {}

    def start(self):
        BaseReporter.start(self)
        self._writeHeader()

    def _writeHeader(self):
        self.writer.start("html")
        self.writer.start("head")
        self.writer.element("title", "TESTOOB unit-test report")
        self.writer.end("head")
        self.writer.start("body")
        self.writer.start("table", border="1")
        self.writer.start("tr")
        self.writer.element("td", "Test Name")
        self.writer.element("td", "Time")
        self.writer.element("td", "Result")
        self.writer.element("td", "More info")
        self.writer.end("tr")

    def done(self):
        BaseReporter.done(self)
        self.writer.end("table")
        self.writer.element("p", "Total time: %.4f"%self.total_time)
        self.writer.end("body")
        self.writer.end("html")

        #assert len(self.test_starts) == 0
        f = file(self.filename, "w")
        try: f.write(self._getHtml())
        finally: f.close()

    def _getHtml(self):
        return self._sio.getvalue()

    def _encodeException(self, str):
        import re
        str = re.sub(r'File "(.+)",', r'<a href="file:///\1"> File "\1",</a>', str)
        return str.replace("\n","<br>")

    def startTest(self, test_info):
        BaseReporter.startTest(self, test_info)
        self.test_starts[test_info] = _time.time()

    def addError(self, test_info, err_info):
        BaseReporter.addError(self, test_info, err_info)
        self._add_unsuccessful_testcase("error", test_info, err_info)

    def addFailure(self, test_info, err_info):
        BaseReporter.addFailure(self, test_info, err_info)
        self._add_unsuccessful_testcase("failure", test_info, err_info)

    _SuccessTemplate='<tr><td>%s</td><td>%s</td><td><font color="green">success</font></td></tr>'
    def addSuccess(self, test_info):
        BaseReporter.addSuccess(self, test_info)
        self._sio.write(HTMLReporter._SuccessTemplate%(str(test_info), self._test_time(test_info)))

    _SkipTemplate='<tr><td>%s</td><td>%s</td><td><font color="blue">skipped</font></td><td>%s</td></tr>'
    def addSkip(self, test_info, err_info, isRegistered=True):
        BaseReporter.addSkip(self, test_info, err_info, isRegistered)
        self._sio.write(HTMLReporter._SkipTemplate%(str(test_info), self._test_time(test_info), str(err_info.exception_value())))

    _FailTemplate="""
    <tr><td>%s</td><td>%s</td><td><font color="red">%s</font></td>
    <td>%s</td></tr>
    """
    def _add_unsuccessful_testcase(self, failure_type, test_info, err_info):
        self._sio.write(HTMLReporter._FailTemplate%(str(test_info), self._test_time(test_info), failure_type, self._encodeException(str(err_info))))

    def _test_time(self, test_info):
        result = _time.time() - self.test_starts[test_info]
        del self.test_starts[test_info]
        return "%.4f" % result
Пример #21
0
def installerLog(package_details):
    '''
    This module writes the required fields of a package to a temporary 
    XML file which is then appended to the list of installedLog.xml.
    Right now - only name and version written to the installedLog.xml.
    '''
    
    #package_details contain all the info about the package
    w = XMLWriter('tempholder.xml')
    xml = w.start("xml")
    w.start("Package")
    w.element("name", package_details[0])
    w.element("version", package_details[1])
    #w.element("architecture", package_details[2])
    #w.element("short-description",package_details[3])
    #w.element("installed",package_details[4])
    #w.element("long-description",package_details[4])
    #-----^basic logging data above^------
    #w.element("section",package_details[5])
    #w.element("installed-size",package_details[6])
    #w.element("maintainer",package_details[7])
    #w.element("original-maintainer",package_details[8])
    #w.element("replaces",package_details[9])
    #w.element("provides",package_details[10])
    #w.element("pre-depends",package_details[11])
    #w.element("depends",package_details[5])
    #w.element("recomends",package_details[13])
    #w.element("suggests",package_details[14])
    #w.element("conflicts",package_details[15])
    #w.element("filename",package_details[16])
    #w.element("size",package_details[17])
    #w.element("md5sum",package_details[18])
    #w.element("homepage",package_details[19])
    w.end()
    w.close(xml)
Пример #22
0
def main(sysargs):
    args = EasyArgs(sysargs)
    cfg = EasyConfig(args.config, group="mapper")
    
    if "help" in args:
        usage()
        return 0
    
    if ["calib", "trainer", "output"] not in args:
        print "Must specify: -calib, -trainer, -output files"
        usage()
        return 1
        
    if len(args) == 1:
        print "Not enough input CSV files"
        usage()
        return 1
    
    if len(args) > 2 and args.map_trainer_mode:
        print "Too many CSV for trainer-mapping mode"
        usage()
        return 1
    
    if "force_side" in args:
        side = Theta.resolve(args.force_side)
        if side == Theta.NonDual:
            print "Invalid force_side argument:", args.force_side
            usage()
            return 1
        
        # set side overrides
        force_button = (side == Theta.Buttonside)
        force_back   = not force_button
    else:
        force_button = force_back = False
    
    # working vars
    csvs = {}
    frame_num = 0
    
    # open source CSV datasets
    for i in range(1, len(args)):
        print args[i]
        csvs[i] = Memset(args[i])
    
    
    # reel all the files up to their first flash
    for i in csvs:
        csvs[i].restrict()
        if len(csvs[i].row()) < 10:
            print "CSV file:", args[i], "contains no marker data!\nAborting."
            return 1
    
    # override csv name
    if args.map_trainer_mode:
        csvs[1]._name = cfg.trainer_target
    
    # open calib files
    try:
        buttonside = Mapper(args.calib, args.trainer, cfg, Theta.Buttonside)
        backside = Mapper(args.calib, args.trainer, cfg, Theta.Backside)
    except Exception as e:
        print e.message
        return 1
    
    count = {'bts':0, 'bks':0, 'rej':0}
    
    # open destination XML
    with open(args.output, "w") as xmlfile:
        w = XMLWriter(xmlfile)
        w.declaration()
        xmlfile.write("<!DOCTYPE dataset SYSTEM \"http://storage.gwillz.com.au/eagleeye_v2.dtd\">")
        doc = w.start("dataset")
        
        # main loop
        while True:
            w.start("frameInformation")
            w.element("frame", number=str(frame_num))
            
            for i in csvs:
                c = csvs[i]
                # determine marker quality
                try:
                    max_reflectors = int(c.row()[8])
                    visible_reflectors = int(c.row()[9])
                except:
                    print "Error in reading quality at row {}".format(i)
                    return 1

                try:
                    # read VICON data
                    x = float(c.row()[2])
                    y = float(c.row()[3])
                    z = float(c.row()[4])
                    
                    # TODO: is this necessary? We never use the object's rotation
                    rx = float(c.row()[5])
                    ry = float(c.row()[6])
                    rz = float(c.row()[7])
                except:
                    print "Error occurred when converting VICON data at row {}".format(i)
                    return 1
                
                # run projection/mapping on VICON data
                if backside.isVisible((x,y,z)):
                    points = backside.reprojpts((x, y, z))
                    side = 'backside'
                    count['bks'] += 1
                
                elif buttonside.isVisible((x,y,z)):
                    points = buttonside.reprojpts((x, y, z))
                    points[0] += 960 # add 960 to x for rightside points
                    side = 'buttonside'
                    count['bts'] += 1
                
                # TODO don't write non visible dots? 
                else:
                    points = [0.,0.]
                    count['rej'] += 1
                
                # TODO: Change DTD and double check with Manjung
                w.start("object", id=str(i), name=c.name(), lens=Theta.name(side))
                w.element("boxinfo", height="99", width="99", x=str(points[0]-50), y=str(points[1]-50))
                w.element("centroid", x=str(points[0]), y=str(points[1]), rx=str(rx), ry=str(ry), rz=str(rz))
                w.element("visibility", visible=str(visible_reflectors), visibleMax=str(max_reflectors))
                w.end()
                
            w.end()
            
            # test end of files
            eofs = 0
            for i in csvs:
                if csvs[i].eof(): eofs += 1
            
            if len(csvs) == eofs:
                print "end of all datasets"
                break
            
            
            # load next frame
            frame_num += 1
            for i in csvs:
                csvs[i].next()
        
        w.close(doc)
        
        print "\nbuttonside", count['bts']
        print "backside", count['bks']
        print "rejected", count['rej']
        
    return 0
Пример #23
0
def main():
    fout_name = os.path.join(data_dir, "geo_coordinates.xml")
    fout = open(fout_name, "w")
    w = XMLWriter(fout)

    w.start("root")
    f_name = os.path.join(data_dir, "crime_geo_coordinates.txt")
    with open(f_name) as f:
        for line in f:
            lat = str(line.split(",")[0])
            lng = str(line.split(",")[1])

            w.start('dataelement')
            w.element('text', "")
            w.start("geodata")
            w.element("latitude", lat)
            w.element("longitude", lng)
            w.end("geodata")
            w.end("dataelement")
            
    w.end("root")
Пример #24
0
def write_keymap(keymap, filename):
  contexts = list(set([ c for c,a,k in keymap ]))
  actions  = list(set([ a for c,a,k in keymap ]))
  
  w = XMLWriter(filename, "utf-8")
  doc = w.start("keymap")
  
  for context in contexts:
    w.start(context)
    w.start("keyboard")
    for c,a,k in keymap:
      if c==context:
        w.element("key", a, id=k)
    w.end()
    w.end()
  w.end()
  w.close(doc)
Пример #25
0
        correction = tokens[2].split("||")[0]
        correction = ' '.join(nltk.word_tokenize(correction))
        required = tokens[3]
        annotator = int(tokens[5])
        annotators[s].add(annotator)
        if c_start == -1 and c_end == -1 and etype.lower() == "noop":
            # Noop --> empty set of edits (source is right)
            pass
        else:
            ref_annot[s].append([c_start, c_end, etype, correction, annotator])
f_in.close()

# Create the output XML
if not out_file:
    out_file = in_file + ".ieval.xml"
f_out = XMLWriter(out_file, "UTF-8")
f_out.declaration()
f_out.start("scripts")
f_out.start("script", id="1") # Assume only one script

# Do clustering
for s in xrange(len(ref_annot)):
    sys.stdout.write("\rSentence %s..." % (s+1))
    sys.stdout.flush()
    
    clusters = []
    # Sort edits from longest to shortest range
    ref_annot[s].sort(key=lambda x: x[0] - x[1])
    for e in ref_annot[s]: # Go through each edit
        merge = False
        for c in clusters:
Пример #26
0
    # convert to lat/lon
    if('X' in huisnr_fields):
        coordinates = projection.to_wgs84(huisnr_fields['X'], huisnr_fields['Y'])
    
        huisnr_fields['LAT'] = coordinates[0]
        huisnr_fields['LON'] = coordinates[1]
    else:
        huisnr_fields['LAT'] = ''
        huisnr_fields['LON'] = ''

fields = [ 'COMMUNE_NL', 'COMMUNE_FR', 'COMMUNE_DE', 'PKANCODE', 'STREET_NL', 'STREET_FR', 'STREET_DE', 'HUISNR', 'LAT', 'LON']

output = open(args.output_csv, 'w')
if(len(args.output_osm) > 0):
    w = XMLWriter(args.OUTFILE)
    w.start("osm", {"generator": "crab-tools" + str(__version__), "version": API_VERSION, "upload": "false"})
rec_str = ''
for field in fields:
    rec_str += field + ','
output.write(rec_str[:-1] + "\n")
            
for (huisnr_id, huisnr_fields) in huisnr_dic.items():
    rec_str = ''
    for field in fields:
        value = ''
        if(field in huisnr_fields):
            rec_str += str(huisnr_fields[field]) + ','
        else:
            rec_str += ','
    output.write(rec_str[:-1] + "\n")
Пример #27
0
parser = argparse.ArgumentParser(description='Convert a ESRI Shapefile (POINT only) to .OSM')
parser.add_argument('INFILE', help='The path to the input ESRI shapefile, will append .shp if omitted')
parser.add_argument('OUTFILE', type=argparse.FileType('w'), default='out.osm', help='The path to the output OSM XML file')
parser.add_argument('--quiet', action='store_true', default=False)
args = parser.parse_args()

osm_id = 0
dt = datetime.now()

sf = shapefile.Reader(args.INFILE)
f = sf.fields
l = len(sf.shapes())
if not args.quiet:
    p = ProgressBar(l) 

w = XMLWriter(args.OUTFILE)
w.start("osm", {"generator": "shape2osm " + str(__version__), "version": API_VERSION, "upload": "false"})
for shape in sf.shapeRecords():
    osm_id -= 1
    (x,y) = shape.shape.points[0]
    w.start("node", {"id": str(osm_id), "timestamp": dt.isoformat(), "version": "1", "visible": "true", "lon": str(x), "lat": str(y)})
    for i in range(1,len(f)):
        w.element("tag", "", {"k": str(f[i][0]), "v": str(shape.record[i-1])})
    w.end()
    if not args.quiet:
        p.update_time(l - (l + osm_id))
        print "{0}\r".format(p),
w.end()
if not args.quiet:
    print "\nfinished."
Пример #28
0
	#Get either every dimension or a single dimension
	val1s = parser.getProperties()
	val2s = parser.getProperties()
	
	# Values that we've found through previous executions of this grid-search
	printFound = True
	found = {}
	#found[('Release Year','Width (mm)')] = {'C':1000,'gamma':0.1}
	
	prefix = "gridoutput/"
	if not os.path.exists(prefix):
	    os.makedirs(prefix)
	t = time.localtime(time.time())
	logfn = os.path.join(prefix,"gridlog_"+str(t[0])+'.'+str(t[1])+'.'+str(t[2])+'_'+str(t[3])+'.'+str(t[4])+'.'+str(t[5])+".xml")
	with open(logfn,'w') as f:
		writer = XMLWriter(f)
		rootnode = writer.start("root")
		for val1 in val1s:
			for val2 in val2s:
				if val1 is not val2:
					start_time = time.time()
					print "Modelling",val1,"(independent) against",val2,"(dependent)."
					ed = None
					if (val1,val2) not in found.keys():
						writer.start("model",ind=val1,dep=val2)
						ods = []
						weights = [0.4,0.3,0.2]
						for w in weights:
							ods.append(ObservedDistribution(parser, val1, contours, val2, w, retrain=True)) 
						#[0.01, 0.02, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20, 50, 100]
						#[0.01, 0.033, 0.1, 0.33, 1, 3, 10, 33, 100] 
Пример #29
0
def _record_key():
    	dialog = KeyListener()
    	dialog.doModal()
    	key = dialog.key
    	del dialog
	w = XMLWriter(twitter_file, "utf-8")
	doc = w.start("keymap")
	w.start("global")
	w.start("keyboard")
        w.element("key", "addon.opensettings(script.service.twitter)", id=str(key))
	w.end()
	w.end()
	w.start("fullscreenvideo")
	w.start("keyboard")
	w.element("key", "addon.opensettings(script.service.twitter)", id=str(key))
	w.end()
	w.end()
	w.end()
	w.close(doc)
Пример #30
0
    def process(self, args):
        # Perform any base class processing.
        if not super(NfoProcess, self).process(args):
            return

        # If we don't have an output parameter, we figure it out from
        # the input filename.
        output = args.output

        if not output:
            output = os.path.splitext(args.tmdb)[0] + ".nfo"

        # Check to see if the file exists.
        if os.path.isfile(output) and not args.force:
            print "Cannot overwrite file: " + output
            return False

        # Open up the output file.
        xml = open(output, 'w')
        xml.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")

        w = XMLWriter(xml, 'utf-8')
        tag = w.start("movie", ThumbGen="1")
        w.element("hasrighttoleftdirection", "false")
        w.element("title", self.movie['title'])
        w.element("originaltitle", self.movie['original_title'])
        w.element("filename", os.path.splitext(args.tmdb)[0] + ".mp4")
        w.element("tagline", self.movie['tagline'])
        w.element("releasedate", self.movie['release_date'])
        w.element("id", self.movie['imdb_id'])
        w.element("runtime", format(self.movie['runtime']))
        w.element("plot", self.movie['overview'])

        # Write out the genres.
        w.start("genre")

        for genre in self.movie['genres']:
            w.element("name", genre['name'])

        w.end()

        # Media information
        w.start("mediainfo")
        w.start("Resolution")
        w.element("Flag", "Resolution_480p")
        w.end()
        w.element("resolution", "480P")
        w.end()

        # Finish up the document.
        w.end()
        w.close(tag)
        xml.close()

        # Report that we created the file.
        self.log.info("Created " + output)
Пример #31
0
class XMLReporter(BaseReporter):
    """Reports test results in XML, in a format resembling Ant's JUnit xml
    formatting output."""
    def __init__(self):
        BaseReporter.__init__(self)

        from cStringIO import StringIO
        self._sio = StringIO()
        try:
            from elementtree.SimpleXMLWriter import XMLWriter
        except ImportError:
            from testoob.compatibility.SimpleXMLWriter import XMLWriter
        self.writer = XMLWriter(self._sio, "utf-8")

        self.test_starts = {}

    def start(self):
        BaseReporter.start(self)
        self.writer.start("results")
        self.writer.start("testsuites")

    def done(self):
        BaseReporter.done(self)

        self.writer.element("total_time", value="%.4f"%self.total_time)
        self.writer.end("testsuites")

        if self.cover_amount is not None and self.cover_amount == "xml":
            self._write_coverage(self.coverage)

        self.writer.end("results")

    def get_xml(self):
        return self._sio.getvalue()

    def startTest(self, test_info):
        BaseReporter.startTest(self, test_info)
        self.test_starts[test_info] = time.time()

    def addError(self, test_info, err_info):
        BaseReporter.addError(self, test_info, err_info)
        self._add_unsuccessful_testcase("error", test_info, err_info)

    def addFailure(self, test_info, err_info):
        BaseReporter.addFailure(self, test_info, err_info)
        self._add_unsuccessful_testcase("failure", test_info, err_info)

    def _add_testcase_element(self, test_info, result, add_elements = lambda:None):
        self._start_testcase_tag(test_info)
        self.writer.element("result", result)
        add_elements()
        self.writer.end("testcase")

    def addSuccess(self, test_info):
        BaseReporter.addSuccess(self, test_info)
        self._add_testcase_element(test_info, "success")

    def addSkip(self, test_info, err_info, isRegistered=True):
        BaseReporter.addSkip(self, test_info, err_info, isRegistered)
        def add_elements():
            self.writer.element( "reason", err_info.exception_value() )
        self._add_testcase_element(test_info, "skip")

    def _add_unsuccessful_testcase(self, failure_type, test_info, err_info):
        def add_elements():
            "Additional elements specific for failures and errors"
            self.writer.element(failure_type, str(err_info), type=err_info.exception_type(), message=err_info.exception_value())
        self._add_testcase_element(test_info, failure_type, add_elements)

    def _start_testcase_tag(self, test_info):
        self.writer.start("testcase", name=str(test_info), time=self._test_time(test_info))

    def _test_time(self, test_info):
        result = time.time() - self.test_starts[test_info]
        return "%.4f" % result

    def _write_coverage(self, coverage):
        self.writer.start("coverage")
        for filename, stats in coverage.getstatistics().items():
            # TODO: can probably extract loop body to a method
            self.writer.start("sourcefile", name=filename,
                              statements=str(stats["lines"]),
                              executed=str(stats["covered"]),
                              percent=str(stats["percent"]))
            lines, covered =  coverage.coverage[filename].values()
            missing = [line for line in covered if line not in lines]
            self.writer.data(str(missing)[1:-1].replace(" ", ""))
            self.writer.end("sourcefile")
        self.writer.end("coverage")
Пример #32
0
def write_graph_xml(graph, output_file='street_graph.xml'):
    w = XMLWriter(output_file)

    graph_e = w.start("graph")

    # write nodes
    w.start("nodes")

    for n in graph:
        w.element("node", id=str(n), lat=str(graph[n].lat), lon=str(graph[n].lng))

    w.end()

    # write arcs
    w.start("arcs")
    for n in graph:
        w.start("node", id=str(n))
        for arc in graph[n]:
            w.element("arc", ref=str(arc), weight=str(graph[n][arc]))
            #print arc, graph[n][arc]
        w.end()
    w.end()

    w.close(graph_e)
Пример #33
0
def main(sysargs):
    # settings
    args = EasyArgs(sysargs)
    cfg = EasyConfig(args.config, group="trainer")
    max_clicks = args.clicks or cfg.default_clicks
    window_name = "EagleEye Trainer"
    
    if "help" in args:
        usage()
        return 0
    
    # grab video flash marks from args
    if len(args) > 5:
        mark_in = args[4]
        mark_out = args[5]
        
        # test integer-ness
        try: int(mark_in) and int(mark_out)
        except: 
            usage()
            return 1
        
    elif len(args) > 3:
        ret, mark_in, mark_out = marker_tool(args[1], cfg.buffer_size, window_name)
        if not ret:
            print "Not processing - exiting."
            return 1
    else:
        usage()
        return 1
    
    #How many frames between flashes?
    cropped_total = mark_out - mark_in
    print "video cropped at:", mark_in, "to", mark_out, "- ({} frames)".format(cropped_total)
    
    # clicking function
    def on_mouse(event, x, y, flags, params):
        # left click to mark
        if event == cv2.EVENT_LBUTTONDOWN:
            params['pos'] = (x, y)
            params['status'] = Status.record
            
        # right click to skip
        elif event == cv2.EVENT_RBUTTONDOWN:
            params['status'] = Status.skip
    
    # working variables
    params = {'status': Status.wait, 'pos': None}
    write_xml = False
    textstatus = ""
    dataQuality = 0   # 0 = good, >0 = bad/potentially bad
    
    # default right side (buttonside)
    if cfg.dual_mode:
        lens = Theta.Right
        trainer_points = {Theta.Right:{}, Theta.Left:{}}
    else: # both sides otherwise
        lens = Theta.Both
        trainer_points = {Theta.Both:{}}
    
    print "Minimum reflectors: {} | Ignore Negative xyz: {}".format(cfg.min_reflectors, cfg.check_negatives)
        
    # load video (again)
    in_vid = BuffSplitCap(args[1], crop=(0,0,0,0), rotate=BuffSplitCap.r0, buff_max=cfg.buffer_size)
    in_vid.restrict(mark_in, mark_out) #reels up to flash sync
    
    # load csv (with appropriate ratio)
    in_csv = Memset(args[2])
    in_csv.restrict() #reels up to flash sync
    #since Vicon records at higher "framerate" than camera, in_csv.next() jumps forwards by <vicon frame count>/<cam frame count> rows.
    #TODO: what about variable (vicon) framerate? No average of all within timstamp range?
    in_csv.setRatio(cropped_total)
    
    # test for marker data
    if len(in_csv.row()) < 10:
        print "This CSV contains no marker data!\nAborting."
        return 1
    
    # status
    print ""
    print "Writing to:", args[3]
    print "Number of clicks at:", max_clicks
    print ""
    
    cv2.namedWindow(window_name)
    cv2.setMouseCallback(window_name, on_mouse, params)
    
    # grab clicks (Process 2)
    while in_vid.isOpened():
        frame = in_vid.frame(side=lens)
        
        sys.stdout.write(in_vid.status() + " | Clicks {} / {}\r".format(
                                                len(trainer_points[lens]), max_clicks))
        sys.stdout.flush()
        
        # prepare CSV data, click data
        tx = float(in_csv.row()[2])
        ty = float(in_csv.row()[3])
        tz = float(in_csv.row()[4])
        rx = float(in_csv.row()[5])
        ry = float(in_csv.row()[6])
        rz = float(in_csv.row()[7])
        
        # data quality status
        visible = int(in_csv.row()[9])
        max_visible = int(in_csv.row()[8])
        
        # status text to write
        textrow = "VICON - x: {:.4f} y: {:.4f} z: {:.4f} | rx: {:.4f} ry: {:.4f} rz: {:.4f}".format(tx, ty, tz, rx, ry, rz)
        textquality = "Visible: {} , Max Visible: {}".format(visible, max_visible)
        textstatus = "{} | {}/{} clicks".format(in_vid.status(), len(trainer_points[lens]), max_clicks)
        
        if lens == Theta.Left:
            textstatus += " - back side"
        elif lens == Theta.Right:
            textstatus += " - button side"
        #else none, no lens split
        
        # if data is qualified bad, reduce timeout by one
        if dataQuality > 0:
            dataQuality -= 1
        
        if dataQuality == 0:
            dataStatus = " - Good data!!"
            dataStatus_colour = (0, 255, 0) # green
        else:
            dataStatus = " - Potentially bad data (wait {})".format(dataQuality)
            dataStatus_colour = (0, 255, 255) # yellow
        
        # Data tests
        # values must be above 0 and minimum reflectors
        if (cfg.check_negatives and (tx <= 0 or ty <= 0 or tz <= 0)) \
                or visible < cfg.min_reflectors:
            dataStatus = " - Bad data!!"
            dataStatus_colour = (0, 0, 255) # red
            
            if cfg.ignore_baddata:
                dataStatus += " Ignored."
                dataQuality = 1 + cfg.quality_delay #default quality_delay=2
        
        # draw the trainer dot (if applicable)
        if in_vid.at() in trainer_points[lens]:
            cv2.circle(frame, trainer_points[lens][in_vid.at()][0], 1, cfg.dot_colour, 2)
            cv2.circle(frame, trainer_points[lens][in_vid.at()][0], 15, cfg.dot_colour, 1)
        
        # draw text and show
        displayText(frame, textrow, top=True)
        displayText(frame, textquality)
        displayText(frame, textstatus)
        displayText(frame, dataStatus, endl=True, colour=dataStatus_colour)
        
        cv2.imshow(window_name, frame)
        
        # pause for input
        while params['status'] == Status.wait:
            key = cv2.waitKey(10)
            if key == Key.esc:      #don't write anything, exit
                params['status'] = Status.stop
            elif key == Key.enter:  #write groundtruth correspondences xml, exit
                write_xml = True
                params['status'] = Status.stop
            elif key == Key.right:  #next frame
                params['status'] = Status.skip
            elif key == Key.left:   #previous frame
                params['status'] = Status.back
            elif key == Key.backspace:  #remove click point in current frame
                params['status'] = Status.remove
            elif Key.char(key, '1') and cfg.dual_mode:
                params['status'] = Status.still #swap lens
                lens = Theta.Left
            elif Key.char(key, '2') and cfg.dual_mode:
                params['status'] = Status.still #swap lens
                lens = Theta.Right
            
        # catch exit status
        if params['status'] == Status.stop:
            print "\nprocess aborted!"
            break
        
        # write data
        if params['status'] == Status.record \
                and len(trainer_points[lens]) != max_clicks: # TODO: does this disable recording clicks on the last frame
                
            if dataQuality == 0:
                trainer_points[lens][in_vid.at()] = (params['pos'], in_csv.row()[2:5], in_csv.row()[8:10])
            params['status'] = Status.skip
        
        # or remove it
        elif params['status'] == Status.remove \
                and in_vid.at() in trainer_points[lens]:
            del trainer_points[lens][in_vid.at()]
            print "\nremoved dot"
        
        # load next csv frame
        if params['status'] == Status.skip:
            if in_vid.next():
                in_csv.next()
            else:
                write_xml = True
                print "\nend of video: {}/{}".format(in_vid.at() -1, mark_out -1)
                break
        
        # or load previous csv frame
        elif params['status'] == Status.back:
            if in_vid.back():
                in_csv.back()
        
        # reset status
        params['status'] = Status.wait
    
    # clean up
    cv2.destroyAllWindows()
    
    
    ## write xml
    if write_xml:
        out_xml = XMLWriter(args[3])
        out_xml.declaration()
        doc = out_xml.start("TrainingSet")
        
        # source information
        out_xml.start("video", mark_in=str(mark_in), mark_out=str(mark_out))
        out_xml.data(os.path.basename(args[1]))
        out_xml.end()
        out_xml.element("csv", os.path.basename(args[2]))
        
        # training point data
        for lens in trainer_points:
            if lens == Theta.Right:
                out_xml.start("buttonside", points=str(len(trainer_points[lens])))
            elif lens == Theta.Left:
                out_xml.start("backside", points=str(len(trainer_points[lens])))
            else: # non dualmode
                out_xml.start("frames", points=str(len(trainer_points[lens])))
            
            for i in trainer_points[lens]:
                pos, row, markers = trainer_points[lens][i]
                x, y = pos
                
                out_xml.start("frame", num=str(i))
                out_xml.element("plane", 
                                x=str(x), 
                                y=str(y))
                out_xml.element("vicon", 
                                x=str(row[0]), y=str(row[1]), z=str(row[2]))
                out_xml.element("visibility", 
                                visibleMax=str(markers[0]), 
                                visible=str(markers[1]))
                out_xml.end()
                
            out_xml.end() # frames
        
        # clean up
        out_xml.close(doc)
        
        print "Data was written."
    else:
        print "No data was written"
    
    print "\nDone."
    return 0