def load_config(args): """Load the configuration file.""" config = Config(args.config) # set Config().gridrealm.debug to True if passed in at cmd line. if config.gridrealm.debug != args.debug: config.update_option('gridrealm', 'debug', args.debug) return
def prep_db(args): """Prepare the database engine and sessions for use.""" config = Config() if args.removedb: path = config.gridrealm.database_url path = path.replace('sqlite:///', '') if os.path.exists(path): print('Removing database file: %s' % path) check_call(['rm', '-f', path]) sys.exit(0) load_db(config.gridrealm.database_url) if args.initdb: print('Initializing Game Database and Exiting.') init_db() create_map() sys.exit(0) # pylint cannot tell that this gets registered with and used by the app # pylint: disable=unused-variable @gridrealm.APP.teardown_appcontext def shutdown_session(exception=None): # pylint: disable=unused-argument gridrealm.DBS.remove()
def prep_flask(): """Do the final prep steps for running the flask app.""" # This patches the python threading stuff with greenlets from gevent gevent.monkey.patch_all() Config().update_flask(gridrealm.APP) # prep a global system message server sent event channel if gridrealm.SYS_MSG is None: gridrealm.SYS_MSG = Channel()
def get(self): """Perform a GET request for the index context.""" # pylint cannot find the logger on the APP # pylint: disable=no-member if flask_g.user is not None: # User is logged in GR.APP.logger.debug('User is logged in: %s' % flask_g.user.name) response = GR.APP.make_response(render_template( get_client_template())) else: # No user is logged in GR.APP.logger.debug('No user is logged in.') response = GR.APP.make_response(render_template( Config().assets.landing_uri)) clear_cookies(response) return response
def post(self): """Perform a POST request for the index context.""" # pylint cannot find the logger on the APP # pylint: disable=no-member if 'username' in request.form and request.form['username'] != "": # TODO: do something better to let the user log in session['username'] = request.form['username'] response = GR.APP.make_response(render_template( get_client_template())) else: # misformed/missing form data, return back the landing page response = GR.APP.make_response(render_template( Config().assets.landing_uri)) # clear cookies in case they haven't been cleared at this point clear_cookies(response) return response # query database for user, since username was not in session before flask_g.user = DB.User.query.filter( DB.User.name == session['username']).first() if flask_g.user is not None: # User has logged back in msg = '%s is back. last logout was at %s' % ( flask_g.user.name, ts_to_str(flask_g.user.last_logout)) GR.SYS_MSG.publish(msg) GR.APP.logger.debug(msg) else: # New user is being created uname = session['username'] msg = 'New player named %s' % uname GR.SYS_MSG.publish(msg) GR.APP.logger.debug(msg) flask_g.user = User(uname, time()) # update user's last login time to now old_last_login = flask_g.user.last_login flask_g.user.last_login = time() # add new user or update existing user in the database GR.DBS.add(flask_g.user) GR.DBS.commit() set_cookies(response=response, username=str(flask_g.user.name), last_login=ts_to_str(old_last_login)) return response
def _load_image_list(self, specific_path=None): """Load a list of images from the asset folder.""" images = [] assets = "_assets" start_path = os.path.join(GR.APP.static_folder, Config().assets.asset_path) if specific_path: start_path = os.path.join(start_path, specific_path) for dpath, _, fnames in os.walk(start_path): common = dpath[dpath.find(assets) + len(assets):] common = common[1:] if common.startswith('/') else common if common == "dev": continue for fname in fnames: if fname.startswith(".") or fname.startswith("_"): # skip files starting with "." or "_" continue if fname not in images: img_uri = os.path.join(assets, common, fname) images.append(img_uri) return images
def get(self, doc=None): """Retrieve the static documentation file.""" if not doc: doc = 'index.html' fpath = Config().assets.docs_stub % doc return GR.APP.send_static_file(fpath)
def get(self): """Retrieve the static favicon icon.""" favicon_uri = Config().assets.favicon_uri return GR.APP.send_static_file(favicon_uri)
def get_client_template(): """Choose a client template based on the debug setting state.""" template = Config().assets.client_uri if Config().gridrealm.debug: template = Config().assets.debug_client_uri return template
def get(self, asset): """Retrieve the requested static asset file.""" fpath = Config().assets.asset_stub % asset return GR.APP.send_static_file(fpath)