def load_from(cls, file_): """Create an instance from a YAML file.""" file_ = path(file_) if not file_.exists: log.error("file does not exist: {0}".format(file_.absolute)) return return cls(list(yaml.load_all(file_.open("r"))), file_)
def include_tag(loader, node): """ Provides simplistic include support for YAML. """ current_file = path(loader.stream.name) include_file = current_file.parent.join(node.value) if not include_file.exists: raise yaml.YAMLError("'%s' does not exist" % include_file) _include_edges[current_file.absolute].append(include_file.absolute) for edges in tarjan.tarjan(_include_edges): if len(edges) > 1: raise yaml.YAMLError("circular dependency detected between %r" % edges) return yaml.load(include_file.open())
def __init__(self, documents, from_file=None): if not isinstance(documents, list): documents = [documents] self.documents = documents self.from_file = path(from_file) self.models = [] for document in self.documents: for name, documents in document.items(): if not name.startswith("_"): for data in documents: model = Model(name, data) model.validate() self.models.append(model) for model in self.models: model.validate()
def main(): setup_logging() parser = argparse.ArgumentParser() parser.add_argument("-w", "--working-directory", dest="path", default=path("."), nargs="?", help="specify path to YAML documents") parser.add_argument("spec", help="name of spec or 'all'") parser.add_argument("operation", help="[execute|dml]", nargs="?", default="dml") args = parser.parse_args() op = args.operation.lower() if op not in ("execute", "dml"): log.error("invalid operation") sys.exit(1) wd = path(args.path) default_wd = path("~/.trine") search_path = [wd] if not wd.exists: log.error("directory '{0}' does not exist".format(wd)) if not default_wd.exists: log.critical("default working directory (%s) does not exist" % default_wd) sys.exit(1) search_path += default_wd config_file = None search_path = [wd] if default_wd.exists: search_path.append(default_wd) for documents in [p.glob("*.yml") for p in search_path]: if not documents: log.error("no YAML documents found in {0}".format(wd.absolute)) else: for document in documents: if document.name == "config.yml": config_file = document break if config_file: break config = AttrDict(yaml.load(config_file.open("r"))) setup_db_sessions(config) specs = [] for documents in [p.glob("*.yml") for p in search_path]: for spec in documents: if spec.name == "config.yml": continue if args.spec == "all" or spec.name.startswith(args.spec): specs.append(spec) if args.spec == spec.name: break if not specs: log.error("invalid spec name") sys.exit(1) install_yaml_tags() for spec in specs: spec = SpecFile.load_from(spec) total_queries = 0 for model, bind, queries in spec.query_generator(): if op == "dml": [printquery(query, bind) for query in queries] elif op == "execute": affected_rows = 0 queries = list(queries) log.info({ "update": "updated", "insert": "created", "delete": "deleted", }[model.stmt_used] + " " + str(model)) for query in queries: result = bind.execute(query) affected_rows += result.rowcount if "items" in model.data: log.info("populating vendor with %d items" % len(model.data["items"])) total_queries += len(queries) if op == "execute": log.debug("executed %d queries (%d records affected)" % (total_queries, affected_rows))