Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions docs/tutorials/basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,25 @@
],
"id": "137e9b3144a4db96"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"# Define the wiki_domain for later reuse:\n",
"wiki_domain = \"demo.open-semantic-lab.org\" # Replace with the domain of your OSL instance"
],
"id": "ad9ed6244dc16547"
},
{
"metadata": {},
"cell_type": "code",
"source": [
"# Set the default wiki domain for the osw package\n",
"from osw.defaults import params as default_params\n",
"default_params.wiki_domain = wiki_domain\n",
"# Now this domain will be used to initialize osw.express\n",
"from osw.express import OswExpress\n",
"# Some modules that will be required several times\n",
"import osw.params as prm\n",
Expand All @@ -328,17 +343,6 @@
"outputs": [],
"execution_count": null
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": [
"# Define the wiki_domain for later reuse:\n",
"wiki_domain = \"demo.open-semantic-lab.org\" # Replace with the domain of your OSL instance"
],
"id": "7d0db4de67f448b9"
},
{
"metadata": {},
"cell_type": "markdown",
Expand Down
20 changes: 11 additions & 9 deletions examples/use_express_functions.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
from pathlib import Path

from osw.express import (
OswExpress,
cred_filepath_default,
osw_download_file,
osw_upload_file,
)
from osw.defaults import params as default_params
from osw.defaults import paths as default_paths

# The domain, osw.express will be initialized with
default_params.wiki_domain = "wiki-dev.open-semantic-lab.org"

from osw.express import OswExpress, osw_download_file, osw_upload_file # noqa: E402

# (Optional) Set the default credentials filepath to desired location. Otherwise,
# it will use the default location (current working directory)
# cred_filepath_default.set_default(r"C:\Users\gold\ownCloud\Personal\accounts.pwd.yaml")

# Check setting
print(f"Credentials loaded from '{str(cred_filepath_default)}")
print(f"Credentials loaded from '{str(default_paths.cred_fp)}'")

# Create an OswExpress object
# The domain to connect to
domain = "wiki-dev.open-semantic-lab.org"
# domain = "arkeve.test.digital.isc.fraunhofer.de"
# domain = "demo.open-semantic-lab.org"
# Create an OswExpress object
osw_obj = OswExpress(domain=domain)

# Create a file
Expand Down
73 changes: 52 additions & 21 deletions src/osw/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
from pydantic.v1 import PrivateAttr

from osw.custom_types import PossibleFilePath
from osw.defaults import paths as default_paths
from osw.model.static import OswBaseModel

CREDENTIALS_FN_DEFAULT = "credentials.pwd.yaml"


class CredentialManager(OswBaseModel):
"""Handles credentials"""
Expand Down Expand Up @@ -253,15 +252,16 @@ def save_credentials_to_file(
If True, the cred_filepath is set to the given filepath. If False, the
cred_filepath of the CredentialManager is not changed.
"""
filepath_ = [filepath]
cred_filepaths = [filepath]
"""The filepath to save the credentials to."""
if filepath is None:
filepath_ = self.cred_filepath
cred_filepaths = self.cred_filepath
if self.cred_filepath is None:
filepath_ = [Path.cwd() / CREDENTIALS_FN_DEFAULT]
cred_filepaths = [default_paths.cred_fp]
if set_cred_filepath:
# Creates error if file does not exist -> Using custom FilePath
self.cred_filepath = filepath_
for fp in filepath_:
self.cred_filepath = cred_filepaths
for fp in cred_filepaths:
file = Path(fp)
if not file.parent.exists():
file.parent.mkdir(parents=True)
Expand All @@ -282,30 +282,61 @@ def save_credentials_to_file(

# Creating or updating .gitignore file in the working directory
cwd = Path.cwd()
potential_fp = [cwd / ".gitignore", cwd.parent / ".gitignore"]
write_to_fp = potential_fp[0]
potential_fp = [
cwd / ".gitignore",
cwd.parent / ".gitignore",
]
gitignore_fp = potential_fp[0]
# Stops if a .gitignore file is found
for fp in potential_fp:
if fp.exists():
write_to_fp = fp
gitignore_fp = fp
break
if not write_to_fp.exists():
if not write_to_fp.parent.exists():
write_to_fp.parent.mkdir(parents=True)
write_to_fp.touch()
with open(write_to_fp, "r") as stream:
# Creates a .gitignore file if none is found
if not gitignore_fp.exists():
if not gitignore_fp.parent.exists():
gitignore_fp.parent.mkdir(parents=True)
gitignore_fp.touch()
# Reads the .gitignore file
with open(gitignore_fp, "r") as stream:
content = stream.read()
comment_set = False
for _ii, fp in enumerate(filepath_):
if fp.name not in content:
print(f"Adding '{fp.name}' to gitignore file '{write_to_fp}'.")
with open(write_to_fp, "a") as stream:
if comment_set:
osw_dir_added = False
# For every file path in the list of credentials file paths
for _ii, fp in enumerate(cred_filepaths):
to_add = ""
if default_paths.osw_files_dir in fp.parents and not osw_dir_added:
print(
f"Adding '{default_paths.osw_files_dir}' to gitignore file "
f"'{gitignore_fp}'."
)
containing_gitignore = gitignore_fp.parent.absolute()

if containing_gitignore in default_paths.osw_files_dir.parents:
# If the default_path.osw_files_dir is a subdirectory of the directory
# containing the .gitignore file, add the relative path to the
# .gitignore file
rel = default_paths.osw_files_dir.relative_to(containing_gitignore)
to_add = f"\n*{str(rel.as_posix())}/*"
else:
# Test if the default_path.osw_files_dir is a subdirectory of the
# directory containing the .gitignore file
to_add = f"\n*{default_paths.osw_files_dir.absolute().as_posix()}/*"
osw_dir_added = True
elif fp.name not in content:
print(f"Adding '{fp.name}' to gitignore file '{gitignore_fp}'.")
to_add = f"\n*{fp.name}"
# If to_add is not empty, write to .gitignore file
if to_add:
with open(gitignore_fp, "a") as stream:
# Only add comment if not already set
if not comment_set:
stream.write(
"\n# Automatically added by osw.auth.CredentialManager."
"save_credentials_to_file:"
)
comment_set = True
stream.write(f"\n*{fp.name}")
stream.write(to_add)


CredentialManager.CredentialConfig.update_forward_refs()
6 changes: 3 additions & 3 deletions src/osw/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def sort_list_of_entities_by_class(
model_type = None
else:
# Get class type if available
model_type = entity.__class__.__fields__["type"].default[0]
model_type = entity.__class__.__fields__["type"].get_default()[0]
# Add entity to by_name
if name not in by_name:
by_name[name] = []
Expand Down Expand Up @@ -1016,12 +1016,12 @@ def __init__(self, **data):
True # Set to True after implementation of asynchronous upload
)
if self.overwrite is None:
self.overwrite = self.__fields__["overwrite"].default
self.overwrite = self.__fields__["overwrite"].get_default()
self._overwrite_per_class = {"by name": {}, "by type": {}}
if self.overwrite_per_class is not None:
for param in self.overwrite_per_class:
model_name = param.model.__name__
model_type = param.model.__fields__["type"].default[0]
model_type = param.model.__fields__["type"].get_default()[0]
if (
model_name in self._overwrite_per_class["by name"].keys()
or model_type in self._overwrite_per_class["by type"].keys()
Expand Down
15 changes: 6 additions & 9 deletions src/osw/data/import_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@
from osw import wiki_tools as wt
from osw.auth import CredentialManager
from osw.core import OSW
from osw.defaults import paths as default_paths
from osw.model import entity as model
from osw.utils.regex import MatchResult, RegExPatternExtended
from osw.utils.regex_pattern import REGEX_PATTERN_LIB, REGEX_PATTERN_LIST
from osw.utils.regex_pattern import REGEX_PATTERN_LIB
from osw.wtsite import WtSite

# Constants
PACKAGE_ROOT_PATH = Path(__file__).parents[2]
CREDENTIALS_FILE_PATH_DEFAULT = PACKAGE_ROOT_PATH / "examples" / "accounts.pwd.yaml"
ENABLE_SORTING = True
# For compatibility with the old version of the module
REGEX_PATTERN = {rep.description: rep.dict() for rep in REGEX_PATTERN_LIST}


# Classes
Expand Down Expand Up @@ -177,7 +174,7 @@ def transform_attributes_and_merge(
sorted_ = ent_as_dict["sorted"]
else:
sorted_ = False
cls_type_str = str(sel_cls.__fields__["type"].default)
cls_type_str = str(sel_cls.__fields__["type"].get_default())

# Merge entries with the same name / uuid
entities_copy = copy.deepcopy(ent) # Copy to loop over
Expand Down Expand Up @@ -298,7 +295,7 @@ def isclass(obj, cls):
obj_type = obj.get("type")
else:
obj_type = getattr(obj, "type", None)
cls_type = cls.__fields__["type"].default
cls_type = cls.__fields__["type"].get_default()
if isinstance(obj_type, list):
obj_type.sort()
if isinstance(cls_type, list):
Expand Down Expand Up @@ -408,7 +405,7 @@ def jsonpath_search_and_return_list(
if sorted_ and class_to_match:
# See definition in loop_and_call_method with argument 'sorted'
try:
cls_type = class_to_match.__fields__["type"].default
cls_type = class_to_match.__fields__["type"].get_default()
# Search in a dramatically reduced number of entries
result = jp_parse.find(search_tar[str(cls_type)])
except Exception as e:
Expand Down Expand Up @@ -879,7 +876,7 @@ def translate_list_with_deepl(
) -> dict:
"""Translates a list of strings with DeepL."""
if credentials_file_path is None:
credentials_file_path = CREDENTIALS_FILE_PATH_DEFAULT
credentials_file_path = default_paths.cred_fp
if translations is None:
translations = {}
domains, accounts = wt.read_domains_from_credentials_file(credentials_file_path)
Expand Down
Loading
Loading