Skip to content

Commit

Permalink
Fixes #217 Move tested code to cruizlib
Browse files Browse the repository at this point in the history
This first introduces the separate cruizlib, which is not to contain any GUI aspects.

Only the ConanHook dataclass is currently tested, so move this over to cruizlib.

mypy and pylint check for any parts of the existing code imports that need changing.
mypy and pylint check _more_ for cruizlib since this is new.
  • Loading branch information
markfinal committed Jan 4, 2025
1 parent 52d0028 commit 9728465
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Date: 2025-01-02

## Status

Proposed
Accepted

## Context

Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ cruiz = "cruiz.__main__:main"

[tool.setuptools]
packages.find.where = ["src"]
packages.find.include = ["cruiz", "cruiz.*"]
packages.find.include = ["cruiz", "cruiz.*", "cruizlib", "cruizlib.*"]
packages.find.exclude = ["cruiz.resources.*"]
include-package-data = false

Expand Down Expand Up @@ -134,10 +134,13 @@ deps = [
commands = [
["pip", "install", "-e", "."],
["flake8", "src/cruiz"],
["flake8", "src/cruizlib"],
["flake8", "tests"],
["mypy", "src/cruiz"],
["mypy", "--warn-return-any", "src/cruizlib"],
["mypy", "tests"],
["pylint", "src/cruiz"],
["pylint", "--enable=all", "src/cruizlib"],
["pip", "install", "conan<2"],
["pylint", "--ignore-paths=^src/cruiz/pyside6/.*$,^src/cruiz/workers/api/v2/.*$", "src/cruiz"], # assuming Conan1 APIs
]
Expand Down
5 changes: 4 additions & 1 deletion src/cruiz/commands/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,15 @@

if typing.TYPE_CHECKING:
from cruiz.interop.packagenode import PackageNode
from cruiz.interop.pod import ConanRemote, ConanHook
from cruiz.interop.pod import ConanRemote
from cruiz.interop.searchrecipesparameters import SearchRecipesParameters
from cruiz.interop.reciperevisionsparameters import RecipeRevisionsParameters
from cruiz.interop.packageidparameters import PackageIdParameters
from cruiz.interop.packagerevisionsparameters import PackageRevisionsParameters
from cruiz.interop.packagebinaryparameters import PackageBinaryParameters

from cruizlib.interop.pod import ConanHook

from .conanconf import ConanConfigBoolean
from .logdetails import LogDetails

Expand Down
32 changes: 0 additions & 32 deletions src/cruiz/interop/pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from __future__ import annotations

import pathlib
import typing
from dataclasses import dataclass, field

Expand Down Expand Up @@ -66,37 +65,6 @@ def from_string(cls, string: str) -> ConanRemote:
return cls(name, url, enabled)


@dataclass(frozen=True)
class ConanHook:
"""Plain old data class representing a Conan hook."""

path: pathlib.Path
enabled: bool

def has_path(self, path: pathlib.Path) -> bool:
"""Return whether the hook contains the specified path."""
return self.path in (path, path.stem)

@classmethod
def from_string(cls, string: str) -> ConanHook:
"""Convert a string to a ConanHook."""
assert string.startswith("ConanHook(")
assert string.endswith(")")
string = string.replace("ConanHook(", "")
string = string.rstrip(")")
args = string.split(",")
assert len(args) == 2
# two args, path=ClassName('/path/to'), enabled=True|False
path_arg = args[0].strip().replace("path=", "")
path_start = path_arg.find("(") + 1
path_end = path_arg.find(")")
path_arg = path_arg[path_start:path_end][1:-1]
path = pathlib.Path(path_arg)
enabled_arg = args[1].strip().replace("enabled=", "")
enabled = _strtobool(enabled_arg)
return cls(path, enabled)


@dataclass(frozen=True)
class ExtraProfileDirectory:
"""Plain old data class representing an additional profile directory."""
Expand Down
3 changes: 2 additions & 1 deletion src/cruiz/manage_local_cache/managelocalcachesdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
from cruiz.commands.context import ConanContext
from cruiz.commands.logdetails import LogDetails
from cruiz.constants import DEFAULT_CACHE_NAME
from cruiz.interop.pod import ConanHook
from cruiz.pyside6.local_cache_manage import Ui_ManageLocalCaches
from cruiz.revealonfilesystem import reveal_on_filesystem
from cruiz.settings.managers.namedlocalcache import (
Expand All @@ -35,6 +34,8 @@
)
from cruiz.widgets.util import BlockSignals

from cruizlib.interop.pod import ConanHook

from .widgets import (
AddEnvironmentDialog,
AddExtraProfileDirectoryDialog,
Expand Down
4 changes: 3 additions & 1 deletion src/cruiz/workers/api/v1/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
import urllib.parse

from cruiz.interop.message import Failure, Message, Success
from cruiz.interop.pod import ConanHook, ConanRemote
from cruiz.interop.pod import ConanRemote

from cruizlib.interop.pod import ConanHook

from . import worker

Expand Down
4 changes: 3 additions & 1 deletion src/cruiz/workers/api/v2/meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import urllib.parse

from cruiz.interop.message import Failure, Message, Success
from cruiz.interop.pod import ConanHook, ConanRemote
from cruiz.interop.pod import ConanRemote

from cruizlib.interop.pod import ConanHook

from . import worker

Expand Down
1 change: 1 addition & 0 deletions src/cruizlib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Library for non-GUI components of cruiz."""
1 change: 1 addition & 0 deletions src/cruizlib/interop/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Utilities for data interop between Conan and the UI."""
53 changes: 53 additions & 0 deletions src/cruizlib/interop/pod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
"""Plain old data utilities for interop between Conan and UI."""

from __future__ import annotations

import pathlib
from dataclasses import dataclass


# copied from distutils.url.strtobool and modified
def _strtobool(val: str) -> bool:
"""Convert a string representation of truth to true (1) or false (0).
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
val = val.lower()
if val in ("y", "yes", "t", "true", "on", "1"):
return True
if val in ("n", "no", "f", "false", "off", "0"):
return False
raise ValueError(f"Invalid truth value {val}")


@dataclass(frozen=True)
class ConanHook:
"""Plain old data class representing a Conan hook."""

path: pathlib.Path
enabled: bool

def has_path(self, path: pathlib.Path) -> bool:
"""Return whether the hook contains the specified path."""
return self.path in (path, path.stem)

@classmethod
def from_string(cls, string: str) -> ConanHook:
"""Convert a string to a ConanHook."""
assert string.startswith("ConanHook(")
assert string.endswith(")")
string = string.replace("ConanHook(", "")
string = string.rstrip(")")
args = string.split(",")
assert len(args) == 2
# two args, path=ClassName('/path/to'), enabled=True|False
path_arg = args[0].strip().replace("path=", "")
path_start = path_arg.find("(") + 1
path_end = path_arg.find(")")
path_arg = path_arg[path_start:path_end][1:-1]
path = pathlib.Path(path_arg)
enabled_arg = args[1].strip().replace("enabled=", "")
enabled = _strtobool(enabled_arg)
return cls(path, enabled)
Empty file added src/cruizlib/py.typed
Empty file.
2 changes: 1 addition & 1 deletion tests/interop/test_pod.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pathlib
import urllib.parse

from cruiz.interop.pod import ConanHook
from cruizlib.interop.pod import ConanHook


def test_pod_hook_invariant_after_serialization() -> None:
Expand Down

0 comments on commit 9728465

Please sign in to comment.