def image2svg(im_path, embed_image=True): ### Only part of the code that uses PIL ###### im = Image.open(im_path) width, height = im.size fmt = im.format.lower() ### End of PIL ############################### if embed_image: f = open(im_path, 'rb') im_contents = f.read() f.close() b64 = base64.b64encode(im_contents) #im_href = "data:image/" + fmt + ";base64," + base64.b64encode(im_contents) im_href = "data:image/{0};base64,{1}".format(fmt, b64) else: im_href = "file:" + urllib.pathname2url(im_path) ### SVG ### doc = etree.parse(blank_svg_path) svg = doc.getroot() svg.set('width', str(width)) svg.set('height', str(height)) svg.set('xmlns:xlink', "http://www.w3.org/1999/xlink") ### Use descriptive variables for the layers image_layer = svg[image_layer_index] shapes_layer = svg[shapes_layer_index] ### Create the 'image' element image = etree.SubElement(image_layer, 'image') image.set('x', '0') image.set('y', '0') image.set('height', str(height)) image.set('width', str(width)) image.set('xlink:href', im_href) # encode base64 ### svg_content = etree.tostring(svg) # remove #### Very Ugly Hack Ahead !!! hack_head, hack_body = svg_content.split('\n', 1) hack_head = hack_head[:-1] hack_head = ''.join([hack_head, ' xmlns="http://www.w3.org/2000/svg">']) svg_content = '\n'.join([hack_head, hack_body]) #### END HACK svg_b64 = "data:image/svg+xml;base64," + base64.b64encode(svg_content) return { 'svg': svg_content, 'svg_b64': svg_b64, 'height': height, 'width': width }
def image2svg(im_path, embed_image=True): ### Only part of the code that uses PIL ###### im = Image.open(im_path) width, height = im.size fmt = im.format.lower() ### End of PIL ############################### if embed_image: f = open(im_path, 'rb') im_contents = f.read() f.close() b64 = base64.b64encode(im_contents) #im_href = "data:image/" + fmt + ";base64," + base64.b64encode(im_contents) im_href = "data:image/{0};base64,{1}".format(fmt, b64) else: im_href = "file:" + urllib.pathname2url(im_path) ### SVG ### doc = etree.parse(blank_svg_path) svg = doc.getroot() svg.set('width', str(width)) svg.set('height', str(height)) svg.set('xmlns:xlink', "http://www.w3.org/1999/xlink") ### Use descriptive variables for the layers image_layer = svg[image_layer_index] shapes_layer = svg[shapes_layer_index] ### Create the 'image' element image = etree.SubElement(image_layer, 'image') image.set('x', '0') image.set('y', '0') image.set('height', str(height)) image.set('width', str(width)) image.set('xlink:href', im_href) # encode base64 ### svg_content = etree.tostring(svg) # remove #### Very Ugly Hack Ahead !!! hack_head, hack_body = svg_content.split('\n', 1) hack_head = hack_head[:-1] hack_head = ''.join([hack_head, ' xmlns="http://www.w3.org/2000/svg">']) svg_content = '\n'.join([hack_head, hack_body]) #### END HACK svg_b64 = "data:image/svg+xml;base64," + base64.b64encode(svg_content) return {'svg': svg_content, 'svg_b64': svg_b64, 'height': height, 'width': width}
def EditChannel(request, device_id=None, channel_name=None): if(device_id in device_list): xml_request = request.body root = ET.fromstring(xml_request) sensor = root.find('sensor') ch_value = sensor.get('value') ch_unit = sensor.get('unit') instance_name = device_list[device_id].get('name') data = self.driver_channel_get(instance_name, channel_name) value = data.value unit = data.unit if(ch_value == None): ch_value = value if(ch_unit == None): ch_unit = unit self.driver_channel_set(instance_name, channel_name, ch_value, ch_unit) xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="202">\n' xml_response += '<device id="%s" message="Channel Updated!"/>\n' % device_id xml_response += '</response>' return Response(xml_response, status=202, content_type='text/xml') else: raise NotFound('device not found!')
def get_aflw_upcoming(): """ similar to get_score but this time we are searching for upcoming live match info """ data = fetch_url(config.AFLW_SCORE_URL) tree = ET.fromstring(data) listing = [] for elem in tree.findall("Day"): for subelem in elem.findall("Game"): if subelem.find('GameState').text == 'COMPLETE': continue v = classes.Video() home = subelem.find('HomeTeam').attrib['FullName'] away = subelem.find('AwayTeam').attrib['FullName'] timestamp = subelem.find('Timestamp').text # convert zulu to local time airtime = get_airtime(timestamp, aflw=True) title = ('[COLOR red]AFLW:[/COLOR] ' '{0} vs {1} - {2}') v.title = title.format(home, away, airtime) v.dummy = True listing.append(v) return listing
def parseCertfromXKMS(inputFile, outputFile): try: # 1. Parse Input File tree = ET.parse(inputFile) root = tree.getroot() namespaces = {'dsig': 'http://www.w3.org/2000/09/xmldsig#'} # 2. Open output file RESFILEHANDLE = open(outputFile, "w") # 3. Iterate over required content and write to file subjectName = tree.findall('.//dsig:X509SubjectName', namespaces) for child in subjectName: print child.text subjectCert = tree.findall('.//dsig:X509Certificate', namespaces) for child in subjectCert: print child.text RESFILEHANDLE.write(child.text + '\n') # 4. Close output file RESFILEHANDLE.close() return True except: print('Exception raised in parseCertfromXKMS') return False
def load(self, path): #reference implementation: #http://jmemorize.svn.sourceforge.net/viewvc/jmemorize/trunk/jmemorize/src/jmemorize/core/io/XmlBuilder.java?view=markup try: with contextlib.closing(zipfile.ZipFile(path, "r")) as f: xmlFile = f.open("lesson.xml", "r") except (zipfile.BadZipfile, KeyError): xmlFile = open(path, "r") #crashes when the file structure is invalid. try: root = ElementTree.parse(xmlFile).getroot() finally: xmlFile.close() items = [] for i, card in enumerate(root.findall(".//Card")): item = { "id": i, "questions": self._parse(card.get("Frontside") or u""), "answers": self._parse(card.get("Backside") or u""), } created = card.get("DateCreated") if created: item["created"] = self._parseDate(created) items.append(item) return { "resources": {}, "list": { "items": sorted(items, key=lambda item: item.get("created")), "tests": [], }, }
def parseCertfromXKMS(inputFile): try: # 1. Parse Input File tree = ET.parse(inputFile) root = tree.getroot() namespaces = { 'dsig': 'http://www.w3.org/2000/09/xmldsig#', 'xkms': 'http://www.xkms.org/schema/xkms-2001-01-20' } # 2. Open output file #RESFILEHANDLE = open(outputFile, "w") # 3. Iterate over required content and write to files for xkmsAnswer in tree.findall('.//xkms:Answer', namespaces): keyID = xkmsAnswer.find('.//xkms:KeyID', namespaces) #print keyID.text certFile = './Certs/' + keyID.text + '.crt' RESFILEHANDLE = open(certFile, "w") subjectCert = xkmsAnswer.find('.//dsig:X509Certificate', namespaces) #print subjectCert.text RESFILEHANDLE.write(subjectCert.text) RESFILEHANDLE.close() formatPEMCert(certFile) return True except Exception, err: logging.exception('Unhandled Exception in parseCertfromXKMS') logging.error("Exit on Error: %s: exiting", sys.exc_info()[1]) sys.exit(1)
def add_notes_overlapping(self, svg_contents): svg = etree.fromstring(svg_contents.encode('utf-8')) mask_fill_color, did, tags, header, footer = get_params_for_add_notes() # Add notes to the current deck of the collection: add_notes_overlapping(svg, mask_fill_color, tags, self.mw.image_occlusion2_image_path, header, footer, did)
def load(self, path): """Loads Teachmaster's .vok2 files. Lesson info is stored in additional files, but that's not loaded. It might not map to the OT result system either (not sure). For documentation on the file format in German (translations are available, but those aren't all complete/up-to-date): http://www.teachmaster.de/wikka/DocumentationDEDateiformate """ with open(path, "r") as f: root = ElementTree.parse(f).getroot() list = { "items": [], "title": root.findtext("header/titel") or u"", "questionLanguage": root.findtext("header/spreins") or u"", "answerLanguage": root.findtext("header/sprzwei"), } for i, itemTree in enumerate(root.findall("vokabelsatz")): word = self._loadWordFromItemTree(itemTree) word["id"] = i list["items"].append(word) return { "resources": {}, "list": list, }
def get_round(round_id): """ Fetch the round and return the results """ round_matches = [] round_url = config.ROUND_URL # Pass a 'latest' string in round_id to get 'this week' if round_id != 'latest': round_url = "%s/%s" % (round_url, round_id) xml = fetch_url(round_url) rnd = ET.fromstring(xml) matches = rnd.find('matches').getchildren() for m in matches: d = dict(m.items()) if d['homeSquadId']: match = {} home_team = utils.get_team(d['homeSquadId'])['name'] away_team = utils.get_team(d['awaySquadId'])['name'] match['name'] = "%s v %s" % (home_team, away_team) match['id'] = d['FixtureId'] match['round_id'] = dict(rnd.items())['id'] # Add date/time round_matches.append(match) return round_matches
def menu_main(id,category=''): text = read_xml(id,1) root = ElementTree.fromstring(text) elemroot = root.iter('Gen') j = 0 for elem in elemroot: name = elem.attrib['name'] if re.search(ListOmit, name.encode('utf-8')): continue id = elem.attrib['id'] se = elem.attrib['enableSearch'] if se=="1": catType = elem.attrib['search'][:-1].encode('utf-8') #change unicode to str else: catType = "" orderID = int(elem.attrib['orderid'][:5]) #xbmc cannot sort >10000 j += 1 li=xbmcgui.ListItem('[COLOR FF00FF00][ ' + name + ' ][/COLOR]') li.setInfo( type="Video", infoLabels={"Title":name, "Episode":orderID}) u=sys.argv[0]+"?mode=gen&name="+urllib.quote_plus(name.encode('utf-8'))+"&id="+id+"&category="+urllib.quote_plus(catType) xbmcplugin.addDirectoryItem(int(sys.argv[1]),u,li,True) #添加一个“PPS搜索” li = xbmcgui.ListItem('[COLOR FFFF00FF]PPS.TV 网络电视[/COLOR][COLOR FFFFFF00] (主页) [/COLOR][COLOR FF00FFFF]共计:'+str(j)+'[/COLOR]【[COLOR FF00FF00]点此输入搜索内容[/COLOR]】') li.setInfo(type="Video", infoLabels={"Title":name, "Episode":1}) u=sys.argv[0]+"?mode=search&name="+urllib.quote_plus('PPS搜索') xbmcplugin.addDirectoryItem(int(sys.argv[1]),u,li,True) xbmcplugin.addSortMethod(int(sys.argv[1]), xbmcplugin.SORT_METHOD_EPISODE)
def load(self, path): wsp = self._modules.default("active", type="wordsStringParser") with open(path) as f: root = ElementTree.parse(f).getroot() wordList = { "items": [], } wordList["questionLanguage"] = root.findtext("fields/field[1]") or u"" wordList["answerLanguage"] = root.findtext("fields/field[2]") or u"" #counter is used as word id counter = itertools.count() for entryTree in root.findall("entries/e"): wordList["items"].append({ "id": next(counter), "questions": wsp.parse(entryTree.findtext("f[1]") or u""), "answers": wsp.parse(entryTree.findtext("f[2]") or u""), "comment": entryTree.findtext("f[3]") or u"", }) return { "resources": {}, "list": wordList, }
def load(self, path): wsp = self._modules.default("active", type="wordsStringParser") root = ElementTree.parse(open(path)).getroot() wordList = { "items": list(), "title": root.get("title", u""), } #a counter is used to suppply the ids. That's because ABBYY #Lingvo Tutor decided to not include ids anymore from version X5 #on. counter = itertools.count() wordList["items"] = [ { "id": next(counter), "questions": wsp.parse(wordTree.findtext("word") or u""), "answers": [[a.text or u"" for a in wordTree.findall("meanings/meaning/translations/word")]], "commentAfterAnswering": u", ".join( e.text or u"" for e in wordTree.findall("meanings/meaning/examples/example") ) } for wordTree in root.findall("card") ] return { "resources": {}, "list": wordList, }
def parseCertfromXKMS(inputFile): try: # 1. Parse Input File tree = ET.parse(inputFile) root = tree.getroot() namespaces = { 'dsig': 'http://www.w3.org/2000/09/xmldsig#', 'xkms': 'http://www.w3.org/2002/03/xkms#' } # 2. Open output file #RESFILEHANDLE = open(outputFile, "w") # 3. Iterate over required content and write to files for xkmsAnswer in tree.findall('.//xkms:Answer', namespaces): keyID = xkmsAnswer.find('.//xkms:KeyID', namespaces) print "######### Printing Details ##########" print keyID.text certFile = './Certs/' + os.path.basename(keyID.text) + '.crt' RESFILEHANDLE = open(certFile, "w") subjectCert = xkmsAnswer.find('.//dsig:X509Certificate', namespaces) print subjectCert.text RESFILEHANDLE.write(subjectCert.text) RESFILEHANDLE.close() print "Closed File" + certFile formatPEMCert(certFile) except Exception, err: print('Exception raised in parseCertfromXKMS: ') print err return False
def load(self, path): with open(path, "r") as f: root = ElementTree.parse(f).getroot() list = { "items": [], } for i, itemTree in enumerate(root.findall("Vokabeldatensatz/Datensatz")): word = { "id": i, "questions": [], "answers": [], } comments = [] for question in itemTree.findall("Vokabeln/string"): word["questions"].append((question.text,)) for answer in itemTree.findall("Vokabeln/string"): word["answers"].append((answer.text,)) for comment in itemTree.findall("Kommentare/string"): comments.append((comment.text)) if comments: word["comment"] = u"; ".join(comments) list["items"].append(word) return { "resources": {}, "list": list, }
def load(self, path): """Loads .fq (FlashQard) files. Based on file format inspection. (even though source code is available, see http://flashqard-project.org/download.php) """ with open(path, "r") as f: root = ElementTree.parse(f).getroot() list = { "items": [], "title": root.find("box").get("name"), } for i, itemTree in enumerate(root.findall(".//card")): questions = self._stripTags(itemTree.findtext("frontsidedocument/html")) answers = self._stripTags(itemTree.findtext("backsidedocument/html")) created = itemTree.findtext("statistics/dateCreated") list["items"].append({ "id": i, "questions": self._parse(questions), "answers": self._parse(answers), "comment": self._stripTags(itemTree.findtext("comments")), "created": datetime.datetime.strptime(created, "%d.%m.%Y") }) return { "resources": {}, "list": list, }
def add_notes_overlapping(self, svg_contents): svg = etree.fromstring(svg_contents) mask_fill_color, did, tags, header, footer = get_params_for_add_notes() # Add notes to the current deck of the collection: notes_from_svg.add_notes_overlapping(svg, mask_fill_color, tags, self.image_path, header, footer, did)
def load(self, path): with open(path) as f: root = ElementTree.parse(f).getroot() #dutch: lijst = list listTree = root.find("lijst") wordList = { "items": list(), } #dutch: titel = title wordList["title"] = listTree.findtext("titel") or u"" #dutch: taal = language wordList["questionLanguage"] = listTree.findtext("taal/a") or u"" wordList["answerLanguage"] = listTree.findtext("taal/b") or u"" #change locale temporary; so strptime can do it's work the way #it should. locale.setlocale(locale.LC_ALL, "C") try: created = datetime.datetime.strptime( listTree.findtext("created").rsplit(" ", 1)[0], #strip tz info "%a, %d %b %Y %H:%M:%S" #since our datetime objects are naive ) except (ValueError, AttributeError): created = None #set locale back to make sure conflicts don't arise with other #modules depending on the locale. locale.resetlocale() #counter is used as word id counter = 1 #dutch: woord = word for wordTree in listTree.findall("woord"): word = { "id": int(), "questions": list(), "answers": list(), "comment": unicode() } word["id"] = counter if created: word["created"] = created wsp = self._modules.default("active", type="wordsStringParser") word["questions"] = wsp.parse(wordTree.findtext("a") or u"") word["answers"] = wsp.parse(wordTree.findtext("b") or u"") wordList["items"].append(word) counter += 1 return { "resources": {}, "list": wordList, }
def getFileTypeOf(self, path): if path.endswith(".t2k"): root = ElementTree.parse(open(path)).getroot() if root.find("message_data/items//item/answers[@type='4']") is not None: return "topo" else: #also support other formats in the future? Well, #everything that's left can be opened like it's of type 'words'... return "words"
def load(self, path): """Loads a .t2k file into the OpenTeacher data structure. http://www.teach2000.nl/help/00513_advanced_file_format.htm """ root = ElementTree.parse(open(path)).getroot() if self.getFileTypeOf(path) == "topo": return self._loadTopo(root) else: return self._loadWords(root)
def load(self, kgmPath): #Feed the xml parser with open(kgmPath) as f: root = ElementTree.parse(f).getroot() #load the map, in various later needed formats mapPath = os.path.join(os.path.dirname(kgmPath), root.findtext("mapFile")) mapImage = Image.open(mapPath).convert("RGB") mapImageData = mapImage.load() items = [] for counter, division in enumerate(root.findall("division")): #iterate over all places ('divisions') if division.findtext("ignore") == "yes": #unimportant division: skip continue item = { "id": counter, "name": division.findtext("name") or u"", } #get the color the place has on the map r = int(division.findtext("color/red")) g = int(division.findtext("color/green")) b = int(division.findtext("color/blue")) color = (r, g, b) #get the average pixel with that color. This is done by #iterating over all pixels and using them to calculate an #average if the color matches. sumX = 0 sumY = 0 count = 0 for x in range(mapImage.size[0]): for y in range(mapImage.size[1]): if mapImageData[x, y] == color: sumX += x sumY += y count += 1 #save the averages as coordinate. item["x"] = sumX / count item["y"] = sumY / count items.append(item) return { "resources": { "mapPath": mapPath, }, "list": { "items": items, }, }
def load(self, path): if path.endswith(".pau"): with open(path) as f: root = ElementTree.parse(f).getroot() else: with contextlib.closing(gzip.open(path)) as f: root = ElementTree.parse(f).getroot() wordList = { #only the first line, because a description can be pretty #long in Pauker... "title": (root.findtext("Description") or u"").split("\n")[0].strip(), "items": [], } cards = root.findall("Batch//Card") if cards is not None: for id, card in enumerate(cards): questions = self._parse( (card.findtext("FrontSide") or u"").strip() or (card.findtext("FrontSide/Text") or u"").strip() or u"" ) answers = self._parse( (card.findtext("BackSide") or u"").strip() or (card.findtext("BackSide/Text") or u"").strip() or (card.findtext("ReverseSide") or u"").strip() or (card.findtext("ReverseSide/Text") or u"").strip() or u"" ) wordList["items"].append({ "id": id, "questions": questions, "answers": answers }) return { "resources": {}, "list": wordList, }
def find_aflw_live_matches(): """ get index of current round's games so we can find the 'box' URL and make a list of game ids, returns a list of ElementTree objects to parse for live matches """ data = fetch_url(config.AFLW_INDEX_URL) tree = ET.fromstring(data) box_list = [] listing = [] for elem in tree.find('HeadlineGames'): box_list.append(elem.attrib['Id']) for game_id in box_list: data = fetch_url(config.AFLW_BOX_URL.format(game_id)) tree = ET.fromstring(data) watch_button = tree.find('WatchButton') if watch_button: if watch_button.find('Title').text != 'WATCH REPLAY': listing.append(tree) return listing
def get_aflw_videos(): data = fetch_url(config.AFLW_LONG_URL) tree = ET.fromstring(data) listing = [] for elem in tree.findall('MediaSection'): for video in elem.findall('Item'): if video.attrib['Type'] == 'V': v = classes.Video() v.title = video.find('Title').text v.thumbnail = video.find('FullImageUrl').text v.ooyalaid = video.find('Video').attrib['Id'] listing.append(v) return listing
def get_aflw_score(match_id): """ fetch score xml and return the scores for corresponding match IDs """ data = fetch_url(config.AFLW_SCORE_URL) tree = ET.fromstring(data) for elem in tree.findall("Day"): for subelem in elem.findall("Game"): if subelem.attrib['Id'] == str(match_id): home_score = str(subelem.find('HomeTeam').attrib['Score']) away_score = str(subelem.find('AwayTeam').attrib['Score']) return '[COLOR yellow]{0} - {1}[/COLOR]'.format( home_score, away_score)
def conflict(request, exception): #'Conflict' builder = ET.TreeBuilder() builder.start('response', {'status':'409'}) builder.start('message', {}) builder.data('conflict: ' + str(exception)) builder.end('message') xml = builder.end('response') xml_str = ET.tostring(xml, encoding="utf8") response = Response(xml_str, status=409, content_type='text/xml') return response.send(request._start_response)
def app_error(request, exception): #'Application Error' builder = ET.TreeBuilder() builder.start('response', {'status':'500'}) builder.start('message', {}) builder.data('application error: ' + str(exception)) builder.end('message') xml = builder.end('response') xml_str = ET.tostring(xml, encoding="utf8") response = Response(xml_str, status=500, content_type='text/xml') return response.send(request._start_response)
def get_round(round_id, live=False): """ Fetch the round and return the results """ round_matches = [] round_url = config.ROUND_URL # Pass a 'latest' string in round_id to get 'this week' if round_id != 'latest': round_url = "%s/%s" % (round_url, round_id) xml = fetch_url(round_url) rnd = ET.fromstring(xml) matches = rnd.find('matches').getchildren() for m in matches: d = dict(m.items()) if d['homeSquadId']: match = {} home_team = utils.get_team(d['homeSquadId'])['name'] away_team = utils.get_team(d['awaySquadId'])['name'] match['name'] = "%s v %s" % (home_team, away_team) match['id'] = d['FixtureId'] match['round_id'] = dict(rnd.items())['id'] # special formatting for the 'upcoming games' list in the live menu if live == True: now = datetime.datetime.now() timestamp = d['dateTime'] timezone = d['timezone'] ts = datetime.datetime.fromtimestamp( time.mktime(time.strptime(timestamp, "%Y-%m-%dT%H:%M:%S"))) delta = now - ts #remove games that have already been played if delta > datetime.timedelta(hours=3): continue airTime = ts.strftime(" - %A @ %I:%M %p A") match['name'] = '[COLOR red]{0}{1}{2}[/COLOR]'.format( match['name'],airTime,timezone) # Add date/time round_matches.append(match) return round_matches
def load(self, path): items = [] with open(path, "r") as f: root = ElementTree.parse(f).getroot() for id, card in enumerate(root.findall("card")): items.append({ "id": id, "questions": self._parse(card.findtext("front") or u""), "answers": self._parse(card.findtext("back") or u""), }) return { "list": { "items": items, }, "resources": {}, }
def load(self, path): """Documentation: http://jvlt.sourceforge.net/download.html (at the bottom) """ with contextlib.closing(zipfile.ZipFile(path)) as jvltZip: root = ElementTree.parse(jvltZip.open("dict.xml")).getroot() list = {"items": []} for i, itemTree in enumerate(root.findall(".//entry")): list["items"].append({ "id": i, "questions": self._parse(itemTree.findtext("orth") or u""), "answers": self._parse(itemTree.findtext("sense/trans") or u""), "comment": u", ".join([pron.text for pron in itemTree.findall("pron")]), }) return { "resources": {}, "list": list, }
def load(self, path): """Tries to load .wcu CueCard files. Based on observation of the file format, not on documentation. Seems like there is source code available though. """ with open(path, "r") as f: root = ElementTree.parse(f).getroot() items = [] for id, card in enumerate(root.findall("Card")): items.append({ "id": id, "questions": self._parse(card.get("Question")), "answers": self._parse(card.get("Answer")), }) return { "list": { "items": items, }, "resources": {}, }
def load(self, path): """Loads .fq (FlashQard) files. Based on file format inspection. (even though source code is available, see http://flashqard-project.org/download.php) """ with open(path, "r") as f: root = ElementTree.parse(f).getroot() list = { "items": [], "title": root.find("box").get("name"), } for i, itemTree in enumerate(root.findall(".//card")): questions = self._stripTags( itemTree.findtext("frontsidedocument/html")) answers = self._stripTags( itemTree.findtext("backsidedocument/html")) created = itemTree.findtext("statistics/dateCreated") list["items"].append({ "id": i, "questions": self._parse(questions), "answers": self._parse(answers), "comment": self._stripTags(itemTree.findtext("comments")), "created": datetime.datetime.strptime(created, "%d.%m.%Y") }) return { "resources": {}, "list": list, }
def get_match_video(round_id, match_id, quality): match_video = [] round_url = "%s/%s" % (config.ROUND_URL, round_id) try: xml = fetch_url(round_url) rnd = ET.fromstring(xml) matches = rnd.find('matches') match = matches.find('match[@FixtureId="%s"]' % match_id) qualities = match.find('qualities') quality = qualities.find('quality[@name="%s"]' % config.REPLAY_QUALITY[quality]) periods = quality.find('periods') for qtr in periods.getchildren(): qtr_dict = dict(qtr.items()) match_video.append(qtr_dict) except: return None return match_video
def AddChannel(request, device_id=None, channel_name=None): if(device_id in device_list): xml_request = request.body root = ET.fromstring(xml_request) sensor = root.find('sensor') ch_unit = sensor.get('unit') instance_name = device_list[device_id].get('name') if(ch_unit == None): type = device_list[device_id].get('type') try: ch_unit = Driver_map[type].get('channel_settings').split(',')[1] except: ch_unit = 'no unit' self.driver_channel_add(instance_name, channel_name, ch_unit) xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="201">\n' xml_response += '<device id="%s" message="Channel Created!"/>\n' % device_id xml_response += '</response>' return Response(xml_response, status=201, content_type='text/xml') else: raise NotFound('device not found!')
def getList(self, listId): fd = self._open(self.service + "api/select_list.php", list=listId) root = ElementTree.parse(fd).getroot() list = root.findtext("list") return self._parseList(list, parseLenient=True)
def EditDevice(request): xml_request = request.body root = ET.fromstring(xml_request) device = root.find('device') device_id = device.get('id') if(device_id == None or not device_id.isdigit()): raise Conflict('Device Id is empty or not integer!') device_id_new = device.get('id_new') if(device_id_new != None and not device_id_new.isdigit()): raise Conflict('New Device Id is not integer!') device_type = device.get('type') if(device_type != None and device_type != ''): device_type = device_type.lower() if(device_type not in Device_types): raise Conflict('Device Type not found!') device_name = device.get('name') if(device_name != None and device_name != device_list[device_id].get('name')): flag = 0 for i in device_list: if(device_list[i].get('name') == device_name): flag = 1 break if(flag): raise Conflict('Device Name already exist!') device_mac = device.get('mac') device_sample_rate = device.get('sample_rate') if(device_sample_rate != None): try: device_sample_rate = float(device_sample_rate) except: raise Conflict('Device Sample Rate not integer or float!') if(device_id in device_list): if(device_type == None or device_type == ''): device_type = device_list[device_id].get('type').lower() if(device_name == None): device_name = device_list[device_id].get('name') if(device_mac == None): device_mac = device_list[device_id].get('mac') if(device_sample_rate == None): device_sample_rate = device_list[device_id].get('sample_rate') instance_name = device_name self.driver_remove(instance_name) del device_list[device_id] if(device_id_new != None): device_id_new = int(device_id_new) while(str(device_id_new) in device_list): #if device_id_new exist -> device_id++ and try again device_id_new += 1 device_id = str(device_id_new) device_list[device_id] = {'type': device_type, 'name': device_name, 'mac': device_mac, 'sample_rate': device_sample_rate} instance_name = device_name driver_path = Driver_map[device_type].get('path') settings = {} if Driver_map[device_type].get('settings').find('xbee_device_manager') != -1: if(self.xbee_device_manager_name == None): self.xbee_device_manager_name = 'xbee_device_manager' self.driver_add('xbee_device_manager', 'devices.xbee.xbee_device_manager.xbee_device_manager:XBeeDeviceManager', settings={'worker_threads':2}) time.sleep(1) settings['xbee_device_manager'] = self.xbee_device_manager_name settings['extended_address'] = device_mac if Driver_map[device_type].get('settings').find('sample_rate_ms') != -1: if(device_sample_rate == None): device_sample_rate = 30 settings['sample_rate_ms'] = int(float(device_sample_rate) * 1000) elif Driver_map[device_type].get('settings').find('sample_rate_sec') != -1: if(device_sample_rate == None): device_sample_rate = 1.0 settings['sample_rate_sec'] = float(device_sample_rate) if Driver_map[device_type].get('settings').find('channel_settings') != -1: settings['channel_settings'] = Driver_map[device_type].get('channel_settings') self.driver_add(instance_name, driver_path, settings) try: self.device_list_update() except: traceback.print_exc() xml_response = '<?xml version="1.0" encoding="UTF-8"?>\n' xml_response += '<response status="202">\n' xml_response += '<device id="%s" message="Device Updated!"/>\n' % device_id xml_response += '</response>' return Response(xml_response, status=202, content_type='text/xml') else: raise NotFound('Device not found!')
def buildPKCS10XKMSReq(reqFile, xkmsReqOutput): numberOfRequests = str(countOccurrence(reqFile, 'DN:')) #date=time.strftime("%Y-%m-%dT%H:%M:%S") date = batchTime #1. Build header for xml output header = buildXKMSHeader(bulk_id, date, numberOfRequests) #2. Build Request Header reqHeader = buildXKMSReqHeader(numberOfRequests) #3. Main section - build pkcs10 requests #Put private key files into an array for use later privateKeyArray = [] keysDir = "./keys" for file in os.listdir(keysDir): if os.path.isdir(os.path.join(keysDir, file)): newdir = os.path.join(keysDir, file) for protfile in os.listdir(newdir): if protfile.endswith(".prot"): #Found Private Key add to array privateKeyArray.append(os.path.join(newdir, protfile)) # Build DN from Request File - used in generation of CSR using system openssl binaries #======================================================================= # So Values could be like this: # DN:/CN=Stuart Wilson/[email protected]/OU=PM/O=Verizon/C=GB #======================================================================= REQFILEHANDLE = open(reqFile, "r") flag = 1 pkcs10Line = '' reqBody = '' counter = 0 #Sort the array due to bug in XKMS Responder (Fixed in MCS 2.7 Version D) privateKeyArray.sort() print len(privateKeyArray) for line in REQFILEHANDLE: if line.startswith("DN:"): pkcs10Line = '' subject = re.sub("DN:", "", line) subject = subject.replace("\n", "") print "openssl" + "req" + "-new" + "-key" + privateKeyArray[ counter] + "-batch" + "-subj" + subject pipe = Popen([ "openssl", "req", "-new", "-key", privateKeyArray[counter], "-batch", "-subj", subject ], stdout=PIPE, stderr=PIPE) print "PIPE return code: " + str(pipe.returncode) removeHeader = pipe.communicate()[0].__str__().replace( "-----BEGIN CERTIFICATE REQUEST-----", "") print "PIPE return code: " + str(pipe.returncode) removeNewLine = removeHeader.replace("\n", "") pkcs10Line = removeNewLine.replace( "-----END CERTIFICATE REQUEST-----", "") print pkcs10Line pipe.stdout.close() pipe.stderr.close() print privateKeyArray[counter].replace(".pvt.prot", "") reqBody = reqBody + buildXKMSReq( privateKeyArray[counter].replace(".pvt.prot", ""), str(buc_id), pkcs10Line) counter += 1 xkmsReqOutputString = header + reqHeader + reqBody + buildXKMSReqFooter( ) + buildXKMSSignedPart(adminCert) namespaces = { 'xbulk': 'http://www.w3.org/2002/03/xkms-xbulk', 'xkms': 'http://www.xkms.org/schema/xkms-2001-01-20', 'ds': 'http://www.w3.org/2000/09/xmldsig#', } #print xkmsReqOutputString xkmsReq = ET.fromstring(xkmsReqOutputString) xkmsTree = ET.ElementTree(xkmsReq) xkmsTree.write(xkmsReqOutput, encoding="us-ascii", xml_declaration=True)
def getCategories(self): fd = self._open(self.service + "api/select_categories.php") root = ElementTree.parse(fd).getroot() for category in root.findall(".//category"): yield (category.text, category.get("id"))
def _get_etree(self): "Contains the generated ElementTree after parsing is finished." return ElementTree(self._root)
def getBook(self, categoryId): fd = self._open(self.service + "api/select_books.php", category=categoryId) root = ElementTree.parse(fd).getroot() for book in root.findall(".//book"): yield (book.text, book.get("id"))
def load(self, path): root = ElementTree.parse(open(path)).getroot() if root.get("version", "").startswith("2"): return self._load2x(root) return self._load1x(root)
def getLists(self, bookId): fd = self._open(self.service + "api/select_lists.php", book=bookId) root = ElementTree.parse(fd).getroot() for list in root.findall(".//list"): yield (list.text, list.get("id"))
def buildPKCS10XKMSReq(reqFile, xkmsReqOutput): try: logging.debug(' Begin buildPKCS10XKMSReq') numberOfRequests = str(countOccurrence(reqFile, '-----BEGIN CERTIFICATE REQUEST-----')) #Identify if numberOfRequests is greater than chunk size if int(numberOfRequests) > int(batchSize): logging.info(' Creating Multiple Chunks') # date=time.strftime("%Y-%m-%dT%H:%M:%S") date = batchTime #1. Calculate number of chunks to create numberOfChunks = (int(numberOfRequests)/int(batchSize)) + (int(numberOfRequests)%int(batchSize)> 0) logging.info(' BatchTime = %s',batchTime) logging.info(' Number of Requests = %s',numberOfRequests) logging.info(' Number of Chunks = %s',numberOfChunks) #2. Main section - build pkcs10 requests REQFILEHANDLE = open(reqFile, "r") flag = 1 pkcs10Line = '' reqBody = '' reqCounter = 0 counter = 0 chunks = 1 for line in REQFILEHANDLE: if line.startswith("-----BEGIN CERTIFICATE REQUEST-----"): flag = 0 pkcs10Line = '' if line.startswith("-----END CERTIFICATE REQUEST-----"): flag = 1 # Found Full Certificate Request so Create XKMS Request # print pkcs10Line reqBody = reqBody + buildXKMSReq(str(reqCounter), str(buc_id), pkcs10Line) counter += 1 reqCounter +=1 if not flag and not line.startswith("-----BEGIN CERTIFICATE REQUEST-----"): newline = line.rstrip('\r\n') pkcs10Line = pkcs10Line + newline #Reached point where we have to create an output file either because: #a. The number of csr's has reached the batch size specified OR #b. We have reached the end of the input file if (int(counter) == int(batchSize)) or (int(reqCounter) == int(numberOfRequests)): # 1. Build header for xml output - set bulk_id chunkBulk_id = bulk_id + '_' + str(chunks) header = buildXKMSHeader(chunkBulk_id, date, str(counter)) # 2. Build Request Header reqHeader = buildXKMSReqHeader(str(counter)) xkmsReqOutputString = header + reqHeader + reqBody + buildXKMSReqFooter() + buildXKMSSignedPart(adminCert) namespaces = { 'xbulk': 'http://www.w3.org/2002/03/xkms-xbulk', 'xkms': 'http://www.xkms.org/schema/xkms-2001-01-20', 'ds': 'http://www.w3.org/2000/09/xmldsig#', } # print xkmsReqOutputString xkmsReq = ET.fromstring(xkmsReqOutputString) xkmsTree = ET.ElementTree(xkmsReq) xkmsReqOutput = 'mcp_bulk_' + str(chunkBulk_id) logging.info(' Creating file: %s', xkmsReqOutput) xkmsTree.write(xkmsReqOutput, encoding="us-ascii", xml_declaration=True) chunks += 1 #reset counter counter = 0 #reset pkcsLine and reqBody reqBody = '' pkcs10Line = '' # print ET.tostring(xkmsReq) REQFILEHANDLE.close() else: logging.info('Creating Single Chunk') # date=time.strftime("%Y-%m-%dT%H:%M:%S") date = batchTime # 1. Build header for xml output header = buildXKMSHeader(bulk_id, date, numberOfRequests) # 2. Build Request Header reqHeader = buildXKMSReqHeader(numberOfRequests) # 3. Main section - build pkcs10 requests REQFILEHANDLE = open(reqFile, "r") flag = 1 pkcs10Line = '' reqBody = '' counter = 0 for line in REQFILEHANDLE: if line.startswith("-----BEGIN CERTIFICATE REQUEST-----"): flag = 0 pkcs10Line = '' if line.startswith("-----END CERTIFICATE REQUEST-----"): flag = 1 # Found Full Certificate Request so Create XKMS Request # print pkcs10Line reqBody = reqBody + buildXKMSReq(str(counter), str(buc_id), pkcs10Line) counter += 1 if not flag and not line.startswith("-----BEGIN CERTIFICATE REQUEST-----"): newline = line.rstrip('\r\n') pkcs10Line = pkcs10Line + newline xkmsReqOutputString = header + reqHeader + reqBody + buildXKMSReqFooter() + buildXKMSSignedPart(adminCert) #Close File Handle REQFILEHANDLE.close() namespaces = { 'xbulk': 'http://www.w3.org/2002/03/xkms-xbulk', 'xkms': 'http://www.xkms.org/schema/xkms-2001-01-20', 'ds': 'http://www.w3.org/2000/09/xmldsig#', } # print xkmsReqOutputString xkmsReq = ET.fromstring(xkmsReqOutputString) xkmsTree = ET.ElementTree(xkmsReq) logging.info(' Creating file: %s', xkmsReqOutput) xkmsTree.write(xkmsReqOutput, encoding="us-ascii", xml_declaration=True) # print ET.tostring(xkmsReq) logging.debug(' End buildPKCS10XKMSReq') except: logging.exception('Unexpected Exception in buildPKCS10XKMSReq') logging.error("Exit on Error: %s: exiting", sys.exc_info()[1]) sys.exit(1)
f = open(adminCertFile, 'r') adminCert = f.read() adminCert = adminCert.rstrip('\r\n') if o == '-b': bulk_id = a if o == '-x': buc_id = a if o == '-t': batchTime = a elif o == '-o': createXMLFile = True outputFile = a # Set up Namespaces ET.register_namespace('xbulk', "http://www.w3.org/2002/03/xkms-xbulk") ET.register_namespace('ogcm', 'http://xkms.ubizen.com/kitoshi') ET.register_namespace('xkms', 'http://www.xkms.org/schema/xkms-2001-01-20') ET.register_namespace("ds", "http://www.w3.org/2000/09/xmldsig#") # Set up log level # Levels: DEBUG, INFO, WARNING, ERROR, CRITICAL logging.basicConfig(filename=logFile,format='%(asctime)s %(message)s',level=logging.INFO) def countOccurrence(reqFile, desired): try: hit_count = 0 with open(reqFile) as f: for line in f: if re.match('-----BEGIN CERTIFICATE REQUEST-----', line):