Esempio n. 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()
Esempio n. 2
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)
Esempio n. 3
0
def create_project(name, path, template=None):
    os.mkdir(path)
    os.mkdir(os.path.join(path, 'bin'))
    os.mkdir(os.path.join(path, 'contabilidad'))
    os.mkdir(os.path.join(path, 'documentos'))
    f = open(os.path.join(path, 'bin','empresa.xml'), 'w')
    w = XMLWriter(f)
    html = w.start("data")
    w.element("empresa", '"' + name + '"')
    w.start('items')
    w.element('item', nombre=name, indice='', parent='root')
    w.end('items')
    w.close(html)
    f.close()
Esempio n. 4
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())
Esempio n. 5
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)
Esempio n. 6
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)
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)
Esempio n. 8
0
def intWriter(path, camMat, distCoe, calibError={}):
    try:
        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('StdIntrinsic')
            
            # 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.close(root)
        print 'Intrinsic calibration has been generated successfully.'
        
    except Exception as e:
        print 'ERROR: occurred in writing intrinsic XML file.'
        raise e # keep it bubbling up, to catch in main()
Esempio n. 9
0
 def to_xml(self):
     from StringIO import StringIO
     myxml = StringIO()
     w = XMLWriter(myxml)
     w.start(self._kind.lower(), name=self._label )
     if self._m_title:    w.element("title", self._m_title )
     if self._m_subtitle: w.element("subtitle", self._m_subtitle )
     if self._m_num:      w.element("num", self._m_num )
     if self._m_source:   w.element("source", self._m_source )
     self.to_xml_body(w)
     w.end(self._kind.lower())
     ret = myxml.getvalue()
     myxml.close()
     return ret
Esempio n. 10
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)
Esempio n. 11
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")
# -*- coding: utf-8 -*-
"""
Created on Sun Dec 28 21:43:03 2014

@author: jmalinchak
"""

from elementtree.SimpleXMLWriter import XMLWriter
import sys

w = XMLWriter(sys.stdout)

html = w.start("html")

w.start("head")
w.element("title", "my document")
w.element("meta", name="generator", value="my application 1.0")
w.end()

w.start("body")
w.element("h1", "this is a heading")
w.element("p", "this is a paragraph")

w.start("p")
w.data("this is ")
w.element("b", "bold")
w.data(" and ")
w.element("i", "italic")
w.data(".")
w.end("p")
Esempio n. 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()
Esempio n. 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)
Esempio n. 15
0
        for c in clusters:
            if cluster_has_overlap(c, e):
                # If the edit overlaps with an existing cluster, merge
                c.append(e)
                merge = True
                break
        if not merge:
            # If the edit couldn't be merged, create a new cluster
            clusters.append([e])
    
    # Sort clusters by start and end offsets
    clusters.sort(key=lambda x: (x[0][0],x[0][1]))
    
    # Write to XML
    f_out.start("sentence", id=str(s+1), numann=str(len(annotators[s])))
    f_out.element("text", ' '.join(src_sents[s]))
    f_out.start("error-list")

    # Clusters
    for i in xrange(len(clusters)):
        alternatives = group_by_alternatives(clusters[i])
        f_out.start("error", id=str(i+1), type=get_type(clusters[i]),
                    req=('yes' if len(alternatives)==len(annotators[s]) else 'no'))
        # Alternatives
        for j in xrange(len(alternatives)):
            f_out.start("alt", ann=str(alternatives[j][0][4]))
            # Corrections
            for k in xrange(len(alternatives[j])):
                f_out.element("c", alternatives[j][k][3], start=str(alternatives[j][k][0]), end=str(alternatives[j][k][1]))
            f_out.end("alt")
        f_out.end("error")
Esempio n. 16
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."
Esempio n. 17
0
def run1():
	x = XMLWriter("test.xml")

	musicLibrary = x.start("musciLibrary")

	x.start("song")
	x.element("id", "2")
	x.element("title", "Jigga What/Faint")
	x.element("artist", "Linkin Park/Jay Z")
	x.element("year", "2004")
	x.element("trackNumber", "3")
	x.element("totalTracks", "6")
	x.element("genre", "Rap/Nu Metal")
	x.end("song")
	x.close(musicLibrary)
Esempio n. 18
0
    resp, content = client.request(url,method=http_method)
    return content

def search():
    URL = 'https://api.twitter.com/1.1/search/tweets.json?'
    Count = '&count=100'
    DCGeocode='&geocode=38.895,-77.036389,4.5mi'
    URL = URL + Count + DCGeocode 
    return oauth_request(URL)

