Beispiel #1
0
    def _process_object(self, object, fetch_proto=False):

        if isinstance(object, db.Model):
            
            obj = {'key':str(object.key()),'type':'model','kind':object.kind(),'app':object.key().app(),'parent':object.key().parent(),'properties':{}}

            if object.key().has_id_or_name():
                obj['key'] = (str(object.key()), object.key().id_or_name())
            
            if hasattr(object, pc_config.get('poly_class_field','data','_class_key_')):
                obj['class'] = '.'.join(getattr(object, pc_config.get('poly_class_field','data','_class_key_')))
            
            for key, value in object.properties().items():
            
                if value.__class__.__name__ == 'ReferenceProperty':
                    continue
                
                obj['properties'][key] = {'type':value.__class__.__name__, 'value':str(getattr(object, key))}
            
                return obj
                
        elif isinstance(object, db.Key):
                            
            obj = {'key':str(object),'type':'key','kind':object.kind(),'app':object.app(),'parent':object.parent()}
            
            if object.has_id_or_name():
                obj['key'] = (str(object), object.id_or_name())
                
            return obj
Beispiel #2
0
    def provision_token(cls):

        key_value, key_hash = cls.provision_key_content(
            str(account_obj.key()),
            pc_config.get(
                "token_key_hash_algorithm", "security", pc_config.get("security", "key_default_hash_algorithm", "md5")
            ),
        )

        return (key_value, Token(key_name=key_value, content=key_value, md5_hash=key_hash).put())
Beispiel #3
0
    def provision_key_content(
        cls, salt="", algorithm=pc_config.get("key_default_hash_algorithm", "security", "sha256")
    ):

        hash_obj = hashlib.new(algorithm)
        hash_obj.update(str(salt) + ":::" + str(datetime.datetime.now().isoformat()) + str(random.random()))

        key_hash = hashlib.new("md5")
        key_hash.update(hash_obj.hexdigest())

        return (hash_obj.hexdigest(), key_hash.hexdigest())
Beispiel #4
0
    def provision_account_key(cls, account_obj=None, **kwargs):

        if account_obj is not None:

            key_value, key_hash = cls.provision_key_content(
                str(account_obj.key()),
                pc_config.get(
                    "account_key_hash_algorithm",
                    "security",
                    pc_config.get("key_default_hash_algorithm", "security", "sha256"),
                ),
            )

            return (
                key_value,
                AccountKey(
                    key_name=key_value, content=key_value, md5_hash=key_hash, account=account_obj, **kwargs
                ).put(),
            )

        else:
            return False
Beispiel #5
0
def QueuedTransaction(mode, eta=None, decorators=None, retries=pc_config.get('default_retries','decorators.data.QueuedTransaction')):
        
    global _mode
    _mode = mode
    
    def _select_op(*args):
        
        global _mode
        
    
        worker = TransactionController.getTxnForDecorator(_mode)
        return worker
    
        
    return _select_op
Beispiel #6
0
def main():
    
    application = webapp.WSGIApplication(ROUTES, debug=pc_config.get('debug','handlers',False))
    wsgiref.handlers.CGIHandler().run(application)
Beispiel #7
0
import os, sys, logging
import wsgiref.handlers

from google.appengine.ext import webapp

from ProvidenceClarity import pc_config
from ProvidenceClarity.handlers import util, input, output, workers, admin, api

from ProvidenceClarity.handlers.util import errors
from ProvidenceClarity.handlers.input import receiver, fetcher
from ProvidenceClarity.handlers.admin import data
from ProvidenceClarity.handlers.workers import mail, xmpp, analyzer, data
from ProvidenceClarity.handlers.api import data as data_api

rp = pc_config.get('pc_url_prefix','handlers','/_pc')

