def __init__(self, path, logger: Logger): self.logger = logger self.path = path self.table_name = "runs" self.conn = None self.columns = set(RunEntry.fields()) self.key = "path" self.fields = RunEntry.fields()
def __init__(self, path, logger: Logger): self.logger = logger self.path = path self.table_name = 'runs' self.conn = None self.columns = set(RunEntry.fields()) self.key = 'path' self.fields = RunEntry.fields() self.condition = f"""
def get( self, patterns: Iterable[PurePath], unless: Iterable[PurePath] = None, order: bool = None, descendants: bool = False, active: bool = False, since: datetime = None, last: timedelta = None, ) -> List[RunEntry]: if descendants: patterns = list(map(str, patterns)) patterns += [f'{str(pattern).rstrip("/%")}/%' for pattern in patterns] condition = DataBase.pattern_match(*patterns) if since or last: if since: time = since if last: time = datetime.now() - last if since and last: time = max(datetime.now() - last, since) condition = condition & GreaterThan("datetime", time) if active: condition = condition & In("path", *TMUXSession.active_runs(self.logger)) if unless: unless = DataBase.pattern_match(*unless) return [ RunEntry(PurePath(p), *e) for (p, *e) in self.select( condition=condition, unless=unless, order=order ).fetchall() ]
def add_run(self, path: PurePath, command: str, commit: str, datetime: str, description: str): self.sub_transactions.new_run.add( RunEntry(path=path, command=command, commit=commit, datetime=datetime, description=description))
def yaml_to_run_entry(node, *parts): parts += (node['name'], ) try: for child in node['children']: for run in yaml_to_run_entry(child, *parts): yield run except KeyError: yield RunEntry(path=PurePath(*parts), command=node['command'], commit=node['commit'], datetime=node['datetime'], description=node['description'])
def add_subparser(subparsers): parser = subparsers.add_parser( 'lookup', help="Lookup specific value associated with database entry.") parser.add_argument( 'key', choices=RunEntry.fields() + ('all', ), help='Key that value is associated with.') add_query_flags(parser, with_sort=True) parser.add_argument( '--porcelain', action='store_true', help='Print value only (for use with scripts)') return parser
def yaml_to_run_entry(node, *parts): parts += (node["name"], ) try: for child in node["children"]: for run in yaml_to_run_entry(child, *parts): yield run except KeyError: yield RunEntry( path=PurePath(*parts), command=node["command"], commit=node["commit"], datetime=node["datetime"], description=node["description"], )
def get(self, patterns: Sequence[PathLike], unless: Sequence[PathLike] = None, order: str = None, descendants: bool = False) -> List[RunEntry]: if descendants: patterns = [f'{pattern}%' for pattern in patterns] return [ RunEntry(PurePath(p), *e) for p, *e in self.execute( command=self.select(like=patterns, unless=unless, order=order), patterns=patterns, unless=unless, ).fetchall() ]
def add_subparser(subparsers): parser = subparsers.add_parser( "lookup", help="Lookup specific value associated with database entry." ) parser.add_argument( "key", choices=RunEntry.fields() + ("all",), help="Key that value is associated with.", ) add_query_args(parser, with_sort=True) parser.add_argument( "--porcelain", action="store_true", help="Print value only (for use with scripts)", ) return parser
def all(self, unless: Condition = None, order: str = None): self.check_field(order) return [ RunEntry(*e) for e in self.select(unless=unless, order=order).fetchall() ]
def all(self, unless=None, order=None): return [ RunEntry(*e) for e in self.execute( self.select(unless=unless, order=order), unless).fetchall() ]
def __getitem__(self, patterns: Sequence[PathLike]) -> List[RunEntry]: return [ RunEntry(PurePath(p), *e) for p, *e in self.execute(self.select( like=patterns), patterns).fetchall() ]
PathLike = Union[str, PurePath, PurePath, Path] DEFAULT_QUERY_FLAGS = { 'patterns': dict(nargs='*', type=PurePath, help='Look up runs matching these patterns'), '--unless': dict(nargs='*', type=PurePath, help='Exclude these paths from the search.'), '--active': dict(action='store_true', help='Include all active runs in query.'), '--descendants': dict(action='store_true', help='Include all descendants of pattern.'), '--sort': dict(default='datetime', choices=RunEntry.fields(), help='Sort query by this field.') } def add_query_flags( parser, with_sort: bool, default_flags: dict = DEFAULT_QUERY_FLAGS, ): if not with_sort: default_flags = copy(default_flags) del default_flags['--sort'] for arg_name, kwargs in default_flags.items(): parser.add_argument(arg_name, **kwargs)
return datetime.strptime("2002-12-04", "%Y-%m-%dT%H:%M:%S.%f%z") DEFAULT_QUERY_ARGS = { "patterns": dict( nargs="*", type=PurePath, help="Look up runs matching these patterns" ), "--unless": dict( nargs="*", type=PurePath, help="Exclude these paths from the search." ), "--active": dict(action="store_true", help="Include all active runs in query."), "--descendants": dict( action="store_true", help="Include all descendants of pattern." ), "--sort": dict( default="datetime", choices=RunEntry.fields(), help="Sort query by this field." ), "--since": dict( default=None, type=date_parse, help="Only display runs since this date. Accepts argument in isoformat.", ), "--last": dict( default=None, type=parse_time_delta, help="Only display runs created in the given time delta. " 'Either use "months", "weeks", "days", "hours" to specify time, e.g.' '"2weeks1day" or specify a date: month/day/year.', ), }