if __name__ == "__main__":
    f = codecs.open('tweet_data.xml', mode = 'w')
    w = XMLWriter(f)

    dictionary = json.loads(search())

    w.start('root')

    for n in range(0,len(dictionary["statuses"])):
        entry = dictionary["statuses"][n]
        if(not(entry["geo"] == None)):
            w.start('dataelement')
            w.element('text', "Status: " + entry["text"].encode('ascii','ignore') + '\n')
            w.start('geodata')
            w.element('latitude',str(entry["geo"]["coordinates"][0]))
            w.element('longitude',str(entry["geo"]["coordinates"][1]))
            w.end('geodata')
            w.end('dataelement')

    w.end('root')    
Esempio n. 19
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
Esempio n. 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
Esempio n. 21
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
Esempio n. 22
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]
Esempio n. 23
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
Esempio n. 24
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)
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)
Esempio n. 26
0
    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")

    if(len(args.output_osm) > 0):
        lat = str(huisnr_fields['LAT'])
        lon = str(huisnr_fields['LON'])
        if(len(lat) > 0 and len(lon) > 0):
            osm_id -= 1
            w.start("node", {"id": str(osm_id), "timestamp": dt.isoformat(), "version": "1", "visible": "true", "lon": lon, "lat": lat})
            w.element("tag", "", {"k": "addr:postal_code", "v": huisnr_fields['PKANCODE'})
            w.element("tag", "", {"k": "addr:street", "v": huisnr_fields['STREET_NL'})
            w.element("tag", "", {"k": "addr:house_number", "v": huisnr_fields['HUISNR'})
            w.element("tag", "", {"k": "addr:city", "v": huisnr_fields['COMMUNE_NL'})
            w.end()
if(len(args.output_osm) > 0):
    w.end()
output.close()

if (args.write_postcodes):
    for postalcode in pkancode_set:
        output = open(str(postalcode) + '.csv', 'w')
        rec_str = ''
        for field in fields:
            rec_str += field + ','
        output.write(rec_str[:-1] + "\n")
Esempio n. 27
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")
Esempio n. 28
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))
Esempio n. 29
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)
Created on Sun Dec 28 21:43:03 2014

@author: jmalinchak
"""

from elementtree.SimpleXMLWriter import XMLWriter
import sys



w = XMLWriter(sys.stdout)

html = w.start("html")

w.start("head")
w.element("title", "my document")
w.element("meta", name="generator", value="my application 1.0")
w.end()

w.start("body")
w.element("h1", "this is a heading")
w.element("p", "this is a paragraph")

w.start("p")
w.data("this is ")
w.element("b", "bold")
w.data(" and ")
w.element("i", "italic")
w.data(".")
w.end("p")
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))
w.element("item_ID", Template("$item_ID").substitute(item_ID=item_ID))
w.element("PIDsafe", Template("$PIDsafe").substitute(PIDsafe=PIDsafe))
w.start("dimensions")
w.element("pheight", Template("$pheight").substitute(pheight=pheight))
w.element("pwidth", Template("$pwidth").substitute(pwidth=pwidth))
w.element("leafs", Template("$leafs").substitute(leafs=leafs))
w.end('dimensions')
w.element("base_URL", Template("$base_URL/$item_ID").substitute(base_URL=base_URL,item_ID=item_ID))
w.start('objects')
## iterate to create image object datastreams #######################
w.start('images')
count = 1 #starts numbering of datastreams at "1" to better reflect relationship to physical pages
Esempio n. 32
0
for param in refs:
    source = param[0]
    title = param[1]

    # debug
    print '[%s]' % title

    filename = title.replace(' ', '_')
    for p in string.punctuation.replace('_', ''):
        filename = filename.replace(p, '')

    w = XMLWriter('cicero_' + filename + '.xml', encoding='utf-8')
    xml = w.start("document")

    # metadata entries of the output files
    w.element("meta", name="author", value="marcus tullius cicero")
    w.element("meta", name="title", value=title)
    w.element("meta", name="source", value=source + '0')

    # upon checking it no text in PHI attributed to cicero
    # has more than 500 pages, so this is a safe download limit
    for x in range(0, 500):
        lines = []
        entry = []
        section = source + str(x)
        reference = base + 'loc/474/' + str(x) + '/0'

        # debug
        print '\t<%s>' % section

        # output filename
Esempio n. 33
0
    def generate(self):
        '''
        XML-File fuer Dale erzeugen
        '''
        context = self.context
        # Layout
        dc = IZopeDublinCore(context, None)
        if dc:
            creator = dc.creators[0]
            principal = Principal(creator, creator, creator)
        else:
            principal = self.request.principal
        principal = self.request.principal
        #stammdaten = IStammdaten(self.request.principal)
        adresse = principal.getAdresse()
        traegeroid = u''
        oid = str(adresse['oid']).strip()
        if len(oid) == 9:
            oid = '000000' + oid
        #session = Session()
        #s = select([traegeruaz], and_(traegeruaz.c.trgrcd == str(oid)))
        #res = session.execute(s).fetchone()
        print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        print oid
        print "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
        #if res:
        #    traegeroid = str(res['trgmnr']).strip()
        xml_file = self.base_file
        io = StringIO()
        xml = XMLWriter(io, encoding='utf-8')
        bv = ''
        iknr = ''  #Ik-Nummer des Mitgliedsunternehmens muss noch ermittelt werden
        ikdav = '99999999'
        iknum = '120692198'
        date = strftime("%d.%m.%Y", localtime())
        time = strftime("%H:%M", localtime())
        unb_6 = '0'
        docid = context.__name__
        if len(docid.split('-')) == 2:
            unb_6 = docid.split('-')[1]

        # Daten schreiben
        suaz = xml.start('suaz_file')

        xml.start("unb")
        xml.element("unb_2", iknr)
        xml.element("unb_3", ikdav)
        xml.element("unb_4", date)
        xml.element("unb_5", '00:00')
        xml.element("unb_6", unb_6)
        xml.element("unb_9", '01')
        xml.end("unb")

        xml.start("unh")
        xml.element("unh_2", 'SUAZ:09:1:01:UV')
        xml.element("unh_3", iknum)
        xml.end("unh")

        xml.start("uvt")
        xml.element("uvt_1", bv)
        xml.element("uvt_2", iknum)
        xml.element("uvt_3", date)
        uvt_4 = "%s.%s.20%s" % (context.unfdatum[0:2], context.unfdatum[3:5],
                                context.unfdatum[8:11])
        xml.element("uvt_4", uvt_4)
        xml.element("uvt_5", '')
        xml.end("uvt")

        xml.start("vin")
        xml.element("vin_1", context.prsname[:30])
        xml.element("vin_2", context.prsvor[:30])
        xml.element("vin_3", context.prssta)
        xml.element("vin_4", context.prssex[0])
        xml.element("vin_5", context.ikzplz[:6])
        xml.element("vin_6", context.ikzort[:30])
        vin_7 = "%s %s" % (context.ikstr, context.iknr)
        xml.element("vin_7", vin_7[:46])
        xml.element("vin_8", '')
        if int(context.prsgeb[6:11]) > int(strftime("%Y", localtime())):
            vin_9 = "%s.%s.19%s" % (context.prsgeb[0:2], context.prsgeb[3:5],
                                    context.prsgeb[8:10])
        else:
            vin_9 = "%s.%s.%s" % (context.prsgeb[0:2], context.prsgeb[3:5],
                                  context.prsgeb[6:10])
        xml.element("vin_9", vin_9)
        xml.element("vin_10", '')
        xml.element("vin_11", '')
        xml.end("vin")

        xml.start("ufb")
        # Mitglied
        if False:
            pass
        #if IMitglied.providedBy(self.request.principal):
        #    enummer = context.enummer
        #    if enummer == None:
        #        enummer = ''
        #    name1 = context.ename
        #    name2 = context.estrasse + ' ' + enummer
        #    name3 = context.eplz + ' ' + context.eort
        #    stra = context.estrasse + ' ' + enummer
        #    plz = context.eplz
        #    ort = context.eort
        #    adressetraeger = adresse['iknam1'] # WICHTIG Hier noch die weitere Adresse einfuegen und testen
        # Einrichtung
        else:
            if 'iknam1' in adresse:
                name1 = adresse['iknam1']
                name2 = adresse['iknam2']
                name3 = adresse['iknam3']
                stra = adresse['ikstr'] + adresse['ikhnr']
                plz = str(adresse['ikhplz'])
                ort = adresse['ikhort']
            else:
                name1 = "fehlt:adresse['iknam1']"
                name2 = "fehlt:adresse['iknam2']"
                name3 = "fehlt:adresse['iknam3']"
                stra = "fehlt:adresse['ikstr'] + adresse['ikhnr']"
                plz = "fehlt:str(adresse['ikhplz'])"
                ort = "fehlt:adresse['ikhort']"

            adressetraeger = context.traeger
        # Daten einfuegen...
        ufb_1 = '%s %s %s %s' % (name1, name2, name3, adressetraeger)
        xml.element("ufb_1", ufb_1[:200])
        xml.element("ufb_2", '')
        xml.element("ufb_3", plz)
        xml.element("ufb_4", ort[:30])
        xml.element("ufb_5", stra[:46])
        xml.element("ufb_6", '')
        xml.element("ufb_7", '')
        xml.end("ufb")

        xml.start("eti")
        xml.element("eti_1", date)
        xml.element("eti_2", time)
        xml.end("eti")

        xml.start("ksd")
        xml.element("ksd_1", 'unbekannt')
        xml.element("ksd_5", '1')
        xml.element("ksd_2", '')
        xml.element("ksd_3", '')
        xml.element("ksd_4", '')
        xml.end("ksd")

        xml.start("ufd")
        xml.element("ufd_1", context.unfzeit)
        xml.element("ufd_2", context.uadbavon)
        xml.element("ufd_3", context.uadbabis)
        xml.end("ufd")

        xml.start("ebh")
        # ebh_1 Pseudodatum eigefuegt 30.11.2011
        xml.element("ebh_1", '99.99.9999')
        if context.unfeba1:
            xml.element("ebh_2", context.unfeba1[:30])
        else:
            xml.element("ebh_2", '')
        xml.end("ebh")

        xml.start("dis")
        dis_1 = "%s %s" % (context.diavkt, context.diaadv)
        if context.prstkz == 'ja':
            dis_1 = u'tödlicher Unfall: %s' % dis_1
        xml.element("dis_1", dis_1[:100])
        xml.element("dis_4", '')
        xml.element("dis_3", '')
        xml.end("dis")

        xml.start("afb")
        if context.unfae1:
            eingestellt = context.unfae1
        else:
            eingestellt = ' '
        toedlich = context.prstkz
        if eingestellt == 'nein':
            afb_1 = '0'
            afb_4 = ''
        elif toedlich == 'ja':
            afb_1 = '1'
            afb_4 = ''
        else:
            afb_1 = '1'
        xml.element("afb_1", afb_1)
        if 'sofort' in eingestellt:
            afb_4 = uvt_4
        elif 'spaeter' in eingestellt:
            afb_4 = '%s.%s.20%s' % (context.unfaedatum[0:2],
                                    context.unfaedatum[3:5],
                                    context.unfaedatum[8:11])
            #In der Unfallanzeige kann das Datum in afb_4 nicht verarbeitet werden
            #afb_4 = '%s.%s.20%s %s' % (context.unfaedatum[0:2],context.unfaedatum[3:5],
            #                           context.unfaedatum[8:11], context.unfaezeit)
        xml.element("afb_4", afb_4)
        arbeitsfaehig = context.unfwa1
        if arbeitsfaehig == 'nein' or arbeitsfaehig == None:
            afb_7 = ''
        else:
            if context.unfwax == None:
                afb_7 = ''
            else:
                afb_7 = '%s.%s.%s' % (context.unfwax[0:2], context.unfwax[3:5],
                                      context.unfwax[6:11])
        xml.element("afb_7", afb_7)
        xml.element("afb_8", '')
        xml.end("afb")

        xml.start("abs")
        xml.element("abs_1", context.unfus2[:81])
        xml.element("abs_2", '')
        xml.element("abs_3", '')
        xml.element("abs_4", 'Extranet')
        xml.element("abs_5", '')
        xml.element("abs_6", context.anspfon)
        xml.element("abs_7", context.anspname)
        xml.end("abs")

        xml.start("uaz")
        xml.element("uaz_1", context.unfhg1[:3000])
        uaz_2 = context.unfhg2
        if uaz_2 == 'des Versicherten':
            uaz_2 = '1'
        else:
            uaz_2 = '2'
        xml.element("uaz_2", uaz_2)
        xml.element("uaz_3", context.unfort[:200])

        if context.prstkz == 'ja':
            uaz_4 = '1'
        else:
            uaz_4 = '2'
        xml.element("uaz_4", uaz_4)
        xml.element("uaz_5", context.unfkn1)
        if context.unfkn2 == 'ja':
            uaz_6 = '1'
        else:
            uaz_6 = '2'
        xml.element("uaz_6", uaz_6)
        xml.element("uaz_7", 'nein')
        xml.element("uaz_8", '1')
        xml.element("uaz_9", '0')
        xml.element("uaz_10", context.prsvtr)
        xml.element("uaz_11", oid)
        xml.end("uaz")

        xml.close(suaz)
        io.seek(0)
        # utf-8 wurde auskommentiert, da cusa nur Dateien mit header iso-8859-1 verarbeiten kann (das verstehe wer will)
        # xml_file.write(etree.tostring(etree.parse(io), pretty_print=True, encoding="utf-8", xml_declaration=True))
        xml_file.write(
            etree.tostring(etree.parse(io),
                           pretty_print=True,
                           encoding="ISO-8859-1",
                           xml_declaration=True))
        xml_file.close()
        return io
def _test_to_html(test):
    iodevice = StringIO()
    w = XMLWriter(iodevice, "utf-8")
    w.start(u"html")
    w.start(u"body")
    w.start(u"div")
    w.start(u"table", border=u"1")
    w.start(u"tr")
    w.element(u"th", u"Base verbale")
    w.element(u"th", u"Preterit")
    w.element(u"th", u"Participe passé")
    w.element(u"th", u"Traduction")
    #w.element(u"th", u"Points")
    w.end(u"tr")
    for entry in test.array:
        _add_row(entry, w)
    w.end(u"table")
    w.end(u"div")
    solution_lines = []
    current_line_nb_solution = 0
    for solution in test.solutions:
        if len(solution_lines) == 0 or current_line_nb_solution >= 10:
            solution_lines.append(solution)
            current_line_nb_solution = 0
        else:
            solution_lines[-1] += " / "+solution
            current_line_nb_solution += 1
    w.element(u"br", u"")
    w.element(u"div", u"Solutions:")
    for line in solution_lines:
        w.element(u"div", line)
    w.end(u"body")
    w.end(u"html")

    html_str = unicode(iodevice.getvalue(), encoding='utf-8')
    iodevice.close()
    return html_str
Esempio n. 35
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")

        # iterates until all vicon csvs reach eof
        while True:
            w.start("frameInformation")
            w.element("frame", number=str(frame_num))

            #iterates through each vicon csv at the current row
            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 (Ricoh video is both frames side by side)
                    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 vicon 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
Esempio n. 36
0
print 'Grade: %.1f' % result[0]
print 'Covers:'
print covered_list
print 'Uncovers:'
print uncovered_list


#####################################################
# to XML

w = XMLWriter("privacy_policy_visual.xhtml")
html = w.start("privacy_policy")

# w.element("grade", name="Privacy Policy Result",
#              value=result[0])
w.element("grade", grade=str(result[0]))

w.start("category_info")
w.element("covered_categories", covered_str)
w.element("uncovered_categories", uncovered_str)
w.end()

w.start("basic")
w.element("title", title)
w.element("link", link)
w.end()

# for each covered category, print the paragraphs in docs_new list
for category in covered_list:
    w.start(category)
    # for names of paragraph tags
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")

    data = zip(subnews, subnews_content)
    for d in data:
        subnews = d[0]
        subnews_content = d[1]
Esempio n. 38
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")
Esempio n. 39
0
def search():
    URL = 'https://api.twitter.com/1.1/search/tweets.json?'
    Count = '&count=100'
    DCGeocode = '&geocode=38.895,-77.036389,4.5mi'
    URL = URL + Count + DCGeocode
    return oauth_request(URL)


if __name__ == "__main__":
    f = codecs.open('tweet_data.xml', mode='w')
    w = XMLWriter(f)

    dictionary = json.loads(search())

    w.start('root')

    for n in range(0, len(dictionary["statuses"])):
        entry = dictionary["statuses"][n]
        if (not (entry["geo"] == None)):
            w.start('dataelement')
            w.element(
                'text',
                "Status: " + entry["text"].encode('ascii', 'ignore') + '\n')
            w.start('geodata')
            w.element('latitude', str(entry["geo"]["coordinates"][0]))
            w.element('longitude', str(entry["geo"]["coordinates"][1]))
            w.end('geodata')
            w.end('dataelement')

    w.end('root')
Esempio n. 40
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
Esempio n. 41
0
def intWriter(path, camMat, distCoe, calibError={}):
    try:
        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('StdIntrinsic')

            # 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.close(root)
        print 'Intrinsic calibration has been generated successfully.'

    except Exception as e:
        print 'ERROR: occurred in writing intrinsic XML file.'
        raise e  # keep it bubbling up, to catch in main()
Esempio n. 42
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]