Skip to content
Permalink
Browse files
fix(ontology): add default values for missing comments (DEV-337) (#141)
* fix property comments

* fix default comment for resources

* rename user variable to user_mail

* make return type of getAllGroups optional

* refactor project

* change return type

* refactor ontology creation

* add try except clauses to list creation

* replace typing types with built-in types

* delete knora library readme

* use indent of 4 instead of 3 when dumping json

* improve code after review
  • Loading branch information
irinaschubert committed Jan 10, 2022
1 parent 656ccff commit 6f0094e9a91ba03ffd7bee84e50e4aa93820ab6f

This file was deleted.

@@ -163,7 +163,7 @@ def program(user_args: list[str]) -> None:
create_ontology(input_file=args.datamodelfile,
lists_file=args.listfile,
server=args.server,
user=args.user,
user_mail=args.user,
password=args.password,
verbose=args.verbose,
dump=args.dump if args.dump else False)
@@ -1,4 +1,4 @@
from typing import Dict, Optional, Any
from typing import Optional, Any

from pystrict import strict

@@ -28,7 +28,7 @@ def value(self) -> str:
def permissions(self) -> Optional[Permissions]:
return self._permissions

def toJsonLdObj(self, action: Actions) -> Dict[str, Any]:
def toJsonLdObj(self, action: Actions) -> dict[str, Any]:
tmp = {}
if action == Actions.Create:
tmp["knora-api:fileValueHasFilename"] = self._value
@@ -1,5 +1,5 @@
import json
from typing import Dict, Optional, Union
from typing import Optional, Union

import requests
from pystrict import strict
@@ -19,11 +19,11 @@ class Connection:
"""

_server: str
_prefixes: Union[Dict[str, str], None]
_prefixes: Union[dict[str, str], None]
_token: Union[str, None]
_log: bool

def __init__(self, server: str, prefixes: Dict[str, str] = None):
def __init__(self, server: str, prefixes: dict[str, str] = None):
"""
Constructor requiring the server address, the user and password of KNORA
:param server: Address of the server, e.g https://api.dasch.swiss
@@ -153,12 +153,12 @@ def post(self, path: str, jsondata: Optional[str] = None):
tmp = path.split('/')
filename = "POST" + "_".join(tmp) + ".json"
with open(filename, 'w') as f:
json.dump(logobj, f, indent=3)
json.dump(logobj, f, indent=4)
self.on_api_error(req)
result = req.json()
return result

def get(self, path: str, headers: Optional[Dict[str, str]] = None):
def get(self, path: str, headers: Optional[dict[str, str]] = None):
"""
Get data from a server using a HTTP GET request
:param path: Path of RESTful route
@@ -1,10 +1,12 @@
from __future__ import annotations

import json
from typing import List, Optional, Any, Union
from typing import Optional, Any, Union
from urllib.parse import quote_plus

from pystrict import strict

from knora.dsplib.models.langstring import LangString, Languages
from knora.dsplib.models.langstring import LangString
from .connection import Connection
from .helpers import Actions, BaseError
from .model import Model
@@ -211,7 +213,7 @@ def toJsonObj(self, action: Actions):
tmp['selfjoin'] = self._selfjoin
return tmp

def create(self):
def create(self) -> Group:
jsonobj = self.toJsonObj(Actions.Create)
jsondata = json.dumps(jsonobj)
result = self._con.post(Group.ROUTE, jsondata)
@@ -238,14 +240,16 @@ def delete(self):
return Group.fromJsonObj(self._con, result['group'])

@staticmethod
def getAllGroups(con: Connection) -> List['Group']:
result = con.get(Group.ROUTE)
if 'groups' not in result:
raise BaseError("Request got no groups!")
return list(map(lambda a: Group.fromJsonObj(con, a), result['groups']))
def getAllGroups(con: Connection) -> Optional[list[Group]]:
try:
result = con.get(Group.ROUTE)
return [Group.fromJsonObj(con, group_item) for group_item in result["groups"]]
except BaseError:
# return None if no groups are found or an error happened
return None

@staticmethod
def getAllGroupsForProject(con: Connection, proj_shortcode: str) -> List['Group']:
def getAllGroupsForProject(con: Connection, proj_shortcode: str) -> list[Group]:
result = con.get(Group.ROUTE)
if 'groups' not in result:
raise BaseError("Request got no groups!")
@@ -265,7 +269,7 @@ def createDefinitionFileObj(self):
}
return group

def print(self):
def print(self) -> None:
print('Group Info:')
print(' Id: {}'.format(self._id))
print(' Name: {}'.format(self._name))
@@ -3,7 +3,7 @@
from dataclasses import dataclass
from enum import Enum, unique
from traceback import format_exc
from typing import NewType, List, Dict, Optional, Any, Tuple, Union, Pattern
from typing import NewType, Optional, Any, Tuple, Union, Pattern

from pystrict import strict

@@ -23,7 +23,7 @@ class OntoInfo:
hashtag: bool


ContextType = NewType("ContextType", Dict[str, OntoInfo])
ContextType = NewType("ContextType", dict[str, OntoInfo])


def LINE() -> int:
@@ -86,7 +86,7 @@ class Cardinality(Enum):
@strict
class ContextIterator:
_context: 'Context'
_prefixes: List[str]
_prefixes: list[str]
_index: int

def __init__(self, context: 'Context'):
@@ -111,10 +111,10 @@ class Context:
This class holds a JSON-LD context with the ontology IRI's and the associated prefixes
"""
_context: ContextType
_rcontext: Dict[str, str]
_rcontext: dict[str, str]
_exp: Pattern[str]

common_ontologies = ContextType({
common_ontologies = ContextType({
"foaf": OntoInfo("http://xmlns.com/foaf/0.1/", False),
"dc": OntoInfo("http://purl.org/dc/elements/1.1/", False),
"dcterms": OntoInfo("http://purl.org/dc/terms/", False),
@@ -148,7 +148,7 @@ def __is_iri(self, val: str) -> bool:
m = self._exp.match(val)
return m.span()[1] == len(val) if m else False

def __init__(self, context: Optional[Dict[str, str]] = None):
def __init__(self, context: Optional[dict[str, str]] = None):
"""
THe Constructor of the Context. It takes one optional parameter which as a dict of
prefix - ontology-iri pairs. If the hashtag "#" is used to append element name, the
@@ -158,17 +158,17 @@ def __init__(self, context: Optional[Dict[str, str]] = None):
# regexp to test for a complete IRI (including fragment identifier)
self._exp = re.compile("^(http)s?://([\\w\\.\\-~]+)?(:\\d{,6})?(/[\\w\\-~]+)*(#[\\w\\-~]*)?")
self._context = ContextType({})

# add ontologies from context, if any
if context:
for prefix, onto in context.items():
self._context[prefix] = OntoInfo(onto.removesuffix('#'), onto.endswith('#'))

# add standard ontologies (rdf, rdfs, owl, xsl)
for k, v in self.base_ontologies.items():
if not self._context.get(k):
self._context[k] = v

# add DSP-API internal ontologies (knora-api, salsah-gui)
for k, v in self.knora_ontologies.items():
if not self._context.get(k):
@@ -224,7 +224,7 @@ def context(self, value: ContextType) -> None:
raise BaseError("Error in parameter to context setter")

@property
def rcontext(self) -> Dict[str, str]:
def rcontext(self) -> dict[str, str]:
return self._rcontext

def add_context(self, prefix: str, iri: Optional[str] = None) -> None:
@@ -400,15 +400,15 @@ def reduce_iri(self, iristr: str, ontoname: Optional[str] = None) -> str:
else:
return iristr

def toJsonObj(self) -> Dict[str, str]:
def toJsonObj(self) -> dict[str, str]:
"""
Return a python object that can be jsonfied...
:return: Object to be jsonfied
"""
return {prefix: oinfo.iri + '#' if oinfo.hashtag else oinfo.iri
for prefix, oinfo in self._context.items()}

def get_externals_used(self) -> Dict[str, str]:
def get_externals_used(self) -> dict[str, str]:
exclude = ["rdf", "rdfs", "owl", "xsd", "knora-api", "salsah-gui"]
return {prefix: onto.iri for prefix, onto in self._context.items() if prefix not in exclude}

@@ -487,7 +487,7 @@ class WithId:
"""
_tmp: str = None

def __init__(self, obj: Optional[Dict[str, str]]):
def __init__(self, obj: Optional[dict[str, str]]):
if obj is None:
return
self._tmp = obj.get('@id')
@@ -1,5 +1,5 @@
from enum import Enum, unique
from typing import List, Dict, Tuple, Optional, Any, Union
from typing import Tuple, Optional, Any, Union

from ..models.helpers import BaseError

@@ -12,7 +12,7 @@ class Languages(Enum):
IT = 'it'


LangStringParam = Optional[Union[Dict[Union[Languages, str], str], str]]
LangStringParam = Optional[Union[dict[Union[Languages, str], str], str]]


class LangStringIterator:
@@ -53,7 +53,7 @@ class LangString:
"some:thing": "a string without language specificer"
```
"""
_langstrs: Dict[Languages, str]
_langstrs: dict[Languages, str]
_simplestring: str

def __init__(self, initvalue: LangStringParam = None):
@@ -204,7 +204,7 @@ def toJsonLdObj(self):
# return list(map(lambda a: {'@language': a[0].value, '@value': a[1]}, self._langstrs.items()))

@classmethod
def fromJsonLdObj(cls, obj: Optional[Union[List[Dict[str, str]], str]]) -> 'LangString':
def fromJsonLdObj(cls, obj: Optional[Union[list[dict[str, str]], str]]) -> 'LangString':
if obj is None:
return None
if isinstance(obj, str):
@@ -213,7 +213,7 @@ def fromJsonLdObj(cls, obj: Optional[Union[List[Dict[str, str]], str]]) -> 'Lang
objs = obj
else:
objs = [obj]
lstrs: Dict[Languages, str] = {}
lstrs: dict[Languages, str] = {}
for o in objs:
lang = o.get('@language')
if lang == 'en':
@@ -239,7 +239,7 @@ def fromJsonObj(cls, obj: Optional[Any]) -> 'LangString':
objs = obj
else:
objs = [obj]
lstrs: Dict[Languages, str] = {}
lstrs: dict[Languages, str] = {}
for o in objs:
lang = o.get('language')
if lang == 'en':
@@ -268,7 +268,7 @@ def print(self, offset: Optional[int] = None):
def langstrs(self):
return self._langstrs

def createDefinitionFileObj(self) -> Union[str, Dict[str, str]]:
def createDefinitionFileObj(self) -> Union[str, dict[str, str]]:
if self._simplestring:
return self._simplestring
langstring = {}
@@ -38,9 +38,8 @@
def list_creator(con: Connection,
project: Project,
parent_node: 'ListNode',
nodes: List[dict]) -> List['ListNode']:

nodelist: List[ListNode] = []
nodes: list[dict]) -> list['ListNode']:
nodelist: list[ListNode] = []

for node in nodes:
new_node = ListNode(
@@ -96,7 +95,7 @@ class ListNode(Model):
isRootNode : bool
Is True if the ListNode is the root node of a list. Cannot be set [read].
children : List[ListNode]
children : list[ListNode]
Contains a list of child nodes. This attribute is only available for nodes that have been read by the
method "getAllNodes()" [read].
@@ -141,7 +140,7 @@ class ListNode(Model):
_name: Optional[str]
_parent: Optional[str]
_isRootNode: bool
_children: Optional[List['ListNode']]
_children: Optional[list['ListNode']]
_rootNodeIri: Optional[str]

def __init__(self,
@@ -153,7 +152,7 @@ def __init__(self,
name: Optional[str] = None,
parent: Optional[Union['ListNode', str]] = None,
isRootNode: bool = False,
children: Optional[List['ListNode']] = None,
children: Optional[list['ListNode']] = None,
rootNodeIri: Optional[str] = None):
"""
This is the constructor for the ListNode object. For
@@ -312,18 +311,18 @@ def isRootNode(self, value: bool) -> None:
raise BaseError('Property isRootNode cannot be set!')

@property
def children(self) -> Optional[List['ListNode']]:
def children(self) -> Optional[list['ListNode']]:
return self._children

@children.setter
def children(self, value: List['ListNode']) -> None:
def children(self, value: list['ListNode']) -> None:
self._children = value

@staticmethod
def __getChildren(con: Connection,
parent_iri: str,
project_iri: str,
children: List[Any]) -> Optional[List['ListNode']]:
children: list[Any]) -> Optional[list['ListNode']]:
"""
Internal method! Should not be used directly!
@@ -334,7 +333,7 @@ def __getChildren(con: Connection,
:return: List of ListNode instances
"""
if children:
child_nodes: List[Any] = []
child_nodes: list[Any] = []
for child in children:

if 'parentNodeIri' not in child:
@@ -530,7 +529,7 @@ def getAllNodes(self) -> 'ListNode':
return root

@staticmethod
def getAllLists(con: Connection, project_iri: Optional[str] = None) -> List['ListNode']:
def getAllLists(con: Connection, project_iri: Optional[str] = None) -> list['ListNode']:
"""
Get all lists. If a project IRI is given, it returns the lists of the specified project
@@ -546,7 +545,7 @@ def getAllLists(con: Connection, project_iri: Optional[str] = None) -> List['Lis
raise BaseError("Request got no lists!")
return list(map(lambda a: ListNode.fromJsonObj(con, a), result['lists']))

def _createDefinitionFileObj(self, children: List["ListNode"]):
def _createDefinitionFileObj(self, children: list["ListNode"]):
"""
Create an object that corresponds to the syntax of the input to "create_onto".
Node: This method must be used only internally (for recursion)!!
@@ -1,12 +1,10 @@
from typing import Set

from .connection import Connection
from .helpers import BaseError


class Model:
_con: Connection
_changed: Set[str]
_changed: set[str]

def __init__(self, con: Connection):
if not isinstance(con, Connection):

0 comments on commit 6f0094e

Please sign in to comment.