Пример #1
0
def init_config():
    # check if config is inited
    if type(list(settings['requestinterfaces'].values())[0]) == str:
        # register request interfaces
        for key, value in settings['requestinterfaces'].items():
            settings['requestinterfaces'][key] = misc.get_rto_by_name(value)
        # register template languages
        for key, value in settings['templatelanguages'].items():
            settings['templatelanguages'][key] = misc.get_rto_by_name(value)
        # load published directories
        from porcupine.config import pubdirs
def init_config():
    # check if config is inited
    if type(settings["requestinterfaces"].values()[0]) == str:
        # register request interfaces
        for key, value in settings["requestinterfaces"].items():
            settings["requestinterfaces"][key] = misc.get_rto_by_name(value)
        # register template languages
        for key, value in settings["templatelanguages"].items():
            settings["templatelanguages"][key] = misc.get_rto_by_name(value)
        # load published directories
        from porcupine.config import pubdirs
Пример #3
0
    def __init__(self, classobj):
        self._class = misc.get_rto_by_name(classobj)
        self._bases = self._class.__bases__
        self._doc = self._class.__doc__
        self._attrs = {}
        self._methods = {}
        self._properties = {}
        self._module = sys.modules[self._class.__module__]

        # check line cache
        linecache.checkcache(filename=self._module.__file__)

        try:
            sourcelines = inspect.getsourcelines(self._class)
            startline = sourcelines[1]
            endline = startline + len(sourcelines[0]) - 1
            self.boundaries = (startline, endline)
        except IOError:
            # the source file does not exist
            self.boundaries = None

        self._instance = self._class()

        # find instance attributes
        sub_attrs = {}
        for base_class in self._class.__bases__:
            s = base_class()
            if hasattr(s, '__dict__'):
                sub_attrs.update(s.__dict__)
        for member_name in self._instance.__dict__:
            if not member_name in sub_attrs:
                self._attrs[member_name] = self._instance.__dict__[member_name]

        # find methods and properties
        for member_name in self._class.__dict__:
            member = self._class.__dict__[member_name]
            if type(member) == types.FunctionType:
                self._methods[member_name] = member
            elif type(member) == property:
                self._properties[member_name] = member

        self._imports = {}

        # locate imports
        moduledict = self._module.__dict__
        for x in moduledict:
            if type(moduledict[x]) == types.ModuleType:
                self._imports[moduledict[x]] = x
            elif hasattr(moduledict[x], '__call__') and \
                    (sys.modules[moduledict[x].__module__] != self._module):
                imported = misc.get_rto_by_name(moduledict[x].__module__ +
                                                '.' + moduledict[x].__name__)
                self._imports[imported] = moduledict[x].__name__
    def __init__(self, classobj):
        self._class = misc.get_rto_by_name(classobj)
        self._bases = self._class.__bases__
        self._doc = self._class.__doc__
        self._attrs = {}
        self._methods = {}
        self._properties = {}
        self._module = sys.modules[self._class.__module__]

        # check line cache
        linecache.checkcache(filename=self._module.__file__)

        try:
            sourcelines = inspect.getsourcelines(self._class)
            startline = sourcelines[1]
            endline = startline + len(sourcelines[0]) - 1
            self.boundaries = (startline, endline)
        except IOError:
            # the source file does not exist
            self.boundaries = None

        self._instance = self._class()

        # find instance attributes
        sub_attrs = {}
        for base_class in self._class.__bases__:
            s = base_class()
            if hasattr(s, '__dict__'):
                sub_attrs.update(s.__dict__)
        for member_name in self._instance.__dict__:
            if not member_name in sub_attrs:
                self._attrs[member_name] = self._instance.__dict__[member_name]

        # find methods and properties
        for member_name in self._class.__dict__:
            member = self._class.__dict__[member_name]
            if type(member) == types.FunctionType:
                self._methods[member_name] = member
            elif type(member) == property:
                self._properties[member_name] = member

        self._imports = {}

        # locate imports
        moduledict = self._module.__dict__
        for x in moduledict:
            if type(moduledict[x]) == types.ModuleType:
                self._imports[moduledict[x]] = x
            elif hasattr(moduledict[x], '__call__') and \
                    (sys.modules[moduledict[x].__module__] != self._module):
                imported = misc.get_rto_by_name(moduledict[x].__module__ +
                                                '.' + moduledict[x].__name__)
                self._imports[imported] = moduledict[x].__name__
def new(self):
    "Displays a generic form for creating a new object"
    sCC = context.request.queryString['cc'][0]
    oNewItem = misc.get_rto_by_name(sCC)()
    role = permsresolver.get_access(self, context.user)

    params = {
        'CC': sCC,
        'URI': self.id,
        'TITLE': '@@CREATE@@ "@@%s@@"' % sCC,
        'ICON': oNewItem.__image__,
        'PROPERTIES': [],
        'EXTRA_TABS': [],
        'ADMIN': role == permsresolver.COORDINATOR,
        'ROLES_INHERITED': 'true',
        'ACTION_DISABLED': 'false',
        'METHOD': 'create'}

    # inspect item properties
    for attr_name in oNewItem.__props__:
        attr = getattr(oNewItem, attr_name)
        if isinstance(attr, datatypes.DataType):
            control, tab = baseitem._getControlFromAttribute(oNewItem,
                                                             attr_name,
                                                             attr,
                                                             False,
                                                             True)
            params['PROPERTIES'].append(control)
            params['EXTRA_TABS'].append(tab)

    return params
