nothing works, shits broken
This commit is contained in:
88
config.py
88
config.py
@@ -1,38 +1,86 @@
|
||||
import json
|
||||
import printer
|
||||
|
||||
config_filename = "server_config.json"
|
||||
|
||||
|
||||
class PrinterConfig:
|
||||
editables = {"printer_address", "printer_port"}
|
||||
required_params = {"printer_address"}
|
||||
|
||||
def __init__(self):
|
||||
self.printer_address = None
|
||||
self.printer_port = 9100
|
||||
self.printer_profile = "RP-F10-80mm"
|
||||
self.__printer = None
|
||||
|
||||
def save(self):
|
||||
with open("config.json", "w", encoding="utf-8") as ofd:
|
||||
json.dump(self.__dict__, ofd, ensure_ascii=False)
|
||||
editables = {"address", "port"}
|
||||
required_params = {"address"}
|
||||
_address = None
|
||||
_port = 9100
|
||||
_profile = "RP-F10-80mm"
|
||||
_printer = None
|
||||
_dirty = True
|
||||
|
||||
def load(self):
|
||||
with open("config.json", "r", encoding="utf-8") as ifd:
|
||||
self.__dict__ = json.load(ifd)
|
||||
"""Load printer configuration from config.json file."""
|
||||
with open(config_filename, "r", encoding="utf-8") as ifd:
|
||||
loaded_config = json.load(ifd)
|
||||
for param_name in self.editables:
|
||||
if param_name in loaded_config:
|
||||
setattr(self, param_name, loaded_config[param_name])
|
||||
|
||||
def save(self):
|
||||
"""Save current printer configuration to config.json file."""
|
||||
with open(config_filename, "w", encoding="utf-8") as ofd:
|
||||
json.dump(
|
||||
{"address": self.address, "port": self.port, "profile": self.profile},
|
||||
ofd,
|
||||
ensure_ascii=False,
|
||||
)
|
||||
|
||||
@property
|
||||
def complete(self):
|
||||
"""Check if all required parameters are set.
|
||||
|
||||
Returns:
|
||||
bool: True if all required parameters are set, False otherwise.
|
||||
"""
|
||||
for param in self.required_params:
|
||||
if getattr(self, param) is None:
|
||||
return False
|
||||
return True
|
||||
|
||||
@property
|
||||
def port(self):
|
||||
return self._port
|
||||
|
||||
@port.setter
|
||||
def port(self, new_value: int):
|
||||
if not isinstance(new_value, int) or 0 > new_value < 65535:
|
||||
new_value = 9100
|
||||
self._port = new_value
|
||||
|
||||
@property
|
||||
def address(self):
|
||||
return self._address
|
||||
|
||||
@address.setter
|
||||
def address(self, new_value: str):
|
||||
if not isinstance(new_value, str):
|
||||
new_value = ""
|
||||
self._address = new_value
|
||||
|
||||
@property
|
||||
def profile(self):
|
||||
return self._profile
|
||||
|
||||
@profile.setter
|
||||
def profile(self, new_value: str):
|
||||
raise AttributeError("Printer profile is a fixed value. Check config.py.")
|
||||
|
||||
@property
|
||||
def printer(self):
|
||||
if self.__printer is None:
|
||||
self.__printer = printer.Printer(
|
||||
address=self.printer_address,
|
||||
port=self.printer_port,
|
||||
profile=self.printer_profile,
|
||||
if self._printer is not None:
|
||||
self._printer.printer.close()
|
||||
|
||||
if self._dirty or self.printer is None:
|
||||
self._printer = printer.Printer(
|
||||
address=self.address,
|
||||
port=self.port,
|
||||
profile=self.profile,
|
||||
)
|
||||
return self.printer
|
||||
self._dirty = False
|
||||
|
||||
return self._printer
|
||||
|
||||
Reference in New Issue
Block a user