def decode_json(str_json: str) -> DecodeReturn: # quick and dirty... if "False" in str_json or "True" in str_json or "None" in str_json: changed_input = True str_json = str_json.replace("False", "false").replace("True", "true") str_json = str_json.replace("None", "null") else: changed_input = False # preferred parsing, supports single quotes and other more "versatile" formats if use_pyjson: try: json_pyjson = pyjson5.decode(str_json) if isinstance(json_pyjson, dict): return DecodeReturn(json_pyjson, changed_input) except Exception: # cant just catch pyjson5 as might not be imported... sad _log.debug( "Exception caught. If the bellow information doesn't mention 'pyjson5' please " "report this to Vexed.", exc_info=True, ) # secondary, less support but can pick up some other stuff try: return DecodeReturn(json.loads(str_json), changed_input) except json.JSONDecodeError: raise JSONDecodeError()
async def update_pools(self): loop = asyncio.get_event_loop() data = "" for url in self.Config.pool_urls: recv = await loop.run_in_executor(None, partial(rq.get, url)) data += recv.text.split("=")[1][:-2] tokens = decode(data) for v in tokens: v['logo'] = self.Config.logo_base + v['logo'] + "?raw=true" self.pools = {x['earnContractAddress'].lower(): x for x in tokens} return self.pools
def load_config_if_not_loaded(should_set_log_level=True): if Config.__is_loaded: return if not os.path.exists(Config.CONFIG_PATH): raise Exception(f"No config file found at: {Config.CONFIG_PATH}.") Config.__logger.info(f"Found config file at: {Config.CONFIG_PATH}") with open(Config.CONFIG_PATH) as config_json: Config.__config = pyjson5.decode(config_json.read()) if 'log_level' in Config.__config and should_set_log_level: Logger.set_level(Config.__config['log_level']) Config.__is_loaded = True
if __name__ == '__main__': basicConfig(level=DEBUG) logger = getLogger(__name__) args = argparser.parse_args() try: # open() does not work with Paths in Python 3.5 with codecs_open(str(args.input.resolve()), 'r', 'UTF-8') as f: data = f.read() except Exception: logger.error('Could not even read file: %s', args.input, exc_info=True) raise SystemExit(-1) try: obj = decode(data) except Exception: logger.error('Could not parse content: %s', args.input) raise SystemExit(1) try: json_obj = loads(data) except Exception: pass else: if not eq_with_nans(obj, json_obj): logger.error('JSON and PyJSON5 did not read the same data: %s, %r != %r', args.input, obj, json_obj) raise SystemExit(2) try: data = encode(obj)