def create(self, data):
    "Creates a new item"
    oNewItem = misc.get_rto_by_name(data.pop('CC'))()

    # get user role
    iUserRole = permsresolver.get_access(self, context.user)
    if '__rolesinherited' in data and iUserRole == permsresolver.COORDINATOR:
        oNewItem.inheritRoles = data.pop('__rolesinherited')
        if not oNewItem.inheritRoles:
            acl = data.pop('__acl')
            if acl:
                security = {}
                for descriptor in acl:
                    security[descriptor['id']] = int(descriptor['role'])
                oNewItem.security = security

    # set props
    for prop in data:
        oAttr = getattr(oNewItem, prop)
        if isinstance(oAttr, datatypes.File):
            if data[prop]['tempfile']:
                oAttr.filename = data[prop]['filename']
                sPath = (context.server.temp_folder + '/' +
                         data[prop]['tempfile'])
                oAttr.load_from_file(sPath)
                os.remove(sPath)
        elif isinstance(oAttr, datatypes.Date):
            oAttr.value = data[prop].value
        elif isinstance(oAttr, datatypes.Integer):
            oAttr.value = int(data[prop])
        else:
            oAttr.value = data[prop]

    oNewItem.append_to(self)
    return oNewItem.id
Пример #7
0
def create(self, data):
    "Creates a new item"
    oNewItem = misc.get_rto_by_name(data.pop('CC'))()

    # get user role
    iUserRole = permsresolver.get_access(self, context.user)
    if '__rolesinherited' in data and iUserRole == permsresolver.COORDINATOR:
        oNewItem.inheritRoles = data.pop('__rolesinherited')
        if not oNewItem.inheritRoles:
            acl = data.pop('__acl')
            if acl:
                security = {}
                for descriptor in acl:
                    security[descriptor['id']] = int(descriptor['role'])
                oNewItem.security = security

    # set props
    for prop in data:
        oAttr = getattr(oNewItem, prop)
        if isinstance(oAttr, datatypes.File):
            if data[prop]['tempfile']:
                oAttr.filename = data[prop]['filename']
                sPath = (context.server.temp_folder + '/' +
                         data[prop]['tempfile'])
                oAttr.load_from_file(sPath)
                os.remove(sPath)
        elif isinstance(oAttr, datatypes.Date):
            oAttr.value = data[prop].value
        elif isinstance(oAttr, datatypes.Integer):
            oAttr.value = int(data[prop])
        else:
            oAttr.value = data[prop]

    oNewItem.append_to(self)
    return oNewItem.id
def getInfo(self):
    "Returns info about the container's contents"
    sLang = context.request.get_lang()
    lstChildren = []
    children = self.get_children()
    for child in children:
        obj = {
            'id': child.id,
            'cc': child.contentclass,
            'image': child.__image__,
            'displayName': child.displayName.value,
            'isCollection': child.isCollection,
            'modified': date.Date(child.modified)}
        if hasattr(child, 'size'):
            obj['size'] = child.size
        lstChildren.append(obj)

    containment = []
    for contained in self.containment:
        image = misc.get_rto_by_name(contained).__image__
        if not type(image) == str:
            image = ''
        localestring = resources.get_resource(contained, sLang)
        containment.append([localestring, contained, image])

    return {
        'displayName': self.displayName.value,
        'path': misc.get_full_path(self),
        'parentid': self.parentid,
        'iscollection': self.isCollection,
        'containment': containment,
        'user_role': permsresolver.get_access(self, context.user),
        'contents': lstChildren}
Пример #9
0
def getInfo(self):
    "Returns info about the container's contents"
    sLang = context.request.get_lang()
    lstChildren = []
    children = self.get_children()
    for child in children:
        obj = {
            'id': child.id,
            'cc': child.contentclass,
            'image': child.__image__,
            'displayName': child.displayName.value,
            'isCollection': child.isCollection,
            'modified': date.Date(child.modified)
        }
        if hasattr(child, 'size'):
            obj['size'] = child.size
        lstChildren.append(obj)

    containment = []
    for contained in self.containment:
        image = misc.get_rto_by_name(contained).__image__
        if not type(image) == str:
            image = ''
        localestring = resources.get_resource(contained, sLang)
        containment.append([localestring, contained, image])

    return {
        'displayName': self.displayName.value,
        'path': misc.get_full_path(self),
        'parentid': self.parentid,
        'iscollection': self.isCollection,
        'containment': containment,
        'user_role': permsresolver.get_access(self, context.user),
        'contents': lstChildren
    }
Пример #10
0
def start():
    for service in settings['services']:
        name = service['name']
        type = service['type']
        service_class = misc.get_rto_by_name(service['class'])

        if type == 'TCPListener':
            address = misc.get_address_from_string(service['address'])
            worker_processes = service['worker_processes']
            worker_threads = int(service['worker_threads'])
            svc = service_class(name, address, worker_processes,
                                    worker_threads)
        elif type == 'ScheduledTask':
            interval = int(service['interval'])
            svc = service_class(name, interval)

        # add parameters
        if 'parameters' in service:
            svc.parameters = service['parameters']

        services.services_list.append(svc)

    # start services
    for service in services:
        service.start()
