Skip to content

Commit ebdbed0

Browse files
committed
refactor HashedFile
1 parent 110ddaa commit ebdbed0

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

mona/files.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55
from abc import ABC, abstractmethod
66
from pathlib import Path
7-
from typing import Iterable, List, Optional, Type, TypeVar, Union, cast
7+
from typing import Iterable, List, Optional, Tuple, Type, TypeVar, Union, cast
88

99
from .hashing import Hash, Hashed, HashedBytes, HashedComposite, HashResolver
1010
from .rules import Rule
@@ -162,7 +162,7 @@ def from_path(
162162
@HashedComposite.register_type(File)
163163
class HashedFile(Hashed[File]):
164164
def __init__(self, file: File):
165-
self._path = file.path
165+
self._file = file
166166
if isinstance(file.content, bytes):
167167
self._content: Optional[HashedBytes] = HashedBytes(file.content)
168168
self._content_hash = self._content.hashid
@@ -172,28 +172,26 @@ def __init__(self, file: File):
172172

173173
@property
174174
def spec(self) -> bytes:
175-
return json.dumps([str(self._path), self._content_hash]).encode()
175+
return json.dumps([str(self._file.path), self._content_hash]).encode()
176176

177177
@classmethod
178178
def from_spec(cls, spec: bytes, resolve: HashResolver) -> 'HashedFile':
179-
path_str: str
180-
content_hash: Hash
181-
path_str, content_hash = json.loads(spec)
179+
path_str, content_hash = cast(Tuple[str, Hash], json.loads(spec))
182180
path = Path(path_str)
183181
fmngr = FileManager.active()
184182
if fmngr:
185-
return cls(File(path, content_hash))
186-
return cls(File(path, cast(HashedBytes, resolve(content_hash)).value))
183+
file = File(path, content_hash)
184+
else:
185+
file = File(path, cast(HashedBytes, resolve(content_hash)).value)
186+
return cls(file)
187187

188188
@property
189189
def value(self) -> File:
190-
return File(
191-
self._path, self._content.value if self._content else self._content_hash
192-
)
190+
return self._file
193191

194192
@property
195193
def label(self) -> str:
196-
return f'./{self._path}'
194+
return f'./{self._file.path}'
197195

198196
@property
199197
def components(self) -> Iterable['Hashed[object]']:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ skip-string-normalization = true
44

55
[tool.poetry]
66
name = "mona"
7-
version = "0.2.4"
7+
version = "0.2.5"
88
description = "Calculation framework"
99
readme = "README.md"
1010
authors = ["Jan Hermann <dev@janhermann.cz>"]

0 commit comments

Comments
 (0)