Skip to content

Commit

Permalink
Remove dupplicated set status, highlight current folder
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrunner committed Jun 26, 2023
1 parent 3ce4fd5 commit ce876b9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
22 changes: 3 additions & 19 deletions scan_to_paperless/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -1665,12 +1665,10 @@ def _process(
with open(config_file_name, encoding="utf-8") as config_file:
config: schema.Configuration = yaml.load(config_file.read())
if config is None:
status.set_status(config_file_name, "Empty config")
print_waiting = True
return dirty, print_waiting

if not is_sources_present(config["images"], root_folder):
status.set_status(config_file_name, "Missing images")
print_waiting = True
return dirty, print_waiting

Expand All @@ -1686,7 +1684,6 @@ def _process(
save_config(config, config_file_name)
if os.path.exists(os.path.join(root_folder, "REMOVE_TO_CONTINUE")):
os.remove(os.path.join(root_folder, "REMOVE_TO_CONTINUE"))
status.set_status(config_file_name, "Rerun step")
print_waiting = True
rerun = True

Expand All @@ -1701,16 +1698,11 @@ def _process(
if is_sources_present(step["sources"], root_folder):
if not disable_remove_to_continue:
if os.path.exists(os.path.join(root_folder, "REMOVE_TO_CONTINUE")) and not rerun:
status.set_status(
config_file_name,
scan_to_paperless.status.WAITING_STATUS_NAME,
scan_to_paperless.status.WAITING_STATUS_DESCRIPTION,
)
return dirty, print_waiting
if os.path.exists(os.path.join(root_folder, "DONE")) and not rerun:
status.set_status(config_file_name, "Done")
return dirty, print_waiting

status.set_current_config(config_file_name)
status.set_status(config_file_name, "Processing")
print_waiting = True
dirty = True
Expand All @@ -1735,19 +1727,13 @@ def _process(
config["steps"].append(next_step)
save_config(config, config_file_name)
if done:
status.set_status(config_file_name, "Done")
with open(os.path.join(root_folder, "DONE"), "w", encoding="utf-8"):
pass
elif not disable_remove_to_continue:
status.set_status(
config_file_name,
scan_to_paperless.status.WAITING_STATUS_NAME,
scan_to_paperless.status.WAITING_STATUS_DESCRIPTION,
)
with open(os.path.join(root_folder, "REMOVE_TO_CONTINUE"), "w", encoding="utf-8"):
pass
else:
status.set_status(config_file_name, "Missing sources")
status.set_current_folder(None)
status.write()

except Exception as exception:
print(exception)
Expand All @@ -1771,7 +1757,6 @@ def _process(
yaml.dump(out, error_file)
stream = ruamel.yaml.compat.StringIO()
yaml.dump(out, stream)
status.set_status(config_file_name, "Error", "<pre>" + stream.getvalue() + "</pre>")
except Exception as exception2:
print(exception2)
print(traceback.format_exc())
Expand All @@ -1781,7 +1766,6 @@ def _process(
yaml.dump(out, error_file)
stream = ruamel.yaml.compat.StringIO()
yaml.dump(out, stream)
status.set_status(config_file_name, "Error", "<pre>" + stream.getvalue() + "</pre>")
return dirty, print_waiting


Expand Down
27 changes: 24 additions & 3 deletions scan_to_paperless/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import glob
import html
import os.path
from typing import Dict, NamedTuple
from typing import Dict, NamedTuple, Optional

from ruamel.yaml.main import YAML

Expand Down Expand Up @@ -37,6 +37,7 @@ def __init__(self, no_write: bool = False) -> None:
self._global_status_update = datetime.datetime.utcnow().replace(microsecond=0)
self._start_time = datetime.datetime.utcnow().replace(microsecond=0)
self._last_scan = datetime.datetime.utcnow()
self._current_folder = None
self.scan()

def set_global_status(self, status: str) -> None:
Expand All @@ -50,6 +51,18 @@ def set_global_status(self, status: str) -> None:

self.write()

def set_current_folder(self, name: Optional[str]) -> None:
"""Set the current folder."""

self._current_folder = name

def set_current_config(self, name: str) -> None:
"""Set the current config file."""

if name.endswith("/config.yaml"):
name = os.path.basename(os.path.dirname(name))
self._current_folder = name

def set_status(self, name: str, status: str, details: str = "") -> None:
"""Set the status of a folder."""

Expand All @@ -68,7 +81,7 @@ def scan(self) -> None:
"""Scan for changes for waiting documents."""

for name, folder in self._status.items():
if folder.status == WAITING_STATUS_NAME:
if name != self._current_folder:
self._update_status(name)

names = []
Expand Down Expand Up @@ -122,6 +135,13 @@ def _update_status(self, name: str, force: bool = False) -> None:
) as config_file:
config = yaml.load(config_file)

if config is None:
self.set_status(name, "Empty config")
return

if os.path.exists(os.path.join(os.environ.get("SCAN_SOURCE_FOLDER", "/source"), name, "DONE")):
self.set_status(name, "Done")

if os.path.exists(
os.path.join(os.environ.get("SCAN_SOURCE_FOLDER", "/source"), name, "REMOVE_TO_CONTINUE")
):
Expand Down Expand Up @@ -207,8 +227,9 @@ def write(self) -> None:
"""
)
for name, folder in self._status.items():
tr_attributes = f' class="alert alert-info"' if name == self._current_folder else ""
status_file.write(
f""" <tr>
f""" <tr{tr_attributes}>
<td><a href="./{name}" target="_blank">{name}</a></td>
<td>{folder.status}</td>
<td>{folder.details}</td>
Expand Down

0 comments on commit ce876b9

Please sign in to comment.