Пример #11
0
def new(self):
    "Displays a generic form for creating a new object"
    sCC = context.request.queryString['cc'][0]
    oNewItem = misc.get_rto_by_name(sCC)()
    role = permsresolver.get_access(self, context.user)

    params = {
        'CC': sCC,
        'URI': self.id,
        'TITLE': '@@CREATE@@ "@@%s@@"' % sCC,
        'ICON': oNewItem.__image__,
        'PROPERTIES': [],
        'EXTRA_TABS': [],
        'ADMIN': role == permsresolver.COORDINATOR,
        'ROLES_INHERITED': 'true',
        'ACTION_DISABLED': 'false',
        'METHOD': 'create'
    }

    # inspect item properties
    for attr_name in oNewItem.__props__:
        attr = getattr(oNewItem, attr_name)
        if isinstance(attr, datatypes.DataType):
            control, tab = baseitem._getControlFromAttribute(
                oNewItem, attr_name, attr, False, True)
            params['PROPERTIES'].append(control)
            params['EXTRA_TABS'].append(tab)

    return params
def new(self):
    "Displays a generic form for creating a new object"
    context = HttpContext.current()
    
    sCC = context.request.queryString['cc'][0]
    oNewItem = misc.get_rto_by_name(sCC)()
    
    params = {
        'CC': sCC,
        'URI': context.request.getRootUrl() + '/' + self.id,
        'ICON': oNewItem.__image__,
        'PROPERTIES_TAB': '',
        'EXTRA_TABS': '',
        'SECURITY_TAB': baseitem._getSecurity(self, context.user, True)
    }
    
    # inspect item properties
    sProperties = ''
    for attr_name in oNewItem.__props__:
        attr = getattr(oNewItem, attr_name)
        if isinstance(attr, datatypes.DataType):
            control, tab = baseitem._getControlFromAttribute(oNewItem,
                                                             attr_name,
                                                             attr,
                                                             False,
                                                             True)
            sProperties += control
            params['EXTRA_TABS'] += tab
    
    params['PROPERTIES'] = sProperties
    
    return params
Пример #13
0
def start():
    for service in settings['services']:
        name = service['name']
        type = service['type']
        service_class = misc.get_rto_by_name(service['class'])

        if type == 'TCPListener':
            address = misc.get_address_from_string(service['address'])
            worker_processes = service['worker_processes']
            worker_threads = int(service['worker_threads'])
            svc = service_class(name, address, worker_processes,
                                worker_threads)
        elif type == 'ScheduledTask':
            interval = int(service['interval'])
            svc = service_class(name, interval)

        # add parameters
        if 'parameters' in service:
            svc.parameters = service['parameters']

        services.services_list.append(svc)

    # start services
    for service in services:
        service.start()
Пример #14
0
def init_session_manager(init_expiration=True):
    return SessionManager.open(
        misc.get_rto_by_name(settings["sessionmanager"]["interface"]),
        int(settings["sessionmanager"]["timeout"]),
        init_expiration,
        **settings["sessionmanager"]["params"]
    )
Пример #15
0
    def __init__(self, name, address, processes, threads):
        asyncserver.BaseServer.__init__(self, name, address, processes, threads, PorcupineThread)

        if self.is_multiprocess:
            # check if session manager supports multiple processes
            sm_class = misc.get_rto_by_name(settings["sessionmanager"]["interface"])
            if not sm_class.supports_multiple_processes:
                raise exceptions.ConfigurationError("The session manager class does not support " "multiple processes.")
Пример #16
0
def open(**kwargs):
    global _db_handle
    if _db_handle == None or not _db_handle.is_open():
        _db_handle = misc.get_rto_by_name(settings['store']['interface'])
        _db_handle.open(**kwargs)
        return True
    else:
        return False
    def __init__(self, classobj):
        self._class = misc.get_rto_by_name(classobj)
        self._bases = self._class.__bases__
        self.doc = self._class.__doc__
        self._attrs = {}
        self._methods = {}
        self._properties = {}
        try:
            sourcelines = inspect.getsourcelines(self._class)
            startline = sourcelines[1]
            endline = startline + len(sourcelines[0]) - 1
            self.boundaries = (startline, endline)
        except IOError:
            # the source file does not exist
            self.boundaries = None

        self._instance = self._class()

        # find instance attributes
        sub_attrs = []
        for base_class in self._class.__bases__:
            s = base_class()
            sub_attrs += s.__dict__.keys()
        for member_name in self._instance.__dict__:
            if not member_name in sub_attrs:
                self._attrs[member_name] = self._instance.__dict__[member_name]

        for member_name in self._class.__dict__:
            member = self._class.__dict__[member_name]
            if type(member) == types.FunctionType:
                self._methods[member_name] = member
            elif type(member) == property:
                self._properties[member_name] = member

        self._module = sys.modules[self._class.__module__]
        self._imports = {}

        moduledict = self._module.__dict__
        for x in moduledict:
            if type(moduledict[x]) == types.ModuleType:
                self._imports[moduledict[x]] = x
            elif callable(moduledict[x]) and (sys.modules[moduledict[x].__module__] != self._module):
                imported = misc.get_rto_by_name(moduledict[x].__module__ + "." + x)
                self._imports[imported] = x
