Skip to content

Commit

Permalink
Add rename command (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmwant authored Nov 29, 2024
1 parent 179c962 commit 6ea0533
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ charset = utf-8
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = true

# 4 space indentation
[*.py]
[*.{py,toml}]
indent_size = 4

[Makefile]
Expand Down
1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

5 changes: 2 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.8
rev: v0.8.1
hooks:
- id: ruff
args: ["--fix"]
Expand All @@ -15,5 +15,4 @@ repos:
hooks:
- id: poetry-check
- id: poetry-lock
args: ["--check"]
files: "^(pyproject.toml|poetry.lock)$"
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ Available sections are:
- `Fixed` for any bug fixes.
- `Security` in case of vulnerabilities.

## [v0.6.0] - 2024-11-29

### Added

- New command `hap rename` to kill to update name of existing hap


## [v0.5.0] - 2024-10-01

Expand Down Expand Up @@ -140,6 +146,9 @@ Available sections are:

- Initial release.


[v0.6.0]: https://github.com/bmwant/hapless/compare/v0.5.1...v0.6.0

[v0.5.0]: https://github.com/bmwant/hapless/compare/v0.4.0...v0.5.0

[v0.4.0]: https://github.com/bmwant/hapless/compare/v0.3.0...v0.4.0
Expand Down
11 changes: 11 additions & 0 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ $ poetry run pytest --cov=hapless --cov-report=html tests/
$ make coverage-report
```

Run against multiple Python versions with [nox](https://nox.thea.codes/en/stable/index.html)

```bash
nox --list
# Check all of the defined Python versions are available
nox -s check_versions

# Run tests against all versions
nox -s test
```

### Releasing

Bump a version with features you want to include and build a package
Expand Down
13 changes: 13 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,23 @@ hap clean --all
# Same as above
hap cleanall
```

➡️ Restart a hap.

- When restart command is called, hap will stop the process and start it again. The command is rerun from the current working directory.

```bash
hap restart [hap-alias]
```

➡️ Rename existing hap.

- When restart command is called, hap will stop the process and start it again. The command is rerun from the current working directory.

```bash
hap rename [hap-alias] [new-hap-name]
# e.g. you can invoke with hap ID like
hap rename 4 hap-new-name
# or by hap current name like
hap rename hap-name hap-new-name
```
15 changes: 15 additions & 0 deletions hapless/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,20 @@ def restart(hap_alias):
hapless.restart(hap)


@cli.command(short_help="Sets new name/alias for the existing hap.")
@click.argument("hap_alias", metavar="hap", required=True)
@click.argument("new_name", metavar="new-name", required=True)
def rename(hap_alias: str, new_name: str):
hap = get_or_exit(hap_alias)
same_name_hap = hapless.get_hap(new_name)
if same_name_hap is not None:
console.print(
f"{config.ICON_INFO} Hap with such name already exists: {same_name_hap}",
style=f"{config.COLOR_ERROR} bold",
)
sys.exit(1)
hapless.rename_hap(hap, new_name)


if __name__ == "__main__":
cli()
7 changes: 5 additions & 2 deletions hapless/hap.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ def __init__(
self._set_raw_name(name)
self._set_cmd(cmd)

def set_name(self, name: str):
with open(self._name_file, "w") as f:
f.write(name)

def _set_raw_name(self, raw_name: Optional[str]):
"""
Sets name for the first time on hap creation.
Expand All @@ -63,8 +67,7 @@ def _set_raw_name(self, raw_name: Optional[str]):
raw_name = f"hap-{suffix}"

if self.raw_name is None:
with open(self._name_file, "w") as f:
f.write(raw_name)
self.set_name(raw_name)

def _set_cmd(self, cmd: Optional[str]):
"""
Expand Down
11 changes: 11 additions & 0 deletions hapless/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ def run_hap(self, hap: Hap):

retcode = proc.wait()

# TODO: accessing private attribute, should be `set_rc` method
with open(hap._rc_file, "w") as rc_file:
rc_file.write(f"{retcode}")

Expand Down Expand Up @@ -251,3 +252,13 @@ def restart(self, hap: Hap):

name = f"{name}{config.RESTART_DELIM}{restarts + 1}"
self.run(cmd=cmd, hid=hid, name=name)

def rename_hap(self, hap: Hap, new_name: str):
rich_text = (
f"{config.ICON_INFO} Renamed [{config.COLOR_ACCENT}]{hap.name}[/] "
f"to [{config.COLOR_MAIN} bold]{new_name}[/]"
)
if hap.restarts:
new_name = f"{new_name}{config.RESTART_DELIM}{hap.restarts}"
hap.set_name(new_name)
self.ui.print(rich_text)
15 changes: 15 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import nox

TARGET_VERSIONS = ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]


@nox.session(python=TARGET_VERSIONS)
def test(session: nox.Session) -> None:
# session.install(".")
session.install("-e", ".[dev]")
session.run("pytest", "tests")


@nox.session(python=TARGET_VERSIONS)
def check_python(session: nox.Session) -> None:
session.run("python", "--version")
Loading

0 comments on commit 6ea0533

Please sign in to comment.