Skip to content

Commit 317c9b8

Browse files
committed
better repr
1 parent 45a48b5 commit 317c9b8

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

src/projspec/artifact/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,10 @@ def clean(self):
7474
pass
7575

7676
def __repr__(self):
77-
return f"{type(self).__name__}, {self.state}"
77+
return f"{type(self).__name__}, '{' '.join(self.cmd)}', {self.state}"
78+
79+
def _repr2(self):
80+
return f"{' '.join(self.cmd)}, {self.state}"
7881

7982

8083
class FileArtifact(BaseArtifact):

src/projspec/content/base.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@
22

33
from projspec.artifact import BaseArtifact
44
from projspec.proj.base import Project
5+
from projspec.utils import Enum
56

67

78
@dataclass
89
class BaseContent:
910
proj: Project = field(repr=False)
1011
artifacts: set[BaseArtifact] = field(repr=False)
12+
13+
def _repr2(self):
14+
return {
15+
k: (v.name if isinstance(v, Enum) else v)
16+
for k, v in self.__dict__.items()
17+
if not k.startswith("_") and k not in ("proj", "artifacts")
18+
}

src/projspec/content/executable.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
class Command(BaseContent):
1010
"""The simplest runnable thing - we don't know what it does"""
1111

12-
cmd: list[str]
13-
background: bool = False
14-
interactive: bool = False
12+
cmd: list[str] | str
13+
14+
def _repr2(self):
15+
return " ".join(self.cmd) if isinstance(self.cmd, list) else self.cmd

src/projspec/proj/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from .conda_package import CondaRecipe, RattlerRecipe
33
from .conda_project import CondaProject
44
from .pixi import Pixi
5+
from .pyscript import PyScriptSpec
56
from .python_code import PythonCode, PythonLibrary
67
from .uv import UVProject
78

@@ -12,6 +13,7 @@
1213
"CondaProject",
1314
"RattlerRecipe",
1415
"Pixi",
16+
"PyScriptSpec",
1517
"PythonCode",
1618
"PythonLibrary",
1719
"UVProject",

src/projspec/proj/pyscript.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66

77
class PyScriptSpec(ProjectSpec):
88
def match(self) -> bool:
9-
basenames = [_.rsplit("/", 1)[0] for _ in self.root.filelist]
9+
basenames = [_.rsplit("/", 1)[-1] for _ in self.root.filelist]
1010
return "pyscript.toml" in basenames or "pyscript.json" in basenames
1111

1212
def parse(self) -> None:
1313
try:
14-
with self.root.fs.open(f"{self.root.url}/pyscript.toml") as f:
14+
with self.root.fs.open(f"{self.root.url}/pyscript.toml", "rt") as f:
1515
meta = toml.load(f)
1616
except FileNotFoundError:
17-
with self.root.fs.open(f"{self.root.url}/pyscript.json") as f:
17+
with self.root.fs.open(f"{self.root.url}/pyscript.json", "rt") as f:
1818
meta = toml.load(f)
1919
cont = AttrDict()
2020
if "packages" in meta:

src/projspec/utils.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,7 @@ def to_dict(obj):
5757
if isinstance(obj, Iterable):
5858
return [to_dict(_) for _ in obj]
5959
if isinstance(obj, (BaseArtifact, BaseContent)):
60-
return to_dict(
61-
{
62-
k: v
63-
for k, v in obj.__dict__.items()
64-
if k not in {"proj", "artifacts"}
65-
}
66-
)
60+
return obj._repr2()
6761
return str(obj)
6862

6963

@@ -134,6 +128,8 @@ def __contains__(self, item):
134128

135129

136130
def _yaml_no_jinja(fileobj):
131+
# TODO: rather than skip jinja stuff, we can copy conda code to parse it, but
132+
# templates can involve function calls and reference to env vars we don't have
137133
txt = fileobj.read().decode()
138134
lines = []
139135
for line in txt.splitlines():

0 commit comments

Comments
 (0)