utils.py 3.16 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
from . import models, __version__
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
2
from collections import MutableMapping
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
3
4
5
6
import pprint

import logging
logger = logging.getLogger(__name__)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7
8
9

# MutableMapping should be enough, but it causes problems with py2
DICTCLASSES = (MutableMapping, dict, models.BaseModel)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
10
11
12


def check_template(indict, template):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
13
    if isinstance(template, DICTCLASSES) and isinstance(indict, DICTCLASSES):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
14
15
        for k, v in template.items():
            if k not in indict:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
16
                raise models.Error('{} not in {}'.format(k, indict))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
17
18
19
20
21
22
            check_template(indict[k], v)
    elif isinstance(template, list) and isinstance(indict, list):
        for e in template:
            for i in indict:
                try:
                    check_template(i, e)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
23
                    break
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
24
                except models.Error as ex:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
25
                    # raise
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
26
                    continue
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
27
28
29
30
            else:
                raise models.Error(('Element not found.'
                                   '\nExpected: {}\nIn: {}').format(pprint.pformat(e),
                                                                    pprint.pformat(indict)))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
31
32
    else:
        if indict != template:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
33
34
35
36
            raise models.Error(('Differences found.\n'
                                '\tExpected: {}\n'
                                '\tFound: {}').format(pprint.pformat(indict),
                                                      pprint.pformat(template)))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
37
38


39
40
41
42
43
44
45
46
47
def convert_dictionary(original, mappings):
    result = {}
    for key, value in original.items():
        if key in mappings:
            key = mappings[key]
        result[key] = value
    return result


J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
48
def easy_load(app=None, plugin_list=None, plugin_folder=None, **kwargs):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
49
50
51
52
53
    '''
    Run a server with a specific plugin.
    '''

    from flask import Flask
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
54
    from .extensions import Senpy
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
55
56
57

    if not app:
        app = Flask(__name__)
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
58
59
60
61
62
63
    sp = Senpy(app, plugin_folder=plugin_folder, **kwargs)
    if not plugin_list:
        from . import plugins
        import __main__
        plugin_list = plugins.from_module(__main__)
    for plugin in plugin_list:
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
64
65
        sp.add_plugin(plugin)
    sp.install_deps()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
66
67
68
69
70
71
72
73
74
75
76
77
78
    sp.activate_all()
    return sp, app


def easy_test(plugin_list=None):
    logger.setLevel(logging.DEBUG)
    logging.getLogger().setLevel(logging.INFO)
    if not plugin_list:
        from . import plugins
        import __main__
        plugin_list = plugins.from_module(__main__)
    for plug in plugin_list:
        plug.test()
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
79
        logger.info('The tests for {} passed!'.format(plug.name))
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    logger.info('All tests passed!')


def easy(host='0.0.0.0', port=5000, debug=True, **kwargs):
    '''
    Run a server with a specific plugin.
    '''
    logging.getLogger().setLevel(logging.DEBUG)
    logging.getLogger('senpy').setLevel(logging.INFO)
    sp, app = easy_load(**kwargs)
    easy_test(sp.plugins())
    app.debug = debug
    import time
    logger.info(time.time())
    logger.info('Senpy version {}'.format(__version__))
    logger.info('Server running on port %s:%d. Ctrl+C to quit' % (host,
                                                                  port))
    app.debug = debug
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
98
99
    app.run(host,
            port,
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
100
            debug=app.debug)