Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
nlef committed Jun 12, 2022
2 parents f410528 + f14040b commit f98eabe
Show file tree
Hide file tree
Showing 17 changed files with 340 additions and 127 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@ jobs:
- name: Run pre-commit on all files
run: |
pre-commit run --all-files --show-diff-on-failure --color=always
- name: Test with pytest
run: |
pytest -v
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,10 @@ repos:
entry: pylint
language: system
types: [python]
# - id: pytest
# name: pytest
# language: system
# types: [python]
# entry: pytest -v
# pass_filenames: false
# always_run: true
36 changes: 24 additions & 12 deletions bot/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,24 @@ def _set_cv2_params(self):
self.cam_cam.set(cv2.CAP_PROP_BUFFERSIZE, 1)

for prop_name, value in self._cv2_params:
if value.isnumeric():
val = int(value)
elif self._isfloat(value):
val = float(value)
if prop_name.upper() == "CAP_PROP_FOURCC":
try:
prop = getattr(cv2, prop_name.upper())
self.cam_cam.set(prop, cv2.VideoWriter_fourcc(*value))
except AttributeError as err:
logger.error(err, err)
else:
val = value
try:
prop = getattr(cv2, prop_name.upper())
self.cam_cam.set(prop, val)
except AttributeError as err:
logger.error(err, err)
if value.isnumeric():
val = int(value)
elif self._isfloat(value):
val = float(value)
else:
val = value
try:
prop = getattr(cv2, prop_name.upper())
self.cam_cam.set(prop, val)
except AttributeError as err:
logger.error(err, err)

@cam_light_toggle
def take_photo(self) -> BytesIO:
Expand Down Expand Up @@ -514,9 +521,9 @@ def _create_timelapse(self, printing_filename: str, gcode_name: str, info_mess:

return video_bio, thumb_bio, width, height, video_filepath, gcode_name

def cleanup(self, lapse_filename: str) -> None:
def cleanup(self, lapse_filename: str, force: bool = False) -> None:
lapse_dir = f"{self._base_dir}/{lapse_filename}"
if self._cleanup:
if self._cleanup or force:
for filename in glob.glob(f"{glob.escape(lapse_dir)}/*.{self._img_extension}"):
os.remove(filename)
for filename in glob.glob(f"{glob.escape(lapse_dir)}/*"):
Expand All @@ -528,6 +535,7 @@ def clean(self) -> None:
for filename in glob.glob(f"{glob.escape(self.lapse_dir)}/*"):
os.remove(filename)

# Todo: check if lapse was in subfolder ( alike gcode folders)
# Todo: refactor into timelapse class
# Todo: check for 64 symbols length in lapse names
def detect_unfinished_lapses(self) -> List[str]:
Expand All @@ -538,3 +546,7 @@ def detect_unfinished_lapses(self) -> List[str]:
glob.glob(f"{self._base_dir}/*/*.lock"),
)
)

def cleanup_unfinished_lapses(self):
for lapse_name in self.detect_unfinished_lapses():
self.cleanup(lapse_name, force=True)
29 changes: 24 additions & 5 deletions bot/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def _check_numerical_value(
min_value: Optional[Union[int, float]] = None,
max_value: Optional[Union[int, float]] = None,
) -> None:
if not self._config.has_option(self._SECTION, option):
return
if above is not None and value <= above:
self._parsing_errors.append(f"Option '{option}: {value}': value is not above {above}")
if below is not None and value >= below:
Expand All @@ -59,10 +61,14 @@ def _check_numerical_value(
self._parsing_errors.append(f"Option '{option}: {value}': value is above maximum value {max_value}")

def _check_string_values(self, option: str, value: str, allowed_values: Optional[List[str]] = None):
if not self._config.has_option(self._SECTION, option):
return
if allowed_values is not None and value not in allowed_values:
self._parsing_errors.append(f"Option '{option}: {value}': value '{value}' is not allowed")

def _check_list_values(self, option: str, values: List[Any], allowed_values: Optional[List[Any]] = None):
if not self._config.has_option(self._SECTION, option):
return
unallowed_params = []
if allowed_values is not None:
for val in values:
Expand Down Expand Up @@ -147,12 +153,12 @@ class BotConfig(ConfigHelper):
"chat_id",
"debug",
"log_parser",
"log_path",
"power_device",
"light_device",
"user",
"password",
"api_token",
"upload_path",
]

def __init__(self, config: configparser.ConfigParser):
Expand All @@ -170,9 +176,19 @@ def __init__(self, config: configparser.ConfigParser):
self.poweroff_device_name: str = self._getstring("power_device", default="")
self.debug: bool = self._getboolean("debug", default=False)
self.log_file: str = self._getstring("log_path", default="/tmp")
self.upload_path: str = self._getstring("upload_path", default="")

self.log_parser: bool = self._getboolean("log_parser", default=False)

@property
def formated_upload_path(self):
if not self.upload_path:
return ""
if not self.upload_path.endswith("/"):
return self.upload_path + "/"
else:
return self.upload_path

def log_path_update(self, logfile: str) -> None:
if logfile:
self.log_file = logfile
Expand Down Expand Up @@ -280,8 +296,9 @@ class TelegramUIConfig(ConfigHelper):
"buttons",
"require_confirmation_macro",
"include_macros_in_command_list",
"disabled_macros",
"show_hidden_macros",
"hidden_macros",
"hidden_bot_commands",
"show_private_macros",
"eta_source",
"status_message_sensors",
"status_message_heaters",
Expand Down Expand Up @@ -321,12 +338,14 @@ def __init__(self, config: configparser.ConfigParser):
)
)
self.require_confirmation_macro: bool = self._getboolean("require_confirmation_macro", default=True)
self.progress_update_message: bool = self._getboolean("progress_update_message", default=True)
self.silent_progress: bool = self._getboolean("silent_progress", default=False)
self.silent_commands: bool = self._getboolean("silent_commands", default=False)
self.silent_status: bool = self._getboolean("silent_status", default=False)
self.include_macros_in_command_list: bool = self._getboolean("include_macros_in_command_list", default=True)
self.disabled_macros: List[str] = self._getlist("disabled_macros", default=[])
self.show_hidden_macros: bool = self._getboolean("show_hidden_macros", default=False)
self.hidden_macros: List[str] = list(map(lambda el: el.upper(), self._getlist("hidden_macros", default=[])))
self.hidden_bot_commands: List[str] = self._getlist("hidden_bot_commands", default=[])
self.show_private_macros: bool = self._getboolean("show_private_macros", default=False)
self.pin_status_single_message: bool = self._getboolean("pin_status_single_message", default=False) # Todo: implement
self.status_message_content: List[str] = self._getlist("status_message_content", default=self._MESSAGE_CONTENT, allowed_values=self._MESSAGE_CONTENT)
self.status_message_m117_update: bool = self._getboolean("status_message_m117_update", default=False)
Expand Down
Loading

0 comments on commit f98eabe

Please sign in to comment.