Ejemplo n.º 1
0
def get_tag(obj, E):
    try:
        data = []
        dictionary = {}
        for item in obj:
            if type(obj[item]) == dict:
                data.append(E(item, *get_tag(obj[item], E)))
            elif type(obj[item]) == list:
                for i in obj[item]:
                    data.append(E(item, *get_tag(i, E)))
            else:
                dictionary[item] = obj[item]
            data.append(dictionary)
        return data
    # Handle other exception (i.e. internal server error)
    except:

        # Get exception:
        other_exception = str(sys.exc_info()[1])
        # Build error from exception:
    other_error = Error(_Pauli_Constants.HTTP_CODES['INTERNAL'],
                        _Pauli_Constants.ERROR_MESSAGES['INTERNAL'])
    # Log exception/error:
    logger.critical(other_error.content + other_exception)
    # Exception was already handled (i.e. error was built and logged int this except) so it is transformed into an already handled exception:
    already_handled_exception = Already_Handled_Exception(other_error)
    # Raise exception to outer except(s):
    raise already_handled_exception
Ejemplo n.º 2
0
def instance_to_dict(obj):
    try:
        if isinstance(obj, _SAT_data.SAT_TYPE):
            dictionary = instance_to_dict(obj.__dict__)
        if type(obj) == dict:
            dictionary = {}
            for item in obj:
                dictionary.update({item: instance_to_dict(obj[item])})
        elif type(obj) == list:
            dictionary = []
            for i in obj:
                dictionary.append(instance_to_dict(i))
        elif type(obj) == unicode or type(obj) == int or type(obj) == str:
            dictionary = obj
        else:
            dictionary = instance_to_dict(obj.__dict__)
        return dictionary
    # Handle other exception (i.e. internal server error)
    except:

        # Get exception:
        other_exception = str(sys.exc_info()[1])
        # Build error from exception:
    other_error = Error(_Pauli_Constants.HTTP_CODES['INTERNAL'],
                        _Pauli_Constants.ERROR_MESSAGES['INTERNAL'])
    # Log exception/error:
    logger.critical(other_error.content + other_exception)
    # Exception was already handled (i.e. error was built and logged int this except) so it is transformed into an already handled exception:
    already_handled_exception = Already_Handled_Exception(other_error)
    # Raise exception to outer except(s):
    raise already_handled_exception
Ejemplo n.º 3
0
def translate(dictionary, tr):
    try:
        new = {}
        for item in dictionary:
            if type(dictionary[item]) == dict:
                new[tr[item]] = translate(dictionary[item], tr)
            elif type(dictionary[item]) == list:
                new[tr[item]] = []
                for i in dictionary[item]:
                    new[tr[item]].append(translate(i, tr))
            else:
                new[tr[item]] = dictionary[item]
        return new
    # Handle other exception (i.e. internal server error)
    except:

        # Get exception:
        other_exception = str(sys.exc_info()[1])
        # Build error from exception:
    other_error = Error(_Pauli_Constants.HTTP_CODES['INTERNAL'],
                        _Pauli_Constants.ERROR_MESSAGES['INTERNAL'])
    # Log exception/error:
    logger.critical(other_error.content + other_exception)
    # Exception was already handled (i.e. error was built and logged int this except) so it is transformed into an already handled exception:
    already_handled_exception = Already_Handled_Exception(other_error)
    # Raise exception to outer except(s):
    raise already_handled_exception