Пример #18
0
 def __get_filters_list(self, context_node):
     filterList = context_node.getElementsByTagName('filter')
     filters = []
     for filterNode in filterList:
         type = filterNode.getAttribute('type')
         filter = [misc.get_rto_by_name(type), {}]
         for attr in filterNode.attributes.keys():
             filter[1][str(attr)] = filterNode.getAttribute(attr)
         filters.append(tuple(filter))
     return tuple(filters)
Пример #19
0
    def on_update(item, new_attr, old_attr):
        from porcupine.systemObjects import Composite
        # load objects
        dctObjects = {}
        for i, obj in enumerate(new_attr.value):
            if isinstance(obj, Composite):
                obj._pid = ':' + item._id
            elif isinstance(obj, str):
                obj = db._db.get_item(obj)
                new_attr.value[i] = obj
            else:
                raise exceptions.ContainmentError(
                    'Invalid object type "%s" in composition.' %
                    obj.__class__.__name__)
            dctObjects[obj._id] = obj

        # check containment
        composite_type = misc.get_rto_by_name(new_attr.compositeClass)

        if [
                obj for obj in dctObjects.values()
                if not isinstance(obj, composite_type)
        ]:
            raise exceptions.ContainmentError(
                'Invalid content class "%s" in composition.' %
                obj.get_contentclass())

        # get previous value
        if old_attr is not None:
            old_ids = set(old_attr.value)
        else:
            old_ids = set()

        new_ids = set([obj._id for obj in new_attr.value])

        # calculate removed composites
        lstRemoved = list(old_ids - new_ids)
        [
            CompositionEventHandler._removeComposite(db._db.get_item(id))
            for id in lstRemoved
        ]

        # calculate added composites
        lstAdded = list(new_ids - old_ids)
        for obj_id in lstAdded:
            db._db.handle_update(dctObjects[obj_id], None)
            db._db.put_item(dctObjects[obj_id])

        # calculate constant composites
        lstConstant = list(new_ids & old_ids)
        for obj_id in lstConstant:
            db._db.handle_update(dctObjects[obj_id], db._db.get_item(obj_id))
            db._db.put_item(dctObjects[obj_id])

        new_attr.value = list(new_ids)
 def on_update(item, new_attr, old_attr, trans):
     from porcupine.systemObjects import Composite
     # load objects
     dctObjects = {}
     for i, obj in enumerate(new_attr.value):
         if isinstance(obj, Composite):
             obj._containerid = item._id
         elif isinstance(obj, str):
             obj = _db.get_item(obj, trans)
             new_attr.value[i] = obj
         else:
             raise exceptions.ContainmentError, \
                 'Invalid object type "%s" in composition.' % \
                 obj.__class__.__name__
         dctObjects[obj._id] = obj
     
     # check containment
     composite_type = misc.get_rto_by_name(new_attr.compositeClass)
     
     if [obj for obj in dctObjects.values()
             if not isinstance(obj, composite_type)]:
         raise exceptions.ContainmentError, \
             'Invalid content class "%s" in composition.' % \
             obj.get_contentclass()
     
     # get previous value
     if old_attr != None:
         old_ids = set(old_attr.value)
     else:
         old_ids = set()
     
     new_ids = set([obj._id for obj in new_attr.value])
     
     # calculate added composites
     lstAdded = list(new_ids - old_ids)
     for obj_id in lstAdded:
         _db.handle_update(dctObjects[obj_id], None, trans)
         _db.put_item(dctObjects[obj_id], trans)
     
     # calculate constant composites
     lstConstant = list(new_ids & old_ids)
     for obj_id in lstConstant:
         _db.handle_update(dctObjects[obj_id],
                           _db.get_item(obj_id, trans),
                           trans)
         _db.put_item(dctObjects[obj_id], trans)
     
     # calculate removed composites
     lstRemoved = list(old_ids - new_ids)
     [CompositionEventHandler._removeComposite(_db.get_item(id, trans),
                                               trans)
      for id in lstRemoved]
             
     new_attr.value = list(new_ids)
Пример #21
0
    def __init__(self, name, address, processes, threads):
        asyncserver.BaseServer.__init__(self, name, address, processes,
                                        threads, PorcupineThread)

        if self.is_multiprocess:
            # check if session manager supports multiple processes
            sm_class = misc.get_rto_by_name(
                settings['sessionmanager']['interface'])
            if not sm_class.supports_multiple_processes:
                raise exceptions.ConfigurationError(
                    'The session manager class does not support '
                    'multiple processes.')
Пример #22
0
def open(**kwargs):
    global _db_handle, _indices
    pid = os.getpid()
    if not pid in _pids or not _db_handle.is_open():
        _db_handle = misc.get_rto_by_name(
            settings['store']['interface'])(**kwargs)
        _pids.append(pid)
        # update indexed attributes
        _indices = [x[0] for x in settings['store']['indices']]
        return True
    else:
        return False
Пример #23
0
def open(**kwargs):
    global _db_handle, _indices
    pid = os.getpid()
    if not pid in _pids or not _db_handle.is_open():
        _db_handle = misc.get_rto_by_name(settings['store']['interface'])\
                     (**kwargs)
        _pids.append(pid)
        # update indexed attributes
        _indices = [x[0] for x in settings['store']['indices']]
        return True
    else:
        return False
