Skip to content

Commit

Permalink
rm import from archetypes and core and target specific files to avoid…
Browse files Browse the repository at this point in the history
… cyclic dependencies
  • Loading branch information
ssssarah committed Oct 30, 2023
1 parent 9ffefac commit 604eed6
Show file tree
Hide file tree
Showing 28 changed files with 73 additions and 52 deletions.
4 changes: 2 additions & 2 deletions kgforge/core/archetypes/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
from pathlib import Path
from typing import Any, Iterable, Iterator, List, Optional, Sequence, Union

from kgforge.core import Resource
from kgforge.core.archetypes import Mapping
from kgforge.core.resource import Resource
from kgforge.core.archetypes.mapping import Mapping
from kgforge.core.commons.attributes import repr_class


Expand Down
4 changes: 2 additions & 2 deletions kgforge/core/archetypes/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import hjson
from pandas import DataFrame

from kgforge.core import Resource
from kgforge.core.archetypes import Mapping
from kgforge.core.resource import Resource
from kgforge.core.archetypes.mapping import Mapping
from kgforge.core.commons.attributes import repr_class, sort_attrs
from kgforge.core.commons.context import Context
from kgforge.core.commons.exceptions import ConfigurationError, ValidationError
Expand Down
2 changes: 1 addition & 1 deletion kgforge/core/archetypes/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Union, Tuple

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.attributes import repr_class
from kgforge.core.commons.exceptions import ConfigurationError, ResolvingError
from kgforge.core.commons.execution import not_supported
Expand Down
15 changes: 8 additions & 7 deletions kgforge/core/archetypes/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
from pathlib import Path
from typing import Any, Dict, List, Match, Optional, Tuple, Union, Type

from kgforge.core import Resource
from kgforge.core.archetypes import Mapping, Mapper
from kgforge.core.resource import Resource
from kgforge.core.archetypes.mapping import Mapping
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.archetypes.mapper import Mapper
from kgforge.core.commons.attributes import repr_class
from kgforge.core.commons.context import Context
from kgforge.core.commons.exceptions import (
Expand All @@ -35,14 +37,13 @@
)
from kgforge.core.commons.execution import not_supported, run
from kgforge.core.reshaping import collect_values

# NB: Do not 'from kgforge.core.archetypes import Resolver' to avoid cyclic dependency.

# FIXME: need to find a comprehensive way (different than list) to get all SPARQL reserved clauses
from kgforge.core.wrappings.dict import DictWrapper

DEFAULT_LIMIT = 100
DEFAULT_OFFSET = 0

# FIXME: need to find a comprehensive way (different than list) to get all SPARQL reserved clauses

