Esempio n. 1
0
    def do_install(self, line):
        print 'install  - Installs engine\n'

        try:
            path, obj_name, email = line.split(' ')
            developer = User.objects(email=email).get()

            __import__(path)
            module = sys.modules[path]

            #   Skip module if the class name not located or is not a class
            if not hasattr(module, obj_name):
                print '[Error] Unable to get {} from {}, exiting...'.format(
                    obj_name, path)
                return

            obj = getattr(module, obj_name)
            if type(obj) != type:
                print '[Error] {} is not the right type, exiting...'.format(
                    obj_name)
                return

            e = obj(DBManager, -1, -1)
            if not isinstance(e, AbstractEngine):
                print '[Error] {} is not an AbstractEngine, exiting...'.format(
                    obj_name)
                return

            e.install()
            engine = Engine(name=e.name,
                            description=e.description,
                            path=path,
                            obj_name=obj_name,
                            developer=developer,
                            active=True)
            engine.save()
            print 'Engine added to FIRST'
            return

        except ValueError as e:
            print e

        except ImportError as e:
            print e

        print(
            'Usage: \n'
            'install <pythonic path> <class name> <developer email>\n'
            '\n'
            'Example of pythonic path: app.first.engines.exact_match\n')
Esempio n. 2
0
    def unapplied(self, sample, user, _id, is_engine=False):
        '''
        @returns Boolean. True if not in metadata's applied list
                            False if still in the applied list
        '''
        if (not isinstance(sample, Sample)) or (not isinstance(user, User)):
            return False

        key = [str(sample.id), str(user.id)]
        if is_engine:
            engine_id = bson.objectid.ObjectId(_id)
            engine = Engine.objects(id=engine_id, applied__contains=key)

            #   Check if user has already applied the signature
            if not len(engine):
                return True

            try:
                engine = Engine.objects(id=engine_id).get()
            except DoesNotExist:
                #   Engine does not exist
                return False

            engine.applied.remove(key)
            engine.save()

        else:
            metadata_id = bson.objectid.ObjectId(_id)
            functions = Function.objects(metadata__id=metadata_id,
                                         metadata__applied__contains=key)

            #   Check if user does not have it applied already
            if not len(functions):
                return True

            try:
                function = functions.get()
            except DoesNotExist:
                #   Metadata does not exist
                return True

            #   Get metadata
            for metadata in function.metadata:
                if metadata.id == metadata_id:
                    metadata.applied.remove(key)
                    break

            function.save()

        return True
Esempio n. 3
0
    def get_metadata_list(self, metadata):
        results = []
        user_metadata, engine_metadata = self.separate_metadata(metadata)

        metadata_ids = map(bson.objectid.ObjectId, user_metadata)
        mongo_filter = Q(metadata__id=metadata_ids[0])
        for mid in metadata_ids[1:]:
            mongo_filter |= Q(metadata__id=mid)

        matches = Function.objects.filter(mongo_filter).only('metadata')
        for function in matches:
            for metadata in function.metadata:
                if metadata.id in metadata_ids:
                    data = metadata.dump()
                    data['id'] = str(metadata.id)
                    results.append(data)

                    #   Remove id from list to shorten list
                    del metadata_ids[metadata_ids.index(metadata.id)]

        for _id in engine_metadata:
            engines = Engine.object(id=_id)
            if (not engines) or (len(engines) > 1):
                continue

            data = {
                'id': _id,
                'engine': engine.name,
                'description': engine.description
            }
            results.append(data)

        return results
Esempio n. 4
0
    def _get_db_engine_obj(self, name):
        engine = Engine.objects(name=name)
        if not engine:
            print 'Unable to locate Engine "{}"'.format(name)
            return

        if len(engine) > 1:
            print 'More than one engine "{}" exists'.format(name)
            for e in engine:
                print ' - {}'.format(e.name)

            return

        return engine.get()
Esempio n. 5
0
    def get_engine(self, engine_id):
        engines = Engine.objects(id=engine_id)
        if not engines:
            return None

        return engines[0]
Esempio n. 6
0
 def engines(self, active=True):
     return Engine.objects(active=bool(active))