Пример #24
0
 def apply(context, item, registration, **kwargs):
     language = context.request.getLang()
     lst_resources = kwargs['using'].split(',')
     bundles = [misc.get_rto_by_name(x)
                for x in lst_resources]
     output = context.response._body.getvalue()
     tokens = frozenset(re.findall(I18n._tokens, output))
     for token, key in tokens:
         for bundle in bundles:
             res = bundle.getResource(key, language)
             if res != key:
                 break
         output = output.replace(token, res)
     context.response.clear()
     context.response.write(output)
 def _add_reference(attr, oid, trans):
     from porcupine.datatypes import Relator1, RelatorN
     ref_item = _db.get_item(attr.value, trans)
     if ref_item != None and isinstance(ref_item,
                                        tuple([misc.get_rto_by_name(cc)
                                               for cc in attr.relCc])):
         ref_attr = getattr(ref_item, attr.relAttr)
         if isinstance(ref_attr, RelatorN):
             ref_attr.value.append(oid)
         elif isinstance(ref_attr, Relator1):
             ref_attr.value = oid
         ref_attr.validate()
         _db.put_item(ref_item, trans)
     else:
         attr.value = None
Пример #26
0
    def _add_reference(attr, oid):
        from porcupine.datatypes import Relator1, RelatorN

        ref_item = db._db.get_item(attr.value)
        if ref_item is not None and isinstance(ref_item, tuple([misc.get_rto_by_name(cc) for cc in attr.relCc])):
            if attr.relAttr not in ref_item.__props__:
                raise AttributeError("'%s' object has no attribute '%s'" % (ref_item.__class__.__name__, attr.relAttr))
            ref_attr = getattr(ref_item, attr.relAttr)
            if isinstance(ref_attr, RelatorN):
                ref_attr.value.append(oid)
            elif isinstance(ref_attr, Relator1):
                ref_attr.value = oid
            ref_attr.validate()
            db._db.put_item(ref_item)
        else:
            attr.value = None
 def _getFullName(self, callable):
     if callable.__module__ == "__builtin__":
         return callable.__name__
     module = misc.get_rto_by_name(callable.__module__)
     if self._imports.has_key(module):
         return self._imports[module] + "." + callable.__name__
     else:
         if module == self._module:
             return callable.__name__
         local_name = callable.__module__.split(".")[-1]
         counter = 2
         while local_name in self._module.__dict__:
             local_name += str(counter)
             counter += 1
         self._imports[module] = local_name
         return local_name + "." + callable.__name__
Пример #28
0
 def apply(context, item, registration, **kwargs):
     language = context.request.get_lang()
     lst_resources = kwargs['using'].split(',')
     bundles = [misc.get_rto_by_name(x) for x in lst_resources]
     output = context.response._body.getvalue()
     tokens = frozenset(re.findall(I18n._tokens, output))
     for token, key in tokens:
         for bundle in bundles:
             res = bundle.get_resource(key.decode(), language)
             if res != key:
                 break
         if isinstance(res, bytes):
             # python 2.6
             res = res.decode('utf-8')
         output = output.replace(token,
                                 res.encode(context.response.charset))
     context.response.clear()
     context.response.write(output)
    def commitChanges(self, generate_code=True):
        from porcupine.oql.command import OqlCommand

        if self._setProps or self._removedProps:
            if generate_code:
                GenericSchemaEditor.commitChanges(self)
                # we must reload the class module
                oMod = misc.get_rto_by_name(self._class.__module__)
                reload(oMod)

            db = offlinedb.getHandle()
            oql_command = OqlCommand()
            rs = oql_command.execute("select * from deep('/') where instanceof('%s')" % self._instance.contentclass)
            try:
                if len(rs):
                    try:
                        for item in rs:
                            txn = db.get_transaction()
                            for name in self._removedProps:
                                if hasattr(item, name):
                                    delattr(item, name)
                            for name in self._setProps:
                                if not hasattr(item, name):
                                    # add new
                                    setattr(item, name, self._setProps[name])
                                else:
                                    # replace property
                                    old_value = getattr(item, name).value
                                    setattr(item, name, self._setProps[name])
                                    new_attr = getattr(item, name)
                                    if isinstance(new_attr, datatypes.Password):
                                        new_attr._value = old_value
                                    else:
                                        new_attr.value = old_value
                            if self.xform:
                                item = self.xform(item)
                            db.put_item(item, txn)
                            txn.commit()
                    except Exception, e:
                        txn.abort()
                        raise e
                        sys.exit(2)
            finally:
                offlinedb.close()
Пример #30
0
 def _add_reference(attr, oid):
     from porcupine.datatypes import Relator1, RelatorN
     ref_item = db._db.get_item(attr.value)
     if (ref_item is not None and isinstance(
             ref_item, tuple(
                 [misc.get_rto_by_name(cc) for cc in attr.relCc]))):
         if attr.relAttr not in ref_item.__props__:
             raise AttributeError(
                 "'%s' object has no attribute '%s'" %
                 (ref_item.__class__.__name__, attr.relAttr))
         ref_attr = getattr(ref_item, attr.relAttr)
         if isinstance(ref_attr, RelatorN):
             ref_attr.value.append(oid)
         elif isinstance(ref_attr, Relator1):
             ref_attr.value = oid
         ref_attr.validate()
         db._db.put_item(ref_item)
     else:
         attr.value = None
