Skip to content

Commit

Permalink
Get rid of the global logger (#200)
Browse files Browse the repository at this point in the history
* Get rid of the global logger

* Debug logging configuration (load at runtime)

* Improve `__init__` var names

* Typo fix
  • Loading branch information
sHermanGriffiths authored Nov 14, 2024
1 parent 52cc2b2 commit 4eb2ec2
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 38 deletions.
18 changes: 18 additions & 0 deletions n2y/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import logging
import os


def log_filter(record: logging.LogRecord) -> logging.LogRecord:
record.pathname = record.pathname.replace(os.getcwd() + "/", "")
return record


LOG_FORMATTER = logging.Formatter(
"%(asctime)s - %(levelname)s (%(pathname)s::%(funcName)s::%(lineno)d): %(message)s"
)
LOG_HANDLER = logging.StreamHandler()
LOG_HANDLER.setLevel(logging.INFO)
LOG_HANDLER.setFormatter(LOG_FORMATTER)
LOG_HANDLER.addFilter(log_filter)
LOG = logging.getLogger(__name__)
LOG.addHandler(LOG_HANDLER)
6 changes: 3 additions & 3 deletions n2y/audit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import os
import sys

from n2y import LOG_HANDLER
from n2y.blocks import LinkToPageBlock
from n2y.database import Database
from n2y.errors import UseNextClass
from n2y.logger import HANDLER
from n2y.logger import logger as log
from n2y.mentions import PageMention
from n2y.notion import Client
from n2y.page import Page
from n2y.utils import id_from_share_link

plugin_key = "audit"
log = logging.getLogger(__name__)


class ReportingPageMention(PageMention):
Expand Down Expand Up @@ -76,7 +76,7 @@ def main(raw_args, access_token, logger=log):
logging_level = logging.__dict__[args.verbosity]
new_formatter = logging.Formatter(args.logging_format)
logger.setLevel(logging_level)
HANDLER.setFormatter(new_formatter)
LOG_HANDLER.setFormatter(new_formatter)

if access_token is None:
logger.critical("No NOTION_ACCESS_TOKEN environment variable is set")
Expand Down
18 changes: 0 additions & 18 deletions n2y/logger.py

This file was deleted.

9 changes: 5 additions & 4 deletions n2y/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

from n2y.config import load_config, merge_default_config
from n2y.export import database_to_files, database_to_yaml, export_page, write_document
from n2y.logger import logger as log
from n2y.notion import Client
from n2y.utils import share_link_from_id

log = logging.getLogger(__name__)


def cli_main():
args = sys.argv[1:]
Expand Down Expand Up @@ -42,7 +43,7 @@ def main(raw_args, access_token, n2y_cache=None, logger=log):
args = parser.parse_args(raw_args)

logging_level = logging.__dict__[args.verbosity]
logger.setLevel(logging_level)
logging.basicConfig(level=logging_level)

if n2y_cache is not None:
try:
Expand Down Expand Up @@ -108,8 +109,8 @@ def _export_node_from_config(client, export):
database = client.get_database(export["id"])
if database is None:
msg = (
"Unable to find database with id '%s' (%s). "
"Perhaps the integration doesn't have permission to access this database?"
"Unable to find database with id '%s' (%s). Perhaps the integration"
" doesn't have permission to access this database?"
)
client.logger.error(msg, export["id"], share_link_from_id(export["id"]))
return False
Expand Down
11 changes: 8 additions & 3 deletions n2y/notion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import importlib.util
import json
import logging
from os import makedirs, path
from urllib.parse import urljoin, urlparse

Expand All @@ -20,7 +21,6 @@
UseNextClass,
)
from n2y.file import File
from n2y.logger import logger as log
from n2y.mentions import DEFAULT_MENTIONS
from n2y.notion_mocks import mock_rich_text_array
from n2y.page import Page
Expand All @@ -31,6 +31,7 @@
from n2y.utils import retry_api_call, sanitize_filename, strip_hyphens

# TODO: Rename this file `client.py`
log = logging.getLogger(__name__)

DEFAULT_NOTION_CLASSES = {
"page": Page,
Expand Down Expand Up @@ -97,7 +98,9 @@ def get_default_classes(self):
notion_classes = {}
for notion_object, object_types in DEFAULT_NOTION_CLASSES.items():
if type(object_types) is dict:
notion_classes[notion_object] = {k: [v] for k, v in object_types.items()}
notion_classes[notion_object] = {
k: [v] for k, v in object_types.items()
}
else:
notion_classes[notion_object] = [object_types]
return notion_classes
Expand All @@ -123,7 +126,9 @@ def load_plugin(self, notion_classes):
else:
raise PluginError(f'Invalid notion object "{notion_object}"')

def _override_notion_classes(self, notion_object, object_types, default_object_types):
def _override_notion_classes(
self, notion_object, object_types, default_object_types
):
# E.g., there are many types of notion blocks but only one type of notion page.
notion_object_has_types = isinstance(default_object_types, dict)

Expand Down
13 changes: 6 additions & 7 deletions n2y/plugins/internallinks.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import typing
import uuid
import urllib.parse
import uuid

from n2y.blocks import Block
from n2y.errors import UseNextClass
from n2y.logger import logger
from n2y.rich_text import TextRichText
from n2y.utils import header_id_from_text

Expand Down Expand Up @@ -64,22 +63,22 @@ class NotionInternalLink(TextRichText):

def __init__(self, client, notion_data, block=None):
super().__init__(client, notion_data, block)
if block is None or not is_internal_link(
self.href, self.block.page.notion_id
):
if block is None or not is_internal_link(self.href, self.block.page.notion_id):
raise UseNextClass

def to_pandoc(self):
target_id = get_notion_id_from_href(self.href)
if target_id is None:
logger.warning(
self.client.logger.warning(
"Internal link missing; defaulting to link with no-op behavior"
)
return super().to_pandoc()

target_block = find_target_block(self.block.page.block, target_id)
if target_block is None:
logger.error(f"Internal link target block not found: {target_id}")
self.client.logger.error(
f"Internal link target block not found: {target_id}"
)
# Fallback to default behavior for TextRichText conversion
return super().to_pandoc()
header_id = header_id_from_text(target_block.rich_text.to_plain_text())
Expand Down
8 changes: 6 additions & 2 deletions n2y/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import functools
import logging
import numbers
import re
import unicodedata
Expand All @@ -16,7 +17,8 @@
ConnectionThrottled,
PandocASTParseError,
)
from n2y.logger import logger

logger = logging.getLogger(__name__)

# see https://pandoc.org/MANUAL.html#exit-codes
PANDOC_PARSE_ERROR = 64
Expand Down Expand Up @@ -182,7 +184,9 @@ def slugify(value, allow_unicode=False):
value = unicodedata.normalize("NFKC", value)
else:
value = (
unicodedata.normalize("NFKD", value).encode("ascii", "ignore").decode("ascii")
unicodedata.normalize("NFKD", value)
.encode("ascii", "ignore")
.decode("ascii")
)
value = re.sub(r"[^\w\s-]", "", value.lower())
return re.sub(r"[-\s]+", "-", value).strip("-_")
Expand Down
4 changes: 3 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import logging

import yaml

Expand All @@ -10,9 +11,10 @@
merge_config,
valid_notion_id,
)
from n2y.logger import logger
from n2y.notion_mocks import mock_id

logger = logging.getLogger(__name__)


def mock_config_item(node_type):
config_item = copy.deepcopy(EXPORT_DEFAULTS)
Expand Down

0 comments on commit 4eb2ec2

Please sign in to comment.