示例#1
0
文件: plugin.py 项目: shitara/core
def plugin(name, default):
    item = lambda i, d: (item(i + 1, d[name[i]]) if i < len(name) else d)
    try:
        return item(0, settings)
    except KeyError as e:
        return default


def dotname(name, data):
    namespaces = name.split('.')
    for v in reversed(namespaces[1:]):
        data = dict({v: data})
    return (namespaces[0], data)


runtimes = propdict()
for i, v in plugin(['runtime', 'plugins'], {}).items():
    i, v = dotname(i, _(v))
    runtimes[i] = v

responses = propdict()
for i, v in plugin(['response'], {}).items():
    i, v = dotname(i, getattr(_(v), 'emit'))
    responses[i] = v

renderers = propdict()
for i, v in plugin(['renderer', 'plugins'], {}).items():
    i, v = dotname(i, getattr(_(v), 'render'))
    renderers[i] = v

excepts = [_(plugin) for plugin in plugin('except', [])]
示例#2
0
文件: __init__.py 项目: shitara/core
import os
import glob
import logging

from core.conf import create_path, load_config, settings
from core.db.model import ActiveRecord, properties
from core.util.importlib import import_attr
from core.util.dict import propdict

import mongoengine


models = propdict()

for model in glob.glob(create_path(settings['application'].get('models'))):
    name, ext = os.path.splitext(os.path.basename(model))
    config = load_config(model)

    pattern = [
        import_attr(v) for v in config.get('plugins') or []
        ]
    pattern.extend([ActiveRecord, mongoengine.Document])
    model = type(name.capitalize(), tuple(pattern),
        properties(name, config)
        )
    models[name.capitalize()] = model



示例#3
0
文件: locale.py 项目: shitara/core
import os
import csv
import glob

from core.conf import create_path, settings
from core.util.dict import propdict


locales = propdict()

for locale in glob.glob(create_path(settings['application'].get('locales') or '', '*.csv')):

    name, ext = os.path.splitext(os.path.basename(locale))

    with open(locale, 'r') as f:
        reader = csv.reader(f)
        header = next(reader)

        locales[name] = {
            row[0]:row[1] for row in reader
        }

class Locale(object):

    def __init__(self, language):
        for l in language.split(';'):
            for l in l.split(','):
                if l in locales:
                    self.locale = locales[l]
                    return
示例#4
0
import re
import logging
from datetime import datetime

from core.util.json import loads
from core.ext.exception import *
from core.db import models
from core.util.dict import propdict

__confs = config(settings['application']['auths'])
__auths = propdict(
    session=__confs.get('session') or {},
    secrets=__confs.get('secrets') or {},
)


def authenticate(meta, request, response):
    def validation(m, current_user):

        if __confs.get('administrator'):
            if len({
                    i: v
                    for i, v in __confs['administrator'].items()
                    if request.headers.get(i) != v
            }) == 0:
                return True

        current_user or errors.AuthenticateError('no user').throw()

        for v in m.get('validation') or []:
            current_user.validation(v) or errors.AuthenticateError(
示例#5
0
import os
import glob
import logging

from core.conf import create_path, load_config, settings
from core.db.model import ActiveRecord, properties
from core.util.importlib import import_attr
from core.util.dict import propdict

import mongoengine

models = propdict()

for model in glob.glob(create_path(settings['application'].get('models'))):
    name, ext = os.path.splitext(os.path.basename(model))
    config = load_config(model)

    pattern = [import_attr(v) for v in config.get('plugins') or []]
    pattern.extend([ActiveRecord, mongoengine.Document])
    model = type(name.capitalize(), tuple(pattern), properties(name, config))
    models[name.capitalize()] = model
示例#6
0
文件: validator.py 项目: shitara/core
import re
import logging
from datetime import datetime

from core.util.json import loads
from core.ext.exception import *
from core.db import models
from core.util.dict import propdict

__confs = config(settings['application']['auths'])
__auths = propdict(
    session = __confs.get('session') or {},
    secrets = __confs.get('secrets') or {},
    )

def authenticate(meta, request, response):

    def validation(m, current_user):

        if __confs.get('administrator'):
            if len({ i:v for i,v in __confs['administrator'].items()
                if request.headers.get(i) != v }) == 0:
                return True

        current_user or errors.AuthenticateError(
            'no user').throw()

        for v in m.get('validation') or []:
            current_user.validation(v) or errors.AuthenticateError(
                'no user').throw()
示例#7
0
from core.util.dict import propdict


class Error(Exception):
    def __init__(self, message):
        self.message = message

    def throw(self):
        raise self

    def __str__(self):
        return self.message


class RuntimeError(Error):
    def __init__(self, exception):
        self.exception = exception

    def __str__(self):
        return str(self.exception)


errors = propdict()

for k, v in config(settings['application']['errors']).items():
    errors[k] = type(k, (Error, ),
                     dict(
                         status=v['status'],
                         format=v['format'],
                     ))
示例#8
0
文件: plugin.py 项目: shitara/core
def plugin(name, default):
    item = lambda i, d: (
        item(i+1, d[name[i]]) if i < len(name) else d
        )
    try: return item(0, settings)
    except KeyError as e:
        return default

def dotname(name, data):
    namespaces = name.split('.')
    for v in reversed(namespaces[1:]):
        data = dict({ v: data })
    return (namespaces[0], data)

runtimes = propdict()
for i,v in plugin(['runtime', 'plugins'], {}).items():
    i,v = dotname(i, _(v))
    runtimes[i] = v

responses = propdict()
for i,v in plugin(['response'], {}).items():
    i,v = dotname(i, getattr(_(v), 'emit'))
    responses[i] = v

renderers = propdict()
for i,v in plugin(['renderer', 'plugins'], {}).items():
    i,v = dotname(i, getattr(_(v), 'render'))
    renderers[i] = v

excepts = [