Ejemplo n.º 4
0
def log_sl1_thread_logs_at_sl1_main_logs(sl1_execution_log=None,
                                         sl1_logger=sl1_logger,
                                         cron_logger=cron_logger):
    try:
        current_taxpayer_index = sl1_execution_log['current_taxpayer_index']
        total_taxpayers = sl1_execution_log['total_taxpayers']
        end = sl1_execution_log['end']
        current_table_row = sl1_execution_log['current_table_row']
        lock = sl1_execution_log['lock']
        total_percentage_done = _Utilities.get_process_percentage_done(
            current_taxpayer_index, total_taxpayers)
        sl1_execution_log['percentage_done'] = total_percentage_done
        with lock:
            current_table_row.value = current_table_row.value + 1
        if current_table_row.value >= SL1_LOGGING_CONFIG['table_row_limit']:
            with lock:
                current_table_row.value = 0
            begin_table_row = _Utilities.format_log(SL1_TABLE_TITLES,
                                                    SL1_TABLE_FIELD_LENGTHS,
                                                    begin=True)
            sl1_logger.info('', extra=begin_table_row)
        sl1_execution_log = _Utilities.format_log(sl1_execution_log,
                                                  SL1_TABLE_FIELD_LENGTHS)
        sl1_logger.info('', extra=sl1_execution_log)
        if end is True:
            end_table_row = _Utilities.format_log(DEFAULT_LOG,
                                                  SL1_TABLE_FIELD_LENGTHS,
                                                  end=True)
            sl1_logger.info('', extra=end_table_row)
            end_message = sl1_execution_log['end_message']
            sl1_logger.info(end_message, extra=DEFAULT_LOG)
    except Exception as e:
        sl1_logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception
Ejemplo n.º 5
0
def generate_simple_xml(dictionary, document_type):
    try:
        ROOT = _SAT_data.ROOT[document_type]
        NSMAP = _SAT_data.NSMAP[document_type]
        NAMESPACE = _SAT_data.NAMESPACE[document_type]
        XSD = _SAT_data.XSD[document_type]

        E = ElementMaker(namespace=NSMAP[NAMESPACE], nsmap=NSMAP)

        xml = E(ROOT, *get_tree(dictionary, E, NSMAP, XSD))
        return xml

    # Exception was already handled (i.e. error was built and logged in a deeper except) so it just raised to outer excepts(s):
    except Already_Handled_Exception as already_handled_exception:

        # Raise exception to outer except(s)
        raise already_handled_exception

    # Handle other exception (i.e. internal server error)
    except:

        # Get exception:
        other_exception = str(sys.exc_info()[1])
        # Build error from exception:
    other_error = Error(_Pauli_Constants.HTTP_CODES['INTERNAL'],
                        _Pauli_Constants.ERROR_MESSAGES['INTERNAL'])
    # Log exception/error:
    logger.critical(other_error.content + other_exception)
    # Exception was already handled (i.e. error was built and logged int this except) so it is transformed into an already handled exception:
    already_handled_exception = Already_Handled_Exception(other_error)
    # Raise exception to outer except(s):
    raise already_handled_exception
Ejemplo n.º 6
0
def get_credentials(identifier, certificates):
    try:
        credentials_folder = _Constants.IDENTIFIER_PATH + identifier + '/' + _Constants.CREDENTIALS_FOLDER + '/'
        logger.info(certificates)
        credentials = {
            'pem_file': credentials_folder + identifier + '_EAC.pem',
            'cer_file': credentials_folder + certificates['cer_file'],
            'key_file': credentials_folder + certificates['key_file'],
            'password': certificates['password']
        }
        return credentials
    # Handle other exception (i.e. internal server error)
    except:

        # Get exception:
        other_exception = str(sys.exc_info()[1])
        # Build error from exception:
    other_error = Error(_Pauli_Constants.HTTP_CODES['INTERNAL'],
                        _Pauli_Constants.ERROR_MESSAGES['INTERNAL'])
    # Log exception/error:
    logger.critical(other_error.content + other_exception)
    # Exception was already handled (i.e. error was built and logged int this except) so it is transformed into an already handled exception:
    already_handled_exception = Already_Handled_Exception(other_error)
    # Raise exception to outer except(s):
    raise already_handled_exception
Ejemplo n.º 7
0
def new_synchronization_layer_1_log(logger=None):
    try:
        synchronization_layer_1_log = {
            'forest_db': {
                'before': {
                    'good': 0,
                    'pending': 0
                },
                'after': {
                    'new': 0,
                    'pending_completed': 0,
                    'pending': 0,
                    'updated': 0
                }
            },
            'firmware': {
                'new': 0,
                'update': 0
            }
        }  # End of synchronization_layer_1_log
        return synchronization_layer_1_log
    except Exception as e:
        logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception
