class DocumentRESTTest(unittest.TestCase):
    """
    Test base metaclasses 
    """

    def setUp(self):
        """
        Load data from previous tests and setup test data
        :return:
        """
        #lbjson_test.TestJSON.setUp(self)

        rest_url = 'http://api.brlight.net/api'
        self.base_rest = BaseREST(rest_url)
        self.base = self.base_rest.get('expedido')
        self.doc_rest = DocumentREST(rest_url, self.base)

    def test_get_doc(self):
        document = self.doc_rest.get(1)

        document_type = self.base.metaclass()
        assert isinstance(document, document_type)

        with open('/tmp/document.json', 'w+') as f:
            f.write(document2json(self.base, document))

    def test_get_doc_colletion(self):

        collection = self.doc_rest.get_collection()
        assert isinstance(collection, Collection)
        assert isinstance(collection.results[0], self.base.metaclass())
        print (collection.results[0]._metadata)
        from liblightbase.lbutils.conv import document2json
        print(document2json(self.base, collection.results[0]))
        raise Exception('efewfv')
Exemplo n.º 2
0
 def __init__(self, nm_base, rest_url=None, response_object=False):
     self.base_nm = nm_base
     if rest_url is None:
         self.rest_url = config.REST_URL
     else:
         self.rest_url = rest_url
     self.base_reports = base_reports.ReportsBase(nm_base, self.rest_url)
     self.base = self.base_reports.lbbase
     self.documentrest = DocumentREST(self.rest_url, self.base,
                                      response_object)
Exemplo n.º 3
0
 def __init__(self, nm_base, rest_url=None, response_object=True):
     self.base_nm = nm_base
     if rest_url is None:
         self.rest_url = config.REST_URL
     else:
         self.rest_url = rest_url
     self.coleta_manual_base = coleta_manual.ColetaManualBase(
         nm_base, self.rest_url)
     self.base = self.coleta_manual_base.lbbase
     self.documentrest = DocumentREST(self.rest_url, self.base,
                                      response_object)
    def setUp(self):
        """
        Load data from previous tests and setup test data
        :return:
        """
        #lbjson_test.TestJSON.setUp(self)

        rest_url = 'http://api.brlight.net/api'
        self.base_rest = BaseREST(rest_url)
        self.base = self.base_rest.get('expedido')
        self.doc_rest = DocumentREST(rest_url, self.base)
Exemplo n.º 5
0
 def __init__(self, rest_url=None):
     """
     Método construtor da base de atividades
     :param rest_url: URL do Rest
     """
     if rest_url is None:
         self.rest_url = config.REST_URL
     else:
         self.rest_url = rest_url
     self.baserest = BaseREST(rest_url=self.rest_url, response_object=False)
     self.documentrest = DocumentREST(rest_url=self.rest_url,
             base=self.lbbase, response_object=False)
     here = os.path.abspath(os.path.dirname(__file__))
     data_dir = os.path.join(here, '../data/')
     self.data_dir = data_dir
Exemplo n.º 6
0
 def __init__(self, rest_url=None):
     """
     Método construtor
     """
     if rest_url is None:
         self.rest_url = config.REST_URL
     else:
         self.rest_url = rest_url
     self.baserest = BaseREST(rest_url=self.rest_url, response_object=False)
     self.documentrest = DocumentREST(rest_url=self.rest_url,
             base=self.lbbase, response_object=False)
