Ejemplo n.º 1
0
 def _create_user_mount(self, root_mount, mount_name, mount_url):
     'create a new user mount given a url'
     store = etree.Element('store',
                           name=mount_name,
                           resource_unid=mount_name,
                           value=mount_url)
     data_service.new_resource(store, parent=root_mount)
Ejemplo n.º 2
0
    def create(self, **kw):
        query = ''
        type_ = kw.pop('type', None)

        if type_:
            resource = data_service.new_resource(type_, **kw)

            # Choose widgets to instantiate on the viewer by the type
            # of the resource.
            log.debug('created %s  -> %s' % (type_, uri))
            return dict(resource=resource
                        )  #, tag_views=None, wpublic = None, search = None)
        raise IllegalOperation("create operation requires type")
Ejemplo n.º 3
0
    def _create_resource(self, resource):
        'create a resource from a blob and return new resource'
        # hashed filename + stuff

        perm = resource.get('permission', 'private')
        filename = resource.get('name')
        if resource.tag == 'resource':  # requires type guessing
            resource.set(
                'resource_type',
                resource.get('resource_type') or self.guess_type(filename))
        if resource.get('resource_uniq') is None:
            resource.set('resource_uniq', data_service.resource_uniq())
        else:
            pass
        ts = resource.get('ts') or datetime.now().isoformat(' ')

        # KGK
        # These are redundant (filename is the attribute name name upload is the ts
        # dima: today needed for organizer to work
        resource.insert(
            0,
            etree.Element('tag',
                          name="filename",
                          value=filename,
                          permission=perm))
        resource.insert(
            1,
            etree.Element(
                'tag',
                name="upload_datetime",
                value=ts,
                type='datetime',
                permission=perm,
            ))

        if resource.get('resource_uniq') is None:
            resource.set('resource_uniq', data_service.resource_uniq())
        log.info("INSERTING NEW RESOURCE <= %s", etree.tostring(resource))
        new_resource = data_service.new_resource(resource=resource,
                                                 flush=False)
        return new_resource
Ejemplo n.º 4
0
    def create(self, args):
        ''' GET /connoisseur/create/template:CaffeNet[/name:SeaCreatures][/dataset:UUID]
        '''
        template = args.get('template')
        dataset = args.get('dataset')
        name = args.get('name')
        log.debug('Create model from: %s', template)

        tmpls = self.get_templates()
        if template not in tmpls:
            raise ConnoisseurException(
                responses.NOT_FOUND,
                'Requested template "%s" not found' % template)
        path_template = tmpls[template]
        model = etree.fromstring(self.get_template(template))

        if name is not None:
            model.set('name', name)

        if dataset is not None:
            t = model.find('tag[@name="training_set"]')
            if t is not None:
                t.set('value', '%s' % dataset)

        # next post the model document and get its UUID
        model = data_service.new_resource(resource=model)

        # finally copy all of the template files over to model storage
        uniq = model.get('resource_uniq')
        path_model = os.path.join(self.path_models, uniq)
        _mkdir(path_model)
        log.debug('Creating model in: %s', path_model)
        self.copy_dir(path_template, path_model)

        response.headers['Content-Type'] = 'text/xml'
        return etree.tostring(model)