Ejemplo n.º 8
0
def set_xml_to_pending_cfdi(cfdi, logger=None, sl1_execution_log=None):
    try:
        forest_db = _Utilities.set_connection_to_forest_db()
        db_CFDI = forest_db['CFDI']
        uuid = cfdi['uuid']
        # Get xml data:
        xml = cfdi['xml']
        validation = _Pauli_Helper.validate_xml(xml)
        xml_warnings = validation['warnings']
        xml_invalidations = validation['invalidations']
        # Updating data:
        updating_cfdi_data = {
            'xml': xml,
            'validated': True if validation['validated'] else False,
            'details': {
                'invalidations': xml_invalidations,
                'warnings': xml_warnings
            }  # End of details
        }  #End of updating_cfdi_data
        db_CFDI.update({'uuid': uuid}, {'$set': updating_cfdi_data})
        sl1_execution_log['forest_db']['after'][
            'pending_completed'] = sl1_execution_log['forest_db']['after'][
                'pending_completed'] + 1
    except Exception as e:
        logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception
Ejemplo n.º 9
0
def get_tree(dictionary, E, NSMAP, XSD):
    try:
        schemaLocation_tag = {'{' + NSMAP['xsi'] + '}schemaLocation': XSD}
        tags = get_tag(dictionary, E)
        tree = [schemaLocation_tag] + tags
        return tree

    # Exception was already handled (i.e. error was built and logged in a deeper except) so it just raised to outer excepts(s):
    except Already_Handled_Exception as already_handled_exception:

        # Raise exception to outer except(s)
        raise already_handled_exception

    # Handle other exception (i.e. internal server error)
    except:

        # Get exception:
        other_exception = str(sys.exc_info()[1])
        # Build error from exception:
    other_error = Error(_Pauli_Constants.HTTP_CODES['INTERNAL'],
                        _Pauli_Constants.ERROR_MESSAGES['INTERNAL'])
    # Log exception/error:
    logger.critical(other_error.content + other_exception)
    # Exception was already handled (i.e. error was built and logged int this except) so it is transformed into an already handled exception:
    already_handled_exception = Already_Handled_Exception(other_error)
    # Raise exception to outer except(s):
    raise already_handled_exception
Ejemplo n.º 10
0
def generate_pem_file(identifier, credentials):
    try:
        key_file = credentials['key_file']
        password = credentials['password']
        file_path = credentials['pem_file']
        params_process = [
            'openssl', 'pkcs8', '-inform', 'DET', '-in', key_file, '-passin',
            'pass:'******'-out', file_path
        ]
        return execute_popen_process(params_process)

    # Exception was already handled (i.e. error was built and logged in a deeper except) so it just raised to outer excepts(s):
    except Already_Handled_Exception as already_handled_exception:

        # Raise exception to outer except(s)
        raise already_handled_exception

    # Handle other exception (i.e. internal server error)
    except:

        # Get exception:
        other_exception = str(sys.exc_info()[1])
        # Build error from exception:
    other_error = Error(_Pauli_Constants.HTTP_CODES['INTERNAL'],
                        _Pauli_Constants.ERROR_MESSAGES['INTERNAL'])
    # Log exception/error:
    logger.critical(other_error.content + other_exception)
    # Exception was already handled (i.e. error was built and logged int this except) so it is transformed into an already handled exception:
    already_handled_exception = Already_Handled_Exception(other_error)
    # Raise exception to outer except(s):
    raise already_handled_exception