Пример #31
0
 def apply(context, item, registration, **kwargs):
     language = context.request.get_lang()
     lst_resources = kwargs['using'].split(',')
     bundles = [misc.get_rto_by_name(x)
                for x in lst_resources]
     output = context.response._body.getvalue()
     tokens = frozenset(re.findall(I18n._tokens, output))
     for token, key in tokens:
         for bundle in bundles:
             res = bundle.get_resource(key.decode(), language)
             if res != key:
                 break
         if isinstance(res, bytes):
             # python 2.6
             res = res.decode('utf-8')
         output = output.replace(
             token, res.encode(context.response.charset))
     context.response.clear()
     context.response.write(output)
Пример #32
0
    def apply(context, item, registration, **kwargs):
        if JSMerge.cache_folder is None:
            config = JSMerge.loadConfig()
            JSMerge.cache_folder = config['cache']
            if not(os.path.isdir(JSMerge.cache_folder)):
                os.makedirs(JSMerge.cache_folder)

        s_files = kwargs['files']
        if ',' in s_files:
            files = ([f.strip() for f in s_files.split(',')])
        else:
            files = misc.get_rto_by_name(s_files)()

        # check if output folder exists
        path = registration.path
        hash = misc.hash(*files).hexdigest()

        merged = path.replace(os.path.sep, '_')
        if os.name == 'nt':
            merged = merged.replace(os.path.altsep, '_').replace(':', '')

        glob_f = '%s/%s' % (JSMerge.cache_folder, merged)
        merged = '%s#%s.merge.js' % (glob_f, hash)

        revision = int(context.request.queryString['r'][0])

        JSMerge.lock.acquire()
        try:
            if (not os.path.isfile(merged) or
                    os.path.getmtime(merged) < revision):
                # remove old merged files
                oldfiles = glob.glob(glob_f + '*')
                [os.remove(old) for old in oldfiles]
                # generate new
                f = open(merged, 'w')
                for fname in files:
                    f.write(open(fname, 'r').read() + '\n')
                f.close()
        finally:
            JSMerge.lock.release()

        registration.context = merged
Пример #33
0
    def apply(context, item, registration, **kwargs):
        if JSMerge.cache_folder is None:
            config = JSMerge.loadConfig()
            JSMerge.cache_folder = config['cache']
            if not (os.path.isdir(JSMerge.cache_folder)):
                os.makedirs(JSMerge.cache_folder)

        s_files = kwargs['files']
        if ',' in s_files:
            files = ([f.strip() for f in s_files.split(',')])
        else:
            files = misc.get_rto_by_name(s_files)()

        # check if output folder exists
        path = registration.path
        hash = misc.hash(*files).hexdigest()

        merged = path.replace(os.path.sep, '_')
        if os.name == 'nt':
            merged = merged.replace(os.path.altsep, '_').replace(':', '')

        glob_f = '%s/%s' % (JSMerge.cache_folder, merged)
        merged = '%s#%s.merge.js' % (glob_f, hash)

        revision = int(context.request.queryString['r'][0])

        JSMerge.lock.acquire()
        try:
            if (not os.path.isfile(merged)
                    or os.path.getmtime(merged) < revision):
                # remove old merged files
                oldfiles = glob.glob(glob_f + '*')
                [os.remove(old) for old in oldfiles]
                # generate new
                f = open(merged, 'w')
                for fname in files:
                    f.write(open(fname, 'r').read() + '\n')
                f.close()
        finally:
            JSMerge.lock.release()

        registration.context = merged
Пример #34
0
    def _get_full_name(self, callable):
        # if it is built-in just return its name
        if callable.__module__ == None.__class__.__module__:
            return callable.__name__

        module = misc.get_rto_by_name(callable.__module__)
        if module in self._imports:
            # the callables module is already imported
            return self._imports[module] + '.' + callable.__name__
        else:
            if module == self._module:
                # the callable belongs to the current module
                return callable.__name__

            # the callable is not imported
            local_name = callable.__module__.split('.')[-1]
            #counter = 2
            #while local_name in self._module.__dict__:
            #    local_name += str(counter)
            #    counter += 1
            self._imports[module] = local_name
            return local_name + '.' + callable.__name__
Пример #35
0
    def _get_full_name(self, callable):
        # if it is built-in just return its name
        if callable.__module__ == None.__class__.__module__:
            return callable.__name__

        module = misc.get_rto_by_name(callable.__module__)
        if module in self._imports:
            # the callables module is already imported
            return self._imports[module] + "." + callable.__name__
        else:
            if module == self._module:
                # the callable belongs to the current module
                return callable.__name__

            # the callable is not imported
            local_name = callable.__module__.split(".")[-1]
            # counter = 2
            # while local_name in self._module.__dict__:
            #    local_name += str(counter)
            #    counter += 1
            self._imports[module] = local_name
            return local_name + "." + callable.__name__
Пример #36
0
def init_session_manager(init_expiration=True):
    return SessionManager.open(
        misc.get_rto_by_name(settings['sessionmanager']['interface']),
        int(settings['sessionmanager']['timeout']), init_expiration,
        **settings['sessionmanager']['params'])
Пример #37
0
 def get_filter_by_type(self, type):
     if isinstance(type, basestring):
         type = misc.get_rto_by_name(type)
     filter = [f for f in self.filters
               if f[0] == type][0]
     return filter
