def _command_CREATE_TABLEs_sql(sin, sout, serr, argv, efx): """write to STDOUT the SQL that our sqlite storage adapter makes""" doc = _command_CREATE_TABLEs_sql.__doc__ defns = _formals_for_CREATE_TABLEs_sql(efx) vals, rc = _common_terminal(serr, argv, defns, doc) if vals is None: return rc path = vals.pop(_path_arg_key) assert not vals mon = efx.produce_monitor() listener = mon.listener from kiss_rdb.magnetics_.abstract_schema_via_definition \ import abstract_schema_via_graph_via_lines as func with open(path) as fh: abs_sch = func(fh, listener) if abs_sch is None: return mon.returncode abstract_tables = abs_sch.to_tables() func = _this_func() lineses = func(abstract_tables, listener) for lines in lineses: sout.writelines(lines) return mon.returncode
def _command_absch(sin, sout, serr, argv, efx): """Can we get as far as an abstract schema?""" doc = _command_absch.__doc__ defns = _formals_for_absch(efx) vals, rc = _common_terminal(serr, argv, defns, doc) if vals is None: return rc path = vals.pop(_path_arg_key) assert not vals mon = efx.produce_monitor() listener = mon.listener from kiss_rdb.magnetics_.abstract_schema_via_definition \ import abstract_schema_via_graph_via_lines as func with open(path) as fh: abs_sch = func(fh, listener) if abs_sch is None: return mon.returncode sout.writelines(abs_sch.to_description_lines()) return mon.returncode
def _command_graphviz_AST(sin, sout, serr, argv, efx): """See if a dotfile parses and how it breaks down in to an AST""" doc = _command_graphviz_AST.__doc__ defns = _formals_for_graphviz_AST(efx) vals, rc = _common_terminal(serr, argv, defns, doc) if vals is None: return rc path = vals.pop(_path_arg_key) assert not vals mon = efx.produce_monitor() listener = mon.listener from kiss_rdb.storage_adapters.graph_viz.AST_via_lines \ import sexps_via_lines as func count = 0 with open(path) as fh: sxs = func(fh, listener) for sx in sxs: count += 1 sout.writelines(sx.to_description_lines()) serr.write(f"(GraphViz file parsed into {count} elements)\n") return mon.returncode
def CLI(sin, sout, serr, argv): """"kiss sqlite3 toolkit": commands for developing kiss's sqlite3 library Reminder: if you're coming to this from 'kss', you can invoke it directly with 'kst' (kiss sqlite3 toolkit) which will save a few blips Although "sqlite" is in the name, this family of commands is in its conception "GraphViz"-centric: it's mainly for developing the vaporware non-command `dot2sql` Commands are vaguely (or not vaguely) in reverse-dependency order, with the highest-level commands at the top. So typically, if the higher- level commands (as visual tests) pass, the lower-level ones will also pass (but the reverse is not not necessarily true). So if you are troubleshooting how a GraphViz dotfile is parsing and you want "regression-friendly" order, start from the bottommost command and work upwards (or bisect the tests or whatever). """ func_argv, rc = _prepare_tail_call(serr, argv, _commands(), CLI) if not func_argv: return rc func, ch_argv = func_argv efx = _ExternalFunctions(serr) return func(sin, sout, serr, ch_argv, efx)
def _command_round_trip(sin, sout, serr, argv, efx): """(dotfile -> abstract schema -> sql -> abstract schema) and compare""" doc = _command_round_trip.__doc__ defns = _formals_for_round_trip(efx) vals, rc = _common_terminal(serr, argv, defns, doc) if vals is None: return rc path = vals.pop(_path_arg_key) assert not vals mon = efx.produce_monitor() listener = mon.listener from kiss_rdb.magnetics_.abstract_schema_via_definition \ import abstract_schema_via_graph_via_lines as func with open(path) as fh: abs_sch_one = func(fh, listener) if abs_sch_one is None: return mon.returncode abstract_tables = abs_sch_one.to_tables() sql_lineses_via = _this_func() def sql_lines(): lineses = sql_lineses_via(abstract_tables, listener) for lines in lineses: for line in lines: yield line sql_lines = sql_lines() from kiss_rdb.storage_adapters_.sqlite3._abstract_schema_to_and_fro \ import abstract_schema_via_sqlite_SQL_lines as func abs_sch_two = func(sql_lines) sch_diff = abs_sch_two.schema_diff_to(abs_sch_one) if sch_diff: serr.write("OH NOES\n") serr.writelines(sch_diff.to_description_lines()) return 123 serr.write("Success! no difference detected after round trip\n") return mon.returncode
def maybe_pop(): typ = my_stack[-1][0] # .. (for now assume always this direc) if 'then_call_this' != typ: return func, = my_stack[-1][1:] my_stack.pop() assert not my_stack # ..(for now always assume it's at the end) assert 'from_custom_state' == stack.pop().__name__ return func()
def stopping_listener(sev, shape, *rest): *mid, payloader = rest assert 'error' == sev if 'expression' == shape: lines = tuple(payloader()) else: assert 'structure' == shape wat = payloader() lines = [] if (reason := wat.pop('reason', None)): lines.append(reason) if (func := wat.pop('build_two_lines_of_ASCII_art')): lines.extend(func())
def abstract_schema_via_sqlite_SQL_lines(lines): # (in impl, this func is just a light wrapper that breaks streaming) def tables(): for typ, val in sxs: assert 'abstract_create_table_statement' == typ yield val sxs = tuple(_abstract_table_defs_via_sqlite_lines(lines)) from kiss_rdb.magnetics_.abstract_schema_via_definition import \ abstract_schema_via_abstract_tables as func return func(tables())
def produce_monitor(): from script_lib.magnetics.error_monitor_via_stderr import func return func(serr)
def _foz_via(defns, prog_name, cxer=None): from script_lib.cheap_arg_parse import formals_via_definitions as func return func(defns, prog_name, cxer)
dct = {k: v for k, v in context_keys_and_values()} return (dct, ) if dct else () def context_keys_and_values(): # If it's an open filehandle, add filename if hasattr(lines, 'name'): yield 'path', lines.name # Maybe an error happened while there were zero lines seen so far if tokens.line_offset is not None: yield 'line', tokens.line yield 'lineno', tokens.line_offset + 1 from text_lib.magnetics.string_scanner_via_string import \ build_throwing_string_scanner_and_friends as func o, build_scanner, stop = func(stopping_listener, cstacker) # (there's probably redundancy where both client and us raise stops on etc) wordy_token = o('wordy token', '[a-zA-Z_][a-zA-Z0-9_]*') # sure why not rxs = ''.join(("'", wordy_token.regex.pattern, "'")) single_quoted_fella = o('sinqle quoted fella', rxs) punctuation = o('punctuation', r'[,();]') space_or_tabs = o('tab or space', r'[ \t]+') newline = o('newline', r'\n') def tokens(): def end_of_line(): yn = scn.skip(newline) if not yn: return assert scn.empty