Browse Source

Revert to argparse absed API

Easier and better because you can pass in everything you need without
having to deal with decorators which after a look at are quite
complicated to play with.
pull/8/head
Luke Murphy 5 years ago
parent
commit
22ebaf1629
No known key found for this signature in database GPG Key ID: 5E2EF5A63E3718CC
  1. 13
      README.md
  2. 2
      etherpump/__init__.py
  3. 16
      etherpump/api/__init__.py
  4. 23
      etherpump/api/_utils.py
  5. 37
      etherpump/api/magicword.py

13
README.md

@ -123,18 +123,15 @@ The settings are placed in a file called `.etherpump/settings.json` and are used
Library API Example Library API Example
------------------- -------------------
Etherpump can be used as a library: Etherpump can be used as a library.
```python All commands can be imported and run programmatically.
from etherpump.api import magicword
@magicword('__VARIA_PUB_CLUB__') ```python
def do_things_with_the_pad_contents(pads): >>> from etherpump.api import pull
print(pads['my-pad-name']['html']) >>> pull(['--all', '--publish-opt-in', '--publish', '__PUB_CLUB__'])
``` ```
This is still a work in progress.
Subcommands Subcommands
---------- ----------

2
etherpump/__init__.py

@ -2,4 +2,4 @@ import os
DATAPATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data") DATAPATH = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data")
__VERSION__ = '0.0.6' __VERSION__ = '0.0.7'

16
etherpump/api/__init__.py

@ -1 +1,15 @@
from etherpump.api.magicword import magicword # noqa from etherpump.commands.creatediffhtml import main as creatediffhtml # noqa
from etherpump.commands.deletepad import main as deletepad # noqa
from etherpump.commands.dumpcsv import main as dumpcsv # noqa
from etherpump.commands.gethtml import main as gethtml # noqa
from etherpump.commands.gettext import main as gettext # noqa
from etherpump.commands.index import main as index # noqa
from etherpump.commands.init import main as init # noqa
from etherpump.commands.list import main as list # noqa
from etherpump.commands.listauthors import main as listauthors # noqa
from etherpump.commands.publication import main as publication # noqa
from etherpump.commands.pull import main as pull # noqa
from etherpump.commands.revisionscount import main as revisionscount # noqa
from etherpump.commands.sethtml import main as sethtml # noqa
from etherpump.commands.settext import main as settext # noqa
from etherpump.commands.showmeta import main as showmeta # noqa

23
etherpump/api/_utils.py

@ -1,23 +0,0 @@
"""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']

37
etherpump/api/magicword.py

@ -1,37 +0,0 @@
"""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
Loading…
Cancel
Save