Ejemplo n.º 11
0
def stamp_xml(identifier, document_type, xml_without_stamp, certificates):
    try:
        credentials = get_credentials(identifier, certificates)
        # if not os.path.isfile(credentials['pem_file']):
        # 	out = generate_pem_file(identifier,credentials)
        # 	logger.info(out)
        out = generate_pem_file(identifier, credentials)
        logger.info(out)

        serial_number = get_serial_number(credentials['cer_file'])
        keys = RSA.load_key(credentials['pem_file'])
        cert_file = open(credentials['cer_file'], 'r')
        cert = base64.b64encode(cert_file.read())
        xml = xml_without_stamp
        xsl_root = ET.parse(_SAT_data.XSLT[document_type])
        xsl = ET.XSLT(xsl_root)
        cadena_original = xsl(xml)
        digest = hashlib.new('sha1', str(cadena_original)).digest()
        sello = base64.b64encode(keys.sign(digest, "sha1"))

        xml.attrib['Sello'] = sello
        xml.attrib['Certificado'] = cert
        xml.attrib['noCertificado'] = serial_number

        xml_with_stamp = ET.tostring(xml,
                                     pretty_print=True,
                                     xml_declaration=True,
                                     encoding='UTF-8')
        logger.info(xml_with_stamp)

        return xml_with_stamp

    # Exception was already handled (i.e. error was built and logged in a deeper except) so it just raised to outer excepts(s):
    except Already_Handled_Exception as already_handled_exception:

        # Raise exception to outer except(s)
        raise already_handled_exception

    # Handle other exception (i.e. internal server error)
    except:

        # Get exception:
        other_exception = str(sys.exc_info()[1])
        # Build error from exception:
    other_error = Error(_Pauli_Constants.HTTP_CODES['INTERNAL'],
                        _Pauli_Constants.ERROR_MESSAGES['INTERNAL'])
    # Log exception/error:
    logger.critical(other_error.content + other_exception)
    # Exception was already handled (i.e. error was built and logged int this except) so it is transformed into an already handled exception:
    already_handled_exception = Already_Handled_Exception(other_error)
    # Raise exception to outer except(s):
    raise already_handled_exception
Ejemplo n.º 12
0
def update_synchronization_data_for_taxpayer(taxpayer,
                                             sl1_execution_log,
                                             logger=None):
    try:
        forest_db = _Utilities.set_connection_to_forest_db()
        db_Taxpayer = forest_db['Taxpayer']
        # Update sl1 success execution date:
        sl1_date = STATUS_DATES['synchronization_layer_1']
        synchronization_date = Datetime.now()
        taxpayer[sl1_date] = synchronization_date
        # Update synchronization logs:
        identifier = taxpayer['identifier']
        begin_date = taxpayer[
            'start_date']  #Since taxpayer claim to be synchronized
        begin_date = begin_date.replace(hour=0, minute=0)
        end_date = synchronization_date  # Until now
        cfdis_in_forest_db_count = _Utilities.get_cfdis_count_in_forest_for_this_taxpayer_at_period(
            taxpayer, begin_date, end_date)
        synchronization_log = {
            'status': 200,
            'temporal_data': {
                'completed_date': synchronization_date,
                'period': {
                    'year': synchronization_date.year,
                    'month': synchronization_date.month
                },
            },
            'cfdis': {
                'new': sl1_execution_log['stored'],
                'updated': sl1_execution_log['updated'],
                'total': cfdis_in_forest_db_count
            }  # End of synchronization_log
        }  # End of synchronization_log
        taxpayer_logs = taxpayer['logs'] if 'logs' in taxpayer and taxpayer[
            'logs'] is not None else {}
        taxpayer_synchronization_logs = taxpayer_logs[_Constants.SYNCHRONIZATION] if _Constants.SYNCHRONIZATION in taxpayer_logs else []
        taxpayer_synchronization_logs.append(synchronization_log)
        if len(taxpayer_synchronization_logs
               ) > _Constants.LIMIT_LOGS_PER_TAXPAYER:
            taxpayer_synchronization_logs[:1] = []
        taxpayer_logs[
            _Constants.SYNCHRONIZATION] = taxpayer_synchronization_logs
        taxpayer['logs'] = taxpayer_logs
        db_Taxpayer.save(taxpayer)
    except Exception as e:
        if logger is not None:
            logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception
Ejemplo n.º 13
0
def main(argv):
    try:
        cron_logger.info('FOREST-CRON PROCESS CALLED')
        cron_logger.info(LOG_INDENT + 'Getting command line params ... ')
        process = get_command_line_params(argv)
        if 'debug' in process:
            cron_logger.info(LOG_INDENT + 'Running in debug mode ... ')
        cron_logger.info(LOG_INDENT + 'Calling process handler ... ')
        _Processes_Handler.execute(process)
    except Already_Handled_Exception as already_handled_exception:
        raise already_handled_exception
    except Exception as e:
        cron_logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        return already_handled_exception
