Skip to content

Commit 42d5fe6

Browse files
committed
refactor: replace print calls with logging across src/osw
- 113 of 123 print calls become _logger calls on a per-module logger - levels chosen per call site: 18 error, 12 warning, 23 info, 60 debug - kept: the output-replay machinery in util.py, the auth login prompt and Entity.explain, which are interactive or are the replay itself - workflow.py gets a module-level logger, replacing function-local ones - README documents set_log_level, disable_logging and OSW_LOG_LEVEL
1 parent 2430204 commit 42d5fe6

15 files changed

Lines changed: 200 additions & 132 deletions

File tree

‎README.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ More runnable scripts live in [examples/](examples/), and the
3939
[Basics tutorial](docs/tutorials/basics.ipynb) walks through the
4040
OpenSemanticLab data model.
4141

42+
## Logging
43+
44+
osw reports what it is doing through the standard `logging` module, on the
45+
`osw` logger, at INFO by default:
46+
47+
```python
48+
import osw
49+
50+
osw.set_log_level("WARNING") # see less
51+
osw.set_log_level("DEBUG") # see more
52+
osw.disable_logging() # detach the handler osw attached
53+
```
54+
55+
Set `OSW_LOG_LEVEL` to a level name, a level number, or `OFF` to choose the
56+
level before the package is imported. `disable_logging()` also restores
57+
propagation to the root logger, so an application that prefers to configure
58+
handlers itself can take over with `logging.basicConfig()`.
59+
4260
## Contributing
4361

4462
Contributions are welcome, see [CONTRIBUTING.md](CONTRIBUTING.md).

‎src/osw/auth.py‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import getpass
4+
import logging
45
import os
56
import warnings
67
from enum import Enum
@@ -16,6 +17,8 @@
1617

1718
from osw.defaults import paths as default_paths
1819

20+
_logger = logging.getLogger(__name__)
21+
1922

2023
def _secret_to_str(v):
2124
"""Unwrap SecretStr to plain str, pass through otherwise."""
@@ -131,7 +134,7 @@ def _load_file_credentials(self):
131134
loaded = _load_credentials(fp, into_store=False)
132135
all_creds.update(loaded)
133136
except Exception as exc:
134-
print(exc)
137+
_logger.error(f"{exc}")
135138
return all_creds
136139

137140
def get_credential(self, config: CredentialConfig) -> BaseCredential:
@@ -243,7 +246,7 @@ def iri_in_file(self, iri: str) -> bool:
243246
if iri_ == iri:
244247
return True
245248
except yaml.YAMLError as exc:
246-
print(exc)
249+
_logger.error(f"{exc}")
247250
return False
248251

