def _open_schema_file(coll_path, listener, opn=None): from kiss_rdb import SCHEMA_FILE_ENTRY_ as tail from os.path import join as os_path_join schema_path = os_path_join(coll_path, tail) try: return (opn or open)(schema_path) except FileNotFoundError as e: # #here4 args = (e,) func = _emit_about_no_schema_file except NotADirectoryError: args = (coll_path,) func = _emit_about_no_extname func(listener, *args)
def _dig(funcs, coll, key_desc_pairs, listener): key_desc_pairs = tuple(key_desc_pairs) assert len(key_desc_pairs) assert all('_' != k[0] for k, _ in key_desc_pairs) # make sure no names.. say_collection = coll._to_noun_phrase from kiss_rdb.magnetics.via_collection import DIGGY_DIG as func return func(funcs, key_desc_pairs, say_collection, listener)
def collection_via(path, listener, opn=None): ext = path[-3:] if '.md' != ext: raise RuntimeError(f"expected '.md' had {ext!r} where?") import kiss_rdb.storage_adapters_.markdown_table as sa_mod from kiss_rdb import collection_via_storage_adapter_and_path as func return func(sa_mod, path, listener, opn=opn)
class error_monitor_via_stderr: """a common CLI-targeted expression strategy for emissions Its main useful exposure is a modality-specific implementation of a [#017.3] listener, one that will reach out to an unified expressor for structured emissions. The reason we need an error monitor bespoke for CLI is that if a structured emission has an `errno`, we need to grab on to it (with mutation).. """ # this is conceptually a sub-class of [#507.9] the generic error monitor def __init__(self, stderr, interceptor=None, default_error_exitstatus=1): self.exitstatus = 0 self.OK = True self._max_non_default_error_status_seen = 0 self._default_error_exitstatus = default_error_exitstatus self._interceptor = interceptor self._stderr = stderr def listener(self, *a): from modality_agnostic import emission_via_tuple as func em = func(a) if self._interceptor is not None: tf = self._interceptor(em) if tf is True: return assert(tf is None) self._see_emission_severity(em) self.__express_emission(em) def __express_emission(self, em): self._write_line = _line_writer_via_IO(self._stderr) # meh if 'expression' == em.shape: return self.__express_when_shape_is_expression(em) assert('structure' == em.shape) self.__express_when_shape_is_structure(em) def __express_when_shape_is_structure(self, em): dct = em.payloader() seen = set() for k in ('returncode', 'errno'): if (n := dct.get(k, None)) is not None: seen.add(n) if seen: n, = seen # implicit assertion self.see_exitstatus(n) write_line = self._write_line from .expression_via_structured_emission import \ lines_via_channel_tail_and_details as func for line in func(em.to_channel_tail(), dct): write_line(line)
def listener(self, *a): from modality_agnostic import emission_via_tuple as func em = func(a) if self._interceptor is not None: tf = self._interceptor(em) if tf is True: return assert(tf is None) self._see_emission_severity(em) self.__express_emission(em)
def re_emit_case_error_CRAZILY(listener, stack, emi_tup): # EXPERIMENTAL """We are preoccupied with whether or not we can derive [#510.14] context-stack-like metadata from a call stack (we can). We haven't yet stopped to think if we should. """ whens = list(_whens_via_stack_until_main(stack)) if not len(whens): return listener(*emi_tup) # To be proper, we should do this work inside the "lineser" but meh from modality_agnostic import emission_via_tuple as func emi = func(emi_tup) # Resolve messages sct_not_expr = ('expression', 'structure').index(emi.shape) if sct_not_expr: # (Case3419DP) sct = emi.payloader() msg = sct.get('reason') if not msg: msg = sct['reason_tail'] msgs = [msg] # #here2 else: msgs = list(emi.to_messages()) # Downcase the first letter of the first messages msg = msgs[0] msgs[0] = ''.join((msg[0].lower(), msg[1:])) # downcase the first letter # Prefix "When " to the first of the "when" phrases, and big flex whens[0] = ' '.join(('When', whens[0])) prefix = ''.join((' and '.join(whens), ', ')) # Synthesize (by making the first message longer, not prepending a message) msgs[0] = ''.join((prefix, msgs[0])) msgs = tuple(msgs) # Pop it back in if sct_not_expr: def use_payloader(): return sct assert 1 == len(msgs) sct['reason'], = msgs # #here2 else: def use_payloader(): return msgs listener(*emi.channel, use_payloader)
def _dereference(self, key): sx = self._reference_via_key[key] if 'loaded' == sx[0]: # #here2 return sx[1] self._reference_via_key[key] = False # lock it for safety typ, hub_offset = sx assert 'reference' == typ hub_mod_name = self._hubs[hub_offset] # #here3 mod_name = '.'.join((hub_mod_name, key)) from importlib import import_module as func module = func(mod_name) sa = _StorageAdapter(module, key) self._reference_via_key[key] = 'loaded', sa # #here2 return sa
def open(self, path, mode): # below used to be _FakeWriteSession before #history-A.1 if mode not in ('w', 'x'): raise Exception(f'cover me: {repr(mode)}') buff = [] mutex = OneShotMutex() def on_OK_exit(): mutex.shoot() self._receive_writes(buff, path) from modality_agnostic import write_only_IO_proxy as func return func( write=lambda s: buff.append(s), # #hi. on_OK_exit=on_OK_exit)
def spy_on_write_via_receivers(receivers, isatty=None): # multiplex `write()` calls if 1 == len(receivers): def write(s): recv(s) return len(s) recv, = receivers else: def write(s): for recv in receivers: recv(s) return len(s) from modality_agnostic import write_only_IO_proxy as func return func(write=write, flush=lambda: None, isatty=isatty)
def check_line(line): x = func(line) return () if x is None else x
def emit_about_no_ent(listener, e): # [pho] func = _emission_details_via_file_not_found_error listener('error', 'structure', 'cannot_load_collection', 'no_such_file_or_directory', lambda: func(e))
def _schema_parse_narrator(opened, main, listener): export, controllerer = _export_and_controllerer() @export def check_that_first_field_line_has_a_particular_name(): if 'storage_adapter' == reso.field.field_name: return def cstacker(): return (scanner.contextualize_about_field_name({}, reso.field),) _emit_about_first_field_name(listener, cstacker) raise stop() @export def resolve_first_field_line(): if there_is_another_block(): if its_a_separator_block(): if there_is_another_block(): if its_a_field_line(): found_it() # (Case1418) else: assert_and_whine_about_effectively_empty_file() elif its_a_field_line(): found_it() else: assert_and_whine_about_literally_empty_file() def found_it(): reso.field = reso.block del reso.block def its_a_field_line(): return reso.block.is_field_line def its_a_separator_block(): return reso.block.is_separator_block def there_is_another_block(): block = scn.next_block(listener) if block is None: raise stop() # (Case1414) reso.block = block return True def assert_and_whine_about_effectively_empty_file(): assert reso.block.is_end_of_file whine_empty('effectively_empty_file') # (Case1417) def assert_and_whine_about_literally_empty_file(): assert reso.block.is_end_of_file whine_empty('literally_empty_file') # (Case1415) def whine_empty(which): _emit_about_empty_schema_file(listener, which, scn) raise stop() class reso: pass from kiss_rdb.storage_adapters_.rec import ErsatzScanner scn = ErsatzScanner(opened) from kiss_rdb.magnetics_.schema_file_scanner_via_recfile_scanner \ import func scanner = func(scn) reso.schema_file_scanner = scanner class stop(RuntimeError): pass controller = controllerer() controller.stop = stop try: return main(reso, controller) except stop: pass
def _write_only_IO_proxy(*args, **kwargs): from modality_agnostic import write_only_IO_proxy as func return func(*args, **kwargs)
def production_collectioner(): from data_pipes import meta_collection_ as func return func()
def PS_via_mixed(mixed): if isinstance(mixed, str): from data_pipes.format_adapters.producer_script import \ producer_script_module_via_path_ as func return func(mixed, None) return FakeProducerScript(**mixed)
def _listener(*args): # used as default argument from modality_agnostic import throwing_listener as func func(*args)
def _null_context(x): from contextlib import nullcontext as func return func(x)
def _emission_details_via_file_not_found_error(exc): from modality_agnostic import \ emission_details_via_file_not_found_error as func return func(exc)
def do_explain(conditions_seen, when_X_is, num_args, instance_x=None): func = _case_lib()._explain_case_failure return func(conditions_seen, when_X_is, num_args, instance_x)
def structurer(): # (Case1413) return func(file_not_found)