def find_and_fix_soci_record(query, output): ''' Find 'socis' that have no record in somenergia_soci and create one. ''' O = OOOP(**configdb.ooop) with io.open(query) as f: query = f.read() step("Connecting to the ERP database...") db = psycopg2.connect(**configdb.psycopg) with db.cursor() as cursor: try: cursor.execute(query) except KeyError as e: fail("Missing variable '{key}'. Specify it in the YAML file or by using the --{key} option" .format(key=e.args[0])) erp_data = nsList(cursor) erp_data = [dict(data) for data in erp_data] erp_ids = set(i['ids'] for i in erp_data) erp_soci = set(i['ref'] for i in erp_data) for partner_id in erp_ids: get_or_create_somenergia_soci(O, partner_id) with open(output, "w") as loadsocis: writer = csv.writer(loadsocis, delimiter = "\t") writer.writerows(zip(erp_ids, erp_soci))
def main(): step("Connectant a l'erp") O = Client(**configdb.erppeek) success("Connectat a: {}".format(O)) success("Usuaria: {}".format(O.user)) fiscal_year_id = O.model('account.fiscalyear').search([('name', 'ilike', '2021')])[0] fiscal_year = O.model('account.fiscalyear').browse(fiscal_year_id) step("Demanem el balanç per l'any {}".format(fiscal_year.name)) O.wizard( 'account.balance.full.report', { 'form': { 'company_id': 1, 'account_list': [[6, 0, []]], 'fiscalyear': fiscal_year_id, 'display_account': 'bal_mouvement', 'state': 'none', 'periods': [(6, 0, [])], 'all_accounts': True, 'context': {}, 'display_account_level': 5 } }, 'excelFile_async')
def migrate_socis(config, query, output): ''' Migrates cancelation date from both excels into ERP In case there are socis missing from both excels, save their data in a file for hand correction. ''' step("Get DNI from {} drive", config.newDrive.filename) load_DNI, load_data = get_nif_from_csv(config.newDrive) load_erp_nif, load_erp_data = get_data_from_erp(query, output) socis2migrate = list(set.intersection(load_erp_nif, load_DNI)) step("There are {} to migrate from {}", len(socis2migrate), config.newDrive.filename) soci, data_baixa = validate_dates(load_data, socis2migrate, "corregir_fechas_Baixa_socis.csv") update_ERP(configdb, soci, data_baixa, "vat", config.newDrive.filename) othersocis = load_erp_nif.difference(load_DNI) othersocis_ref = [i['ref_cliente'] for i in load_erp_data if 'nif' in i if i['nif'] in othersocis] step("Get DNI from {}, that are in ERP but not in {}", config.oldDrive.filename , config.newDrive.filename) load_ref, data_ref = get_ref_from_csv(config.oldDrive) socis2migrate_ref = set.intersection(set(load_ref), set(othersocis_ref)) soci_ref, data_baixa_ref = validate_dates(data_ref, socis2migrate_ref, "corregir_fechas_Baixes_de_soci.csv") step("There are {} to migrate from {}", len(socis2migrate_ref), config.oldDrive.filename) update_ERP(configdb, soci_ref, data_baixa_ref, "ref", config.oldDrive.filename) missing_socis = set(othersocis_ref).difference(socis2migrate_ref) warn("There are {} 'socis' missing from both excels and need to be updated.", len(missing_socis)) step("Saving missing 'socis' into {}", "hand_migration.csv") with open("hand_migration.csv", "w") as loadsocis: writer = csv.writer(loadsocis, delimiter = "\t") writer.writerows(missing_socis)
def passB2BTest(datapath, case, command, outputs): step("Test: %s Command: '%s'"%(case,command)) for output in outputs : removeIfExists(output) try : commandError = subprocess.call(command, shell=True) if commandError : return ["Command failed with return code %i:\n'%s'"%(commandError,command)] except OSError as e : return ["Unable to run command: '%s'"%(command)] failures = [] for output in outputs : extension = os.path.splitext(output)[-1] base = prefix(datapath, case, output) expected = expectedName(base, extension) diffbase = diffBaseName(base) difference = diff_files(expected, output, diffbase) #diffbase = diffbase+'.wav' diffbase = diffbase + extension if not difference: printcolor('32;1', " Passed") removeIfExists(diffbase) removeIfExists(diffbase+'.png') removeIfExists(badResultName(base,extension)) else: printcolor('31;1', " Failed") os.system('cp %s %s' % (output, badResultName(base,extension)) ) formatted = [ '\n\t '.join(item.split('\n')) for item in difference] failures.append("Output '%s':\n%s"%(base, '\n'.join(['\t- %s'%item for item in formatted]))) removeIfExists(output) return failures
def loadmeteofiles(meteofiles): if os.path.exists('meteo.csv'): meteo = pd.read_csv('meteo.csv', sep=';', encoding='utf-8') return meteo d = None for meteofile in meteofiles: _start = meteofile.find('20') _end = meteofile.find('\.xls') - 3 filedate = datetime.strptime(meteofile[_start:_end], '%Y-%m-%d') step("\t" + str(filedate)) p = pd.read_excel(meteofile, skiprows=4) p.columns = [ 'station', 'province', 'tempMax', 'tempMin', 'tempMean', 'wind', 'windMax', 'rain0024', 'rain0006', 'rain0612', 'rain1218', 'rain1824' ] p['year'] = filedate.year p['month'] = filedate.month p['day'] = filedate.day if not isinstance(d, pd.DataFrame): d = p else: d = pd.concat([d, p]) d.to_csv('meteo.csv', sep=';', encoding='utf-8') return d
def print_list(lst): for item in lst: pol_id = item[0] pol_text = item[1] pol_name = pol_o.read(pol_id, ['name'])['name'] step(" - id {} polissa {}", pol_id, pol_name) step(pol_text)
def change_to_tg(pol_ids): success('') success('Modificant polisses:') res = ns() totals = len(pol_ids) for counter, pol_id in enumerate(pol_ids): polissa = ns( pol_obj.read(pol_id, [ "name", "no_estimable", "observacions", "observacions_estimacio", ])) step("{}/{} polissa {}".format(counter + 1, totals, polissa.name)) header = "[{}] ".format(str(datetime.today())[:19]) if polissa.observacions: polissa.observacions = polissa.observacions.encode("utf-8") changes = { "observacions": header + missatge + "\n\n" + (polissa.observacions or ""), "observacions_estimacio": header + missatge, "no_estimable": True, } res[pol_id] = changes if doit: pol_obj.write(pol_id, changes) warn("modificat") return res
def send_contract_erp_email(pol_id): step("Enviant correus a la polissa id {}", pol_id) if doit: send_erp_email(pol_id, 'giscedata.polissa', EMAIL_TEMPLATE_ID, EMAIL_FROM_ACCOUNT) else: warn("email sending disabled, set --doit.")
def main(): import sys args = parseArgs() if args.body is not None: content = _unicode(args.body) elif args.bodyfile is not None: step("Loading body from stdin...") with open(args.bodyfile) as f: content = _unicode(f.read()) else: content = _unicode(sys.stdin.read()) #sys.stdout.write(content) sendMail( sender = args.sender, to = args.to, subject = args.subject, cc = args.cc, bcc = args.bcc, replyto = args.replyto, config = args.config, attachments = args.attachments, template = args.template, stylesheets = args.style, dumpfile = args.dump, **{args.format: content} )
def setPlantParameters(plantname, **data): step("Setting parameters for plant '{plantname}'") with orm.db_session: plant = Plant.get(name=plantname) if not plant: error(f"Plant '{plantname}' not found") return if PlantModuleParameters.exists(plant=plant.id): if not force: error(f"Plant '{plant.name}' already has parameters, use --force to overwrite") return warn("Forcing removal of previous plant parameters") oldparams = PlantModule[plant.id] out(ns(oldparams.as_dict()).dump()) oldparams.delete() data = ns(data) params = PlantModuleParameters( plant=plant, n_modules = data.nModules, max_power_current_ma = int(data.Imp*1000), max_power_voltage_mv = int(data.Vmp*1000), current_temperature_coefficient_mpercent_c = int(data.temperatureCoefficientI*1000), voltage_temperature_coefficient_mpercent_c = int(data.temperatureCoefficientV*1000), standard_conditions_irradiation_w_m2 = int(data.irradiationSTC), standard_conditions_temperature_dc = int(data.temperatureSTC*10), degradation_cpercent = int(data.degradation*100), opencircuit_voltage_mv = int(data.Voc*1000), shortcircuit_current_ma = int(data.Isc*1000), expected_power_correction_factor_cpercent = int(data.get('correctionFactorPercent', 100)*100), )
def archive_members_from_list(list_name, email_list): list_id = get_mailchimp_list_id(list_name) operations = [] for email in email_list: operation = { "method": "DELETE", "path": "/lists/{list_id}/members/{subscriber_hash}".format( list_id=list_id, subscriber_hash=get_subscriber_hash(email)), "operation_id": email, } operations.append(operation) payload = {"operations": operations} try: response = MAILCHIMP_CLIENT.batches.start(payload) except ApiClientError as error: msg = "An error occurred an archiving batch request, reason: {}" error(msg.format(error.text)) else: batch_id = response['id'] while response['status'] != 'finished': time.sleep(2) response = MAILCHIMP_CLIENT.batches.status(batch_id) step("Archived operation finished!!") step( "Total operations: {}, finished operations: {}, errored operations: {}" .format(response['total_operations'], response['finished_operations'], response['errored_operations'])) result_summary = requests.get(response['response_body_url']) result_summary.raise_for_status() return result_summary.content
def main(): import sys args = parseArgs() if args.body is not None: content = _unicode(args.body) elif args.bodyfile is not None: step("Loading body from stdin...") with open(args.bodyfile) as f: content = _unicode(f.read()) else: content = _unicode(sys.stdin.read()) #sys.stdout.write(content) sendMail(sender=args.sender, to=args.to, subject=args.subject, cc=args.cc, bcc=args.bcc, replyto=args.replyto, config=args.config, attachments=args.attachments, template=args.template, stylesheets=args.style, dump=args.dump, **{args.format: content})
def get_data_from_erp(queryfile, filename): ''' Find the 'socis' from ERP that need a cancelation date. ''' with io.open(queryfile) as f: query = f.read() step("Connecting to the ERP database...") db = psycopg2.connect(**configdb.psycopg) with db.cursor() as cursor: try: cursor.execute(query) except KeyError as e: fail("Missing variable '{key}'. Specify it in the YAML file or by using the --{key} option" .format(key=e.args[0])) erp_data = nsList(cursor) erp_data = [dict(data) for data in erp_data] erp_count_duplicated = collections.Counter(i['nif'] for i in erp_data) erp_duplicated = collections.Counter({elem: count for elem, count in erp_count_duplicated.iteritems() if count > 1 }) warn("There are {} duplicated records:{}", len(erp_duplicated), erp_duplicated) erp_nif = set(i['nif'] for i in erp_data) erp_ref = set(i['ref_cliente'] for i in erp_data) erp_vat = [i['vat'] for i in erp_data] erp_categoria = [i['categoria'] for i in erp_data] erp_name = [i['name'] for i in erp_data] step("Saving ERP data in {}, there are {} cases that meet the migration criteria" , filename, len(erp_nif)) with open(filename, "w") as loadsocis: writer = csv.writer(loadsocis, delimiter = "\t") writer.writerows( zip(erp_vat, erp_ref, erp_categoria, erp_name)) return erp_nif, erp_data
def process(cases, debug=False): result = {} cur = get_db_cursor() for key in sorted(cases.keys()): ids = execute_sql(cur, get_the_select(**cases[key])) result[key] = {'constrains': cases[key], 'ids': ids} step("{} : {}", key, ids) return result
def generate_and_save_pdf(id, name): try: pdf_data = generate_inv_pdf(id) step("saving pdf to {}", name) save_file(name, pdf_data) success("done!") except Exception as e: warn(str(e))
def connect_erp(): global O if O: return O step("Connectant a l'erp") O = Client(**configdb.erppeek) step("connectat...") return O
def generate_pdfs_cases(cases): for count, key in enumerate(cases.keys()): step("generating pdf {}/{} cases: {}", count + 1, len(cases.keys()), key) ids = cases[key]['ids'] for id in ids: step("id:{}", id) generate_and_save_pdf(id, "{}-{}.pdf".format(id, key))
def treat_duplicateds(mongo_db, mongo_collection, cups, doit=False): total_deleted = 0 for cups_name in cups: duplicateds = get_mongo_name_datetime_duplicateds(mongo_db, mongo_collection, cups_name) deleted_by_cups = duplicateds_by_cups(duplicateds, mongo_db, mongo_collection, doit) total_deleted += deleted_by_cups if deleted_by_cups: step("Trobats {} duplicats del CUPS {}".format(deleted_by_cups, cups_name)) success("Eliminats {} registres".format(total_deleted))
def output_results(result): success("Resultat final") success("----------------") success("Pòlisses analitzades: {}", len(pols_to_check_ids)) success("Forats trobats: {}", len(result)) success("Errors: ") step(','.join(errors_buscant))
def expire( member=None, contract=None, idmode=None, **_): member = preprocessMembers([member], idmode)[0] step("Markin as expired assignation between contract {} and member {}" .format(contract, member)) c.GenerationkwhAssignment.expire(contract, member) success("Done.")
def validate_account_move(draft_move_ids, sem): ''' draft_move_ids: list of ids to apply the validation acctions sem: semaphore to control how many validations can be done at the same time ''' with sem: step("Validating account_move for ids: {}".format(draft_move_ids)) res = erp_client.AccountMove.button_validate(draft_move_ids) return res
def send_killing_payer_email_warning(pol_ids): first = True for batch in chunks(pol_ids, CONTRACTS_PER_BATCH): if not first: step("Esperant {} segons per no saturar", TIME_BETWEEN_BATCHS) time.sleep(TIME_BETWEEN_BATCHS) success("Enviant email a bloc de {} polisses.", len(batch)) for pol_id in batch: send_contract_erp_email(pol_id)
def get_not_active(emails): to_archive = [] total = len(emails) for counter, email in enumerate(emails): email = email.strip() if not is_titular_partner_mail(email): to_archive.append(email) step("{}/{} - {} --> no titular", counter + 1, total, email) else: step("{}/{} - {} --> titular", counter + 1, total, email) return to_archive
def assign(member=None,contract=None,priority=None, idmode=None, contractMode=None): member = preprocessMembers([member], idmode)[0] contract = preprocessContracts([contract], contractMode)[0] expire(member=member,contract=contract) step("Assigning contract {} to member {}" .format(contract, member)) c.GenerationkwhAssignment.create({ 'member_id': member, 'contract_id': contract, 'priority': priority })
def load_invoice_data(fact_id): step("Carregant dades de la factura...") error = False data = "" data_yaml = rprt_obj.get_components_data_yaml([fact_id]) try: data = ns.loads(data_yaml) except Exception as e: error = True step("Error localitzat ...") data = data_yaml.replace("\n", '\n') return error, data
def search_invoice_by_name(invoice_number): step("Cerquem la factura...{}", invoice_number) fact_ids = fact_obj.search([('number', '=', invoice_number)]) if len(fact_ids) == 0: warn("Cap factura trobada amb aquest numero!!") return None if len(fact_ids) > 1: warn("Multiples factures trobades!! {}", fact_ids) return None fact_id = fact_ids[0] step("Factura amb ID {} trobada", fact_id) return fact_id
def get_soci_or_not_soci(emails): to_archive = [] total = len(emails) category_id = get_member_category_id() for counter, email in enumerate(emails): email = email.strip() if not is_soci_partner_mail(email, category_id): to_archive.append(email) step("{}/{} - {} --> no soci", counter+1, total, email) else: step("{}/{} - {} --> soci", counter+1, total, email) return to_archive
def search_invoice_by_id(invoice_id): step("Cerquem la factura... {}", invoice_id) fact_ids = fact_obj.search([('id', '=', invoice_id)]) if len(fact_ids) == 0: warn("Cap factura trobada amb aquest id!!!!") return None if len(fact_ids) > 1: warn("Multiples factures trobades!! {}", fact_ids) return None fact_id = fact_ids[0] step("Factura amb ID {} existeix", fact_id) return fact_id
def search_invoice_by_ids(invoice_ids): ret_ids = [] invoice_ids = [int(i) for i in invoice_ids.split(',')] for invoice_id in invoice_ids: step("Cerquem la factura...", invoice_id) fact_ids = fact_obj.search([('id', '=', invoice_id)]) if len(fact_ids) == 0: warn("Cap factura trobada amb aquest id!!") if len(fact_ids) > 1: warn("Multiples factures trobades!! {}", fact_ids) ret_ids.append(fact_ids[0]) step("Factura amb ID {} existeix", fact_ids[0]) return ret_ids
def main(doit=False): mongo_client = pymongo.MongoClient(**configdb.mongodb) mongo_db = mongo_client.somenergia erpclient = Client(**configdb.erppeek) cups_names = get_cups_names(erpclient) step("Trobats {} CUPS".format(len(cups_names))) for col in ['tg_cchfact', 'tg_cchval']: step("Tractant la coŀlecció {}".format(col)) treat_duplicateds(mongo_db, col, cups_names, doit) if not doit: warn("S'ha executat sense el doit: mode consulta")
def main(): options = ns() optarg = None cliargs = ns() keyarg = None args = [] for arg in sys.argv[1:]: if keyarg: cliargs[keyarg]=eval(arg) if arg.startswith("(") else arg keyarg=None continue if optarg: options[optarg]=arg optarg=None continue if arg.startswith('--'): keyarg = arg[2:] continue if arg.startswith('-'): optarg = arg[1:] continue args.append(arg) """ if not args: fail("Argument required. Usage:\n" "{} <sqlfile> [-C <dbconfig.py>] [<yamlfile>] [--<var1> <value1> [--<var2> <value2> ..] ]".format(sys.argv[0])) """ sqlfilename = os.path.join(os.path.dirname(__file__), "draftinvoices.sql") step("Loading {}...".format(sqlfilename)) with open(sqlfilename) as sqlfile: query = sqlfile.read() if 'C' in options: import imp config=imp.load_source('config',options.C) else: import config step("Connecting to the database...") db = psycopg2.connect(**config.psycopg) with db.cursor() as cursor : try: cursor.execute(query) except KeyError as e: fail("Missing variable '{key}'. Specify it in the YAML file or by using the --{key} option" .format(key=e.args[0])) print template.format(**dbutils.nsList(cursor)[0])
def main(): options = ns() optarg = None cliargs = ns() keyarg = None args = [] for arg in sys.argv[1:]: if keyarg: cliargs[keyarg] = eval(arg) if arg.startswith("(") else arg keyarg = None continue if optarg: options[optarg] = arg optarg = None continue if arg.startswith('--'): keyarg = arg[2:] continue if arg.startswith('-'): optarg = arg[1:] continue args.append(arg) """ if not args: fail("Argument required. Usage:\n" "{} <sqlfile> [-C <dbconfig.py>] [<yamlfile>] [--<var1> <value1> [--<var2> <value2> ..] ]".format(sys.argv[0])) """ sqlfilename = os.path.join(os.path.dirname(__file__), "draftinvoices.sql") step("Loading {}...".format(sqlfilename)) with open(sqlfilename) as sqlfile: query = sqlfile.read() if 'C' in options: import imp config = imp.load_source('config', options.C) else: import config step("Connecting to the database...") db = psycopg2.connect(**config.psycopg) with db.cursor() as cursor: try: cursor.execute(query) except KeyError as e: fail( "Missing variable '{key}'. Specify it in the YAML file or by using the --{key} option" .format(key=e.args[0])) print template.format(**dbutils.nsList(cursor)[0])
def search_known_errors(res, fact_id): errors = [] for k in known_errors.keys(): if k in res: errors.append(known_errors[k]) if errors: success("S'ha trobat {} possible(s) error(s):", len(errors)) for error in errors: success("POSSIBLE ERROR >> {}", error) else: warn("Error no reconegut, fes captura i obre incidència!!") step("Traça interna de l'error:") step(res) return errors
def set_general_condition(polissa, sem): step("Setting general conditions for contract: {}".format(polissa.name)) conditions = get_general_condition(polissa.titular.lang) with sem: try: polissa.condicions_generals_id = conditions res = True, polissa.name except Exception as e: msg = "An error ocurred setting general conditions for "\ "contract {}, reason: {}" error(msg.format(polissa.name, str(e))) res = False, polissa.name return res
def main(): state = 'draft' from_date = '2017-01-01' to_date = '2017-12-31' step("Getting drafts moves") draft_move_ids = get_draft_moves_ids(state, from_date, to_date) step("There are {} account moves to validate".format(len(draft_move_ids))) if draft_move_ids: step("Do you want to continue? (Y/n)") answer = raw_input() while answer.lower() not in ['y', 'n', '']: answer = raw_input() step("Do you want to continue? (Y/n)") if answer in ['n', 'N']: raise KeyboardInterrupt draft_move_ids_gen = (tuple(draft_move_ids[i:i + MAX_ELEMS]) for i in range(0, len(draft_move_ids), MAX_ELEMS)) res = validate_moves(draft_move_ids_gen) failed = { move_ids: result for move_ids, result in res.iteritems() if result is False } while failed: warn("There were failed validation, tring again") res = validate_moves(failed) failed = { elem: result for elem, result in res.iteritems() if result is False } success("Done!")
def default( members=None, force=False, insist=False, idmode=None, all=None, mail=False, effectiveon=None, purchaseduntil=None, **_): members=preprocessMembers(members, idmode, all, effectiveon, purchaseduntil, force, insist) step("Generating default assignments for members: {}".format( ','.join(str(i) for i in members))) c.GenerationkwhAssignment.createDefaultForMembers(members) if mail: step("Sending notification mails for members: {}".format( ','.join(str(i) for i in members))) c.GenerationkwhAssignment.notifyAssignmentByMail(members) success("Done.")
def main(): options = ns() optarg = None cliargs = ns() keyarg = None args = [] for arg in sys.argv[1:]: if keyarg: cliargs[keyarg]=eval(arg) if arg.startswith("(") else arg keyarg=None continue if optarg: options[optarg]=arg optarg=None continue if arg.startswith('--'): keyarg = arg[2:] continue if arg.startswith('-'): optarg = arg[1:] continue args.append(arg) if not args: fail("Argument required. Usage:\n" "{} <sqlfile> [-C <dbconfig.py>] [<yamlfile>] [--<var1> <value1> [--<var2> <value2> ..] ]".format(sys.argv[0])) step("Loading {}...".format(args[0])) with open(args[0]) as sqlfile: query = sqlfile.read() variables = ns() if len(args)>=2: step("Loading variables...".format(args[1])) variables = ns.load(args[1]) warn(variables.dump()) variables.update(cliargs) if 'C' in options: import imp config=imp.load_source('config',options.C) else: import config step("Connecting to the database...") db = psycopg2.connect(**config.psycopg) with db.cursor() as cursor : try: cursor.execute(query, variables) except KeyError as e: fail("Missing variable '{key}'. Specify it in the YAML file or by using the --{key} option" .format(key=e.args[0])) print dbutils.csvTable(cursor)
def price( members=None, force=False, insist=False, idmode=None, all=None, mail=False, effectiveon=None, purchaseduntil=None, **_): members=preprocessMembers(members, idmode, all, effectiveon, purchaseduntil, force, insist) if mail: step("Sending advanced effectiveness notification for members: {}".format( ','.join(str(i) for i in members))) c.GenerationkwhAssignment.notifyAdvancedEffectiveDate(members) else: step("(Simulating) Sending advanced effectiveness notification for members: {}".format( ','.join(str(i) for i in members))) step("Use --mail to send") success("Done.")
def generateMaps(year, month): import datetime import dbutils beginingNextMonth = ( datetime.date(year, month+1, 1) if month != 12 else datetime.date(year+1, 1, 1) ) - datetime.timedelta(days=1) populationPerCCAA = dict( (line.code, int(line.population_2014_01)) for line in readCsvNs('poblacio_ccaa.csv') ) populationPerProvincia = dict( (line.code, int(line.population_2015_01)) for line in readCsvNs('poblacio_provincies-20150101.csv') ) # distribucioSocis = readCsvNs("distribucio.csv") distribucioSocis = distribucioSocies(str(beginingNextMonth), dbutils.nsList) socisPerCCAA = countBy('codi_ccaa', distribucioSocis, noneValue='00') socisPerProvincia = countBy('codi_provincia', distribucioSocis, noneValue='00') socisPerMunicipi = countBy('codi_ine', distribucioSocis, noneValue='00000') socisPerPais = countBy('codi_pais', distribucioSocis, noneValue='00') relativeSocisPerCCAA = dict( (ccaa, socis*10000./populationPerCCAA[ccaa]) for ccaa, socis in socisPerCCAA.items() ) relativeSocisPerProvincia = dict( (prov, socis*10000./populationPerProvincia[prov]) for prov, socis in socisPerProvincia.items() if prov in populationPerProvincia ) totalSocis = sum(value for value in socisPerCCAA.values()) months = ( "Enero Febrero Marzo Abril Mayo Junio " "Julio Agosto Septiembre Octubre Noviembre Diciembre" ).split() output = ns( month = months[month-1].upper(), year = year, ) step("Generant mapa amb valors absoluts") output.titol = "" output.subtitol = "" maxSocis = max(value for value in socisPerCCAA.values()) and 1800 minSocis = min(value for value in socisPerCCAA.values()) for ccaa, population in populationPerCCAA.items(): socis = socisPerCCAA.get(ccaa,0) output['number_'+ccaa] = socis output['percent_'+ccaa] = '{:.1f}%'.format(socis*100./totalSocis).replace('.',',') output['color_'+ccaa] = mapColor(socis, minSocis, maxSocis) renderMap('SocisPerCCAA-absoluts-{:04}-{:02}'.format(year,month), output) step("Generant mapa amb valors relatius") output.titol = "Socixs/10.000 hab." output.subtitol = " (datos INE 01/2014)" maxRelativeSocis = max(value for value in relativeSocisPerCCAA.values()) and 10. minRelativeSocis = min(value for value in relativeSocisPerCCAA.values()) for ccaa, population in populationPerCCAA.items(): relativeSoci = relativeSocisPerCCAA.get(ccaa,0) output['number_'+ccaa] = '{:.1f}'.format(relativeSoci).replace('.',',') output['color_'+ccaa] = mapColor( relativeSoci, minRelativeSocis, maxRelativeSocis) output['percent_'+ccaa] = '' renderMap('SocisPerCCAA-relatius-{:04}-{:02}'.format(year,month), output) output = ns( month = months[month-1].upper(), year = year, ) step("Generant mapa per provincies amb valors absoluts") maxSocis = max(value for value in socisPerProvincia.values()) and 1800 minSocis = min(value for value in socisPerProvincia.values()) for prov, population in populationPerProvincia.items(): socis = socisPerProvincia.get(prov,0) output['number_'+prov] = socis output['percent_'+prov] = '{:.1f}%'.format(socis*100./totalSocis).replace('.',',') output['color_'+prov] = mapColor(socis, minSocis, maxSocis) renderMap('SocisPerProvincies-absoluts-{:04}-{:02}'.format(year,month), output, 'MapaProvincias-template.svg') step("Generant mapa per provincies amb valors relatius") output.titol = "Socixs/10.000 hab." output.subtitol = " (datos INE 01/2015)" maxRelativeSocis = max(value for value in relativeSocisPerProvincia.values()) and 10. minRelativeSocis = min(value for value in relativeSocisPerProvincia.values()) for prov, population in populationPerProvincia.items(): relativeSoci = relativeSocisPerProvincia.get(prov,0) output['number_'+prov] = '{:.1f}'.format(relativeSoci).replace('.',',') output['color_'+prov] = mapColor( relativeSoci, minRelativeSocis, maxRelativeSocis) output['percent_'+prov] = '' renderMap('SocisPerProvincies-relatius-{:04}-{:02}'.format(year,month), output, 'MapaProvincias-template.svg')
def sendMail( sender, to, subject, text=None, html=None, md=None, ansi=None, cc=[], bcc=[], replyto=[], attachments = [], template=None, config=None, stylesheets = [], dump = None, verbose=True ): import smtplib from email.mime.multipart import MIMEMultipart from email.mime.base import MIMEBase from email.mime.text import MIMEText from email.encoders import encode_base64 from email.utils import formataddr, parseaddr if not config: from config import smtp else: import imp smtp=imp.load_source('config',config).smtp def formatAddress(address): return formataddr(parseaddr(address)) def formatAddresses(addresses): return ', '.join(formatAddress(a) for a in addresses) # Headers msg = MIMEMultipart() msg['Subject'] = subject msg['From'] = formatAddress(sender) msg['To'] = formatAddresses(to) if cc: msg['CC'] = formatAddresses(cc) if bcc: msg['BCC'] = formatAddresses(bcc) if replyto: msg['Reply-To'] = formatAddresses(replyto) recipients = to + (cc if cc else []) + (bcc if bcc else []) # Attachments for filename in attachments: step("Attaching {}...".format(filename)) part = MIMEBase('application', "octet-stream") part.set_payload(open(filename, "rb").read()) encode_base64(part) import os part.add_header( 'Content-Disposition', 'attachment; filename="{}"'.format( os.path.basename(filename.replace('"', '')))) msg.attach(part) # Content formatting style='' for stylesheet in stylesheets or []: with open(stylesheet) as stylefile: style+=stylefile.read() if md: step("Formating markdown input...") import markdown text = md # TODO: Format plain text html = htmltemplate.format( style = style, body = markdown.markdown(md, output_format='html') ) if ansi: step("Formating ansi input...") import deansi text = ansi # TODO: Clean ansi sequences html = htmltemplate.format( style = deansi.styleSheet()+style, body = "<div class='ansi_terminal'>"+deansi.deansi(ansi)+"</div>", ) content = MIMEMultipart('alternative') if text: content.attach(MIMEText(text,'plain','utf8')) if html: step("Adapting html to mail clients...") import premailer html = premailer.transform(html) content.attach(MIMEText(html,'html','utf8')) import sys #sys.stdout.write(html) msg.attach(content) if dump: with open(dump,'w') as dumpfile: dumpfile.write(msg.as_string()) success("Email dumped as {}".format(dump)) return return # Sending step("Connecting to {host}:{port} as {user}...".format(**smtp)) server = smtplib.SMTP(smtp['host'], smtp['port']) server.starttls() server.login(smtp['user'], smtp['password']) step("\tSending...") server.sendmail(sender, recipients, msg.as_string()) success("\tMail sent") server.quit()
import psycopg2 with psycopg2.connect(**config) as db: for f in ( unansweredRequests, unactivatedRequests, acceptedRequests, rejectedRequests, activatedRequests, sentRequests, cancelledRequests, dropoutRequests, ) : results = f(db, inici, final, cursorManager=csvTable) csvname = 'report-{:04}{:02}-{}.csv'.format( args.year, args.month, f.__name__) step("Generating '{}'...".format(csvname)) with open(csvname,'w') as output: output.write(results) sys.exit(0) if args.sequence is None: args.sequence = 0 while True: args.sequence+=1 xmlname = reportName(args.year, args.month, args.agent, args.sequence) if os.access(xmlname, os.F_OK) : continue break else: xmlname = reportName(args.year, args.month, args.agent, args.sequence) step("Collecting data...")
'Name', 'Call name', 'Soci', 'DNI', 'E-mail', 'Language', 'Legal entity', 'Contracts', 'Anual use', 'Recommended shares', 'Covered use', 'Recommended investment', ]) for i,soci_id in enumerate(soci_ids): step("Soci {}...".format(soci_id)) try: soci = O.ResPartner.get(soci_id) contractes = O.GiscedataPolissa.search( [ '|', ('titular.id', '=', soci_id), ('pagador.id', '=', soci_id), ] ) consums = [ dict( contract_id = contracte['name'], supply_address = contracte['cups_direccio'],