249252
def save_credentials_to_file(
@@ -292,9 +295,9 @@ def save_credentials_to_file(
292295
with open(fp, "w", encoding="utf-8") as stream:
293296
yaml.dump(data, stream)
294297
if file_already_exists:
295-
print(f"Credentials file updated at '{fp.resolve()}'.")
298+
_logger.info(f"Credentials file updated at '{fp.resolve()}'.")
296299
else:
297-
print(f"Credentials file created at '{fp.resolve()}'.")
300+
_logger.info(f"Credentials file created at '{fp.resolve()}'.")
298301

299302

300303
CredentialManager.CredentialConfig.update_forward_refs()

‎src/osw/controller/database.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import logging
12
from typing import Optional, Union
23

34
from opensemantic.v1 import OswBaseModel
@@ -9,6 +10,8 @@
910
from osw.auth import CredentialManager
1011
from osw.core import OSW
1112

13+
_logger = logging.getLogger(__name__)
14+
1215

1316
class DatabaseController(model.Database):
1417
"""
@@ -130,4 +133,4 @@ def execute(self, sql: str):
130133
with self.engine.connect() as conn:
131134
result_set = conn.execute(sql_text(sql))
132135
for r in result_set:
133-
print(r)
136+
_logger.debug(f"{r}")

‎src/osw/controller/file/wiki.py‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
import logging
23
import os
34
from typing import IO, Any, Dict, List, Optional
45

@@ -8,6 +9,8 @@
89
from osw.utils.wiki import get_namespace, get_title
910
from osw.wtsite import WtSite
1011

12+
_logger = logging.getLogger(__name__)
13+
1114

1215
class WikiFileController(model.WikiFile, RemoteFileController):
1316
"""File controller for wiki files"""
@@ -70,10 +73,9 @@ def get(self) -> IO:
7073
if web_api_failed:
7174
# fallback: use direct download
7275
url = file.imageinfo["url"]
73-
print(
74-
"Extension FileApi not installed on the server. Fallback: use direct "
75-
"download from ",
76-
url,
76+
_logger.warning(
77+
f"Extension FileApi not installed on the server. Fallback: use "
78+
f"direct download from {url}"
7779
)
7880
response = self.osw.mw_site.connection.get(url, stream=True)
7981

‎src/osw/core.py‎

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,11 @@ def register_schema(self, schema_registration: SchemaRegistration):
384384

385385
page.set_slot_content("jsonschema", schema)
386386
else:
387-
print("Error: Unsupported entity type")
387+
_logger.error("Error: Unsupported entity type")
388388
return
389389

390390
page.edit()
391-
print("Entity stored at " + page.get_url())
391+
_logger.info("Entity stored at " + page.get_url())
392392

393393
class SchemaUnregistration(BaseModel):
394394
"""dataclass param of register_schema()"""
@@ -421,7 +421,7 @@ def unregister_schema(self, schema_unregistration: SchemaUnregistration):
421421
):
422422
uuid = schema_unregistration.model_cls.__uuid__
423423
else:
424-
print("Error: Neither model nor model id provided")
424+
_logger.error("Error: Neither model nor model id provided")
425425

426426
entity_title = "Category:" + OSW.get_osw_id(uuid)
427427
page = self.site.get_page(WtSite.GetPageParam(titles=[entity_title])).pages[0]
@@ -607,15 +607,15 @@ def _fetch_schema(
607607
fetchSchemaParam.offline_pages is not None
608608
and schema_title in fetchSchemaParam.offline_pages
609609
):
610-
print(f"Fetch {schema_title} from offline pages")
610+
_logger.info(f"Fetch {schema_title} from offline pages")
611611
page = fetchSchemaParam.offline_pages[schema_title]
612612
else:
613-
print(f"Fetch {schema_title} from online pages")
613+
_logger.info(f"Fetch {schema_title} from online pages")
614614
page = self.site.get_page(WtSite.GetPageParam(titles=[schema_title])).pages[
615615
0
616616
]
617617
if not page.exists:
618-
print(f"Error: Page {schema_title} does not exist")
618+
_logger.error(f"Error: Page {schema_title} does not exist")
619619
# the $ref that led here was already rewritten to this file name
620620
write_schema_stub(get_model_dir_path(), schema_name)
621621
return OSW.FetchSchemaResult(
@@ -637,7 +637,7 @@ def _fetch_schema(
637637
)
638638
schema_str = json.dumps(schema)
639639
if (schema_str is None) or (schema_str == ""):
640-
print(f"Warning: Schema slot of {schema_title} is empty")
640+
_logger.warning(f"Warning: Schema slot of {schema_title} is empty")
641641
schema_str = "{}" # empty schema to make reference work
642642
if fetchSchemaParam.warning_messages is None:
643643
fetchSchemaParam.warning_messages = []
@@ -1027,7 +1027,7 @@ def _fetch_schema(
10271027
# may overlap partially, e.g.
10281028
# from datetime import date
10291029
# from datetime import date, datetime
1030-
print("Replace import statement:", import_stmt)
1030+
_logger.info(f"Replace import statement: {import_stmt}")
10311031
content = re.sub(
10321032
r"^" + re.escape(import_stmt) + r"$",
10331033
"",
@@ -1216,7 +1216,7 @@ def load_entity(
12161216
param = entity_title
12171217

12181218
if param.model_to_use:
1219-
print(f"Using schema {param.model_to_use.__name__} to create entity")
1219+
_logger.info(f"Using schema {param.model_to_use.__name__} to create entity")
12201220

12211221
# store original cache state
12221222
cache_state = self.site.get_cache_enabled()
@@ -1266,7 +1266,7 @@ def load_entity(
12661266
)
12671267
if not hasattr(model, cls_name):
12681268
schemas_fetched = False
1269-
print(
1269+
_logger.error(
12701270
f"Error: Model {cls_name} not found. Schema {category} "
12711271
f"needs to be fetched first."
12721272
)
@@ -1408,7 +1408,7 @@ def validate_entity(cls, entity, values):
14081408
try:
14091409
uuid_from_title = get_uuid(title)
14101410
except ValueError:
1411-
print(
1411+
_logger.error(
14121412
f"Error: UUID could not be determined from title: '{title}', "
14131413
f"nor fromjsondata: {jsondata}"
14141414
)
@@ -1450,7 +1450,7 @@ def _apply_overwrite_policy(param: OSW._ApplyOverwriteParam) -> WtPage:
14501450

14511451
def set_content(content_to_set: dict) -> None:
14521452
if param.debug:
1453-
print(f"content_to_set: {content_to_set!s}")
1453+
_logger.debug(f"content_to_set: {content_to_set!s}")
14541454
for slot_ in content_to_set.keys():
14551455
page.set_slot_content(slot_, content_to_set[slot_])
14561456

@@ -1481,7 +1481,7 @@ def set_content(content_to_set: dict) -> None:
14811481
page.exists
14821482
and param.policy.overwrite == AddOverwriteClassOptions.keep_existing
14831483
):
1484-
print(
1484+
_logger.warning(
14851485
f"Entity '{entity_title}' already exists and won't be stored "
14861486
f"with overwrite set to 'keep existing'!"
14871487
)
@@ -1509,15 +1509,15 @@ def set_content(content_to_set: dict) -> None:
15091509
if remote_content["footer"]:
15101510
new_content["footer"] = remote_content["footer"]
15111511
if param.debug:
1512-
print(f"'remote_content': {remote_content!s}")
1512+
_logger.debug(f"'remote_content': {remote_content!s}")
15131513
# Get the local content
15141514
# Properties that are not set in the local content will be set to None
15151515
# We want those not to be listed as keys
15161516
local_content["jsondata"] = json.loads(param.entity.json(exclude_none=True))
15171517
if param.remove_empty:
15181518
remove_empty(local_content["jsondata"])
15191519
if param.debug:
1520-
print(f"'local_content': {local_content!s}")
1520+
_logger.debug(f"'local_content': {local_content!s}")
15211521
# Apply the overwrite logic
15221522
# a) If there is a key in the remote content that is not in the local
15231523
# content, we have to keep it
@@ -1530,7 +1530,7 @@ def set_content(content_to_set: dict) -> None:
15301530
# if key not in local_content["jsondata"].keys()
15311531
# }
15321532
if param.debug:
1533-
print(f"'New content' after 'remote' update: {new_content!s}")
1533+
_logger.debug(f"'New content' after 'remote' update: {new_content!s}")
15341534
# b) If there is a key in the local content that is not in the remote
15351535
# content, we have to keep it
15361536
new_content["jsondata"].update({
@@ -1539,7 +1539,7 @@ def set_content(content_to_set: dict) -> None:
15391539
if key not in remote_content["jsondata"].keys()
15401540
})
15411541
if param.debug:
1542-
print(f"'New content' after 'local' update: {new_content!s}")
1542+
_logger.debug(f"'New content' after 'local' update: {new_content!s}")
15431543
# c) If there is a key in both contents, we have to apply the overwrite
15441544
# logic
15451545
# todo: include logic for hidden and read_only properties!
@@ -1549,14 +1549,14 @@ def set_content(content_to_set: dict) -> None:
15491549
if param.policy.get_overwrite_setting(key) == OverwriteOptions.true
15501550
})
15511551
if param.debug:
1552-
print(f"'New content' after 'True' update: {new_content!s}")
1552+
_logger.debug(f"'New content' after 'True' update: {new_content!s}")
15531553
new_content["jsondata"].update({
15541554
key: value
15551555
for (key, value) in remote_content["jsondata"].items()
15561556
if param.policy.get_overwrite_setting(key) == OverwriteOptions.false
15571557
})
15581558
if param.debug:
1559-
print(f"'New content' after 'False' update: {new_content!s}")
1559+
_logger.debug(f"'New content' after 'False' update: {new_content!s}")
15601560
new_content["jsondata"].update({
15611561
key: value
15621562
for (key, value) in local_content["jsondata"].items()
@@ -1566,8 +1566,8 @@ def set_content(content_to_set: dict) -> None:
15661566
)
15671567
})
15681568
if param.debug:
1569-
print(f"'New content' after 'only empty' update: {new_content!s}")
1570-
print(f"'New content' to be stored: {new_content!s}")
1569+
_logger.debug(f"'New content' after 'only empty' update: {new_content!s}")
1570+
_logger.debug(f"'New content' to be stored: {new_content!s}")
15711571
set_content(new_content)
15721572
return page # Guard clause --> exit function
15731573

@@ -1792,7 +1792,9 @@ def store_entity_(
17921792
)
17931793
generated_schemas[key] = json.loads(schema_str)
17941794
except Exception as e:
1795-
print(f"Schema generation from template failed for {entity_}: {e}")
1795+
_logger.error(
1796+
f"Schema generation from template failed for {entity_}: {e}"
1797+
)
17961798

17971799
mode = AggregateGeneratedSchemasParamMode.ROOT_LEVEL
17981800
# Put generated schema in definitions section,
@@ -1816,16 +1818,16 @@ def store_entity_(
18161818
) # will set page.changed if the content of the page has changed
18171819
if not param.offline and page.changed:
18181820
if index is None:
1819-
print(f"Entity stored at '{page.get_url()}'.")
1821+
_logger.info(f"Entity stored at '{page.get_url()}'.")
18201822
else:
1821-
print(
1823+
_logger.info(
18221824
f"({index + 1}/{max_index}) Entity stored at "
18231825
f"'{page.get_url()}'."
18241826
)
18251827
created_pages[page.title] = page
18261828

18271829
sorted_entities = OSW.sort_list_of_entities_by_class(param.entities)
1828-
print(
1830+
_logger.info(
18291831
"Entities to be uploaded have been sorted according to their type.\n"
18301832
"If you would like to overwrite existing entities or properties, "
18311833
"pass a StoreEntityParam to store_entity() with "
@@ -1853,7 +1855,7 @@ class UploadObject(BaseModel):
18531855
overwrite=param.overwrite,
18541856
)
18551857
if param.debug:
1856-
print(
1858+
_logger.debug(
18571859
f"Now adding entities of class type '{class_type}' "
18581860
f"({entity_model.__name__}) to upload list. No class specific"
18591861
f" overwrite setting found. Using fallback option '"
@@ -1978,7 +1980,7 @@ def delete_entity_(entity_, comment_: str = None):
19781980
if title_ is None:
19791981
title_ = OSW.get_osw_id(entity_.uuid)
19801982
if namespace_ is None or title_ is None:
1981-
print("Error: Unsupported entity type")
1983+
_logger.error("Error: Unsupported entity type")
19821984
return
19831985
entity_title = namespace_ + ":" + title_
19841986
page = self.site.get_page(WtSite.GetPageParam(titles=[entity_title])).pages[
@@ -1987,9 +1989,9 @@ def delete_entity_(entity_, comment_: str = None):
19871989

19881990
if page.exists:
19891991
page.delete(comment_)
1990-
print("Entity deleted: " + page.get_url())
1992+
_logger.info("Entity deleted: " + page.get_url())
19911993
else:
1992-
print(f"Entity '{entity_title}' does not exist!")
1994+
_logger.warning(f"Entity '{entity_title}' does not exist!")
19931995

19941996
if entity.parallel:
19951997
_ = parallelize(

‎src/osw/data/import_utility.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import copy
22
import inspect
3+
import logging
34
import re
45
import uuid as uuid_module
56
import warnings
@@ -21,6 +22,8 @@
2122
from osw.utils.regex_pattern import REGEX_PATTERN_LIB
2223
from osw.wtsite import WtPage, WtSite
2324

25+
_logger = logging.getLogger(__name__)
26+
2427
# Constants
2528
ENABLE_SORTING = True
2629

@@ -206,7 +209,7 @@ def transform_attributes_and_merge(
206209
del ent_as_dict[other_k]
207210
del ent[other_k]
208211
del_keys.append(other_k)
209-
print(f"Merge operation complete. Deleted keys: {del_keys}")
212+
_logger.info(f"Merge operation complete. Deleted keys: {del_keys}")
210213
return {"entities": ent, "entities_as_dict": ent_as_dict}
211214

212215

@@ -805,7 +808,7 @@ def test_if_empty_list_or_none(obj) -> bool:
805808
wtsite_obj = osw_obj.site
806809
entities_from_osw = []
807810
if debug:
808-
print(f"Searching for instances of {category_to_search} in OSW...")
811+
_logger.debug(f"Searching for instances of {category_to_search} in OSW...")
809812
entities = wtsite_obj.semantic_search(
810813
query=(
811814
wt.SearchParam(

0 commit comments

Comments
 (0)