Ejemplo n.º 14
0
def get_pending_uuids_in_forest_db(pending_cfdis_in_db=None,
                                   logger=None,
                                   sl1_execution_log=None):
    try:
        pending_cfdi_uuids = {}
        for pending_cfdi_in_db in pending_cfdis_in_db:
            uuid = pending_cfdi_in_db['uuid']
            pending_cfdi_uuids[uuid] = True
        number_of_pending_cfdi_uuids = len(pending_cfdi_uuids)
        sl1_execution_log['forest_db']['before'][
            'pending'] = number_of_pending_cfdi_uuids
        return pending_cfdi_uuids
    except Exception as e:
        logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception
Ejemplo n.º 15
0
def get_existing_uuids_in_forest_db(cfdis_in_db=None,
                                    logger=None,
                                    sl1_execution_log=None):
    try:
        existing_cfdi_uuids = []
        for cfdi_in_db in cfdis_in_db:
            uuid = cfdi_in_db['uuid']
            status = cfdi_in_db['status']
            existing_uuid = {
                'uuid': uuid,
                'status': status
            }  # End of existing_uuid
            existing_cfdi_uuids.append(existing_uuid)
        number_of_existing_cfdi_uuids = len(existing_cfdi_uuids)
        sl1_execution_log['forest_db']['before'][
            'good'] = number_of_existing_cfdi_uuids
        return existing_cfdi_uuids
    except Exception as e:
        logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception
Ejemplo n.º 16
0
def chart_of_accounts(params):

    try:

        logger.info(_Pauli_Constants.FUNCTION_EXECUTION_BEGINNING_MESSAGE)

        # ... C o d e     m o n k e y ...

        certificates = params['certificates']

        # Instance class
        COA = Chart_of_Accounts(params)

        # GET stamp_xml
        xml = COA.get_xml(certificates)

        logger.info(_Pauli_Constants.FUNCTION_EXECUTION_ENDING_MESSAGE)

        return xml

    # Exception was already handled (i.e. error was built and logged in a deeper except) so it just raised to outer excepts(s):
    except Already_Handled_Exception as already_handled_exception:

        # Raise exception to outer except(s)
        raise already_handled_exception

    # Handle other exception (i.e. internal server error)
    except:

        # Get exception:
        other_exception = str(sys.exc_info()[1])
        # Build error from exception:
    other_error = Error(_Pauli_Constants.HTTP_CODES['INTERNAL'],
                        _Pauli_Constants.ERROR_MESSAGES['INTERNAL'])
    # Log exception/error:
    logger.critical(other_error.content + other_exception)
    # Exception was already handled (i.e. error was built and logged int this except) so it is transformed into an already handled exception:
    already_handled_exception = Already_Handled_Exception(other_error)
    # Raise exception to outer except(s):
    raise already_handled_exception
Ejemplo n.º 17
0
def set_cancelled_status_to_cfdi(cfdi,
                                 updated_cfdi,
                                 logger=None,
                                 sl1_execution_log=None):
    try:
        forest_db = _Utilities.set_connection_to_forest_db()
        db_CFDI = forest_db['CFDI']
        cfdi['status'] = updated_cfdi['status']  # Equalize status
        if 'cancelation_date' in updated_cfdi:
            cfdi['cancelation_date'] = _Helper.sat_date_to_ISODate(
                updated_cfdi['cancelation_date'])
        xml_exists = 'xml' in cfdi and cfdi['xml'] != ''
        if not xml_exists:
            cfdi['xml'] = _Helper.build_default_xml(
                cfdi['seller'], cfdi['buyer'], cfdi['certification_date'],
                cfdi['issued_date'], cfdi['voucher_effect'], cfdi['uuid'])
        sl1_execution_log['forest_db']['after'][
            'updated'] = sl1_execution_log['forest_db']['after']['updated'] + 1
        db_CFDI.save(cfdi)
    except Exception as e:
        logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception