Coverage for gramex\transforms\badgerfish.py: 36%

# Coverage for gramex\transforms\badgerfish.py : 36%

## 17 statements 9 run 8 missing 0 excluded 0 partial

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 import yaml

2 import xmljson

3 import lxml.html # nosec - we use lxml.html to generate HTML, not parse

4 import tornado.gen

5 from orderedattrdict.yamlutils import AttrDictYAMLLoader

6 from gramex.config import walk, load_imports

7 from . import build_transform

8

9

10@tornado.gen.coroutine

11 def badgerfish(content, handler=None, mapping={}, doctype='<!DOCTYPE html>'):

12 '''

13 A transform that converts string content to YAML, then maps nodes

14 using other functions, and renders the output as HTML.

15

16 The specs for this function are in progress.

17 '''

18 data = yaml.load(content, Loader=AttrDictYAMLLoader) # nosec

19 if handler is not None and hasattr(handler, 'file'):

20 load_imports(data, handler.file)

21 maps = {tag: build_transform(trans, vars={'val': None}, filename='badgerfish')

22 for tag, trans in mapping.items()}

23 for tag, value, node in walk(data):

24 if tag in maps:

25 node[tag] = yield maps[tag](value)

26 raise tornado.gen.Return(lxml.html.tostring(xmljson.badgerfish.etree(data)[0],

27 doctype=doctype, encoding='unicode'))