Luke Murphy
5 years ago
8 changed files with 112 additions and 9 deletions
@ -0,0 +1 @@ |
|||||
|
from etherpump.api.magicword import magicword # noqa |
@ -0,0 +1,23 @@ |
|||||
|
"""Utilities for API functions.""" |
||||
|
|
||||
|
from pathlib import Path |
||||
|
from urllib.parse import urlencode |
||||
|
|
||||
|
from etherpump.commands.common import getjson, loadpadinfo |
||||
|
from etherpump.commands.init import main |
||||
|
|
||||
|
|
||||
|
def ensure_init(): |
||||
|
"""Ensure etherpump has already been init'd.""" |
||||
|
try: |
||||
|
main([]) |
||||
|
except SystemExit: |
||||
|
pass |
||||
|
|
||||
|
|
||||
|
def get_pad_ids(): |
||||
|
"""Retrieve all available pad ids.""" |
||||
|
info = loadpadinfo(Path('.etherpump/settings.json')) |
||||
|
data = {'apikey': info['apikey']} |
||||
|
url = info['localapiurl'] + 'listAllPads?' + urlencode(data) |
||||
|
return getjson(url)['data']['padIDs'] |
@ -0,0 +1,37 @@ |
|||||
|
"""API for programming against pads marked with __MAGIC_WORDS__.""" |
||||
|
|
||||
|
__all__ = ['magicword'] |
||||
|
|
||||
|
import json |
||||
|
from pathlib import Path |
||||
|
|
||||
|
from etherpump.api._utils import ensure_init, get_pad_ids |
||||
|
from etherpump.commands.pull import main as pull |
||||
|
|
||||
|
|
||||
|
def magicword(word): |
||||
|
"""Decorator for handling magic words.""" |
||||
|
|
||||
|
ensure_init() |
||||
|
pull(['--all', '--publish-opt-in', '--publish', word]) |
||||
|
|
||||
|
pads = {} |
||||
|
for pad_id in get_pad_ids(): |
||||
|
pads[pad_id] = {} |
||||
|
try: |
||||
|
pads[pad_id]['html'] = open(Path(f'./p/{pad_id}.raw.html')).read() |
||||
|
pads[pad_id]['txt'] = open(Path(f'./p/{pad_id}.raw.txt')).read() |
||||
|
pads[pad_id]['meta'] = json.loads( |
||||
|
open(Path(f'./p/{pad_id}.meta.json')).read() |
||||
|
) |
||||
|
pads[pad_id]['dhtml'] = open(Path(f'./p/{pad_id}.raw.dhtml')).read() |
||||
|
except FileNotFoundError: |
||||
|
pass |
||||
|
|
||||
|
def wrap(userfunc): |
||||
|
def wrappedf(*args): |
||||
|
userfunc(pads) |
||||
|
|
||||
|
return wrappedf |
||||
|
|
||||
|
return wrap |
@ -0,0 +1,25 @@ |
|||||
|
"""How to use the 'magicword' API. |
||||
|
|
||||
|
In this example, we have a pad called "my-pad-name" on our etherpad-lite |
||||
|
instance. Inside the content of that pad is the so-called "magic word" |
||||
|
__WORSE_IS_BETTER__. The "@magicword" takes care of retrieving all the formats |
||||
|
of those pads and returns a dictionary called "pads" ready to go. |
||||
|
|
||||
|
Run this example like so: |
||||
|
|
||||
|
$ python etherdump/examples/magicword.py |
||||
|
|
||||
|
""" |
||||
|
|
||||
|
from etherpump.api import magicword |
||||
|
|
||||
|
|
||||
|
@magicword('__WORSE_IS_BETTER__') |
||||
|
def can_be_called_anything_you_like(pads): |
||||
|
print(pads['foobar']['html']) |
||||
|
print(pads['foobar']['txt']) |
||||
|
print(pads['foobar']['meta']) |
||||
|
|
||||
|
|
||||
|
if __name__ == "__main__": |
||||
|
can_be_called_anything_you_like() |
Loading…
Reference in new issue