Skip to content

Commit 175f479

Browse files
committed
Adding cli_notifier.py
1 parent d589fb8 commit 175f479

File tree

3 files changed

+22
-36
lines changed

3 files changed

+22
-36
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ The `linkbacks.py` module can be used as script to test this plugin behavior:
8484
export SITEURL=...
8585
python path/to/pelican/plugins/linkbacks/linkbacks.py $pelican_generated_html_file
8686

87-
Optionally the `colorama` package can be installed to get colored logs in the output.
87+
To test sending a notification for a given URL to an endpoint, you can use the `cli_notifier.py` script.
8888

8989

9090
## Contributing
@@ -108,7 +108,7 @@ With a valid configuration in `~/.config/pypoetry/`:
108108
## Linter & tests
109109
To execute them:
110110

111-
pylint pelican/plugins/linkbacks/
111+
pylint *.py pelican/plugins/linkbacks/
112112
pytest
113113

114114
### Integration tests

cli_notifier.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
# USAGE: ./cli_notifier.py [pingback|webmention] $source_url $link_url
3+
import logging, os, sys
4+
from pelican.plugins.linkbacks import LinkbackConfig, PingbackNotifier, WebmentionNotifier
5+
6+
def main(kind, source_url, link_url):
7+
logging.basicConfig(format="%(levelname)s [%(name)s] %(message)s",
8+
datefmt="%H:%M:%S", level=logging.DEBUG)
9+
config = LinkbackConfig(os.environ)
10+
notifier_class = PingbackNotifier if kind == "pingback" else WebmentionNotifier
11+
notifier = notifier_class(source_url, link_url, config)
12+
notifier.discover_server_uri()
13+
if notifier.server_uri:
14+
print(f"{notifier.kind} URI detected: {notifier.server_uri} - Now sending notification...")
15+
notifier.send()
16+
17+
if __name__ == '__main__':
18+
main(*sys.argv[1:])

pelican/plugins/linkbacks/linkbacks.py

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python3
12
from abc import ABC, abstractmethod
23
from collections import defaultdict
34
from contextlib import closing
@@ -228,7 +229,7 @@ class Notifier(ABC):
228229
* server_uri: URL of the notification endpoint
229230
"""
230231
@abstractmethod
231-
def discover_server_uri(self):
232+
def discover_server_uri(self, resp_content=None, resp_headers=None):
232233
"""
233234
Sets .server_uri if a notification endpoint is found for target_url.
234235
Must be called before calling send().
@@ -379,37 +380,4 @@ def cli(html_filepath):
379380
cache.dump_to_json()
380381

381382
if __name__ == '__main__':
382-
try: # Optional logs coloring:
383-
from colorama import Back, Fore, Style
384-
# Recipe from: https://chezsoi.org/lucas/blog/colored-logs-in-python.html
385-
class ColorLogsWrapper:
386-
COLOR_MAP = {
387-
'debug': Fore.CYAN,
388-
'info': Fore.GREEN,
389-
'warning': Fore.YELLOW,
390-
'error': Fore.RED,
391-
'critical': Back.RED,
392-
}
393-
def __init__(self, logger):
394-
self.logger = logger
395-
def __getattr__(self, attr_name):
396-
if attr_name == 'warn':
397-
attr_name = 'warning'
398-
if attr_name not in 'debug info warning error critical':
399-
return getattr(self.logger, attr_name)
400-
log_level = getattr(logging, attr_name.upper())
401-
# mimicking logging/__init__.py behaviour
402-
if not self.logger.isEnabledFor(log_level):
403-
return None
404-
def wrapped_attr(msg, *args, **kwargs):
405-
style_prefix = self.COLOR_MAP[attr_name]
406-
msg = style_prefix + msg + Style.RESET_ALL
407-
# We call _.log directly to not increase the callstack
408-
# so that Logger.findCaller extract the corrects filename/lineno
409-
# pylint: disable=protected-access
410-
return self.logger._log(log_level, msg, args, **kwargs)
411-
return wrapped_attr
412-
LOGGER = ColorLogsWrapper(LOGGER)
413-
except ImportError:
414-
print("colorama not available - Logs coloring disabled")
415383
cli(sys.argv[1])

0 commit comments

Comments
 (0)