Ejemplo n.º 5
0
    def _create_full_path(self,
                          store,
                          path,
                          resource_uniq=None,
                          resource_name=None,
                          count=1,
                          **kw):
        """Create the full path relative to store
        @param store: a string name or etreeElement
        @param path: a path relative to the store
        @param resource_uniq: optional resource to be placed
        @param resource_name: options name of resource
        """

        if isinstance(path, basestring):
            path = path.split('/')
        path = list(path)
        root = None
        log.debug("CREATE_PATH %s %s", store, path)
        if isinstance(store, basestring):
            resource = root = etree.Element('store',
                                            name=store,
                                            resource_unid=store)
            store = self._load_root_mount()

        # Scan path for all existing directories and possible existing filelink
        parent = store
        while parent is not None and path:
            el = path.pop(0)
            if not el:
                continue
            #q  = data_service.query(parent=parent, resource_unid=el, view='full', cache=False)
            q = data_service.query(parent=parent,
                                   resource_unid=el,
                                   view='short',
                                   cache=False)
            if len(q) == 0:
                # no element we are done
                path.insert(0, el)
                break
            if len(q) > 1:
                log.error('multiple names (%s) in store level %s', el,
                          q.get('uri'))
                #path.insert(0, el)
                parent = q[0]
                return None
                #break # return Fail?
            # len(q) == 1 .. we have a result .. just keep searching down the path
            parent2 = parent
            parent = q[0]
        # directories do not exists len(path) > 1
        # directories exist but filelink does not len(path) == 1
        # directories and filelink exists len(path) == 0

        log.debug("create: at %s rest %s", (parent is not None)
                  and parent.get('uri'), path)
        while len(path) > 1:
            # any left over path needs to be created
            nm = path.pop(0)
            if root is None:
                resource = root = etree.Element('dir',
                                                name=nm,
                                                resource_unid=nm)
            else:
                resource = etree.SubElement(resource,
                                            'dir',
                                            name=nm,
                                            resource_unid=nm)
        # The last element might be dir or a link
        if len(path) == 1:
            nm = resource_name or path.pop(0)
            #nm = path.pop(0)
            if root is None:
                resource = root = etree.Element(
                    'link' if resource_uniq else 'dir',
                    name=nm,
                    resource_unid=nm)
            else:
                resource = etree.SubElement(resource,
                                            'link' if resource_uniq else 'dir',
                                            name=nm,
                                            resource_unid=nm)

            if resource_uniq:
                resource.set('value', resource_uniq)
            # create the new resource
            log.debug("New resource %s at %s ", etree.tostring(root),
                      (parent is not None) and parent.get('uri'))
            q = data_service.new_resource(resource=root,
                                          parent=parent,
                                          flush=False)
        elif len(path) == 0:
            log.warn("NAME conflict? %s %s", resource_name, count)
            # Conflict in link name?  Possible when mutltfile name has not be disambiguated by underlying filesystem
            nm = "%s-%s" % (resource_name, count)
            if root is None:
                resource = root = etree.Element(
                    'link' if resource_uniq else 'dir',
                    name=nm,
                    resource_unid=nm)
            else:
                resource = etree.SubElement(resource,
                                            'link' if resource_uniq else 'dir',
                                            name=nm,
                                            resource_unid=nm)
            if resource_uniq:
                resource.set('value', resource_uniq)
            q = data_service.new_resource(resource=root,
                                          parent=parent2,
                                          flush=False)

        return q
Ejemplo n.º 6
0
    def run(self):

        load_config(self.options.config)
        from lxml import etree
        from tg import config, session, request
        from bq import data_service
        from bq.core.identity import set_admin_mode
        import transaction
        load_bisque_services()

        prefs = config_path('preferences.xml')

        set_admin_mode(True)
        if self.args[0].startswith('init'):
            x = data_service.query('system')
            if len(x):
                if self.options.force:
                    print("deleting current system object")
                    data_service.del_resource(x[0])
                else:
                    print("NO ACTION: System object initialized at %s " %
                          etree.tostring(x[0]))
                    sys.exit(1)

            if os.path.exists(prefs):
                if self.options.force:
                    print("deleting %s" % prefs)
                    os.remove(prefs)
                else:
                    print('NO ACTION: %s exists.. cannot init' % prefs)
                    sys.exit(1)

            system = etree.parse(
                defaults_path('preferences.xml.default')).getroot()
            for el in system.getiterator(tag=etree.Element):
                el.set('permission', 'published')
            system = data_service.new_resource(system, view='deep')
        elif self.args[0].startswith('read'):
            system = data_service.query('system', view='deep')
            if len(system):
                system = system[0]
            else:
                system = None
        else:
            if not os.path.exists(prefs):
                print "Need %s" % prefs
                return
            system = etree.parse(prefs).getroot()
            # Esnure all elements are published
            for el in system.getiterator(tag=etree.Element):
                el.set('permission', 'published')
            # Read system object
            uri = system.get('uri')
            print 'system = %s' % etree.tostring(system)

            system = data_service.update_resource(new_resource=system,
                                                  resource=uri,
                                                  view='deep')
            print etree.tostring(system)
        transaction.commit()
        if system is not None:
            with open(prefs, 'w') as f:
                f.write(etree.tostring(system, pretty_print=True))
                print "Wrote %s" % prefs