Add YAPF style + ignore, and format tracked Python files
This commit is contained in:
@@ -19,7 +19,7 @@ import pipeline as ctx
|
||||
from . import _shared as sh
|
||||
from SYS.logger import log, debug
|
||||
from Store import Store
|
||||
from config import resolve_output_dir
|
||||
from SYS.config import resolve_output_dir
|
||||
|
||||
|
||||
class Get_File(sh.Cmdlet):
|
||||
@@ -35,7 +35,10 @@ class Get_File(sh.Cmdlet):
|
||||
sh.SharedArgs.QUERY,
|
||||
sh.SharedArgs.STORE,
|
||||
sh.SharedArgs.PATH,
|
||||
sh.CmdletArg("name", description="Output filename (default: from metadata title)"),
|
||||
sh.CmdletArg(
|
||||
"name",
|
||||
description="Output filename (default: from metadata title)"
|
||||
),
|
||||
],
|
||||
detail=[
|
||||
"- Exports file from storage backend to local path",
|
||||
@@ -66,7 +69,9 @@ class Get_File(sh.Cmdlet):
|
||||
debug(f"[get-file] file_hash={file_hash} store_name={store_name}")
|
||||
|
||||
if not file_hash:
|
||||
log('Error: No file hash provided (pipe an item or use -query "hash:<sha256>")')
|
||||
log(
|
||||
'Error: No file hash provided (pipe an item or use -query "hash:<sha256>")'
|
||||
)
|
||||
return 1
|
||||
|
||||
if not store_name:
|
||||
@@ -98,12 +103,18 @@ class Get_File(sh.Cmdlet):
|
||||
|
||||
def resolve_display_title() -> str:
|
||||
candidates = [
|
||||
sh.get_field(result, "title"),
|
||||
sh.get_field(result, "name"),
|
||||
sh.get_field(result, "filename"),
|
||||
(metadata.get("title") if isinstance(metadata, dict) else None),
|
||||
(metadata.get("name") if isinstance(metadata, dict) else None),
|
||||
(metadata.get("filename") if isinstance(metadata, dict) else None),
|
||||
sh.get_field(result,
|
||||
"title"),
|
||||
sh.get_field(result,
|
||||
"name"),
|
||||
sh.get_field(result,
|
||||
"filename"),
|
||||
(metadata.get("title") if isinstance(metadata,
|
||||
dict) else None),
|
||||
(metadata.get("name") if isinstance(metadata,
|
||||
dict) else None),
|
||||
(metadata.get("filename") if isinstance(metadata,
|
||||
dict) else None),
|
||||
]
|
||||
for candidate in candidates:
|
||||
if candidate is None:
|
||||
@@ -121,9 +132,9 @@ class Get_File(sh.Cmdlet):
|
||||
debug(f"[get-file] backend.get_file returned: {source_path}")
|
||||
|
||||
# Check if backend returned a URL (HydrusNetwork case)
|
||||
if isinstance(source_path, str) and (
|
||||
source_path.startswith("http://") or source_path.startswith("https://")
|
||||
):
|
||||
if isinstance(source_path,
|
||||
str) and (source_path.startswith("http://")
|
||||
or source_path.startswith("https://")):
|
||||
# Hydrus backend returns a URL; open it only for this explicit user action.
|
||||
try:
|
||||
webbrowser.open(source_path)
|
||||
@@ -186,9 +197,9 @@ class Get_File(sh.Cmdlet):
|
||||
filename = output_name
|
||||
else:
|
||||
title = (
|
||||
(metadata.get("title") if isinstance(metadata, dict) else None)
|
||||
or resolve_display_title()
|
||||
or "export"
|
||||
(metadata.get("title") if isinstance(metadata,
|
||||
dict) else None)
|
||||
or resolve_display_title() or "export"
|
||||
)
|
||||
filename = self._sanitize_filename(title)
|
||||
|
||||
@@ -231,15 +242,15 @@ class Get_File(sh.Cmdlet):
|
||||
return
|
||||
|
||||
if suffix in {
|
||||
".png",
|
||||
".jpg",
|
||||
".jpeg",
|
||||
".gif",
|
||||
".webp",
|
||||
".bmp",
|
||||
".tif",
|
||||
".tiff",
|
||||
".svg",
|
||||
".png",
|
||||
".jpg",
|
||||
".jpeg",
|
||||
".gif",
|
||||
".webp",
|
||||
".bmp",
|
||||
".tif",
|
||||
".tiff",
|
||||
".svg",
|
||||
}:
|
||||
# Use default web browser for images.
|
||||
if self._open_image_in_default_browser(path):
|
||||
@@ -250,11 +261,17 @@ class Get_File(sh.Cmdlet):
|
||||
return
|
||||
if sys.platform == "darwin":
|
||||
subprocess.Popen(
|
||||
["open", str(path)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
||||
["open",
|
||||
str(path)],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL
|
||||
)
|
||||
return
|
||||
subprocess.Popen(
|
||||
["xdg-open", str(path)], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL
|
||||
["xdg-open",
|
||||
str(path)],
|
||||
stdout=subprocess.DEVNULL,
|
||||
stderr=subprocess.DEVNULL
|
||||
)
|
||||
except Exception as exc:
|
||||
log(f"Error opening file: {exc}", file=sys.stderr)
|
||||
@@ -274,15 +291,21 @@ class Get_File(sh.Cmdlet):
|
||||
return False
|
||||
|
||||
class OneFileHandler(http.server.SimpleHTTPRequestHandler):
|
||||
|
||||
def __init__(self, *handler_args, **handler_kwargs):
|
||||
super().__init__(*handler_args, directory=str(directory), **handler_kwargs)
|
||||
super().__init__(
|
||||
*handler_args,
|
||||
directory=str(directory),
|
||||
**handler_kwargs
|
||||
)
|
||||
|
||||
def log_message(self, format: str, *args) -> None: # noqa: A003
|
||||
# Keep normal output clean.
|
||||
return
|
||||
|
||||
def do_GET(self) -> None: # noqa: N802
|
||||
if self.path in {"/", ""}:
|
||||
if self.path in {"/",
|
||||
""}:
|
||||
self.path = "/" + filename
|
||||
return super().do_GET()
|
||||
|
||||
@@ -292,7 +315,8 @@ class Get_File(sh.Cmdlet):
|
||||
self.send_error(404)
|
||||
|
||||
def do_HEAD(self) -> None: # noqa: N802
|
||||
if self.path in {"/", ""}:
|
||||
if self.path in {"/",
|
||||
""}:
|
||||
self.path = "/" + filename
|
||||
return super().do_HEAD()
|
||||
|
||||
@@ -311,7 +335,11 @@ class Get_File(sh.Cmdlet):
|
||||
|
||||
# Run server in the background.
|
||||
server_thread = threading.Thread(
|
||||
target=httpd.serve_forever, kwargs={"poll_interval": 0.2}, daemon=True
|
||||
target=httpd.serve_forever,
|
||||
kwargs={
|
||||
"poll_interval": 0.2
|
||||
},
|
||||
daemon=True
|
||||
)
|
||||
server_thread.start()
|
||||
|
||||
@@ -350,7 +378,9 @@ class Get_File(sh.Cmdlet):
|
||||
return False
|
||||
|
||||
# Create a stable wrapper filename to reduce temp-file spam.
|
||||
wrapper_path = Path(tempfile.gettempdir()) / f"medeia-open-image-{resolved.stem}.html"
|
||||
wrapper_path = Path(
|
||||
tempfile.gettempdir()
|
||||
) / f"medeia-open-image-{resolved.stem}.html"
|
||||
try:
|
||||
wrapper_path.write_text(
|
||||
"\n".join(
|
||||
@@ -381,7 +411,10 @@ class Get_File(sh.Cmdlet):
|
||||
"""Sanitize filename by removing invalid characters."""
|
||||
allowed_chars = []
|
||||
for ch in str(name):
|
||||
if ch.isalnum() or ch in {"-", "_", " ", "."}:
|
||||
if ch.isalnum() or ch in {"-",
|
||||
"_",
|
||||
" ",
|
||||
"."}:
|
||||
allowed_chars.append(ch)
|
||||
else:
|
||||
allowed_chars.append(" ")
|
||||
|
||||
Reference in New Issue
Block a user