Add YAPF style + ignore, and format tracked Python files
This commit is contained in:
@@ -22,16 +22,22 @@ get_field = sh.get_field
|
||||
should_show_help = sh.should_show_help
|
||||
from API.folder import API_folder_store
|
||||
from Store import Store
|
||||
from config import get_local_storage_path
|
||||
from SYS.config import get_local_storage_path
|
||||
|
||||
|
||||
def _extract_hash(item: Any) -> Optional[str]:
|
||||
h = get_field(item, "hash_hex") or get_field(item, "hash") or get_field(item, "file_hash")
|
||||
h = get_field(item,
|
||||
"hash_hex") or get_field(item,
|
||||
"hash") or get_field(item,
|
||||
"file_hash")
|
||||
return normalize_hash(str(h)) if h else None
|
||||
|
||||
|
||||
def _upsert_relationships(
|
||||
db: API_folder_store, file_hash: str, relationships: Dict[str, Any]
|
||||
db: API_folder_store,
|
||||
file_hash: str,
|
||||
relationships: Dict[str,
|
||||
Any]
|
||||
) -> None:
|
||||
conn = db.connection
|
||||
if conn is None:
|
||||
@@ -46,12 +52,17 @@ def _upsert_relationships(
|
||||
time_modified = CURRENT_TIMESTAMP,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
""",
|
||||
(file_hash, json.dumps(relationships) if relationships else "{}"),
|
||||
(file_hash,
|
||||
json.dumps(relationships) if relationships else "{}"),
|
||||
)
|
||||
|
||||
|
||||
def _remove_reverse_link(
|
||||
db: API_folder_store, *, src_hash: str, dst_hash: str, rel_type: str
|
||||
db: API_folder_store,
|
||||
*,
|
||||
src_hash: str,
|
||||
dst_hash: str,
|
||||
rel_type: str
|
||||
) -> None:
|
||||
meta = db.get_metadata(dst_hash) or {}
|
||||
rels = meta.get("relationships") if isinstance(meta, dict) else None
|
||||
@@ -86,7 +97,8 @@ def _refresh_relationship_view_if_current(
|
||||
target_hash: Optional[str],
|
||||
target_path: Optional[str],
|
||||
other: Optional[str],
|
||||
config: Dict[str, Any],
|
||||
config: Dict[str,
|
||||
Any],
|
||||
) -> None:
|
||||
"""If the current subject matches the target, refresh relationships via get-relationship."""
|
||||
try:
|
||||
@@ -149,7 +161,9 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
"""
|
||||
try:
|
||||
if should_show_help(args):
|
||||
log(f"Cmdlet: {CMDLET.name}\nSummary: {CMDLET.summary}\nUsage: {CMDLET.usage}")
|
||||
log(
|
||||
f"Cmdlet: {CMDLET.name}\nSummary: {CMDLET.summary}\nUsage: {CMDLET.usage}"
|
||||
)
|
||||
return 0
|
||||
|
||||
# Parse arguments
|
||||
@@ -169,22 +183,37 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
# Allow store/hash-first usage when no pipeline items were provided
|
||||
if (not results) and override_hashes:
|
||||
if not override_store:
|
||||
log("-store is required when using -query without piped items", file=sys.stderr)
|
||||
log(
|
||||
"-store is required when using -query without piped items",
|
||||
file=sys.stderr
|
||||
)
|
||||
return 1
|
||||
results = [{"hash": h, "store": str(override_store)} for h in override_hashes]
|
||||
results = [
|
||||
{
|
||||
"hash": h,
|
||||
"store": str(override_store)
|
||||
} for h in override_hashes
|
||||
]
|
||||
|
||||
if not results:
|
||||
# Legacy -path mode below may still apply
|
||||
if raw_path:
|
||||
results = [{"file_path": raw_path}]
|
||||
results = [{
|
||||
"file_path": raw_path
|
||||
}]
|
||||
else:
|
||||
log("No results to process", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
# Decide store (for same-store enforcement + folder-store DB routing)
|
||||
store_name: Optional[str] = str(override_store).strip() if override_store else None
|
||||
store_name: Optional[str] = str(override_store
|
||||
).strip() if override_store else None
|
||||
if not store_name:
|
||||
stores = {str(get_field(r, "store")) for r in results if get_field(r, "store")}
|
||||
stores = {
|
||||
str(get_field(r,
|
||||
"store"))
|
||||
for r in results if get_field(r, "store")
|
||||
}
|
||||
if len(stores) == 1:
|
||||
store_name = next(iter(stores))
|
||||
elif len(stores) > 1:
|
||||
@@ -230,9 +259,12 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
if not file_hash:
|
||||
# Try path -> hash lookup within this store
|
||||
fp = (
|
||||
get_field(single_result, "file_path")
|
||||
or get_field(single_result, "path")
|
||||
or get_field(single_result, "target")
|
||||
get_field(single_result,
|
||||
"file_path")
|
||||
or get_field(single_result,
|
||||
"path")
|
||||
or get_field(single_result,
|
||||
"target")
|
||||
)
|
||||
if fp:
|
||||
try:
|
||||
@@ -247,7 +279,9 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
return 1
|
||||
|
||||
meta = db.get_metadata(file_hash) or {}
|
||||
rels = meta.get("relationships") if isinstance(meta, dict) else None
|
||||
rels = meta.get("relationships"
|
||||
) if isinstance(meta,
|
||||
dict) else None
|
||||
if not isinstance(rels, dict) or not rels:
|
||||
continue
|
||||
|
||||
@@ -299,7 +333,12 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
|
||||
_upsert_relationships(db, file_hash, rels)
|
||||
conn.commit()
|
||||
_refresh_relationship_view_if_current(file_hash, None, None, config)
|
||||
_refresh_relationship_view_if_current(
|
||||
file_hash,
|
||||
None,
|
||||
None,
|
||||
config
|
||||
)
|
||||
deleted_count += 1
|
||||
|
||||
log(
|
||||
@@ -327,10 +366,15 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
for single_result in results:
|
||||
# Get file path from result
|
||||
file_path_from_result = (
|
||||
get_field(single_result, "file_path")
|
||||
or get_field(single_result, "path")
|
||||
or get_field(single_result, "target")
|
||||
or (str(single_result) if not isinstance(single_result, dict) else None)
|
||||
get_field(single_result,
|
||||
"file_path") or get_field(single_result,
|
||||
"path")
|
||||
or get_field(single_result,
|
||||
"target")
|
||||
or (
|
||||
str(single_result) if not isinstance(single_result,
|
||||
dict) else None
|
||||
)
|
||||
)
|
||||
|
||||
if not file_path_from_result:
|
||||
@@ -349,7 +393,10 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
file_hash = None
|
||||
file_hash = normalize_hash(str(file_hash)) if file_hash else None
|
||||
if not file_hash:
|
||||
log(f"File not in database: {file_path_obj.name}", file=sys.stderr)
|
||||
log(
|
||||
f"File not in database: {file_path_obj.name}",
|
||||
file=sys.stderr
|
||||
)
|
||||
continue
|
||||
|
||||
meta = db.get_metadata(file_hash) or {}
|
||||
@@ -404,14 +451,20 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
_upsert_relationships(db, file_hash, rels)
|
||||
conn.commit()
|
||||
_refresh_relationship_view_if_current(
|
||||
file_hash, str(file_path_obj), None, config
|
||||
file_hash,
|
||||
str(file_path_obj),
|
||||
None,
|
||||
config
|
||||
)
|
||||
deleted_count += 1
|
||||
except Exception as exc:
|
||||
log(f"Error deleting relationship: {exc}", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
log(f"Successfully deleted relationships from {deleted_count} file(s)", file=sys.stderr)
|
||||
log(
|
||||
f"Successfully deleted relationships from {deleted_count} file(s)",
|
||||
file=sys.stderr
|
||||
)
|
||||
return 0
|
||||
|
||||
except Exception as exc:
|
||||
@@ -422,16 +475,22 @@ def _run(result: Any, args: Sequence[str], config: Dict[str, Any]) -> int:
|
||||
CMDLET = Cmdlet(
|
||||
name="delete-relationship",
|
||||
summary="Remove relationships from files.",
|
||||
usage="@1 | delete-relationship --all OR delete-relationship -path <file> --all OR @1-3 | delete-relationship -type alt",
|
||||
usage=
|
||||
"@1 | delete-relationship --all OR delete-relationship -path <file> --all OR @1-3 | delete-relationship -type alt",
|
||||
arg=[
|
||||
SharedArgs.PATH,
|
||||
SharedArgs.STORE,
|
||||
SharedArgs.QUERY,
|
||||
CmdletArg("all", type="flag", description="Delete all relationships for the file(s)."),
|
||||
CmdletArg(
|
||||
"all",
|
||||
type="flag",
|
||||
description="Delete all relationships for the file(s)."
|
||||
),
|
||||
CmdletArg(
|
||||
"type",
|
||||
type="string",
|
||||
description="Delete specific relationship type ('alt', 'king', 'related'). Default: delete all types.",
|
||||
description=
|
||||
"Delete specific relationship type ('alt', 'king', 'related'). Default: delete all types.",
|
||||
),
|
||||
],
|
||||
detail=[
|
||||
|
||||
Reference in New Issue
Block a user