Skip to content

Commit ba77066

Browse files
committed
Return YAML in command get
1 parent 043554c commit ba77066

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

pathtraits/cli.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def main():
1414

1515

1616
@main.command(help="Update database once, searches for all directories recursively.")
17-
@click.argument("path", required=True)
17+
@click.argument("path", required=True, type=click.Path(exists=True))
1818
@click.option("-v", "--verbose", "verbose", flag_value=True, default=False)
1919
@click.option(
2020
"--include-files",
@@ -42,7 +42,7 @@ def batch(path, verbose, include_files):
4242

4343

4444
@main.command(help="Update database continiously, watches for new or changed files.")
45-
@click.argument("path", required=True)
45+
@click.argument("path", required=True, type=click.Path(exists=True))
4646
@click.option("-v", "--verbose", "verbose", flag_value=True, default=False)
4747
def watch(path, verbose):
4848
if verbose:
@@ -65,5 +65,34 @@ def watch(path, verbose):
6565
db.add_pathpair(pair)
6666

6767

68+
@main.command(help="Get traits of a given path")
69+
@click.argument("path", required=True, type=click.Path(exists=True))
70+
@click.option("-v", "--verbose", "verbose", flag_value=True, default=False)
71+
def get(path, verbose):
72+
if verbose:
73+
logging.basicConfig(level=logging.DEBUG)
74+
75+
abs_path = os.path.abspath(path)
76+
leaf_dir = os.path.dirname(abs_path) if os.path.isfile(abs_path) else abs_path
77+
dirs = leaf_dir.split("/")
78+
for i in reversed(range(0, len(dirs))):
79+
if i == 0:
80+
db_dir = "/"
81+
else:
82+
db_dir = "/".join(dirs[0 : i + 1])
83+
84+
db_path = db_dir + "/.pathtraits.db"
85+
if not os.path.exists(db_path):
86+
continue
87+
88+
# TODO: recursive inheritance of pathtraits
89+
db = TraitsDB(db_dir)
90+
data = db.get("data", path=abs_path)
91+
print(yaml.safe_dump(data))
92+
return
93+
94+
KeyError(f"No pathtraits database found in {abs_path} and its parents.")
95+
96+
6897
if __name__ == "__main__":
6998
main()

pathtraits/traitsdb.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ def get(self, table, cols="*", condition=None, **kwargs):
4343
}
4444
condition = " AND ".join([f"{k}={v}" for (k, v) in escaped_kwargs.items()])
4545
get_row_query = f"SELECT {cols} FROM {table} WHERE {condition} LIMIT 1;"
46-
res = self.execute(get_row_query).fetchone()
46+
response = self.execute(get_row_query)
47+
values = response.fetchone()
48+
49+
if values is None:
50+
return None
51+
52+
keys = map(lambda x: x[0], response.description)
53+
res = {k: v for k, v in zip(keys, values)}
4754
return res
4855

4956
def put_path_id(self, path):
@@ -54,7 +61,7 @@ def put_path_id(self, path):
5461
else:
5562
# create
5663
self.put("path", path=path)
57-
path_id = self.get("path", path=path, cols="id")[0]
64+
path_id = self.get("path", path=path, cols="id")["id"]
5865
return path_id
5966

6067
def escape(value):

0 commit comments

Comments
 (0)