def saved_data(self): """Загружает и возвращает настройки вида, описанные в файле метаданных. :returns: Словарь с настройками вида :rtype: dict """ try: with open(join(self.path, "metadata.json"), 'r') as f: data = load(f) return data except (IOError, JSONDecodeError): return {}
def __restore_species__(self): """Выполняет восстановление видов после перезагрузки.""" try: for species_id in os.listdir(self.species_dir): if os.path.isdir(os.path.join(self.species_dir, species_id)): try: with open(os.path.join(self.species_dir, species_id, "metadata.json"), "r") as m: yield self.create_species(load(m), initialize=False) except (TypeError, ValueError, IOError): pass except OSError: pass IOLoop.current().spawn_callback(self.__restore_leaves__)
def schema(argument=None): """Создает валидирующий схему декоратор. Файлы с описанием декораторов ищутся в поддиректории schema, по имени без расширения. Если имя отсутствует, декоратор подставляет пустую схему, валидация которой проходит всегда, если во входных данных словарь. Такая схема автоматически преобразует его в kwargs запроса. :param argument: Путь к описанию схемы :type argument: str """ try: assert argument schema = join(dirname(realpath(__file__)), "schema", "{}.json".format(argument)) with open(schema, "r") as schema_file: schema_descriptor = load(schema_file) except IOError: raise Exception("Can't import schema: {}".format(argument)) except ValueError: raise Exception("Invalid schema declaration: {}".format(argument)) except AssertionError: schema_descriptor = { "$schema": "http://json-schema.org/draft-04/schema#", "id": "", "type": "object", "properties": {} } def real_decorator(function): def wrapper(self, *args, **kwargs): try: data = loads(self.request.body) validate(data, schema_descriptor) except (ValidationError, JSONDecodeError) as e: self.set_status(400) self.finish({"result": "failure", "message": str(e)}) else: data.update(kwargs) return function(self, *args, **data) return wrapper return real_decorator