media-sucker/src/worker.py

33 lines
919 B
Python
Raw Normal View History

2022-08-25 10:17:25 -06:00
import threading
import os
import json
2022-08-25 10:17:25 -06:00
import logging
2022-08-25 10:17:25 -06:00
class Worker(threading.Thread):
def __init__(self, directory, **kwargs):
self.directory = directory
self.status = {
"state": "idle",
}
kwargs["daemon"] = True
return super().__init__(**kwargs)
def workdir(self, *path):
return os.path.join(self.directory, *path)
def write_state(self, subdir, state):
2022-08-25 10:17:25 -06:00
logging.debug("Writing state: %s" % repr(state))
statefn = self.workdir(subdir, "sucker.json")
newstatefn = statefn + ".new"
with open(newstatefn, "w") as f:
json.dump(state, f)
os.rename(newstatefn, statefn)
2022-08-25 10:17:25 -06:00
def read_state(self, subdir):
2022-08-25 10:17:25 -06:00
with open(self.workdir(subdir, "sucker.json")) as f:
2022-08-25 10:17:25 -06:00
return json.load(f)
2022-08-25 10:17:25 -06:00
def clear_state(self, subdir):
os.unlink(self.workdir(subdir, "sucker.json"))