Skip to content

Commit

Permalink
refactor: adjustments to satisfy linters
Browse files Browse the repository at this point in the history
  • Loading branch information
clay-lake committed Jan 30, 2025
1 parent 5cd38a4 commit 99760d5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
13 changes: 6 additions & 7 deletions craft_providers/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,18 @@ def temporarily_pull_file(
yield tmp_file

@contextlib.contextmanager
def modify_file(
def edit_file(
self,
*,
source: pathlib.PurePath,
missing_ok: bool = False,
pull_file: bool = True,
) -> Generator[Optional[pathlib.Path], None, None]:
"""Copy a file from the environment for modification via context manager.
Upon exiting the context, the file is pushed back to the environment.
If the environment file does not exist, a new file will be created for writing.
) -> Generator[pathlib.Path, None, None]:
"""Edit a file from the environment for modification via context manager.
The tememporary file is stored in a temporary path which is cleaned later.
A file is pulled from an environment for editing via a context manager. Upon
exiting, the file is pushed back to the environment. If the environment file
does not exist, a new file will be created.
:param source: Environment file to copy.
:param missing_ok: Do not raise an error if the file does not exist in the
Expand All @@ -157,7 +157,6 @@ def modify_file(
directory does not exist (and `missing_ok` is False).
:raises ProviderError: On error copying file content.
"""

# Note: This is a convenience function to cache the pro services state in the
# environment. However, it may be better to use existing methods to reduce complexity.
with craft_providers.util.temp_paths.home_temporary_file() as tmp_file:
Expand Down
15 changes: 7 additions & 8 deletions craft_providers/lxd/lxd_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
import shutil
import subprocess
import tempfile
from typing import Any, Dict, Iterable, List, Optional
import warnings
from typing import Any, Dict, Iterable, List, Optional

import yaml

from craft_providers import pro
Expand Down Expand Up @@ -655,18 +656,17 @@ def enable_pro_service(self, services: Iterable[str]) -> None:
)

@property
def pro_services(self) -> set[str]:
def pro_services(self) -> Optional[set[str]]:
"""Get the Pro services enabled on the instance."""

# first check if the services are cached in memory
if hasattr(self, "_pro_services"):
return self._pro_services

Check warning on line 663 in craft_providers/lxd/lxd_instance.py

View check run for this annotation

Codecov / codecov/patch

craft_providers/lxd/lxd_instance.py#L662-L663

Added lines #L662 - L663 were not covered by tests
# then check the instance state
try:
with self.modify_file(
with self.edit_file(

Check warning on line 666 in craft_providers/lxd/lxd_instance.py

View check run for this annotation

Codecov / codecov/patch

craft_providers/lxd/lxd_instance.py#L665-L666

Added lines #L665 - L666 were not covered by tests
source=PRO_SERVICES_YAML,
) as temp_state_path:
with open(temp_state_path, "r") as fh:
with temp_state_path.open("r") as fh:
return yaml.safe_load(fh)

Check warning on line 670 in craft_providers/lxd/lxd_instance.py

View check run for this annotation

Codecov / codecov/patch

craft_providers/lxd/lxd_instance.py#L669-L670

Added lines #L669 - L670 were not covered by tests

except FileNotFoundError:
Expand All @@ -675,14 +675,13 @@ def pro_services(self) -> set[str]:
@pro_services.setter
def pro_services(self, services: set[str]) -> None:
"""Set the Pro services enabled on the instance."""

self._pro_services = services # cache the services in memory ...

Check warning on line 678 in craft_providers/lxd/lxd_instance.py

View check run for this annotation

Codecov / codecov/patch

craft_providers/lxd/lxd_instance.py#L678

Added line #L678 was not covered by tests
# ... and write them to the instance
with self.modify_file(
with self.edit_file(

Check warning on line 680 in craft_providers/lxd/lxd_instance.py

View check run for this annotation

Codecov / codecov/patch

craft_providers/lxd/lxd_instance.py#L680

Added line #L680 was not covered by tests
source=PRO_SERVICES_YAML,
pull_file=False,
) as temp_state_path:
with open(temp_state_path, "w") as fh:
with temp_state_path.open("w") as fh:
yaml.safe_dump(set(services), fh)

Check warning on line 685 in craft_providers/lxd/lxd_instance.py

View check run for this annotation

Codecov / codecov/patch

craft_providers/lxd/lxd_instance.py#L684-L685

Added lines #L684 - L685 were not covered by tests

def install_pro_client(self) -> None:
Expand Down

0 comments on commit 99760d5

Please sign in to comment.