diff --git a/.djlintrc b/.djlintrc new file mode 100644 index 0000000..84e566f --- /dev/null +++ b/.djlintrc @@ -0,0 +1,6 @@ +{ + "profile": "jinja", + "ignore": "H005,H007,H013,H019,H020,H021,H023,H026,H030,H036,H043,H047,H050,J018,T002", + "max_blank_lines": 1, + "use_gitignore": true +} diff --git a/.editorconfig b/.editorconfig index ad4ec7d..e7be252 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,11 +11,6 @@ insert_final_newline = true trim_trailing_whitespace = true max_line_length = 90 -# Python stays at 88 (black/isort default) until the linters move to 90 too; -# isort reads this value. -[*.py] -max_line_length = 88 - [*.{yml,yaml,json,less,md}] indent_size = 2 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b3be6e1..effce84 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,7 +20,7 @@ env: TEST_DEPS: "" TEST_PATH: "" # "pre-commit" once the repo has a hook config, else "ruff" with RUFF_ARGS. - LINT: ruff + LINT: pre-commit RUFF_ARGS: "" # setup.py imports octoprint_setuptools, so those builds need OctoPrint installed. BUILD_ARGS: "" @@ -36,26 +36,6 @@ jobs: with: python-version: "3.13" - # Hooks with `language: system` need the node packages and ShellCheck 0.11. - - name: Set up Node.js - if: env.LINT == 'pre-commit' && hashFiles('package.json') != '' - uses: actions/setup-node@v7 - with: - node-version: "22" - - - name: Install node packages - if: env.LINT == 'pre-commit' && hashFiles('package.json') != '' - run: | - if [ -f package-lock.json ]; then npm ci --no-audit --no-fund - else npm install --no-audit --no-fund; fi - - - name: Install ShellCheck - if: env.LINT == 'pre-commit' && hashFiles('.githooks/shellcheck-hook.sh') != '' - run: | - url=https://github.com/koalaman/shellcheck/releases/download/v0.11.0/shellcheck-v0.11.0.linux.x86_64.tar.xz - curl -fsSL "$url" | tar -xJ -C "$RUNNER_TEMP" - sudo install "$RUNNER_TEMP/shellcheck-v0.11.0/shellcheck" /usr/local/bin/shellcheck - - name: Cache pre-commit hook environments if: env.LINT == 'pre-commit' uses: actions/cache@v6 @@ -65,9 +45,6 @@ jobs: - name: Run pre-commit if: env.LINT == 'pre-commit' - # The translations hook needs a local venv; the Translations job covers it. - env: - SKIP: check-translations run: | python -m pip install pre-commit pre-commit run --all-files --show-diff-on-failure @@ -88,20 +65,20 @@ jobs: matrix: include: # Every Python version each OctoPrint generation supports. - - { octoprint: "1.x", python: "3.9", runner: ubuntu-24.04 } - - { octoprint: "1.x", python: "3.10", runner: ubuntu-latest } - - { octoprint: "1.x", python: "3.11", runner: ubuntu-latest } - - { octoprint: "1.x", python: "3.12", runner: ubuntu-latest } - - { octoprint: "1.x", python: "3.13", runner: ubuntu-latest } - - { octoprint: "2.x", python: "3.9", runner: ubuntu-24.04 } - - { octoprint: "2.x", python: "3.10", runner: ubuntu-latest } - - { octoprint: "2.x", python: "3.11", runner: ubuntu-latest } - - { octoprint: "2.x", python: "3.12", runner: ubuntu-latest } - - { octoprint: "2.x", python: "3.13", runner: ubuntu-latest, coverage: true } - - { octoprint: "2.x", python: "3.14", runner: ubuntu-latest } - - { octoprint: "2.x", python: "3.15", runner: ubuntu-latest } + - {octoprint: "1.x", python: "3.9", runner: ubuntu-24.04} + - {octoprint: "1.x", python: "3.10", runner: ubuntu-latest} + - {octoprint: "1.x", python: "3.11", runner: ubuntu-latest} + - {octoprint: "1.x", python: "3.12", runner: ubuntu-latest} + - {octoprint: "1.x", python: "3.13", runner: ubuntu-latest} + - {octoprint: "2.x", python: "3.9", runner: ubuntu-24.04} + - {octoprint: "2.x", python: "3.10", runner: ubuntu-latest} + - {octoprint: "2.x", python: "3.11", runner: ubuntu-latest} + - {octoprint: "2.x", python: "3.12", runner: ubuntu-latest} + - {octoprint: "2.x", python: "3.13", runner: ubuntu-latest, coverage: true} + - {octoprint: "2.x", python: "3.14", runner: ubuntu-latest} + - {octoprint: "2.x", python: "3.15", runner: ubuntu-latest} env: - CODACY_PROJECT_TOKEN: ${{ secrets.CODACY_API_TOKEN }} + CODACY_API_TOKEN: ${{ secrets.CODACY_API_TOKEN }} steps: - uses: actions/checkout@v7 @@ -141,9 +118,15 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} fail_ci_if_error: false - # Skipped until the repo has a CODACY_API_TOKEN secret. + # Needs the Codacy account API token as CODACY_API_TOKEN; skipped without it. + # Like Codecov, a failed upload must not turn a passing test run red. - name: Upload coverage to Codacy - if: matrix.coverage && env.CODACY_PROJECT_TOKEN != '' + if: matrix.coverage && env.CODACY_API_TOKEN != '' + continue-on-error: true + env: + CODACY_ORGANIZATION_PROVIDER: gh + CODACY_USERNAME: ${{ github.repository_owner }} + CODACY_PROJECT_NAME: ${{ github.event.repository.name }} run: bash <(curl -Ls https://coverage.codacy.com/get.sh) report -r coverage.xml build: @@ -153,8 +136,8 @@ jobs: fail-fast: false matrix: include: - - { octoprint: "1.x", python: "3.9", runner: ubuntu-24.04, disabled: false } - - { octoprint: "2.x", python: "3.13", runner: ubuntu-latest, disabled: false } + - {octoprint: "1.x", python: "3.9", runner: ubuntu-24.04, disabled: false} + - {octoprint: "2.x", python: "3.13", runner: ubuntu-latest, disabled: false} steps: - uses: actions/checkout@v7 diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 0000000..b4a0b9c --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,5 @@ +# Import order shared by all plugin repos; compatible with ruff format. +[settings] +profile = black +line_length = 90 +extend_skip_glob = **/vendor/*,3rdPartySoftware/*,archive/* diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..4a4cbd3 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,48 @@ +# Same hooks in every plugin repo, following OctoPrint core's setup. +# Set up once with `pre-commit install`; CI runs them on every push. +exclude: (^|/)(translations|vendor)/|^(docs|scripts|archive|3rdPartySoftware)/|\.(css|svg|map|gcode)$|\.min\.js$ +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v6.0.0 + hooks: + - id: end-of-file-fixer + - id: trailing-whitespace + - id: check-case-conflict + - id: check-json + - id: check-yaml + # mkdocs.yml uses custom tags; only the syntax is checked. + args: [--unsafe] + - id: check-toml + - id: check-merge-conflict + - repo: https://github.com/PyCQA/isort + rev: 9.0.2 + hooks: + - id: isort + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: v0.17.6 + hooks: + - id: ruff-check + args: [--fix] + - id: ruff-format + - repo: https://github.com/djlint/djlint + rev: v1.54.2 + hooks: + - id: djlint-reformat-jinja + - id: djlint-jinja + - repo: https://github.com/rbubley/mirrors-prettier + rev: v3.10.1 + hooks: + - id: prettier + files: \.(js|json|less|ya?ml)$ + - repo: https://github.com/codespell-project/codespell + rev: v2.4.3 + hooks: + - id: codespell + # Identifiers, config keys and product names, not typos. + args: + - --ignore-words-list + - commitish,offsetp,colorfull,basf,paht,sme,unparseable,pre-selected + - repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.11.0.1-1 + hooks: + - id: shellcheck diff --git a/.prettierignore b/.prettierignore new file mode 100644 index 0000000..5b11dbf --- /dev/null +++ b/.prettierignore @@ -0,0 +1,12 @@ +# Same scope as the prettier hook in .pre-commit-config.yaml. +*.md +*.css +*.min.js +**/package.json +**/package-lock.json +**/translations/ +**/vendor/ +docs/ +scripts/ +archive/ +3rdPartySoftware/ diff --git a/.prettierrc.yaml b/.prettierrc.yaml new file mode 100644 index 0000000..b677871 --- /dev/null +++ b/.prettierrc.yaml @@ -0,0 +1,12 @@ +# Prettier settings shared by all plugin repos, taken from OctoPrint core. +printWidth: 90 +tabWidth: 4 +semi: true +trailingComma: "none" +singleQuote: false +quoteProps: consistent +bracketSpacing: false +overrides: + - files: ["*.yaml", "*.yml", "*.json", "*.less"] + options: + tabWidth: 2 diff --git a/README.md b/README.md index 5bcdd37..2412277 100644 --- a/README.md +++ b/README.md @@ -132,9 +132,9 @@ git clone https://github.com/ajimaru/OctoPrint-AutoConnectPlus.git cd OctoPrint-AutoConnectPlus python -m venv .venv && source .venv/bin/activate pip install "OctoPrint>=1.11" -e ".[dev]" +pre-commit install # lint and format hooks, the same ones CI runs -pytest # run the unit tests -ruff check . # lint +pytest # run the unit tests ``` The tests mock OctoPrint's printer and the connector framework, so they run against diff --git a/octoprint_autoconnectplus/__init__.py b/octoprint_autoconnectplus/__init__.py index 4a04a12..c7bb7d2 100644 --- a/octoprint_autoconnectplus/__init__.py +++ b/octoprint_autoconnectplus/__init__.py @@ -299,9 +299,7 @@ def do_auto_connect(self): if attempted: self._register_failure() except Exception: # pylint: disable=broad-exception-caught - self._logger.error( - f"Exception in do_auto_connect {get_exception_string()}" - ) + self._logger.error(f"Exception in do_auto_connect {get_exception_string()}") self._register_failure() def _connect_serial(self, profile: str) -> bool: @@ -315,9 +313,7 @@ def _connect_serial(self, profile: str) -> bool: self._logger.debug(f"Port {port} not present yet, waiting") return False - self._logger.info( - f"Attempting to connect to {port} with profile {profile}" - ) + self._logger.info(f"Attempting to connect to {port} with profile {profile}") self._printer.connect(port=port, profile=profile) return True @@ -379,8 +375,7 @@ def _connect_connector(self, connector: str, profile: str) -> bool: return False except Exception: # pylint: disable=broad-exception-caught self._logger.debug( - f"Precondition check for '{connector}' raised, " - f"attempting connect anyway" + f"Precondition check for '{connector}' raised, attempting connect anyway" ) # Connector preconditions only resolve the hostname, so a literal IP @@ -388,17 +383,13 @@ def _connect_connector(self, connector: str, profile: str) -> bool: # with "No route to host". Probe the TCP port first and just wait if # unreachable, keeping the retry quiet. if not self._host_reachable(connector, parameters): - self._logger.debug( - f"Host for '{connector}' not reachable yet, waiting" - ) + self._logger.debug(f"Host for '{connector}' not reachable yet, waiting") return False self._logger.info( f"Attempting to connect via '{connector}' with profile {profile}" ) - self._printer.connect( - connector=connector, parameters=parameters, profile=profile - ) + self._printer.connect(connector=connector, parameters=parameters, profile=profile) return True def _host_reachable(self, connector: str, parameters: dict) -> bool: @@ -420,9 +411,7 @@ def _host_reachable(self, connector: str, parameters: dict) -> bool: return True try: - with socket.create_connection( - (host, port), timeout=REACHABILITY_TIMEOUT - ): + with socket.create_connection((host, port), timeout=REACHABILITY_TIMEOUT): return True except OSError: return False @@ -442,9 +431,7 @@ def _portretry_plugins_enabled(self) -> list[str]: return [ name for identifier, name in plugin_names.items() - if plugin_manager.get_plugin_info( - identifier, require_enabled=True - ) + if plugin_manager.get_plugin_info(identifier, require_enabled=True) is not None ] except Exception: # pylint: disable=broad-exception-caught @@ -461,9 +448,13 @@ def _detected_connection(self) -> dict[str, Any]: if self._is_serial_connector(connector): target = self.serial_port or "" - warning = "" if target else ( - "No serial port detected yet; set one in OctoPrint's " - "connection dialog or configure a forced port below." + warning = ( + "" + if target + else ( + "No serial port detected yet; set one in OctoPrint's " + "connection dialog or configure a forced port below." + ) ) return { "label": label, @@ -483,9 +474,7 @@ def _detected_connection(self) -> dict[str, Any]: "No preferred connection stored. Connect once via OctoPrint's " "connection dialog so AutoConnectPlus knows what to reconnect." ) - elif ConnectedPrinter is not None and ( - ConnectedPrinter.find(connector) is None - ): + elif ConnectedPrinter is not None and (ConnectedPrinter.find(connector) is None): warning = ( f"Connector '{connector}' is not installed. Install the " "matching connector plugin." @@ -589,12 +578,9 @@ def on_settings_save(self, data) -> dict[Any, Any]: # Match the entry-point key so the runtime identifier is explicit (otherwise # defaults to the package name). __plugin_identifier__ = "autoconnectplus" -__plugin_author__ = ( - "ajimaru, based on work from vehystrix and hprombex" -) +__plugin_author__ = "ajimaru, based on work from vehystrix and hprombex" __plugin_description__ = ( - "Automatically reconnects the printer over serial, Moonraker or Bambu " - "connectors" + "Automatically reconnects the printer over serial, Moonraker or Bambu connectors" ) __plugin_url__ = "https://github.com/ajimaru/OctoPrint-AutoConnectPlus" __plugin_license__ = "AGPL-3.0-or-later" @@ -619,6 +605,5 @@ def __plugin_load__(): module_globals = globals() module_globals["__plugin_implementation__"] = plugin module_globals["__plugin_hooks__"] = { - "octoprint.plugin.softwareupdate.check_config": - plugin.get_update_information, + "octoprint.plugin.softwareupdate.check_config": plugin.get_update_information, } diff --git a/octoprint_autoconnectplus/static/js/autoconnectplus.js b/octoprint_autoconnectplus/static/js/autoconnectplus.js index 65ee5d7..91e3c31 100644 --- a/octoprint_autoconnectplus/static/js/autoconnectplus.js +++ b/octoprint_autoconnectplus/static/js/autoconnectplus.js @@ -18,11 +18,13 @@ $(function () { new PNotify({ title: "AutoConnectPlus error", - text: plugins.join(" and ") + " is also enabled. Disable " + + text: + plugins.join(" and ") + + " is also enabled. Disable " + "one of the reconnect plugins to prevent competing " + "reconnect attempts.", type: "error", - hide: false, + hide: false }); portRetryWarningShown = true; } @@ -69,6 +71,6 @@ $(function () { OCTOPRINT_VIEWMODELS.push({ construct: AutoConnectPlusViewModel, dependencies: ["settingsViewModel"], - elements: ["#settings_plugin_autoconnectplus"], + elements: ["#settings_plugin_autoconnectplus"] }); }); diff --git a/octoprint_autoconnectplus/templates/autoconnectplus_settings.jinja2 b/octoprint_autoconnectplus/templates/autoconnectplus_settings.jinja2 index f0244a8..2e54b93 100644 --- a/octoprint_autoconnectplus/templates/autoconnectplus_settings.jinja2 +++ b/octoprint_autoconnectplus/templates/autoconnectplus_settings.jinja2 @@ -1,7 +1,7 @@