def get_active_customers(self, cust_id=None):
     """Gets all active customer(s) of active software provider(s) or given customer id"""
     if(cust_id):
         self._db_get_customer(cust_id)
     else:
         self._db_get_active_customers()
     
     # For each active customer, create its class object
     LOGGER.info('Building active customer object list.')
     cust_no = 1
     for customer in self._active_customers:
         try:
             LOGGER.info("\t{0}) Instantiating '{1}'".format(cust_no, customer.customerName))
             cust_no = cust_no + 1
             # Customer name should be unique
             class_name = customer.customerName.title().replace(' ', '')
             LOGGER.debug('\tCustomer ID: {0} - alt ID: {1} -  Class Name: {2}'.format(customer.customerId, \
                                                                                     customer.altCustomerId, class_name))
             cust_obj = globals()[class_name](customer.customerId, customer.customerName, customer.altCustomerId, \
                                              self.back_days, self.forward_days)
             self.active_customers_objects.append(cust_obj)
             LOGGER.info("Active customer list, added: '{0}'".format(customer.customerName))
         except Exception:
             LOGGER.error('Unable to instantiate customer object. Customer Name: %s', customer.customerName, exc_info=1)
             msg = "Unable to add customer: {0} - Error details: \n{1}".format(customer.customerName, traceback.format_exc())
             log_helpers.log_error_and_send_email('software_providers/process_software_providers', msg)
             continue
     return self.active_customers_objects
def main():

    # Command line parsing
    parser = OptionParser(usage="usage: %prog [options]", version="%prog v 0.1")
    parser.add_option("-c", "--customer",
                      action="store",
                      type="int",
                      dest="customer_id",
                      metavar = "CUSTOMER-ID",
                      help="specify CUSTOMER-ID to process")
    parser.add_option("-a", "--all",
                      action="store_true",
                      dest="process_all",
                      default=False,
                      help="process all active customer(s) of active software provider(s)[default]")
    parser.add_option("-b", "--backdays",
                      action="store",
                      type="int",
                      dest="back_days",
                      default=2,
                      metavar = "BACK-DAYS",
                      help="specify no of BACK-DAYS to fetch data [default 2 days]")
    parser.add_option("-f", "--forwarddays",
                      action="store",
                      type="int",
                      dest="forward_days",
                      default=7,
                      metavar = "FORWARD-DAYS",
                      help="specify no of FORWARD-DAYS to fetch data [default 7 days]")
    (options, args) = parser.parse_args()
    if(not options.process_all and not options.customer_id):
        parser.error("No option given, use -a or -c")
    
    try:
        # Process customer(s)
        LOGGER.info('===============================================================================================')
        LOGGER.info('|                                                                                             |')
        LOGGER.info('|             Processing Software Provider Customers                                          |')
        LOGGER.info('|                                                                                             |')
        LOGGER.info('===============================================================================================')
        manager = SoftwareProviderManager(options.back_days, options.forward_days)
        LOGGER.debug('Getting list of active customers.')
        if(options.process_all):
            customers = manager.get_active_customers()
        elif (options.customer_id):
            customers = manager.get_active_customers(options.customer_id)
        LOGGER.info('')
        LOGGER.info('------------------------------------------------------------------------------------')
        LOGGER.info('|              Active Customers Count is: {0}  '.format(len(customers)))
        LOGGER.info('------------------------------------------------------------------------------------')
        cust_no = 1
        for customer in customers:
            try:
                LOGGER.info('------------------------------------------------------------------------------------')
                LOGGER.info("|              {0}) Processing Customer: '{1}'  ".format(cust_no, customer.customer_name))
                LOGGER.info('------------------------------------------------------------------------------------')
                cust_no = cust_no + 1
                customer.fetch_data()
                appointments_data = customer.serialize_data()
                customer.save_data()
                LOGGER.debug('Calling status app for: %s - id: %s', customer.customer_name, customer.customer_id)
                try:
                    vi_script_model.do_script_log(customer.customer_id)
                except:
                    LOGGER.error('Unable to send customer status')
                LOGGER.info('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
            except:
                LOGGER.error('Exception processing customer: %s', customer.customer_name, exc_info=1)
                msg = "Unable to process customer: {0} - Error details: \n{1}".format(customer.customer_name, traceback.format_exc())
                log_helpers.log_error_and_send_email('software_providers/process_software_providers', msg)
                LOGGER.info('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')
                #TODO: Call status app with execution failure
                continue
    except:
        LOGGER.error('Process Software Provider Exception.', exc_info=1)
        msg = "Process Software Provider Exception. Details: \n%s" % traceback.format_exc()
        log_helpers.log_error_and_send_email('software_providers/process_software_providers', msg)
        LOGGER.info('++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++')