Пример #38
0
    def execute_command(self, cmd, request):
        try:
            # DB maintenance commands
            if cmd == 'DB_BACKUP':
                output_file = request.data
                if not os.path.isdir(os.path.dirname(output_file)):
                    raise IOError
                services.lock_db()
                try:
                    _db.backup(output_file)
                finally:
                    services.unlock_db()
                return (0, 'Database backup completed successfully.')

            elif cmd == 'DB_RESTORE':
                backup_set = request.data
                if not os.path.exists(backup_set):
                    raise IOError
                services.lock_db()
                services.close_db()
                try:
                    _db.restore(backup_set)
                finally:
                    services.open_db()
                    services.unlock_db()
                return (0, 'Database restore completed successfully.')

            elif cmd == 'DB_SHRINK':
                iLogs = _db.shrink()
                if iLogs:
                    return (0, 'Successfully removed %d log files.' % iLogs)
                else:
                    return (0, 'No log files removed.')

            # package management commands
            elif cmd == 'PACKAGE':
                ini_file = request.data
                my_pkg = None
                try:
                    my_pkg = Package(ini_file=ini_file)
                    my_pkg.create()
                finally:
                    if my_pkg is not None:
                        my_pkg.close()
                return (0, 'The package "%s-%s.ppf" was created succefully.'
                        % (my_pkg.name, my_pkg.version))

            elif cmd == 'INSTALL':
                # some scripts might require a security context
                from porcupine import context
                ppf_file = request.data
                my_pkg = None
                try:
                    my_pkg = Package(package_file=ppf_file)
                    # install as system
                    context.user = _db.get_item('system')
                    my_pkg.install()
                finally:
                    if my_pkg is not None:
                        my_pkg.close()
                    context.user = None
                return (0, 'The package "%s" was installed succefully.'
                        % ppf_file)

            elif cmd == 'UNINSTALL':
                # some scripts might require a security context
                from porcupine import context
                ppf_file = request.data
                my_pkg = None
                try:
                    my_pkg = Package(package_file=ppf_file)
                    # uninstall as system
                    context.user = _db.get_item('system')
                    my_pkg.uninstall()
                finally:
                    if my_pkg is not None:
                        my_pkg.close()
                    context.user = None
                return (0, 'The package "%s" was uninstalled succefully.'
                        % ppf_file)

            # replication commands
            elif cmd == 'SITE_INFO':
                rep_mgr = _db.get_replication_manager()
                if rep_mgr is not None:
                    site_list = list(rep_mgr.get_site_list().values())
                    site_list.append(
                        rep_mgr.local_site.address + (1,))
                    #print site_list
                    info = [str.format('{0:25}{1:10}{2:6}',
                                      'SITE', 'STATUS', 'MASTER'),
                            '-' * 41]
                    for site in site_list:
                        site_address = site[:2]
                        if site[2] == 1:
                            s = 'ONLINE'
                        else:
                            s = 'OFFLINE'
                        if rep_mgr.master and \
                                rep_mgr.master.address == site_address:
                            m = 'X'
                        else:
                            m = ''
                        info.append(str.format('{0:25}{1:10}{2:6}',
                                               site_address, s, m))

                    info.append('')
                    info.append('Total sites: %d' % len(site_list))
                    return (0, '\n'.join(info))
                else:
                    raise NotImplementedError

            elif cmd == 'REP_JOIN_SITE':
                rep_mgr = _db.get_replication_manager()
                if rep_mgr is not None:
                    site = request.data
                    #print('adding remote site %s' % (site.address, ))
                    site_list = rep_mgr.sites.values() + [rep_mgr.local_site]
                    rep_mgr.broadcast(MgtMessage('REP_ADD_REMOTE_SITE', site))
                    rep_mgr.add_remote_site(site)
                    return (0, [rep_mgr.master, site_list])
                else:
                    raise NotImplementedError

            elif cmd == 'REP_ADD_REMOTE_SITE':
                rep_mgr = _db.get_replication_manager()
                if rep_mgr is not None:
                    site = request.data
                    #print('adding remote site %s' % (site.address, ))
                    rep_mgr.add_remote_site(site)
                    return (0, None)
                else:
                    raise NotImplementedError

            elif cmd == 'REP_NEW_MASTER':
                rep_mgr = _db.get_replication_manager()
                if rep_mgr is not None:
                    master = request.data
                    #print('new master is %s' % (master.address, ))
                    rep_mgr.master = master
                    services.notify(('NEW_MASTER', master))
                    return (0, None)
                else:
                    raise NotImplementedError

            # other
            elif cmd == 'RELOAD':
                mod = misc.get_rto_by_name(request.data)
                misc.reload_module_tree(mod)
                services.notify(('RELOAD_PACKAGE', request.data))
                return (0, 'Reloaded module tree "%s"' % request.data)

            # unknown command
            else:
                logger.warning(
                    'Management service received unknown command: %s' % cmd)
                return (-1, 'Unknown command.')

        except IOError:
            return (-1, 'Invalid file path.')
        except NotImplementedError:
            return (-1, 'Unsupported command.')
    def execute_command(self, cmd, request):
        try:
            # DB maintenance commands
            if cmd == 'DB_BACKUP':
                output_file = request.data
                if not os.path.isdir(os.path.dirname(output_file)):
                    raise IOError
                services.lock_db()
                try:
                    _db.backup(output_file)
                finally:
                    services.unlock_db()
                return (0, 'Database backup completed successfully.')

            elif cmd == 'DB_RESTORE':
                backup_set = request.data
                if not os.path.exists(backup_set):
                    raise IOError
                services.lock_db()
                services.close_db()
                try:
                    _db.restore(backup_set)
                finally:
                    services.open_db()
                    services.unlock_db()
                return (0, 'Database restore completed successfully.')

            elif cmd == 'DB_SHRINK':
                iLogs = _db.shrink()
                if iLogs:
                    return (0, 'Successfully removed %d log files.' % iLogs)
                else:
                    return (0, 'No log files removed.')

            # package management commands
            elif cmd == 'PACKAGE':
                ini_file = request.data
                my_pkg = None
                try:
                    my_pkg = Package(ini_file=ini_file)
                    my_pkg.create()
                finally:
                    if my_pkg is not None:
                        my_pkg.close()
                return (0, 'The package "%s-%s.ppf" was created succefully.'
                        % (my_pkg.name, my_pkg.version))

            elif cmd == 'INSTALL':
                # some scripts might require a security context
                from porcupine import context
                ppf_file = request.data
                my_pkg = None
                try:
                    my_pkg = Package(package_file=ppf_file)
                    # install as system
                    context.user = _db.get_item('system')
                    my_pkg.install()
                finally:
                    if my_pkg is not None:
                        my_pkg.close()
                    context.user = None
                return (0, 'The package "%s" was installed succefully.'
                        % ppf_file)

            elif cmd == 'UNINSTALL':
                # some scripts might require a security context
                from porcupine import context
                ppf_file = request.data
                my_pkg = None
                try:
                    my_pkg = Package(package_file=ppf_file)
                    # uninstall as system
                    context.user = _db.get_item('system')
                    my_pkg.uninstall()
                finally:
                    if my_pkg is not None:
                        my_pkg.close()
                    context.user = None
                return (0, 'The package "%s" was uninstalled succefully.'
                        % ppf_file)

            # replication commands
            elif cmd == 'SITE_INFO':
                rep_mgr = _db.get_replication_manager()
                if rep_mgr is not None:
                    site_list = list(rep_mgr.get_site_list().values())
                    site_list.append(
                        rep_mgr.local_site.address + (1,))
                    #print site_list
                    info = [str.format('{0:25}{1:10}{2:6}',
                                      'SITE', 'STATUS', 'MASTER'),
                            '-' * 41]
                    for site in site_list:
                        site_address = site[:2]
                        if site[2] == 1:
                            s = 'ONLINE'
                        else:
                            s = 'OFFLINE'
                        if rep_mgr.master and \
                                rep_mgr.master.address == site_address:
                            m = 'X'
                        else:
                            m = ''
                        info.append(str.format('{0:25}{1:10}{2:6}',
                                               site_address, s, m))

                    info.append('')
                    info.append('Total sites: %d' % len(site_list))
                    return (0, '\n'.join(info))
                else:
                    raise NotImplementedError

            elif cmd == 'REP_JOIN_SITE':
                rep_mgr = _db.get_replication_manager()
                if rep_mgr is not None:
                    site = request.data
                    #print('adding remote site %s' % (site.address, ))
                    site_list = rep_mgr.sites.values() + [rep_mgr.local_site]
                    rep_mgr.broadcast(MgtMessage('REP_ADD_REMOTE_SITE', site))
                    rep_mgr.add_remote_site(site)
                    return (0, [rep_mgr.master, site_list])
                else:
                    raise NotImplementedError

            elif cmd == 'REP_ADD_REMOTE_SITE':
                rep_mgr = _db.get_replication_manager()
                if rep_mgr is not None:
                    site = request.data
                    #print('adding remote site %s' % (site.address, ))
                    rep_mgr.add_remote_site(site)
                    return (0, None)
                else:
                    raise NotImplementedError

            elif cmd == 'REP_NEW_MASTER':
                rep_mgr = _db.get_replication_manager()
                if rep_mgr is not None:
                    master = request.data
                    #print('new master is %s' % (master.address, ))
                    rep_mgr.master = master
                    services.notify(('NEW_MASTER', master))
                    return (0, None)
                else:
                    raise NotImplementedError

            # other
            elif cmd == 'RELOAD':
                mod = misc.get_rto_by_name(request.data)
                misc.reload_module_tree(mod)
                services.notify(('RELOAD_PACKAGE', request.data))
                return (0, 'Reloaded module tree "%s"' % request.data)

            # unknown command
            else:
                logger.warning(
                    'Management service received unknown command: %s' % cmd)
                return (-1, 'Unknown command.')

        except IOError:
            return (-1, 'Invalid file path.')
        except NotImplementedError:
            return (-1, 'Unsupported command.')
Пример #40
0
def h_66(params, variables, for_object):
    className = evaluate_stack(params[0][:], variables, for_object)
    return isinstance(for_object, misc.get_rto_by_name(className))
Пример #41
0
def h_66(params, variables, for_object):
    className = evaluate_stack(params[0][:], variables, for_object)
    return isinstance(for_object, misc.get_rto_by_name(className))
def init_session_manager(init_expiration=True):
    return SessionManager.open(
        misc.get_rto_by_name(settings['sessionmanager']['interface']),
        int(settings['sessionmanager']['timeout']),
        init_expiration,
        **settings['sessionmanager']['params'])