def load_config(path=None): ''' This extends the tornado parser to enable use in heroku where options are accessed through os.getenv Will read file at path if exists Will then read environment variables to override Will then parse command line to override ''' if path is not None and os.path.isfile(path): logging.info("loading config from %s", path) parse_config_file(path) for k in options.as_dict(): ''' danger: access of private variables ''' value = os.getenv(k) if value: name = options._normalize_name(k) option = options._options.get(name) option.parse(value) parse_command_line()
def new_cursor(): try: return cursor() except pymysql.err.OperationalError as e: print("MySQL error, recreating connection") server = options.mysql["server"] user = options.mysql["user"] password = options.mysql["password"] database = options.mysql["database"] connection = pymysql.connect(host=server, user=user, password=password, db=database,cursorclass=pymysql.cursors.DictCursor, charset='utf8') conn = makePwnedConnection(connection) normalized = options._normalize_name("connection") if normalized in options._options: options._options[normalized].set(connection) print("Connection recreated")
def parse_config_file(path, final=True): """Parses and loads the Python config file at the given path. This version allow customize new options which are not defined before from a configuration file. """ config = {} with open(path, 'rb') as f: exec_in(native_str(f.read()), {}, config) for name in config: normalized = options._normalize_name(name) if normalized in options._options: options._options[normalized].set(config[name]) else: tornado.options.define(name, config[name]) if final: options.run_parse_callbacks()
def safe_define( key, default=None, type=None, help=None, metavar=None, multiple=False, group=None, callback=None, *args, **kwargs ): """ We try to redefine the tornado.options.define to allow redefining, but safe-re-updates as well. :param key: :param default: :param type: :param help: :param metavar: :param multiple: :param group: :param callback: :param args: :param kwargs: :return: """ try: _ = option_parser.__getattr__(key) # Redefine things here normalized = option_parser._normalize_name(key) option_object = option_parser._options[normalized] if type is None: if not multiple and default is not None: type = default.__class__ else: type = str frame = sys._getframe(0) options_file = frame.f_code.co_filename # Can be called directly, or through top level define() fn, in which # case, step up above that frame to look for real caller. if ( frame.f_back.f_code.co_filename == options_file and frame.f_back.f_code.co_name == "define" ): frame = frame.f_back file_name = frame.f_back.f_code.co_filename if file_name == options_file: file_name = "" if type is None: if not multiple and default is not None: type = default.__class__ else: type = str if group: group_name = group else: group_name = file_name option_object.default = default option_object.help = help option_object.type = type option_object.metavar = metavar option_object.multiple = multiple option_object.file_name = file_name option_object.group_name = group_name option_object.callback = callback except AttributeError: option_parser.define( name=key, default=default, help=help, metavar=metavar, type=type, multiple=multiple, group=group, callback=callback, *args, **kwargs ) return