Exemplo n.º 7
0
class DescriptionsBase(object):
    """
    Classe para a base de atividades
    """

    def __init__(self, rest_url=None):
        """
        Método construtor da base de atividades
        :param rest_url: URL do Rest
        """
        if rest_url is None:
            self.rest_url = config.REST_URL
        else:
            self.rest_url = rest_url
        self.baserest = BaseREST(rest_url=self.rest_url, response_object=False)
        self.documentrest = DocumentREST(rest_url=self.rest_url,
                base=self.lbbase, response_object=False)
        here = os.path.abspath(os.path.dirname(__file__))
        data_dir = os.path.join(here, '../data/')
        self.data_dir = data_dir

    @property
    def lbbase(self):
        """
        Base no LB
        :return: LB Base object
        """
        win32_physicalmemory_memorytype_list = Content()

        win32_physicalmemory_memorytype_key = Field(**dict(
            name='win32_physicalmemory_memorytype_key',
            description='Chave do atributo MemoryType',
            alias='win32_physicalmemory_memorytype_key',
            datatype='Integer',
            indices=['Ordenado'],
            multivalued=False,
            required=False
        ))

        win32_physicalmemory_memorytype_list.append(win32_physicalmemory_memorytype_key)

        win32_physicalmemory_memorytype_value = Field(**dict(
            name='win32_physicalmemory_memorytype_value',
            description='Valro do atributo MemoryType',
            alias='win32_physicalmemory_memorytype_value',
            datatype='Text',
            indices=['Textual', 'Ordenado'],
            multivalued=False,
            required=False
        ))

        win32_physicalmemory_memorytype_list.append(win32_physicalmemory_memorytype_value)

        win32_physicalmemory_memorytype_metadata = GroupMetadata(**dict(
            name='win32_physicalmemory_memorytype',
            alias='win32_physicalmemory_memorytype',
            description='Descrição do campo win32_physicalmemory_memorytype',
            multivalued=True
        ))

        win32_physicalmemory_memorytype = Group(
            metadata=win32_physicalmemory_memorytype_metadata,
            content=win32_physicalmemory_memorytype_list
        )

        win32_processor_family_list = Content()

        win32_processor_family_key = Field(**dict(
            name='win32_processor_family_key',
            description='Chave do atributo MemoryType',
            alias='win32_processor_family_key',
            datatype='Integer',
            indices=['Ordenado'],
            multivalued=False,
            required=False
        ))

        win32_processor_family_list.append(win32_processor_family_key)

        win32_processor_family_value = Field(**dict(
            name='win32_processor_family_value',
            description='Valor do atributo Family',
            alias='win32_processor_family_value',
            datatype='Text',
            indices=['Textual', 'Ordenado'],
            multivalued=False,
            required=False
        ))

        win32_processor_family_list.append(win32_processor_family_value)

        win32_processor_family_metadata = GroupMetadata(**dict(
            name='win32_processor_family',
            alias='win32_processor_family',
            description='Descrição do campo win32_processor_family',
            multivalued=True
        ))

        win32_processor_family = Group(
            metadata=win32_processor_family_metadata,
            content=win32_processor_family_list
        )

        content_list = Content()
        content_list.append(win32_physicalmemory_memorytype)
        content_list.append(win32_processor_family)

        base_metadata = BaseMetadata(
            name='descriptions',
            description='Field descriptions'
        )

        lbbase = Base(
            metadata=base_metadata,
            content=content_list
        )

        return lbbase

    @property
    def metaclass(self):
        """
        Retorna metaclass para essa base
        """
        return self.lbbase.metaclass()

    def create_base(self):
        """
        Cria base no LB
        """
        try:
            response = self.baserest.create(self.lbbase)
            return True
        except HTTPError:
            raise IOError('Error inserting base in LB')

    def remove_base(self):
        """
        Remove base from Lightbase
        :param lbbase: LBBase object instance
        :return: True or Error if base was not excluded
        """
        url = self.rest_url + '/' + self.lbbase.metadata.name
        response = requests.delete(url)

        #response = self.baserest.delete(self.lbbase)

        return response

    def is_created(self):
        """
        Retorna se a base já existe
        """
        try:
            response = self.baserest.get(self.lbbase.metadata.name)
            return True
        except HTTPError:
            return False

    def load_static(self):
        """
        Carrega valores dos grupos do arquivo JSON contido no Projeto
        :return:
        """
        # TODO: Coloca esses grupos e um valor definido pelo usuario
        groups_list = [
            'win32_physicalmemory_memorytype',
            'win32_processor_family'
        ]

        saida = dict()
        for group in groups_list:
            saida[group] = list()
            # Carrega valor do JSON para cada um ds grupos
            filename = self.data_dir + group + ".json"
            log.debug("Caminho completo do arquivo para carga: %s", filename)
            with open(filename, 'r', encoding='utf-8') as fd:
                value = json.loads(fd.read())
            #log.debug(value)
            for elm in value:
                #log.debug("111111111111111111111111111111")
                #log.debug(elm)
                # Faz o parsing do JSON por linha
                for key in elm.keys():
                    saida[group].append({
                        group + '_key': int(key),
                        group + '_value': elm[key]
                    })

        # Valida documento
        #log.debug(saida)
        document = conv.dict2document(self.lbbase, saida)
        document_json = conv.document2json(self.lbbase, document)
        log.debug(document_json)

        # FIXME: Verificar para que serve a remoção da base.
        # Recria base e insere um documento
        #if self.is_created():
        #    self.remove_base()
        #self.create_base()
        self.documentrest.create(document_json)