ROUTES = [

    ## Admin Panel Handlers
    (rp+'/manage/*$',admin.IndexHandler),
    (rp+'/manage/data',admin.data.DataIndex),
    (rp+'/manage/data/(.+)/list',admin.data.DataList),
    (rp+'/manage/data/(.+)/create',admin.data.DataCreate),
    (rp+'/manage/data/(.+)/view/(.+)',admin.data.DataView),
    (rp+'/manage/data/(.+)/edit/(.+)',admin.data.DataEdit),    
    (rp+'/manage/data/(.+)/delete/(.+)',admin.data.DataDelete),

    ## Data API Handlers
    (rp+'/api/data/list', data_api.MasterTypeListHandler), # master types list
    (rp+'/api/data/query', data_api.MasterQueryHandler), # raw GQL query endpoint
    (rp+'/api/data/(.+)/list',data_api.TypeListHandler), # listing a type
Beispiel #8
0
    def render(self, tmpl, vars={}, **kwds):
        
        ######## //    Page   // ########
        ######## //  Caching  // ########
        
        global COMPILED_TEMPLATE_PATH
        
        if pc_config.get('page_caching','handlers',False) == True:
            if 'caching' not in kwds or kwds['caching'] == True:
                user = users.get_current_user()
                if user != None:
                    key = str(tmpl)+'::'+str(user.user_id())
                else:
                    key = str(tmpl)+'::NOT_LOGGED_IN'
                    
                cached_page = memcache.get(key)
                if cached_page != None:
                    
                    # append cached page header if debug headers are turned on
                    if pc_config.get('debug_headers','handlers',False) == True:
                        self.response.headers['X-PC-Is-Cached'] = "true"
                    
                    self.response.out.write(cached_page)
                    return True # finish out if we write a cached page
        
        ######## //   Prepare   // ########
        ######## //  Variables  // ########
        
        ## init vars
        variables = {}
        
        ## prepare page variables
        sys = {'urls':{},'config':{},'security':{},'env':{},'version':VERSION,'subnav':False}
        sys['urls']['images'] = pc_config.get('images_url','handlers','/assets/images/static/')
        sys['urls']['script'] = pc_config.get('script_url','handlers','/assets/script/static/')
        sys['urls']['style'] = pc_config.get('style_url','handlers','/assets/style/static/')
        
        ## platform config
        sys['config'] = pc_config.dump()
        
        ## user & security variables
        if pc_config.get('enable_security','security',True) == True:
            sys['security']['user'] = users.get_current_user()
            sys['security']['permissions'] = {'sys_admin':users.is_current_user_admin()}
            sys['security']['logout_url'] = users.create_logout_url(pc_config.get('logout_destination','security','/'))
            sys['security']['login_url'] = users.create_login_url('/_pc/manage/')
        else:
            sys['security']['user'] = False
            sys['security']['permissions'] = {'sys_admin':False}
            sys['security']['logout_url'] = users.create_logout_url(pc_config.get('logout_destination','security','/'))            
            sys['security']['login_url'] = users.create_login_url('/_pc/manage/')            
        
        ## page variables
        if 'page' in kwds:
            variables['page'] = kwds['page']
        
        ## environment variables
        for name in os.environ.keys():
            sys['env'][name] = os.environ[name]
        
        ## add in sys
        variables['sys'] = sys        
        
        ## add extra vars
        if len(vars) > 0:
            for var in vars.keys():

                # pull subnav
                if key == 'subnav':
                    sys['subnav'] = kwds['subnav']
                else:
                    variables[key] = kwds[key]
    
        ######## //   Render   // ########
        ######## //    Page   // ########
        
        ## get template root
        if COMPILED_TEMPLATE_PATH == None:
            source = PC_PATH
            if PC_PATH[-1] != '/':
                source = source+'/'

            template_root = pc_config.get('template_root','handlers',None)

            if template_root != None and isinstance(template_root, str):
            
                if source[-1] != '/' and template_root[0] != '/':
                    template_root = template_root+'/'
                
                source = source+template_root
            
            if source[-1] != '/' and tmpl[0] != '/':
                source = source+'/'
            
            COMPILED_TEMPLATE_PATH = source
            source = source+tmpl
            
        else:
            source = COMPILED_TEMPLATE_PATH+tmpl
        
        logging.info('SOURCE: '+str(source))
        
        ## set headers, render, and go
        self.render_raw(template.render(source,variables))
Beispiel #9
0
import logging, sys
from . import DataManager
from . import DataController

from ProvidenceClarity import pc_config

from ProvidenceClarity.data.proto import P

do_log = pc_config.get('log_imports','api.data.proto.ProtoController',False)


class ProtoController(DataController):
            
    @classmethod
    def import_helper(cls,name,fromlist=None):
        
        if do_log: logging.info("[i]: Import request: \'"+str(name)+"\' with fromlist \'"+str(fromlist)+"\'.")
        
        try:
            
            if fromlist is not None:
            
                _fromlist_i = []
            
                if isinstance(fromlist, str):
                    _fromlist_i[0] = fromlist
                    
                elif isinstance(fromlist, list):
                    _fromlist_i = fromlist
                    
                for subitem in fromlist:
Beispiel #10
0
import logging
import sys
import os

from google.appengine.ext import db
from google.appengine.api import datastore
from ProvidenceClarity.data.core import _PC_MODEL_BRANCH_POLY
from ProvidenceClarity.data.core.model import Model
from ProvidenceClarity.data.core.properties.polymodel import _ClassKeyProperty
from ProvidenceClarity.data.core.properties.polymodel import _ModelPathProperty

from ProvidenceClarity import pc_config
from ProvidenceClarity.api.data import DataController

_LOG_IMPORTS = pc_config.get('log_imports','data.core.polymodel.PolyModel',False)
_PATH_SEPERATOR = pc_config.get('poly_path_seperator','api.data.core.polymodel.PolyModel',':')
_KEY_NAME_SEPERATOR = pc_config.get('key_name_seperator','api.data.core.polymodel.PolyModel','//')

_CLASS_KEY_PROPERTY = pc_config.get('poly_class_field','data','_class_key_')
_PATH_KEY_PROPERTY  = pc_config.get('poly_path_field','data','_class_path_')

_class_map = {}


## +=+=+ Metaclass controlling the creation of Providence/Clarity polymodel objects.
class PolymorphicModel(db.PropertiedClass):

    """ Populates properties like __root_class__ and __class_hierarchy__ and enforces logic about direct instantiation. """

    def __init__(cls, name, bases, dct):
            
Beispiel #11
0
import logging
from ProvidenceClarity import pc_config

do_logging = pc_config.get("import_logging", "api.util", False)


def import_helper(name, fromlist=None):

    global do_logging

    if do_logging:
        logging.debug("Import logging turned on for api.util.")
    if do_logging:
        logging.debug('Import request for name: "%s" with fromlist "%s".' % name, str(fromlist))

    try:

        if fromlist is None:

            if isinstance(name, list):
                mod = __import__(".".join(name))
            elif isinstance(name, str):
                mod = __import__(name)
            else:
                return False

            if do_logging:
                logging.debug('Import request successful. Returning "%s".' % str(mod))

        else:
Beispiel #12
0
from google.appengine.ext import db
from google.appengine.api import datastore_types
from google.appengine.api import datastore_errors

from ProvidenceClarity import pc_config

_PATH_PREFIX = pc_config.get('path_prefix','data',False)
_IMPORT_PREFIX = pc_config.get('import_prefix','data',False)


## +=+=+ Stores the Python package import path from the application root, for lazy-load on search.
class _ModelPathProperty(db.StringProperty):
    
    def __init__(self, name, **kwargs):
        super(_ModelPathProperty, self).__init__(name=name, default=None, **kwargs)

    def __set__(self, *args):
        raise db.DerivedPropertyError('Model-path is a derived property and cannot be set.')

    def __get__(self, model_instance, model_class):
        if model_instance is None: return self
        # @TODO: make seperator customizable from some import/config
        return model_instance._getModelPath(':')
        

## +=+=+ Stores the polymodel class inheritance path.
class _ClassKeyProperty(db.ListProperty):

    def __init__(self, name):
        super(_ClassKeyProperty, self).__init__(name=name,item_type=str,default=None)
Beispiel #13
0
import exceptions
from ProvidenceClarity import PCController, PCAdapter, pc_config
from ProvidenceClarity.data.data import ORIGIN_LIST, FORMAT_LIST, DataStub, DataBackend
from ProvidenceClarity.data.core.model import Model
from ProvidenceClarity.api.util import import_helper

adapters = pc_config.get('adapters','api.storage',[])



class StorageController(PCController):

    @classmethod
    def loadAdapter(cls,name):

        global adapters
        
        if backend in adapters:
            adapter = import_helper(adapters[backend])
        
            if adapter == False:
                raise exceptions.InvalidBackend()
                
            else:
                ### STOPPED HERE
                pass
                
    @classmethod
    def loadStubClass(cls,adapter):
        
        if isinstance(adapter, (str,basestring,unicode)):