def from_string(cls, data: Union[bytes, str]) -> Editor: if isinstance(data, bytes): try: data = data.decode() except UnicodeDecodeError: raise EditorError("Invalid level data received.") from None if not data: # nothing interesting... return cls() info, *objects = data.split(";") # remove last object if none try: last = objects.pop() if last: objects.append(last) except IndexError: pass header = Header.from_string(info) objects = list(map(Object.from_string, objects)) return cls(*objects).set_header(header)
def get_id(x: str, ret_enum: bool = False, delim: str = ":") -> Any: """Calculate required value from the given directive ``x``. The format is, as follows: ``class:name``, e.g. ``special:h``. Spaces around ``:`` are allowed. Parameters ---------- x: :class:`str` Directive to get value from. ret_enum: :class:`bool` Whether to convert found value to enum. By default, ``False``. delim: :class:`str` Character to split given directive string with. It is not recommended to pass this argument to the function. Returns ------- `Any` The value found, if any. Raises ------ :exc:`.EditorError` Failed to convert directive to the value. """ typeof, name = (string.strip().replace("_", "").lower() for string in x.split(delim, maxsplit=1)) try: found = supported[typeof][name] if isinstance(found, str) and delim in found: # inner directive return get_id(found) elif ret_enum: return mapping[typeof](found) return found except Exception: raise EditorError(f"ID by directive {x!r} was not found.") from None
def dump(self) -> None: raise EditorError("Level API can not be dumped.")
def from_string(cls, string: str) -> None: raise EditorError("Level API can not be created from string.")
def from_string(cls, string: str) -> Struct: try: return cls.from_mapping(cls._convert(string)) except Exception as exc: raise EditorError("Failed to process string.") from exc