Exemplo n.º 1
0
    def reference(self, session):
        from uber.server import jsonrpc_services as jsonrpc
        newlines = re.compile(r'(^|[^\n])\n([^\n]|$)')
        admin_account = session.current_admin_account()
        services = []
        for name in sorted(jsonrpc.keys()):
            service = jsonrpc[name]
            methods = []
            for method_name, method in getmembers(service, ismethod):
                if not method_name.startswith('_'):
                    method = unwrap(method)
                    doc = method.__doc__ or ''
                    args = getargspec(method).args
                    if 'self' in args:
                        args.remove('self')
                    access = getattr(method, 'required_access', set())
                    required_access = sorted(
                        [opt[4:].title() for opt in access])
                    methods.append({
                        'name': method_name,
                        'doc': newlines.sub(r'\1 \2', doc).strip(),
                        'args': args,
                        'required_access': required_access
                    })
            doc = service.__doc__ or ''
            services.append({
                'name': name,
                'doc': newlines.sub(r'\1 \2', doc).strip(),
                'methods': methods
            })

        return {'services': services, 'admin_account': admin_account}
Exemplo n.º 2
0
    def sitemap(self):
        site_sections = cherrypy.tree.apps[c.PATH].root
        modules = {
            name: getattr(site_sections, name)
            for name in dir(site_sections) if not name.startswith('_')
        }
        pages = defaultdict(list)
        access_set = AdminAccount.access_set()
        for module_name, module_root in modules.items():
            for name in dir(module_root):
                method = getattr(module_root, name)
                if getattr(method, 'exposed', False):
                    spec = inspect.getfullargspec(unwrap(method))
                    has_defaults = len([
                        arg for arg in spec.args[1:] if arg != 'session'
                    ]) == len(spec.defaults or [])
                    if set(getattr(method, 'restricted', []) or []).intersection(access_set) \
                            and not getattr(method, 'ajax', False) \
                            and (getattr(method, 'site_mappable', False)
                                 or has_defaults and not spec.varkw):

                        pages[module_name].append({
                            'name':
                            name.replace('_', ' ').title(),
                            'path':
                            '/{}/{}'.format(module_name, name)
                        })

        return {'pages': sorted(pages.items())}
Exemplo n.º 3
0
def cached_page(func):
    innermost = unwrap(func)
    if hasattr(innermost, 'cached'):
        from sideboard.lib import config as sideboard_config
        func.lock = RLock()

        @wraps(func)
        def with_caching(*args, **kwargs):
            fpath = os.path.join(sideboard_config['root'], 'data',
                                 func.__module__ + '.' + func.__name__)
            with func.lock:
                if not os.path.exists(fpath) or datetime.now().timestamp(
                ) - os.stat(fpath).st_mtime > 60 * 15:
                    contents = func(*args, **kwargs)
                    with open(fpath, 'wb') as f:
                        # Try to write assuming content is a byte first, then try it as a string
                        try:
                            f.write(contents)
                        except Exception:
                            f.write(bytes(contents, 'UTF-8'))
                with open(fpath, 'rb') as f:
                    return f.read()

        return with_caching
    else:
        return func
Exemplo n.º 4
0
def sessionized(func):
    innermost = unwrap(func)
    if 'session' not in inspect.getfullargspec(innermost).args:
        return func

    @wraps(func)
    def with_session(*args, **kwargs):
        with uber.models.Session() as session:
            try:
                retval = func(*args, session=session, **kwargs)
                session.expunge_all()
                return retval
            except HTTPRedirect:
                session.commit()
                raise
    return with_session
Exemplo n.º 5
0
def sessionized(func):
    innermost = unwrap(func)
    if 'session' not in inspect.getfullargspec(innermost).args:
        return func

    @wraps(func)
    def with_session(*args, **kwargs):
        with uber.models.Session() as session:
            try:
                retval = func(*args, session=session, **kwargs)
                session.expunge_all()
                return retval
            except HTTPRedirect:
                session.commit()
                raise
    return with_session
Exemplo n.º 6
0
    def _decorator(fn):
        inner_func = unwrap(fn)
        if getattr(inner_func, 'required_access', None) is not None:
            return fn
        else:
            inner_func.required_access = required_access

        @wraps(fn)
        def _with_api_auth(*args, **kwargs):
            error = None
            for auth in [auth_by_token, auth_by_session]:
                result = auth(required_access)
                error = error or result
                if not result:
                    return fn(*args, **kwargs)
            raise HTTPError(*error)
        return _with_api_auth
Exemplo n.º 7
0
    def _decorator(fn):
        inner_func = unwrap(fn)
        if getattr(inner_func, 'required_access', None) is not None:
            return fn
        else:
            inner_func.required_access = required_access

        @wraps(fn)
        def _with_api_auth(*args, **kwargs):
            error = None
            for auth in [auth_by_token, auth_by_session]:
                result = auth(required_access)
                error = error or result
                if not result:
                    return fn(*args, **kwargs)
            raise HTTPError(*error)
        return _with_api_auth
Exemplo n.º 8
0
    def sitemap(self):
        site_sections = cherrypy.tree.apps[c.CHERRYPY_MOUNT_PATH].root
        modules = {name: getattr(site_sections, name) for name in dir(site_sections) if not name.startswith('_')}
        pages = defaultdict(list)
        access_set = AdminAccount.access_set()
        for module_name, module_root in modules.items():
            for name in dir(module_root):
                method = getattr(module_root, name)
                if getattr(method, 'exposed', False):
                    spec = inspect.getfullargspec(unwrap(method))
                    has_defaults = len([arg for arg in spec.args[1:] if arg != 'session']) == len(spec.defaults or [])
                    if set(getattr(method, 'restricted', []) or []).intersection(access_set) \
                            and not getattr(method, 'ajax', False) \
                            and (getattr(method, 'site_mappable', False)
                                 or has_defaults and not spec.varkw):

                        pages[module_name].append({
                            'name': name.replace('_', ' ').title(),
                            'path': '/{}/{}'.format(module_name, name)
                        })

        return {'pages': sorted(pages.items())}
Exemplo n.º 9
0
def cached_page(func):
    innermost = unwrap(func)
    if hasattr(innermost, 'cached'):
        from sideboard.lib import config as sideboard_config
        func.lock = RLock()

        @wraps(func)
        def with_caching(*args, **kwargs):
            fpath = os.path.join(sideboard_config['root'], 'data', func.__module__ + '.' + func.__name__)
            with func.lock:
                if not os.path.exists(fpath) or datetime.now().timestamp() - os.stat(fpath).st_mtime > 60 * 15:
                    contents = func(*args, **kwargs)
                    with open(fpath, 'wb') as f:
                        # Try to write assuming content is a byte first, then try it as a string
                        try:
                            f.write(contents)
                        except Exception:
                            f.write(bytes(contents, 'UTF-8'))
                with open(fpath, 'rb') as f:
                    return f.read()
        return with_caching
    else:
        return func