SPARQL_CLAUSES = [
"where",
"filter",
Expand Down Expand Up @@ -402,7 +403,7 @@ def _deprecate_one(self, resource: Resource) -> None:
# Querying.

def search(
self, resolvers: Optional[List["Resolver"]], *filters, **params
self, resolvers: Optional[List[Resolver]], *filters, **params
) -> List[Resource]:

# Positional arguments in 'filters' are instances of type Filter from wrappings/paths.py
Expand Down
2 changes: 1 addition & 1 deletion kgforge/core/commons/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from collections import Counter
from typing import Any, Callable, Iterable, Iterator, List, Optional, Union

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.attributes import eq_class, repr_class


Expand Down
2 changes: 1 addition & 1 deletion kgforge/core/commons/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from functools import wraps
from typing import Any, Callable, List, Optional, Tuple, Union

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.actions import (Action, Actions, collect_lazy_actions,
execute_lazy_actions)
from kgforge.core.commons.exceptions import NotSupportedError
Expand Down
2 changes: 1 addition & 1 deletion kgforge/core/conversions/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import numpy as np
from pandas import DataFrame, Series

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.context import Context
from kgforge.core.conversions.json import as_json, from_json

Expand Down
2 changes: 1 addition & 1 deletion kgforge/core/conversions/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import hjson

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.attributes import sort_attrs
from kgforge.core.commons.context import Context
from kgforge.core.conversions.rdf import as_jsonld
Expand Down
7 changes: 5 additions & 2 deletions kgforge/core/forge.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@
from pandas import DataFrame
from rdflib import Graph

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.files import load_file_as_byte
from kgforge.core.archetypes import Mapping, Model, Resolver, Store
from kgforge.core.archetypes.mapping import Mapping
from kgforge.core.archetypes.model import Model
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.archetypes.store import Store
from kgforge.core.commons.actions import LazyAction
from kgforge.core.commons.dictionaries import with_defaults
from kgforge.core.commons.exceptions import ResolvingError
Expand Down
2 changes: 1 addition & 1 deletion kgforge/core/reshaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from typing import Callable, Dict, Iterator, List, Union

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.attributes import repr_class
from kgforge.core.commons.execution import dispatch
from kgforge.core.conversions.json import as_json
Expand Down
10 changes: 8 additions & 2 deletions kgforge/core/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ def _sync_resource(item, vlist, sync_value=True):
else:
vlist.append(item)

def set_validated(self, validation_status: bool):
self._validated = validation_status

def set_last_action(self, action: "Action"):
self._last_action = action

def _get_synchronized(self) -> bool:
inner = []
for v in self.__dict__.values():
Expand Down Expand Up @@ -160,12 +166,12 @@ def _(d: Union[Dict, List[Dict]], nas: List[Any]) -> Resource:
return [_(d, nas) for d in data] if isinstance(data, List) else _(data, nas)


def encode(data: Any) -> Union[str, Dict]:
def encode(data: Any) -> Union[str, Dict, set]:
if isinstance(data, Resource):
return {k: v for k, v in data.__dict__.items() if k not in data._RESERVED}
if type(data).__name__ == "LazyAction":
return str(data)
if isinstance(data, set) or isinstance(data, list):
if isinstance(data, (set, list)):
return {encode(r) for r in data}

return data.__dict__
6 changes: 3 additions & 3 deletions kgforge/core/wrappings/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, path: List[str], operator: Union[str, FilterOperator], value:
try:
self.operator: str = operator.value if isinstance(operator, FilterOperator) \
else FilterOperator(operator).value
except Exception as e:
except Exception:
raise ValueError(
f"Invalid operator value '{operator}'. Allowed operators are {[member.value for name, member in FilterOperator.__members__.items()]}"
)
Expand Down Expand Up @@ -90,7 +90,7 @@ def __init__(self, path: List[str]) -> None:

class PathsWrapper(FilterMixin):
def __init__(self, path: List[str], paths: Dict) -> None:
check_collisions(self._RESERVED, paths.keys())
check_collisions(FilterMixin._RESERVED, paths.keys())
super().__init__(path)
self.__dict__ = paths

Expand All @@ -107,7 +107,7 @@ def _wrap(data: Any, path: List[str]) -> Union[PathsWrapper, PathWrapper]:


def create_filters_from_dict(filter_dict: Dict, path_prefix=None) -> List[Filter]:
filters = list()
filters = []
if path_prefix is None:
path_prefix = []
for k, v in filter_dict.items():
Expand Down
10 changes: 6 additions & 4 deletions kgforge/specializations/mappers/dictionaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, Union

from kgforge.core import Resource
from kgforge.core.archetypes import Mapper, Mapping
from kgforge.core.resource import Resource
from kgforge.core.archetypes.mapper import Mapper
from kgforge.core.archetypes.mapping import Mapping
from kgforge.core.conversions.json import from_json
from kgforge.core.wrappings.dict import DictWrapper, wrap_dict

Expand All @@ -30,8 +31,9 @@ class DictionaryMapper(Mapper):
def __init__(self, forge: Optional["KnowledgeGraphForge"] = None) -> None:
super().__init__(forge)

def _map_one(self, data: Union[Path, Dict], mappings: List[Mapping], nas: List[Any]
) -> List[Resource]:
def _map_one(
self, data: Union[Path, Dict], mappings: List[Mapping], nas: List[Any]
) -> List[Resource]:
variables = {
"forge": self.forge,
"x": self._load_one(data),
Expand Down
5 changes: 3 additions & 2 deletions kgforge/specializations/models/demo_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
from pathlib import Path
from typing import Callable, Dict, List, Optional, Tuple, Union

from kgforge.core import Resource
from kgforge.core.archetypes import Mapping, Model
from kgforge.core.resource import Resource
from kgforge.core.archetypes.mapping import Mapping
from kgforge.core.archetypes.model import Model
from kgforge.core.commons.context import Context
from kgforge.core.commons.exceptions import ValidationError

Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/models/rdf/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pyshacl.shapes_graph import ShapesGraph
from rdflib import Graph, URIRef, RDF, XSD

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.context import Context
from kgforge.core.commons.exceptions import ConfigurationError
from kgforge.core.conversions.rdf import as_graph
Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/models/rdf/store_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

from kgforge.core.commons.exceptions import RetrievalError
from kgforge.core.conversions.rdf import as_jsonld, as_graph
from kgforge.core.archetypes import Store
from kgforge.core.archetypes.store import Store
from kgforge.specializations.models.rdf.node_properties import NodeProperties
from kgforge.specializations.models.rdf.service import RdfService, ShapesGraphWrapper
from kgforge.specializations.stores.nexus import Service
Expand Down
12 changes: 7 additions & 5 deletions kgforge/specializations/models/rdf_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
from rdflib import URIRef, Literal
from rdflib.namespace import XSD

from kgforge.core import Resource
from kgforge.core.archetypes import Model, Store
from kgforge.core.resource import Resource
from kgforge.core.archetypes.store import Store
from kgforge.core.archetypes.model import Model
from kgforge.core.commons.actions import Action
from kgforge.core.commons.context import Context
from kgforge.core.commons.exceptions import ValidationError
Expand Down Expand Up @@ -115,15 +116,16 @@ def _validate_many(self, resources: List[Resource], type_: str) -> None:
for resource in resources:
conforms, graph, _ = self.service.validate(resource, type_=type_)
if conforms:
resource._validated = True
resource.set_validated(True)
action = Action(self._validate_many.__name__, conforms, None)
else:
resource._validated = False
resource.set_validated(False)
violations = set(" ".join(re.findall('[A-Z][^A-Z]*', as_term(o)))
for o in graph.objects(None, SH.sourceConstraintComponent))
message = f"violation(s) of type(s) {', '.join(sorted(violations))}"
action = Action(self._validate_many.__name__, conforms, ValidationError(message))
resource._last_action = action

resource.set_last_action(action)

def _validate_one(self, resource: Resource, type_: str) -> None:
conforms, _, report = self.service.validate(resource, type_)
Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/resolvers/agent_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pathlib import Path
from typing import List, Dict, Any, Optional, Callable, Union

from kgforge.core.archetypes import Resolver
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.archetypes.resolver import _build_resolving_query
from kgforge.core.commons.execution import not_supported
from kgforge.core.commons.sparql_query_builder import SPARQLQueryBuilder
Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/resolvers/demo_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from pathlib import Path
from typing import Callable, Dict, Iterator, List, Optional, Union, Any

from kgforge.core.archetypes import Resolver
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.commons.exceptions import ConfigurationError
from kgforge.core.commons.execution import not_supported
from kgforge.core.commons.strategies import ResolvingStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from abc import ABCMeta
from typing import Dict, List, Optional, Tuple, Union, Any

from kgforge.core.archetypes import Resolver
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.commons.actions import LazyAction
from kgforge.core.commons.exceptions import ResolvingError
from kgforge.core.commons.strategies import ResolvingStrategy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import requests

from kgforge.core.archetypes import Store
from kgforge.core.archetypes.store import Store
from kgforge.core.conversions.json import as_json
from kgforge.core.resource import encode
from kgforge.core.wrappings import Filter, FilterOperator
Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/resolvers/ontology_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from pathlib import Path
from typing import List, Dict, Any, Optional, Callable, Union

from kgforge.core.archetypes import Resolver
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.archetypes.resolver import _build_resolving_query
from kgforge.core.commons.execution import not_supported
from kgforge.core.commons.sparql_query_builder import SPARQLQueryBuilder
Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/resolvers/store_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# along with Blue Brain Nexus Forge. If not, see <https://choosealicense.com/licenses/lgpl-3.0/>.
from typing import Callable, Dict, List, Optional

from kgforge.core.archetypes import Store
from kgforge.core.archetypes.store import Store
from kgforge.core.conversions.json import as_json


Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/resources/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from typing import List, Union
from warnings import warn

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.actions import LazyAction
from kgforge.core.commons.execution import catch, not_supported
from kgforge.core.forge import KnowledgeGraphForge
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# along with Blue Brain Nexus Forge. If not, see <https://choosealicense.com/licenses/lgpl-3.0/>.
from typing import Any

from kgforge.core import Resource
from kgforge.core.resource import Resource


class EntityLinkingCandidate(Resource):
Expand Down
4 changes: 2 additions & 2 deletions kgforge/specializations/stores/bluebrain_nexus.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
from kgforge.core.commons.dictionaries import update_dict
from kgforge.core.commons.es_query_builder import ESQueryBuilder
from kgforge.core.commons.sparql_query_builder import SPARQLQueryBuilder
from kgforge.core import Resource
from kgforge.core.archetypes import Store
from kgforge.core.resource import Resource
from kgforge.core.archetypes.store import Store
from kgforge.core.commons.actions import LazyAction
from kgforge.core.commons.context import Context
from kgforge.core.commons.exceptions import (
Expand Down
14 changes: 10 additions & 4 deletions kgforge/specializations/stores/demo_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@
from typing import Dict, List, Optional, Union, Type
from uuid import uuid4

from kgforge.core import Resource
from kgforge.core.archetypes import Resolver, Store, Mapper, Mapping
from kgforge.core.resource import Resource
from kgforge.core.archetypes.resolver import Resolver
from kgforge.core.archetypes.store import Store
from kgforge.core.archetypes.mapper import Mapper
from kgforge.core.archetypes.mapping import Mapping

from kgforge.core.commons.context import Context
from kgforge.core.commons.exceptions import (DeprecationError, RegistrationError,
RetrievalError, TaggingError, UpdatingError)
from kgforge.core.commons.exceptions import (
DeprecationError, RegistrationError,
RetrievalError, TaggingError, UpdatingError
)
from kgforge.core.commons.execution import not_supported
from kgforge.core.conversions.json import as_json, from_json
from kgforge.core.wrappings.dict import wrap_dict
Expand Down
2 changes: 1 addition & 1 deletion kgforge/specializations/stores/nexus/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from numpy import nan
from requests import HTTPError

from kgforge.core import Resource
from kgforge.core.resource import Resource
from kgforge.core.commons.actions import (
Action,
collect_lazy_actions,
Expand Down

0 comments on commit 604eed6

Please sign in to comment.