def apply(self, evt): if evt.what == "orders": if evt.action in ("remove", "change"): r = self.remove_orders(evt.id, evt.slot) if failed(r): raise IOError("Unable to remove the order %s from %s (%s)..." % (evt.slot, evt.id, r[1])) if evt.action in ("create", "change"): r = self.insert_order(evt.id, evt.slot, evt.change) if failed(r): raise IOError( "Unable to insert the order %s (%r) from %s (%s)..." % (evt.slot, evt.change, evt.id, r[1])) if evt.slot == -1: evt.slot = len(server.cache.orders[evt.id]) o = self.get_orders(evt.id, evt.slot) if failed(o): raise IOError("Unable to get the order %s from %s (%s)..." % (evt.slot, evt.id, o[1])) evt.change = o[0] else: raise ValueError("Can't deal with that yet!")
def apply(self, evt): if evt.what == "orders": if evt.action in ("remove", "change"): r = self.remove_orders(evt.id, evt.slot) if failed(r): raise IOError("Unable to remove the order %s from %s (%s)..." % (evt.slot, evt.id, r[1])) if evt.action in ("create", "change"): r = self.insert_order(evt.id, evt.slot, evt.change) if failed(r): raise IOError("Unable to insert the order %s (%r) from %s (%s)..." % (evt.slot, evt.change, evt.id, r[1])) if evt.slot == -1: evt.slot = len(server.cache.orders[evt.id]) o = self.get_orders(evt.id, evt.slot) if failed(o): raise IOError("Unable to get the order %s from %s (%s)..." % (evt.slot, evt.id, o[1])) evt.change = o[0] else: raise ValueError("Can't deal with that yet!")
def updateCache(host, port, username, password): """Updates the current cache""" conn = connect(host, port, username, password) games = conn.games() if failed(games): print "Getting the game object failed!" return cache = createCache(host, port, username, password, games) cache.update(conn, callback) print("Cache Updated") return conn, cache
host, username, game, password = url2bits(uri) print host, username, game, password if not game is None: username = "******" % (username, game) connection = Connection() # Download the entire universe try: connection.setup(host=host, debug=debug) except Exception,e: #TODO make the exception more specific print "Unable to connect to the host." return if failed(connection.connect("daneel-ai/%i.%i.%i" % version)): print "Unable to connect to the host." return if failed(connection.login(username, password)): # Try creating the user.. print "User did not exist, trying to create user." if failed(connection.account(username, password, "", "daneel-ai bot")): print "Username / Password incorrect." return if failed(connection.login(username, password)): print "Created username, but still couldn't login :/" return games = connection.games()
def login(environ, start_response): """Login handler""" global cache print("--Logging In--") if environ['REQUEST_METHOD'].lower() == 'post': postdata = parse_qs(environ['wsgi.input'].read()) host, colon, port = postdata['host'][0].rpartition(':') if colon == '': host = port port = 6923 username = postdata['user'][0] password = postdata['pass'][0] data = {'auth': True, 'error': 'Wrong username or password.'} try: conn = middleman.connect(host, port, username, password) except middleman.ConnectionError, e: data['auth'] = False data['error'] = str(e) if data['auth']: # Set session when login was ok import datetime, hashlib session = environ.get('session') session['uid'] = (host, port, username, password, datetime.datetime.now()) session.save() games = conn.games() if failed(games): print "Getting the game object failed!" return cache = middleman.createCache(host, port, username, password, games) conn.disconnect() #Get the media list from the server import urllib import gzip import os # Get the media file zip if not os.path.exists(mediaListDir): os.mkdir(mediaListDir) mediaListZip = mediaListDir+'media-new.gz' mediaListNameServerVer = mediaListDir + 'media-new-server.txt' urllib.urlretrieve(serverMediaZip, mediaListZip) if os.path.exists(mediaListZip): fileObj = gzip.GzipFile(mediaListZip, 'rb'); fileObjOut = file(mediaListNameServerVer, 'wb'); while 1: lines = fileObj.readline() if lines == '': break fileObjOut.write(lines) fileObj.close() fileObjOut.close() os.remove(mediaListZip) # Check if local media file is different from server file if os.path.exists(mediaListName) and os.path.exists(mediaListNameServerVer): print "Both txt files are now local" localMediaSize = os.path.getsize(mediaListName) serverMediaSize = os.path.getsize(mediaListNameServerVer) print "Sizes: " + str(localMediaSize) + ", " + str(serverMediaSize) if not localMediaSize is serverMediaSize: os.remove(mediaListName) os.rename(mediaListNameServerVer, mediaListName) else: os.remove(mediaListNameServerVer) elif not os.path.exists(mediaListName): if os.path.exists(mediaListNameServerVer): os.rename(mediaListNameServerVer, mediaListName) else: print "There is no local image list file" fileHolder = open(mediaListName, 'w') fileHolder.write('') fileHolder.close()
state, message="", todownload=None, total=None, amount=None): """Callback function used when downloading all game data""" pass def connect(host, port, username, password): """Connect function, used by the backend""" try: connection = Connection(host=host, port=port, debug=False) except socket.error, e: raise ConnectionError('Unable to connect to the host.') if failed(connection.connect()): raise ConnectionError('Unable to connect to the host.') if failed(connection.login(username, password)): raise ConnectionError('Wrong user name or password.') cache = Cache(Cache.key(host, username), configdir='/tmp/tpclient-pyweb/cache/') return connection, cache def cache(host, username): return Cache(Cache.key(host, username), configdir='/tmp/tpclient-pyweb/cache/')
"""Exception used in connect""" def __init__(self, value): self.value = value def __str__(self): return str(self.value) def callback(mode, state, message="", todownload=None, total=None, amount=None): """Callback function used when downloading all game data""" pass def connect(host, port, username, password): """Connect function, used by the backend""" try: connection = Connection(host=host, port=port, debug=False) except socket.error, e: raise ConnectionError('Unable to connect to the host.') if failed(connection.connect()): raise ConnectionError('Unable to connect to the host.') if failed(connection.login(username, password)): raise ConnectionError('Wrong user name or password.') cache = Cache(Cache.key(host, username), configdir='/tmp/tpclient-pyweb/cache/') return connection, cache def cache(host, username): return Cache(Cache.key(host, username), configdir='/tmp/tpclient-pyweb/cache/')