-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2.0.4 release, more info on changelog.md
- Loading branch information
Showing
58 changed files
with
3,948 additions
and
1 deletion.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import logging | ||
import os | ||
|
||
from .settings import LOG_FILE | ||
|
||
|
||
NOTIONPY_LOG_LEVEL = os.environ.get("NOTIONPY_LOG_LEVEL", "warning").lower() | ||
|
||
logger = logging.getLogger("notion") | ||
|
||
|
||
def enable_debugging(): | ||
set_log_level(logging.DEBUG) | ||
|
||
|
||
def set_log_level(level): | ||
logger.setLevel(level) | ||
handler.setLevel(level) | ||
|
||
|
||
if NOTIONPY_LOG_LEVEL == "disabled": | ||
handler = logging.NullHandler() | ||
logger.addHandler(handler) | ||
else: | ||
handler = logging.FileHandler(LOG_FILE) | ||
formatter = logging.Formatter("\n%(asctime)s - %(levelname)s - %(message)s") | ||
handler.setFormatter(formatter) | ||
logger.addHandler(handler) | ||
|
||
if NOTIONPY_LOG_LEVEL == "debug": | ||
set_log_level(logging.DEBUG) | ||
elif NOTIONPY_LOG_LEVEL == "info": | ||
set_log_level(logging.INFO) | ||
elif NOTIONPY_LOG_LEVEL == "warning": | ||
set_log_level(logging.WARNING) | ||
elif NOTIONPY_LOG_LEVEL == "error": | ||
set_log_level(logging.ERROR) | ||
else: | ||
raise Exception( | ||
"Invalid value for environment variable NOTIONPY_LOG_LEVEL: {}".format( | ||
NOTIONPY_LOG_LEVEL | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
from inspect import signature | ||
|
||
from .logger import logger | ||
from .markdown import markdown_to_notion, notion_to_markdown | ||
|
||
|
||
class mapper(property): | ||
def __init__(self, path, python_to_api, api_to_python, *args, **kwargs): | ||
self.python_to_api = python_to_api | ||
self.api_to_python = api_to_python | ||
self.path = ( | ||
".".join(map(str, path)) | ||
if isinstance(path, list) or isinstance(path, tuple) | ||
else path | ||
) | ||
super().__init__(*args, **kwargs) | ||
|
||
|
||
def field_map(path, python_to_api=lambda x: x, api_to_python=lambda x: x): | ||
""" | ||
Returns a property that maps a Block attribute onto a field in the API data structures. | ||
- `path` can either be a top-level field-name, a list that specifies the key names to traverse, | ||
or a dot-delimited string representing the same traversal. | ||
- `python_to_api` is a function that converts values as given in the Python layer into the | ||
internal representation to be sent along in the API request. | ||
- `api_to_python` is a function that converts what is received from the API into an internal | ||
representation to be returned to the Python layer. | ||
""" | ||
|
||
if isinstance(path, str): | ||
path = path.split(".") | ||
|
||
def fget(self): | ||
kwargs = {} | ||
if ( | ||
"client" in signature(api_to_python).parameters | ||
and "id" in signature(api_to_python).parameters | ||
): | ||
kwargs["client"] = self._client | ||
kwargs["id"] = self.id | ||
return api_to_python(self.get(path), **kwargs) | ||
|
||
def fset(self, value): | ||
kwargs = {} | ||
if "client" in signature(python_to_api).parameters: | ||
kwargs["client"] = self._client | ||
self.set(path, python_to_api(value, **kwargs)) | ||
|
||
return mapper( | ||
fget=fget, | ||
fset=fset, | ||
path=path, | ||
python_to_api=python_to_api, | ||
api_to_python=api_to_python, | ||
) | ||
|
||
|
||
def property_map( | ||
name, python_to_api=lambda x: x, api_to_python=lambda x: x, markdown=True | ||
): | ||
""" | ||
Similar to `field_map`, except it works specifically with the data under the "properties" field | ||
in the API's block table, and just takes a single name to specify which subkey to reference. | ||
Also, these properties all seem to use a special "embedded list" format that breaks the text | ||
up into a sequence of chunks and associated format metadata. If `markdown` is True, we convert | ||
this representation into commonmark-compatible markdown, and back again when saving. | ||
""" | ||
|
||
def py2api(x, client=None): | ||
kwargs = {} | ||
if "client" in signature(python_to_api).parameters: | ||
kwargs["client"] = client | ||
x = python_to_api(x, **kwargs) | ||
if markdown: | ||
x = markdown_to_notion(x) | ||
return x | ||
|
||
def api2py(x, client=None, id=""): | ||
x = x or [[""]] | ||
if markdown: | ||
x = notion_to_markdown(x) | ||
kwargs = {} | ||
params = signature(api_to_python).parameters | ||
if "client" in params: | ||
kwargs["client"] = client | ||
if "id" in params: | ||
kwargs["id"] = id | ||
return api_to_python(x, **kwargs) | ||
|
||
return field_map(["properties", name], python_to_api=py2api, api_to_python=api2py) | ||
|
||
|
||
def joint_map(*mappings): | ||
""" | ||
Combine multiple `field_map` and `property_map` instances together to map an attribute to multiple API fields. | ||
Note: when "getting", the first one will be used. When "setting", they will all be set in parallel. | ||
""" | ||
|
||
def fget(self): | ||
return mappings[0].fget(self) | ||
|
||
def fset(self, value): | ||
for m in mappings: | ||
m.fset(self, value) | ||
|
||
return property(fget=fget, fset=fset) |
Oops, something went wrong.