def get_content_by_path(self, cake_path): ''' get_content_by_path (cake/a/b/c) -> cake Read_, Read_Any_Data Read_Any_Portal Read data behind cake, require specific Read_ permission or Read_Any_* . Also if there are portals in the path permissions will be validated on them before you able to proceed further. ''' if cake_path.relative(): raise AssertionError(f'Has to be absolute: {cake_path}') root = cake_path.root if root.header.type == CakeType.VTREE: return self._read_vtree(cake_path) elif root.header.type == CakeType.DMOUNT: return self._read_dmount(cake_path) content = self.get_content(root) for next_name in cake_path.path: bundle = CakeRack(utf8_reader(content.stream())) try: next_cake = bundle[next_name] except: reraise_with_msg(f'{cake_path} {bundle.content()}') if next_cake.is_resolved(): content = self.blob_store().get_content(next_cake) else: content = self.get_content(next_cake) if content.file_type == BINARY: content.file_type = guess_name(cake_path.filename()) content.mime = file_types[self.file_type].mime return content
def restore_inner(cakepath, path): content = store.get_content(cake_or_path=cakepath) try: bundle = CakeRack(utf8_reader(content.stream())) except: data = content.get_data() reraise_with_msg('Cake: {cakepath} {data}'.format(**locals())) ensure_directory(path) for child_name in bundle: file_path = os.path.join(path, child_name) child_path = cakepath.child(child_name) neuron = bundle.is_neuron(child_name) if neuron: restore_inner(child_path, file_path) else: file_cake = bundle[child_name] try: out_fp = open(file_path, "wb") in_fp = store.get_content(file_cake).stream() for chunk in read_in_chunks(in_fp): out_fp.write(chunk) in_fp.close() out_fp.close() except: reraise_with_msg("%s -> %s" % (file_cake, file_path)) return bundle.cake() if bundle.is_defined() else None
def test_reraise(): class _Ex(Exception): def __init__(self, a, b): Exception.__init__(self, a + " " + b) for e_type in range(3): for i in range(2): try: try: if e_type == 0: raise ValueError("EOF") elif e_type == 1: raise _Ex("a", "EOF") else: eval("hello(") except: if i == 0: kernel.reraise_with_msg("bye") else: kernel.reraise_with_msg("bye", sys.exc_info()[1]) except: e = sys.exc_info()[1] msg = kernel.exception_message(e) assert "EOF" in msg assert "bye" in msg
def convert(self, v: Any, direction: Conversion, flator: Optional[Flator] = None) -> Any: try: if (direction.needs_flator() and flator is not None and flator.is_applied(self.cls)): if direction.produces_object(): if isinstance(v, str): return flator.inflate(v, self.cls) else: if isinstance(v, self.cls): return flator.deflate(v) if direction.produces_object(): return self._from_json(v) else: return to_json(v) except: reraise_with_msg(f"{self.cls} {v}")
def convert(self, v: Any, direction: Conversion, flator: Optional[Flator] = None) -> Any: try: if direction.produces_object(): if v is None: if self.default is not None: return self.default else: return self.typing.default() else: return self.typing.convert(v, direction, flator) else: if v is None: return None else: return self.typing.convert(v, direction, flator) except: reraise_with_msg(f"error in {self}")