Skip to content

Commit

Permalink
initial support for request generator & response parser loading (#2 and
Browse files Browse the repository at this point in the history
  • Loading branch information
petri committed Feb 19, 2017
1 parent 3ee2922 commit 2efdb7e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
History
=======

0.3.0dev (unreleased)
-----------------------

* Implement request generator loading (issue #2)
* Implement response parser loading (issue #1))

0.2.0 (2017-02-19)
------------------

Expand Down
43 changes: 43 additions & 0 deletions httpreverse/httpreverse.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-

import re
from importlib import import_module
from collections import ChainMap
import yaml
import jinja2
Expand Down Expand Up @@ -74,3 +75,45 @@ def parametrize(opspec, context={}, implicit=False):

return opspec


def _load_callable(specstr):
"try to import and return mypkg.mymodule:mycallable"

try:
module, callable = specstr.split(":")
except:
raise Exception("bad specification string given (use pkg.module:callable syntax)")

try:
imported = import_module(module)
except ModuleNotFoundError:
raise Exception("no module '%s' found!" % module)

try:
callable = getattr(imported, callable)
except:
raise Exception("callable '%s' not found in module '%s'!" % (callable, module))

return callable


def _load_parser(opspec, assign=True):
"parse & load and (by default) assign response parser callable, if found"

parser = _load_callable(opspec["response"]["parser"])

if assign:
opspec["response"]["parser"] = parser

return parser


def _load_generator(opspec, assign=True):
"parse & load and (by default) assign request generator callable, if found"

generator = _load_callable(opspec["request"]["generator"])

if assign:
opspec["request"]["generator"] = generator

return generator

0 comments on commit 2efdb7e

Please sign in to comment.