Exemplo n.º 8
0
 def liblightbase_documentrest(self, basename):
     if basename in self._liblightbase_DocumentREST:
         return self._liblightbase_DocumentREST[basename]
     self._liblightbase_DocumentREST[basename] = DocumentREST(
         URL_LB_API, self.liblightbase_schemes_df(basename))
     return self._liblightbase_DocumentREST[basename]
Exemplo n.º 9
0
class ConfReports(object):
    def __init__(self, nm_base, rest_url=None, response_object=False):
        self.base_nm = nm_base
        if rest_url is None:
            self.rest_url = config.REST_URL
        else:
            self.rest_url = rest_url
        self.base_reports = base_reports.ReportsBase(nm_base, self.rest_url)
        self.base = self.base_reports.lbbase
        self.documentrest = DocumentREST(self.rest_url, self.base,
                                         response_object)

    def get_base(self):
        """
        Retorna todos os documentos da base
        """
        # A resposta nao pode ser object aqui
        self.documentrest.response_object = False
        search = Search(limit=None)
        get = self.documentrest.get_collection(search_obj=search)

        return get

    def coleta_to_dict(self):
        """
        Convert status object to Python dict
        :return:
        """

        return conv.document2dict(self.base, self)

    def coleta_to_json(self, document):
        """
        Convert object to json
        :return:
        """

        return conv.document2json(document.lbbase, self)

    def create_coleta(self, document):
        """
        Insere dados de coleta
        """
        result = self.documentrest.create(document)

        return result

    def update_coleta(self, id, document):
        """
        Altera dados de coleta
        """
        coleta = self.documentrest.update(id, document)

        return coleta

    def delete_coleta(self, id, document):
        """
        Apaga os dados de coleta
        """
        coleta = self.documentrest.update(id)
        return coleta

    def is_created(self):
        """
        Retorna se a base já existe
        """
        try:
            response = self.baserest.get(self.lbbase.metadata.name)
            return True
        except:
            return False

    def search_orgao(self, sigla):
        """
        Busca registro completo do órgao pelo nome
        :return: obj collection com os dados da base
        """
        data = ['']
        search = Search(literal="document->>'sigla' = '" + sigla + "'")
        results = self.documentrest.get_collection(search_obj=search)

        return results

    def get_attribute(self, attr):
        """
        Testa recuperar atributo do Documento
        """
        # A resposta nao pode ser object aqui
        self.documentrest.response_object = False

        search = Search(limit=None, select=[attr])
        get = self.documentrest.get_collection(search_obj=search)

        return get

    def search_item(self, group, item, valor):
        """
        Busca registro completo do órgao pelo nome
        :return: obj collection com os dados da base
        """
        search = Search(limit=1,
                        literal="document->'" + group + "'->>'" + item +
                        "' = '" + valor + "'")
        #print(search._asdict())
        results = self.documentrest.get_collection(search_obj=search)

        return results
