Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove option to log to file and polish a bit #5

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions checkmk_weblate_syncer/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from pathlib import Path
from typing import TypeVar
from typing import TypeVar, assert_never

from .cli import Mode, parse_arguments
from .config import PoModeConfig, PotModeConfig
Expand All @@ -11,13 +11,15 @@

def _main() -> None:
args = parse_arguments()
configure_logger(args.log_path, args.log_level)
configure_logger(args.log_level)

match args.mode:
case Mode.POT:
sys.exit(run_pot_mode(_load_config(args.config_path, PotModeConfig)))
case Mode.PO:
sys.exit(run_po_mode(_load_config(args.config_path, PoModeConfig)))
case _:
assert_never(args.mode)


_ConfigTypeT = TypeVar("_ConfigTypeT", PotModeConfig, PoModeConfig)
Expand All @@ -28,7 +30,6 @@ def _load_config(config_path: Path, config_type: type[_ConfigTypeT]) -> _ConfigT
return config_type.model_validate_json(config_path.read_text())
except Exception as e:
LOGGER.error("Loading config failed")
LOGGER.exception(e)
raise e


Expand Down
7 changes: 0 additions & 7 deletions checkmk_weblate_syncer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def _parse_log_level(raw: int) -> int:
class Arguments(BaseModel, frozen=True):
mode: Mode
config_path: Path
log_path: Path | None
log_level: Annotated[int, AfterValidator(_parse_log_level)] = Field(alias="verbose")


Expand All @@ -42,12 +41,6 @@ def parse_arguments() -> Arguments:
metavar="CONFIG_PATH",
help="Configuration file path.",
)
parser.add_argument(
"--log_path",
type=Path,
metavar="LOG_PATH",
help="Log file path. If left unspecified, the program will log to standard error.",
)
parser.add_argument(
"--verbose",
"-v",
Expand Down
2 changes: 0 additions & 2 deletions checkmk_weblate_syncer/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ def repository_in_clean_state(
LOGGER.error(
"Error while cleaning up and updating %s repository", repo_config.path
)
LOGGER.exception(e)
raise e


Expand All @@ -46,7 +45,6 @@ def commit_and_push_files(
LOGGER.error(
"Committing and pushing files for repository %s failed", repo.working_dir
)
LOGGER.exception(e)
raise e
LOGGER.info(
"Committing and pushing files for repository %s succeeded", repo.working_dir
Expand Down
8 changes: 2 additions & 6 deletions checkmk_weblate_syncer/logging.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import logging
from pathlib import Path

LOGGER = logging.getLogger()


def configure_logger(path: Path | None, level: int) -> None:
if path:
handler: logging.Handler = logging.FileHandler(path, encoding="UTF-8")
else:
handler = logging.StreamHandler()
def configure_logger(level: int) -> None:
handler = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s %(filename)s:%(lineno)d [%(levelname)s] %(message)s"
)
Expand Down
20 changes: 8 additions & 12 deletions checkmk_weblate_syncer/po.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ def run(config: PoModeConfig) -> int:
failures.append(result)
case _:
assert_never(result)
LOGGER.info("Checking if any .po files changed in the checkmk repository")

if _check_if_repo_is_dirty(checkmk_repo):
LOGGER.info("Committing and pushing .po file to checkmk repository")
LOGGER.info("Checking if any .po files changed in the checkmk repository")
if _is_repo_dirty(checkmk_repo):
LOGGER.info("Committing and pushing .po files to checkmk repository")
commit_and_push_files(
repo=checkmk_repo,
files_to_push_to_repo=[success.path for success in successes],
Expand Down Expand Up @@ -100,33 +100,29 @@ def _process_po_file_pair(
LOGGER.info("Writing stripped .po file to checkmk repository: %s", checkmk_po_file)
try:
checkmk_po_file.write_text(po_file_content)
except Exception as e: # pylint: disable=broad-except
except IOError as e:
return _Failure(
f"Encountered error while writing po file to checkmk repository: {str(e)}",
f"Encountered error while writing po file to checkmk repository: {e}",
checkmk_po_file,
)
return _Success(checkmk_po_file)


def _check_if_repo_is_dirty(repo: Repo) -> bool:
def _is_repo_dirty(repo: Repo) -> bool:
try:
if not repo.is_dirty(untracked_files=True):
LOGGER.info("No changes, exiting")
return False
return repo.is_dirty(untracked_files=True)
except Exception as e:
LOGGER.error(
"Checking if any .po files changed in the checkmk repository failed"
)
LOGGER.exception(e)
raise e
return True


def _remove_unwanted_lines(file_path: Path) -> str | _Failure:
LOGGER.info("Reading %s", file_path)
try:
po_file_content = file_path.read_text()
except Exception as e: # pylint: disable=broad-except
except IOError as e:
return _Failure(
error_message=f"Encountered error while reading file: {str(e)}",
path=file_path,
Expand Down
4 changes: 0 additions & 4 deletions checkmk_weblate_syncer/pot.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ def run(config: PotModeConfig) -> int:
e.stdout,
e.stderr,
)
LOGGER.exception(e)
raise e
except Exception as e:
LOGGER.error("Generating pot file failed")
LOGGER.exception(e)
raise e

LOGGER.info("Writing pot file to locale repository")
Expand All @@ -37,7 +35,6 @@ def run(config: PotModeConfig) -> int:
path_pot_file.write_text(completed_pot_generation_process.stdout)
except Exception as e:
LOGGER.error("Writing pot file failed")
LOGGER.exception(e)
raise e

LOGGER.info("Checking if pot file has changed in locale repository")
Expand All @@ -47,7 +44,6 @@ def run(config: PotModeConfig) -> int:
return 0
except Exception as e:
LOGGER.error("Checking if pot file has changed failed")
LOGGER.exception(e)
raise e

LOGGER.info("Committing and pushing pot file to locale repository")
Expand Down