def asxnd(self, model="simple"): e = xnd.Element(None, str(self[name][0].content)) empty = None node = misc.first(self[bodycontent], None) if node is not None: value = str(node[0].content) if value in ("tagdependent", "JSP"): empty = False elif value == "empty": empty = True else: raise ValueError( "value {!r} is not allowed for tag <bodycontent>".format( value)) if model != "none": if model == "simple": e.modeltype = not empty else: e.modeltype = "sims.Empty" if empty else "sims.Any" node = misc.first(self[info], None) if node is not None: e.doc = node.asxnd(model=model) for attr in self[attribute]: e += attr.asxnd(model=model) return e
def asxnd(self, model="simple"): ns = xnd.Module() node = misc.first(self[uri], None) xmlns = str(node[0].content) if node is not None else None node = misc.first(self[info], None) if node is not None: ns.doc = node.asxnd(model=model) for node in self[tag]: e = node.asxnd(model=model) if xmlns is not None and isinstance(e, xnd.Element): e.xmlns = xmlns ns += e return ns
def asxnd(self, model="simple"): ns = xnd.Module() node = misc.first(self[uri], None) xmlns = str(node[0].content) if node is not None else None node = misc.first(self[info], None) if node is not None: ns.doc = node.asxnd(model=model) for node in self[tag]: e = node.asxnd(model=model) if xmlns is not None and isinstance(e, xnd.Element): e.xmlns = xmlns ns += e return ns
def asxnd(self, model="simple"): isrequired = False node = misc.first(self[required], None) if node is not None: value = str(node).strip() if value in ("true", "yes"): isrequired = True elif value not in ("false", "no"): raise ValueError(f"value {value!r} not allowed for tag <required>") return xnd.Attr(str(self[name][0].content), "xsc.TextAttr", isrequired)
def asxnd(self, model="simple"): isrequired = False node = misc.first(self[required], None) if node is not None: value = str(node).strip() if value in ("true", "yes"): isrequired = True elif value not in ("false", "no"): raise ValueError( f"value {value!r} not allowed for tag <required>") return xnd.Attr(str(self[name][0].content), "xsc.TextAttr", isrequired)
def asxnd(self, model="simple"): e = xnd.Element(None, str(self[name][0].content)) empty = None node = misc.first(self[bodycontent], None) if node is not None: value = str(node[0].content) if value in ("tagdependent", "JSP"): empty = False elif value == "empty": empty = True else: raise ValueError(f"value {value!r} is not allowed for tag <bodycontent>") if model != "none": if model == "simple": e.modeltype = not empty else: e.modeltype = "sims.Empty" if empty else "sims.Any" node = misc.first(self[info], None) if node is not None: e.doc = node.asxnd(model=model) for attr in self[attribute]: e += attr.asxnd(model=model) return e
def test_walkgetitem(): e = html.div(1, html.div(2, html.div(3))) isdiv = xfind.selector(html.div) # Test ``walknodes`` assert str(misc.first(e.walknodes(isdiv))) == "123" assert str(misc.last(e.walknodes(isdiv))) == "3" misc.item(e.walknodes(isdiv), 3) is None misc.item(e.walknodes(isdiv), -4) is None # Test ``walkpaths`` assert str(misc.item(e.walkpaths(isdiv), (0, -1))) == "123" assert str(misc.item(e.walkpaths(isdiv), (-1, -1))) == "3" misc.item(e.walkpaths(isdiv), 3) is None misc.item(e.walkpaths(isdiv), -4) is None
def makexnd(stream, encoding=None, shareattrs="dupes", model="simple"): # :obj:`stream` can be a stream, an :class:`URL` or ``str``/``bytes`` encoding = None if isinstance(stream, str): encoding = "utf-8" stream = stream.encode(encoding) node = parse.tree(stream, parse.Expat(encoding=encoding), parse.NS(tld), parse.Node()) # get and convert the taglib object xnd = misc.first(node.walknodes(tld.taglib)).asxnd(model=model) if shareattrs=="dupes": xnd.shareattrs(False) elif shareattrs=="all": xnd.shareattrs(True) return xnd
def makexnd(stream, encoding=None, shareattrs="dupes", model="simple"): # :obj:`stream` can be a stream, an :class:`URL` or ``str``/``bytes`` encoding = None if isinstance(stream, str): encoding = "utf-8" stream = stream.encode(encoding) node = parse.tree(stream, parse.Expat(encoding=encoding), parse.NS(tld), parse.Node()) # get and convert the taglib object xnd = misc.first(node.walknodes(tld.taglib)).asxnd(model=model) if shareattrs=="dupes": xnd.shareattrs(False) elif shareattrs=="all": xnd.shareattrs(True) return xnd
def _alarm_fork(self, signum, frame): pids = self._kill_children() maxtime = self.getmaxtime() self.endtime = datetime.datetime.now() exc = RuntimeError(f"maximum runtime {maxtime} exceeded in forked child (pid {misc.sysinfo.pid})") if self.noisykills: self.log.email(exc) else: self.log(exc) if len(pids) == 1: pid = misc.first(pids) self.log.sisyphus.result.kill(f"Terminated child {pid} after {maxtime}") else: pids = ", ".join(str(pid) for pid in pids) self.log.sisyphus.result.kill(f"Terminated children {pids} after {maxtime}") for logger in self._loggers: logger.close() os._exit(1)
def test_walkgetitem(): e = html.div( 1, html.div( 2, html.div( 3 ) ) ) isdiv = xfind.selector(html.div) # Test ``walknodes`` assert str(misc.first(e.walknodes(isdiv))) == "123" assert str(misc.last(e.walknodes(isdiv))) == "3" misc.item(e.walknodes(isdiv), 3) is None misc.item(e.walknodes(isdiv), -4) is None # Test ``walkpaths`` assert str(misc.item(e.walkpaths(isdiv), (0, -1))) == "123" assert str(misc.item(e.walkpaths(isdiv), (-1, -1))) == "3" misc.item(e.walkpaths(isdiv), 3) is None misc.item(e.walkpaths(isdiv), -4) is None
def main(args=None): p = argparse.ArgumentParser(description="Check Oracle database schema for cyclic foreign key references", epilog="For more info see http://python.livinglogic.de/orasql_scripts_oracycles.html") p.add_argument("connectstring", help="Oracle connect string") p.add_argument("-v", "--verbose", dest="verbose", help="Give a progress report? (default %(default)s)", action=misc.FlagAction, default=False) p.add_argument("-c", "--color", dest="color", help="Color output (default %(default)s)", default="auto", choices=("yes", "no", "auto")) args = p.parse_args(args) if args.color == "yes": color = True elif args.color == "no": color = False else: color = None stdout = astyle.Stream(sys.stdout, color) stderr = astyle.Stream(sys.stderr, color) connection = orasql.connect(args.connectstring) cs = s4connectstring(connection.connectstring()) refs = {} # type: Dict[Table, Tuple[ForeignKey, Table]] # Collect tables tables = list(connection.tables()) # Collect foreign keys from tables for (i, table) in enumerate(tables, 1): if args.verbose: stderr.writeln("oracycles.py: ", cs, f": Collecting fks from table #{i:,}/{len(tables):,} ", s4object(table.name), f" -> {len(refs):,} found") for constraint in list(table.constraints()): if isinstance(constraint, orasql.ForeignKey): pk = constraint.refconstraint() if isinstance(pk, orasql.PrimaryKey): if table not in refs: refs[table] = [] reftable = pk.table() refs[table].append((constraint, reftable)) # Find cycles in foreign keys cycles = {} def collect(path): for i in range(len(path)-2, 0, -1): if path[i] == path[-1]: cyclepath = path[i:] pathkey = frozenset(cyclepath) if pathkey not in cycles: cycles[pathkey] = cyclepath return def findcycles(path): table = path[-1] if table in refs: for (constraint, reftable) in refs[table]: path.append(constraint) cycle = reftable in path path.append(reftable) if cycle: collect(path) else: findcycles(path) path.pop() path.pop() for (i, table) in enumerate(tables, 1): if args.verbose: stderr.writeln("oracycles.py: ", cs, f": Testing table #{i:,}/{len(tables):,} ", s4object(table.name), f" for cycles -> {len(cycles):,} found") findcycles([table]) # Sort and output result def pathstr(path): v = [] for obj in path: if isinstance(obj, orasql.ForeignKey): v.append(f"{misc.first(obj.columns()).name}({obj.name})") else: v.append(obj.name) return " -> ".join(v) cycles = sorted(cycles.values(), key=pathstr) for path in cycles: for (i, obj) in enumerate(path): if i: stdout.write(" -> ") if isinstance(obj, orasql.ForeignKey): stdout.write(s4object(misc.first(obj.columns()).name), "(", s4object(obj.name), ")") else: stdout.write(s4object(obj.name)) stdout.writeln()
def test_first(): e = iter(range(10)) assert misc.first(e) == 0 assert misc.first(e) == 1 assert misc.first([]) is None assert misc.first([], 42) == 42
def main(args=None): p = argparse.ArgumentParser( description= "Check Oracle database schema for cyclic foreign key references", epilog= "For more info see http://python.livinglogic.de/orasql_scripts_oracycles.html" ) p.add_argument("connectstring", help="Oracle connect string") p.add_argument("-v", "--verbose", dest="verbose", help="Give a progress report? (default %(default)s)", action=misc.FlagAction, default=False) p.add_argument("-c", "--color", dest="color", help="Color output (default %(default)s)", default="auto", choices=("yes", "no", "auto")) args = p.parse_args(args) if args.color == "yes": color = True elif args.color == "no": color = False else: color = None stdout = astyle.Stream(sys.stdout, color) stderr = astyle.Stream(sys.stderr, color) connection = orasql.connect(args.connectstring) cs = s4connectstring(connection.connectstring()) refs = {} # type: Dict[Table, Tuple[ForeignKey, Table]] # Collect tables tables = list(connection.tables()) # Collect foreign keys from tables for (i, table) in enumerate(tables, 1): if args.verbose: stderr.writeln( "oracycles.py: ", cs, f": Collecting fks from table #{i:,}/{len(tables):,} ", s4object(table.name), f" -> {len(refs):,} found") for constraint in list(table.constraints()): if isinstance(constraint, orasql.ForeignKey): pk = constraint.refconstraint() if isinstance(pk, orasql.PrimaryKey): if table not in refs: refs[table] = [] reftable = pk.table() refs[table].append((constraint, reftable)) # Find cycles in foreign keys cycles = {} def collect(path): for i in range(len(path) - 2, 0, -1): if path[i] == path[-1]: cyclepath = path[i:] pathkey = frozenset(cyclepath) if pathkey not in cycles: cycles[pathkey] = cyclepath return def findcycles(path): table = path[-1] if table in refs: for (constraint, reftable) in refs[table]: path.append(constraint) cycle = reftable in path path.append(reftable) if cycle: collect(path) else: findcycles(path) path.pop() path.pop() for (i, table) in enumerate(tables, 1): if args.verbose: stderr.writeln("oracycles.py: ", cs, f": Testing table #{i:,}/{len(tables):,} ", s4object(table.name), f" for cycles -> {len(cycles):,} found") findcycles([table]) # Sort and output result def pathstr(path): v = [] for obj in path: if isinstance(obj, orasql.ForeignKey): v.append(f"{misc.first(obj.columns()).name}({obj.name})") else: v.append(obj.name) return " -> ".join(v) cycles = sorted(cycles.values(), key=pathstr) for path in cycles: for (i, obj) in enumerate(path): if i: stdout.write(" -> ") if isinstance(obj, orasql.ForeignKey): stdout.write(s4object(misc.first(obj.columns()).name), "(", s4object(obj.name), ")") else: stdout.write(s4object(obj.name)) stdout.writeln()