Exemplo n.º 10
0
class Reports(object):
    def __init__(self, nm_base, rest_url=None, response_object=True):
        self.base_nm = nm_base
        if rest_url is None:
            self.rest_url = config.REST_URL
        else:
            self.rest_url = rest_url
        self.coleta_manual_base = coleta_manual.ColetaManualBase(
            nm_base, self.rest_url)
        self.base = self.coleta_manual_base.lbbase
        self.documentrest = DocumentREST(self.rest_url, self.base,
                                         response_object)

    def get_base_orgao(self):
        """
        Retorna todos os documentos da base
        """
        # A resposta nao pode ser object aqui
        self.documentrest.response_object = False
        search = Search(limit=None)
        try:
            get = self.documentrest.get_collection(search_obj=search)
        except HTTPError as e:
            log.error("Erro ao buscar base do órgão %s\n%s", self.base_nm, e)
            return None

        return get

    def coleta_to_dict(self):
        """
        Convert status object to Python dict
        :return:
        """

        return conv.document2dict(self.base, self)

    def coleta_to_json(self, document):
        """
        Convert object to json
        :return:
        """

        return conv.document2json(document.lbbase, self)

    def create_coleta(self, document):
        """
        Insere dados de coleta
        """
        result = self.documentrest.create(document)
        return result

    def update_coleta(self, id, document):
        """
        Altera dados de coleta
        """
        coleta = self.documentrest.update(id, document)
        return coleta

    def delete_coleta(self, id, document):
        """
        Apaga os dados de coleta
        """
        coleta = self.documentrest.update(id)
        return coleta

    def get_attribute(self, attr):
        """
        Testa recuperar atributo do Documento
        """
        # A resposta nao pode ser object aqui
        self.documentrest.response_object = False

        # FIXME: Adicionar lista de atributos obrigatórios nos campos que vao retornar na busca
        # Referência: http://dev.lightbase.cc/projects/liblightbase/repository/revisions/master/entry/liblightbase/lbbase/content.py#L34

        # A busca deve obrigatoriamente retornar todos os atributos obrigatórios
        search = Search(limit=None, select=[attr, "data_coleta"])
        get = self.documentrest.get_collection(search_obj=search)

        return get

    def count_attribute(self, attr, child=None, clean_view=False):
        """
        retorna dicionário de atributos agrupados por contador
        """
        release_expressions = [  # para uso em agrupamento
            "Professional", "Ultimate", "Standard", "Enterprise", "Premium",
            "Starter", "MUI"
        ]
        # FIXME: Pegar um filtro mais dinâmico de offices a excluir
        excluir = [
            "Security Update".lower(), "Atualiza".lower(), "Help Pack".lower(),
            "Compatibility".lower(), "Definition Update".lower(),
            "Disco".lower(), "UNO runtime".lower(),
            "Office Access Runtime".lower(),
            "Office com Clique para Executar".lower(), "Components".lower(),
            "Update for".lower(), "Hotfix for".lower(),
            "Web Components".lower(), "Service Pack".lower(),
            "File Validation Add-In".lower(), "Office InfoPath".lower(),
            "Office Outlook Connector".lower(),
            "Office Proofing Tools".lower(), "Office Shared".lower(),
            "Pacote de Compatibilidade".lower(), "Office Groove".lower(),
            "Office OneNote".lower()
        ]
        if attr == 'todos':
            saida = self.count_all_attr()
            return saida
        attr_dict = self.get_attribute(attr)
        results = attr_dict.results
        orgaos = self.get_attribute("nome_orgao")
        orgao_name_list = [type(orgao).__name__ for orgao in orgaos.results]
        orgao_name_set = set(orgao_name_list)
        saida_orgao = dict()
        saida = dict()
        for elm in results:
            if child:
                parent = getattr(elm, attr)
                # Verifica valores nulos
                try:
                    attribute = getattr(parent, child)
                except AttributeError:
                    continue
            else:
                attribute = getattr(elm, attr)

            if attr == 'softwarelist':
                for software in attribute:
                    # ignorar updates e atualizações
                    pula = False
                    agrupa = False
                    expressao_atual = ''
                    for ignora in excluir:
                        if software.lower().find(ignora) > -1:
                            # Se chegou aqui esse software deve ser excluído
                            pula = True
                            break
                    # Se 'pula' for verdadeiro, o software deve ser ignorado
                    if pula:
                        continue
                    #atualizando expressão de agrupamento
                    expressao_atual = software
                    agrupa = False
                    if expressao_atual.lower().find('(') > 0:
                        expressao_atual = expressao_atual.split('(',1)[0] + \
                                          expressao_atual.split(')')[-1]
                        agrupa = True
                    if expressao_atual.lower().find('.') > 0:
                        expressao_atual = expressao_atual.split('.', 1)[0]
                        agrupa = True
                    if expressao_atual.lower().find('-') > 0:
                        expressao_atual = expressao_atual.split('-', 1)[0]
                        agrupa = True
                    if software.lower().find('20') > -1:
                        expressao_atual = expressao_atual.translate(
                            str.maketrans('', '', '1234567890'))
                        agrupa = True
                    # Verifica se existe alguma expressão de release
                    for release_expression in release_expressions:
                        if expressao_atual.lower().find(
                                release_expression.lower()) > 0:
                            expressao_atual = expressao_atual.split(
                                release_expression, 1)[0]
                            agrupa = True

                    # Se 'agrupa' for verdadeiro, o software será agrupado
                    if agrupa and clean_view:
                        expressao_atual = expressao_atual.strip()
                        if saida.get(expressao_atual) is None:
                            saida[expressao_atual] = 1
                        else:
                            saida[expressao_atual] += 1
                        continue
                    if saida.get(software) is None:
                        saida[software] = 1
                    else:
                        saida[software] += 1
                # Criando arquivo 'software_list.json' para uso em testes
                here = os.path.abspath(os.path.dirname(__file__))
                data_path = os.path.join(here, "../test/fixtures/reports/")
                software_list_file = open(data_path + "software_list.json",
                                          "w+")
                saida_orgao[self.base_nm] = saida
                software_list_file.write(json.dumps(saida_orgao))
            elif saida.get(attribute):
                saida[attribute] = saida.get(attribute) + 1
            else:
                saida[attribute] = 1
        if attr == 'win32_physicalmemory':
            elm = 'win32_physicalmemory_memorytype'
            saida_dict = convert.dict_desc(elm)
            dict_saida = dict()
            for x in saida.keys():
                dict_saida[saida_dict[x]] = saida[x]

            return dict_saida
        else:
            # log.info(saida)
            return saida

    def count_all_attr(self):
        release_expressions = [  # para uso em agrupamento
            "Professional", "Ultimate", "Standard", "Enterprise", "Premium",
            "Starter", "MUI"
        ]
        # FIXME: Pegar um filtro mais dinâmico de offices a excluir
        excluir = [
            "Security Update".lower(), "Atualiza".lower(), "Help Pack".lower(),
            "Compatibility".lower(), "Definition Update".lower(),
            "Disco".lower(), "UNO runtime".lower(),
            "Office Access Runtime".lower(),
            "Office com Clique para Executar".lower(), "Components".lower(),
            "Update for".lower(), "Hotfix for".lower(),
            "Web Components".lower(), "Service Pack".lower(),
            "File Validation Add-In".lower(), "Office InfoPath".lower(),
            "Office Outlook Connector".lower(),
            "Office Proofing Tools".lower(), "Office Shared".lower(),
            "Pacote de Compatibilidade".lower(), "Office Groove".lower(),
            "Office OneNote".lower()
        ]
        saida_final = dict()
        for attr in [
                'softwarelist', 'win32_physicalmemory', 'win32_bios',
                'win32_diskdrive', 'operatingsystem', 'win32_processor'
        ]:
            attr_dict = self.get_attribute(attr)
            results = attr_dict.results
            saida = dict()
            orgaos = self.get_attribute("nome_orgao")
            orgao_name_list = [
                type(orgao).__name__ for orgao in orgaos.results
            ]
            orgao_name_set = set(orgao_name_list)
            saida_orgao = dict()
            if attr == 'win32_physicalmemory':
                child = 'win32_physicalmemory_memorytype'
            elif attr == 'win32_bios':
                child = 'win32_bios_manufacturer'
            elif attr == 'win32_diskdrive':
                child = 'win32_diskdrive_caption'
            elif attr == 'operatingsystem':
                child = 'operatingsystem_caption'
            elif attr == 'win32_processor':
                child = 'win32_processor_manufacturer'
            else:
                child = None
            for elm in results:
                if child:
                    parent = getattr(elm, attr)
                    # Verifica valores nulos
                    try:
                        attribute = getattr(parent, child)
                    except AttributeError:
                        continue
                else:
                    attribute = getattr(elm, attr)

                if attr == 'softwarelist':
                    for software in attribute:
                        # ignorar updates e atualizações
                        pula = False
                        agrupa = False
                        expressao_atual = ''
                        for ignora in excluir:
                            if software.lower().find(ignora) > -1:
                                # Se chegou aqui esse software deve ser excluído
                                pula = True
                                break
                        # Se 'pula' for verdadeiro, o software deve ser ignorado
                        if pula:
                            continue
                        #atualizando expressão de agrupamento
                        expressao_atual = software
                        agrupa = False
                        if expressao_atual.lower().find('(') > 0:
                            expressao_atual = expressao_atual.split('(',1)[0] + \
                                              expressao_atual.split(')')[-1]
                            agrupa = True
                        if expressao_atual.lower().find('.') > 0:
                            expressao_atual = expressao_atual.split('.', 1)[0]
                            agrupa = True
                        if expressao_atual.lower().find('-') > 0:
                            expressao_atual = expressao_atual.split('-', 1)[0]
                            agrupa = True
                        if software.lower().find('20') > -1:
                            expressao_atual = expressao_atual.translate(
                                str.maketrans('', '', '1234567890'))
                            agrupa = True
                        # Verifica se existe alguma expressão de release
                        for release_expression in release_expressions:
                            if expressao_atual.lower().find(
                                    release_expression.lower()) > 0:
                                expressao_atual = expressao_atual.split(
                                    release_expression, 1)[0]
                                agrupa = True
                        # Se 'agrupa' for verdadeiro, o software será agrupado
                        """if agrupa:
                            expressao_atual = expressao_atual.strip()
                            if saida.get(expressao_atual) is None:
                                saida[expressao_atual] = 1
                            else:
                                saida[expressao_atual] += 1
                            continue"""
                        if saida.get(software) is None:
                            saida[software] = 1
                        else:
                            saida[software] += 1
                elif saida.get(attribute):
                    saida[attribute] = saida.get(attribute) + 1
                else:
                    saida[attribute] = 1

            if attr == 'win32_physicalmemory':
                elm = 'win32_physicalmemory_memorytype'
                saida_dict = convert.dict_desc(elm)
                dict_saida = dict()
                for x in saida.keys():
                    dict_saida[saida_dict[x]] = saida[x]

                saida_final[attr] = dict_saida
            else:
                # log.info(saida)
                saida_final[attr] = saida
        # Criando arquivo 'software_list.json' para uso em testes
        here = os.path.abspath(os.path.dirname(__file__))
        data_path = os.path.join(here, "../test/fixtures/reports/")
        software_list_file = open(data_path + "attribute_list.json", "w+")
        saida_orgao[self.base_nm] = saida_final
        software_list_file.write(json.dumps(saida_orgao))
        return saida_final