def main(url = Config.get("URL", "ships"), debug=False): req = urllib2.Request(url) req.add_header('User-Agent', useragent) stats = urllib2.urlopen(req).read() session.execute(Ship.__table__.delete()) if Config.get("DB", "dbms") == "mysql": session.execute(text("ALTER TABLE ships AUTO_INCREMENT=1;", bindparams=[false])) else: session.execute(text("SELECT setval('ships_id_seq', 1, :false);", bindparams=[false])) for line in sre.findall(stats): ship = Ship() line = list(line) for index, key in enumerate(keys): if line[index] in mapping: line[index] = mapping[line[index]] elif line[index].isdigit(): line[index] = int(line[index]) if line[index] not in ('-', '',): setattr(ship,key,line[index]) ship.total_cost = ship.metal + ship.crystal + ship.eonium if debug: print "%12s%12s%12s%12s" % (ship.name, ship.class_, ship.race, ship.type,) session.add(ship) session.commit() session.close()
def main(api_url = Config.get("URL", "api")): loadfromapi(api_url+"?constructions", Construction) loadfromapi(api_url+"?research", Research) loadfromapi(api_url+"?races", Race) loadfromapi(api_url+"?governments", Gov, gen_mods=True) loadfromapi(api_url+"?settings", GameSetup) session.close()
def run(self, message): m = self.match(message, self.commandre) if m is None: if self.match(message, self.helpre) is not None: self.help(message) return command = m.group(2) try: route, subcommand, user, params = self.router(message, command) route(message, user, params) session = Session() session.add(Command(command_prefix = message.get_prefix(), command = self.name, subcommand = subcommand, command_parameters = self.hide_passwords(self.name, message.get_msg()[len(m.group(1))+1:].strip()), nick = message.get_nick(), username = "" if user is True else user.name, hostname = message.get_hostmask(), target = message.get_chan() if message.in_chan() else message.get_nick(),)) session.commit() session.close() except PNickParseError: message.alert(self.PParseError) except UserError: message.alert(self.AccessError) except PrefError: message.alert(self.PrefError) except ChanParseError, e: message.alert(self.ChanError%e)
def main(url=Config.get("URL", "ships"), debug=False): stats = urllib2.urlopen(url).read() session.execute(Ship.__table__.delete()) session.execute( text("SELECT setval('ships_id_seq', 1, :false);", bindparams=[false])) for line in sre.findall(stats): ship = Ship() line = list(line) for index, key in enumerate(keys): if line[index] in mapping: line[index] = mapping[line[index]] elif line[index].isdigit(): line[index] = int(line[index]) if line[index] not in ( '-', '', ): setattr(ship, key, line[index]) ship.total_cost = ship.metal + ship.crystal + ship.eonium if debug: print "%12s%12s%12s%12s" % ( ship.name, ship.class_, ship.race, ship.type, ) session.add(ship) session.commit() session.close()
def main(url=Config.get("URL", "ships")): # Remove old stats session.execute(Ship.__table__.delete()) session.execute( text("SELECT setval('ships_id_seq', 1, :false);", bindparams=[false])) # Fetch and parse new stats req = urllib2.Request(url) req.add_header('User-Agent', useragent) json.load(urllib2.urlopen(req), object_hook=add_ship) session.commit() session.close()
def main(url = Config.get("URL", "ships"), debug=False): stats = urllib2.urlopen(url).read() session.execute(Ship.__table__.delete()) session.execute(text("SELECT setval('ships_id_seq', 1, :false);", bindparams=[false])) for line in sre.findall(stats): ship = Ship() line = list(line) for index, key in enumerate(keys): if line[index] in mapping: line[index] = mapping[line[index]] elif line[index].isdigit(): line[index] = int(line[index]) if line[index] not in ('-', '',): setattr(ship,key,line[index]) ship.total_cost = ship.metal + ship.crystal + ship.eonium if debug: print "%12s%12s%12s%12s" % (ship.name, ship.class_, ship.race, ship.type,) session.add(ship) session.commit() session.close()
def run(self, message): m = self.match(message, self.commandre) if m is None: if self.match(message, self.helpre) is not None: self.help(message) return command = m.group(2) try: route, subcommand, user, params = self.router(message, command) route(message, user, params) session = Session() session.add( Command( command_prefix=message.get_prefix(), command=self.name, subcommand=subcommand, command_parameters=self.hide_passwords( self.name, message.get_msg()[len(m.group(1)) + 1:].strip()), nick=message.get_nick(), username="" if user is True else user.name, hostname=message.get_hostmask(), target=message.get_chan() if message.in_chan() else message.get_nick(), )) session.commit() session.close() except PNickParseError: message.alert(self.PParseError) except UserError: message.alert(self.AccessError) except PrefError: message.alert(self.PrefError) except ChanParseError, e: message.alert(self.ChanError % e)
def main(url=Config.get("URL", "ships"), debug=False): req = urllib2.Request(url) req.add_header('User-Agent', useragent) stats = urllib2.urlopen(req).read() session.execute(Ship.__table__.delete()) if Config.get("DB", "dbms") == "mysql": session.execute( text("ALTER TABLE ships AUTO_INCREMENT=1;", bindparams=[false])) else: session.execute( text("SELECT setval('ships_id_seq', 1, :false);", bindparams=[false])) for line in sre.findall(stats): ship = Ship() line = list(line) for index, key in enumerate(keys): if line[index] in mapping: line[index] = mapping[line[index]] elif line[index].isdigit(): line[index] = int(line[index]) if line[index] not in ( '-', '', ): setattr(ship, key, line[index]) ship.total_cost = ship.metal + ship.crystal + ship.eonium if debug: print "%12s%12s%12s%12s" % ( ship.name, ship.class_, ship.race, ship.type, ) session.add(ship) session.commit() session.close()
session.commit() t2 = time.time() - t1 print "History and final update in %.3f seconds" % (t2,) t1 = time.time() break except Exception, e: print "Something random went wrong, sleeping for 15 seconds to hope it improves" print e.__str__() traceback.print_exc() session.rollback() time.sleep(15) continue session.close() t1 = time.time() - t_start print "Total time taken: %.3f seconds" % (t1,) # Measure some dicks last_tick = Updates.current_tick() history_tick = max(last_tick - 72, 1) t_start = time.time() t1 = t_start session.execute(epenis.__table__.delete()) session.execute(text("SELECT setval('epenis_rank_seq', 1, :false);", bindparams=[false])) session.execute( text( "INSERT INTO epenis (user_id, penis) SELECT users.id, planet.score - planet_history.score FROM users, planet, planet_history WHERE users.active = :true AND users.access >= :member AND planet.active = :true AND users.planet_id = planet.id AND planet.id = planet_history.id AND planet_history.tick = :tick ORDER BY planet.score - planet_history.score DESC;", bindparams=[true, bindparam("member", Config.getint("Access", "member")), bindparam("tick", history_tick)],
print "To migrate from an old round use: createdb.py --migrate <previous_round>" sys.exit() if round: print "Moving tables to '%s' schema" % (round, ) try: session.execute(text("ALTER SCHEMA public RENAME TO %s;" % (round, ))) except ProgrammingError: print "Oops! Either you don't have permission to modify schemas or you already have a backup called '%s'" % ( round, ) session.rollback() sys.exit() else: session.commit() finally: session.close() print "Importing database models" from Core.maps import Channel print "Creating schema and tables" try: session.execute(text("CREATE SCHEMA public;")) except ProgrammingError: print "A public schema already exists, but this is completely normal" session.rollback() else: session.commit() finally: session.close()