def lenovo_internal(self, model): self.response.out.write("Cron: Lenovo FUS ({0})\n".format(model)) conn = httplib.HTTPConnection("fus.lenovomm.com") conn.request("GET", "/firmware/3.0/updateservlet?action=querynewfirmware&devicemodel={0}&locale=".format(model)) resp = conn.getresponse() if resp.status == 200: try: xml = etree.fromstring(resp.read()) pr = PollResult(deviceid=model, otaname=xml.xpath("/firmwareupdate/firmware/name")[0].text, otadesc=lenovo_getdesc(xml)[0:500], otachecksum=xml.xpath("/firmwareupdate/firmware/md5")[0].text, otaurl=xml.xpath("/firmwareupdate/firmware/downloadurl")[0].text) pr.put() lastchange = DBAL.get_latest_change(model) if lastchange is None or lastchange.otachecksum != pr.otachecksum or lastchange.otaname != pr.otaname or lastchange.otaurl != pr.otaurl: ArchivedPollResult(deviceid=model, otaname=xml.xpath("/firmwareupdate/firmware/name")[0].text, otadesc=lenovo_getdesc(xml)[0:500], otachecksum=xml.xpath("/firmwareupdate/firmware/md5")[0].text, otaurl=xml.xpath("/firmwareupdate/firmware/downloadurl")[0].text).put() return except: PollResult(deviceid=model,error="{0} {1}".format(sys.exc_info()[0], sys.exc_info()[1])).put() else: PollResult(deviceid=model,error="{0} {1}".format(resp.status, resp.reason)).put() lastchange = DBAL.get_latest_change(model) if lastchange is None: ArchivedPollResult(deviceid=model, error="No successful polls yet.").put()
def get(self, deviceidp=None, deviceidc=None): deviceid = None change = False if deviceidp is not None: deviceid = deviceidp else: deviceid = deviceidc change = True if not deviceid in devices.keys(): show_error(self, "No such device \"{0}\"; note that paths are case-sensitive!".format(deviceid)) return template = je.get_template('device.html') polls = DBAL.get_changes(deviceid) if change else DBAL.get_polls(deviceid) self.response.out.write(template.render({"id":deviceid, "name": devices[deviceid], "polls": polls, 'uri_for':self.uri_for, 'change':change}))
class JapaneseDataParser: db = None COMMIT_INTERVAL = 500 def __init__(self): self.db = DBAL({'adapter': 'postgres', 'dsn': config.dsn}) self.db.connection.autocommit = False def flatten(self, tree, xpath, sep=', '): return sep.join([x.text for x in tree.findall(xpath)]) def jmdict(self): tree = xml.etree.ElementTree.ElementTree() tree.parse(config.path + '/doc/jmdict.dat') k = 0 for entry in tree.getroot(): sense = [] for s in entry.findall('sense'): sense.append(xml.etree.ElementTree.tostring(s).decode('utf-8')) self.db.insert( 'dict', dict(ent_seq=entry.find('ent_seq').text, kanji=self.flatten(entry, ".//keb"), reading=self.flatten(entry, ".//reb"), sense="<ul><li>" + ("</li><li>".join(sense)) + "</li></ul>")) k += 1 if k % self.COMMIT_INTERVAL: self.db.connection.commit() self.db.connection.commit() def example(self): match_regex = re.compile('^A:(.+)\t(.+)#') k = 0 with open(config.path + '/doc/example.dat') as f: for line in f: matched = match_regex.match(line) if not matched: continue self.db.insert( 'example', dict(sentence=matched.group(1), translation=matched.group(2))) k += 1 if k % self.COMMIT_INTERVAL: self.db.connection.commit() self.db.connection.commit()
def get(self): template = je.get_template('overview.html') pagedevices = [] for id, name in devices.items(): pagedevices.append({"name": name, "id": id, "change": DBAL.get_latest_change(id), "poll": DBAL.get_latest_poll(id)}) self.response.out.write(template.render({'devices': pagedevices, 'uri_for':self.uri_for}))
def __init__(self): self.db = DBAL({'adapter': 'postgres', 'dsn': config.dsn}) self.db.connection.autocommit = False