Skip to content
Snippets Groups Projects
Commit 0214c5bb authored by Maxence Naud's avatar Maxence Naud Committed by Maxence Naud
Browse files

fix: release with python3.8

parent 35e890ba
No related branches found
No related tags found
No related merge requests found
...@@ -4,8 +4,9 @@ import builtins ...@@ -4,8 +4,9 @@ import builtins
import aidge_core import aidge_core
import numpy as np import numpy as np
from pathlib import Path from pathlib import Path
from typing import Any, Dict, List, Optional
def _retrieve_operator_attrs(node : aidge_core.Node) -> dict[str, int, float, bool, None]: def _retrieve_operator_attrs(node : aidge_core.Node) -> Dict[str, Optional[Any]]:
""" """
Returns the dictionary containing the attributes of a given Node. Returns the dictionary containing the attributes of a given Node.
...@@ -13,7 +14,7 @@ def _retrieve_operator_attrs(node : aidge_core.Node) -> dict[str, int, float, bo ...@@ -13,7 +14,7 @@ def _retrieve_operator_attrs(node : aidge_core.Node) -> dict[str, int, float, bo
:type graph: aidge_core.Node :type graph: aidge_core.Node
:return: A dictionary with the Node's attributes. :return: A dictionary with the Node's attributes.
:rtype: dict[str, int, float, bool, None] :rtype: Dict[str, Optional[Any]]
""" """
if node.get_operator().attr is not None: if node.get_operator().attr is not None:
...@@ -27,7 +28,7 @@ def _retrieve_operator_attrs(node : aidge_core.Node) -> dict[str, int, float, bo ...@@ -27,7 +28,7 @@ def _retrieve_operator_attrs(node : aidge_core.Node) -> dict[str, int, float, bo
return node_attr_dict return node_attr_dict
def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_embed : bool, write_trainable_params_ext : bool, path_trainable_params : Path, params_file_format : str) -> dict[str, int, float, bool, None]: def _create_dict(ordered_nodes : List[aidge_core.Node], write_trainable_params_embed : bool, write_trainable_params_ext : bool, path_trainable_params : Path, params_file_format : str) -> Dict[str, Optional[Any]]:
""" """
Creates a dictionary to store the information of a given ordered GraphView. Creates a dictionary to store the information of a given ordered GraphView.
...@@ -43,7 +44,7 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e ...@@ -43,7 +44,7 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e
:type params_file_format: str :type params_file_format: str
:return: A dictionary with the GraphView description. :return: A dictionary with the GraphView description.
:rtype: dict[str, int, float, bool, None] :rtype: Dict[str, Optional[Any]]
""" """
graphview_dict = {'graph': []} graphview_dict = {'graph': []}
...@@ -79,7 +80,7 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e ...@@ -79,7 +80,7 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e
if parents[0] is None: parents.append(parents.pop(0)) if parents[0] is None: parents.append(parents.pop(0))
else: else:
pass pass
parents_inputs = [] parents_inputs = []
input_idx = 0 input_idx = 0
for parent in node.get_parents(): for parent in node.get_parents():
...@@ -88,11 +89,11 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e ...@@ -88,11 +89,11 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e
for child in children: for child in children:
if child[0] == node and child[1] == input_idx: if child[0] == node and child[1] == input_idx:
parents_inputs.append((parent.name(), input_idx)) parents_inputs.append((parent.name(), input_idx))
elif parent is None: elif parent is None:
if input_idx not in [item[1] for item in parents_inputs]: if input_idx not in [item[1] for item in parents_inputs]:
parents_inputs.append((None, input_idx)) parents_inputs.append((None, input_idx))
input_idx += 1 input_idx += 1
node_dict['parents'] = parents_inputs node_dict['parents'] = parents_inputs
...@@ -167,7 +168,7 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e ...@@ -167,7 +168,7 @@ def _create_dict(ordered_nodes : list[aidge_core.Node], write_trainable_params_e
return graphview_dict return graphview_dict
def _write_dict_json(graphview_dict : dict[str, int, float, bool, None], json_path : str) -> None: def _write_dict_json(graphview_dict : Dict[str, Optional[Any]], json_path : str) -> None:
""" """
Writes dictionary containing GraphView description to a JSON file. Writes dictionary containing GraphView description to a JSON file.
......
...@@ -43,6 +43,7 @@ For more evolved scenarii, specialize the provided FileTreeCache class. ...@@ -43,6 +43,7 @@ For more evolved scenarii, specialize the provided FileTreeCache class.
from pathlib import Path from pathlib import Path
import shutil import shutil
import sys
import filecmp import filecmp
from typing import Optional, Union, List from typing import Optional, Union, List
...@@ -54,6 +55,21 @@ __all__ = [ ...@@ -54,6 +55,21 @@ __all__ = [
"tree_update_from_cache", "tree_update_from_cache",
] ]
def is_relative_to(path: Path, other: Path) -> bool:
"""
Dynamically choose implementation based on Python version
"""
# Python 3.9+
if sys.version_info >= (3, 9):
return path.is_relative_to(other)
# Python 3.8 and earlier
try:
path.relative_to(other)
return True
except ValueError:
return False
class FileTreeCache(): class FileTreeCache():
""" """
...@@ -66,8 +82,8 @@ class FileTreeCache(): ...@@ -66,8 +82,8 @@ class FileTreeCache():
default_tmp_prefix = "__tmp_" default_tmp_prefix = "__tmp_"
def __init__(self, def __init__(self,
src_path: Union[str|Path], src_path: Union[str, Path],
cache_path: Optional[Union[str|Path]] = None cache_path: Optional[Union[str, Path]] = None
) -> None: ) -> None:
self.src_path = Path(src_path).absolute() self.src_path = Path(src_path).absolute()
self.cache_path = ( self.cache_path = (
...@@ -78,7 +94,7 @@ class FileTreeCache(): ...@@ -78,7 +94,7 @@ class FileTreeCache():
) )
ctx_msg = f"tree_cache: {src_path = }, {cache_path = }" ctx_msg = f"tree_cache: {src_path = }, {cache_path = }"
assert self.src_path != self.cache_path, f"src_path and cache_path must differ on {ctx_msg}" assert self.src_path != self.cache_path, f"src_path and cache_path must differ on {ctx_msg}"
assert not self.src_path.is_relative_to(self.cache_path), f"src_path must not be relative to cache_path on {ctx_msg}" assert not is_relative_to(self.src_path, self.cache_path), f"src_path must not be relative to cache_path on {ctx_msg}"
self._tmp_path = ( self._tmp_path = (
self.src_path.parent / self.src_path.parent /
f"{self.default_tmp_prefix}{self.src_path.name}") f"{self.default_tmp_prefix}{self.src_path.name}")
...@@ -92,7 +108,7 @@ class FileTreeCache(): ...@@ -92,7 +108,7 @@ class FileTreeCache():
assert not dst_cache_dir.exists() assert not dst_cache_dir.exists()
assert src_dir.is_dir() assert src_dir.is_dir()
assert not cache_dir.exists() or cache_dir.is_dir() assert not cache_dir.exists() or cache_dir.is_dir()
assert not cache_dir.is_relative_to(src_dir) assert not is_relative_to(cache_dir, src_dir)
def copy_or_cache(src, dst): def copy_or_cache(src, dst):
base_src = Path(src).relative_to(src_dir) base_src = Path(src).relative_to(src_dir)
...@@ -132,8 +148,8 @@ class FileTreeCache(): ...@@ -132,8 +148,8 @@ class FileTreeCache():
def tree_update_from_cache( def tree_update_from_cache(
src_path: Union[str|Path], src_path: Union[str, Path],
cache_path: Optional[Union[str|Path]] = None) -> None: cache_path: Optional[Union[str, Path]] = None) -> None:
""" """
Update from cache the current generation of a tree from the Update from cache the current generation of a tree from the
older generations, preserving file stamps when files contents are identical. older generations, preserving file stamps when files contents are identical.
......
...@@ -5,8 +5,9 @@ Provide utility function for file trees manipulations. ...@@ -5,8 +5,9 @@ Provide utility function for file trees manipulations.
""" """
import shutil import shutil
import sys
from pathlib import Path from pathlib import Path
from typing import Union, Optional from typing import Union
__all__ = [ __all__ = [
...@@ -15,8 +16,24 @@ __all__ = [ ...@@ -15,8 +16,24 @@ __all__ = [
] ]
def is_relative_to(path: Path, other: Path) -> bool:
"""
Dynamically choose implementation based on Python version
"""
# Python 3.9+
if sys.version_info >= (3, 9):
return path.is_relative_to(other)
# Python 3.8 and earlier
try:
path.relative_to(other)
return True
except ValueError:
return False
def tree_remove( def tree_remove(
path: Union[str|Path], path: Union[str, Path],
ignore_missing: bool = False, ignore_missing: bool = False,
) -> None: ) -> None:
""" """
...@@ -35,8 +52,8 @@ def tree_remove( ...@@ -35,8 +52,8 @@ def tree_remove(
def tree_move( def tree_move(
src_path: Union[str|Path], src_path: Union[str, Path],
dst_path: Union[str|Path], dst_path: Union[str, Path],
ignore_missing: bool = False, ignore_missing: bool = False,
exist_ok: bool = False, exist_ok: bool = False,
) -> None: ) -> None:
...@@ -56,8 +73,8 @@ def tree_move( ...@@ -56,8 +73,8 @@ def tree_move(
assert ignore_missing or src_path.exists(), f"src_path must exists when ignore_missing is False on {ctx_msg}" assert ignore_missing or src_path.exists(), f"src_path must exists when ignore_missing is False on {ctx_msg}"
assert exist_ok or not dst_path.exists(), f"dst_path must not exists when exist_ok is False on {ctx_msg}" assert exist_ok or not dst_path.exists(), f"dst_path must not exists when exist_ok is False on {ctx_msg}"
assert src_path != dst_path, f"paths must not be identical on {ctx_msg}" assert src_path != dst_path, f"paths must not be identical on {ctx_msg}"
assert not dst_path.is_relative_to(src_path), f"dst_path must not be relative to src_path on {ctx_msg}" assert not is_relative_to(dst_path, src_path), f"dst_path must not be relative to src_path on {ctx_msg}"
assert not src_path.is_relative_to(dst_path), f"src_path must not be relative to dst_path on {ctx_msg}" assert not is_relative_to(src_path, dst_path), f"src_path must not be relative to dst_path on {ctx_msg}"
if ignore_missing and not src_path.exists(): if ignore_missing and not src_path.exists():
return return
if exist_ok and dst_path.exists(): if exist_ok and dst_path.exists():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment