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 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 wrapper(partner, start, end, output): start_date, end_date = getDateRange(start, end) partner_lang = getPartnerLang(partner) if not partner_lang: fail("Identificador de titular no trobat! {}", partner) tmp_dir = tempfile.mkdtemp() contractsfile = os.path.join( tmp_dir, "contracts_{}_{}.csv".format(partner, end_date)) billsfile = os.path.join( tmp_dir, "invoices_{}_{}.csv".format(partner, end_date)) step("start date ............ {}", start_date) step("end date .............. {}", end_date) step("partner ref ........... {}", partner) step("contracts file ........ {}", contractsfile) step("bills file ............ {}", billsfile) step("language .............. {}", partner_lang) build( start_date, end_date, partner, contractsfile, billsfile, lang=partner_lang) step("contract file ......... {}", contractsfile) step("invoices file ......... {}", billsfile) with ZipFile(output, 'w') as zipObj: zipObj.write(contractsfile, basename(contractsfile)) zipObj.write(billsfile, basename(billsfile)) step("zip file generated .... {}", output) shutil.rmtree(tmp_dir)
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 main(): if len(sys.argv)<3: fail("Wrong arguments\n{}".format(usage)) a,b = (Path(x) for x in sys.argv[1:3]) output = Path(sys.argv[3]) if len(sys.argv)>3 else None tmpchanges("start") print(visualEqual(a,b,output)) tmpchanges("end")
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 resum_qc(text_evol): from consolemsg import fail args = parseargs() if not (args.date and args.date_end): fail( "Introdueix una data fins on fer l'estudi i la data on iniciar a fer l'estudi de ccvv" ) contractesTarifa('3.0A', args.date) contractes_nif('ESH', '3.0A', args.date, args.date_end) contractes_nif('ESF', '3.0A', args.date, args.date_end) contractes_nif('ESG', '3.0A', args.date, args.date_end) contractes_nif('ESP', '3.0A', args.date, args.date_end) contractesTarifa('3.1A', args.date)
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 runBack2BackProgram(datapath, argv, back2BackCases, help=help, extensions={}) : "--help" not in argv or fail(help, 0) architectureSpecific = "--arch" in argv if architectureSpecific : argv.remove("--arch") os.access( datapath, os.X_OK ) or fail( "Datapath at '%s' not available. "%datapath + "Check the back 2 back script on information on how to obtain it.") availableCases = [case for case, command, outputs in back2BackCases] if "--list" in argv : sys.stdout.write("Available cases:\n") sys.stdout.write(_caseList(availableCases)) sys.exit() if "--accept" in argv : cases = argv[argv.index("--accept")+1:] cases or fail( "Option --accept needs a set of cases to accept.\n" "Available cases:\n"+ _caseList((case for case, command, outputs in back2BackCases)) ) unsupportedCases = set(cases).difference(set(availableCases)) unsupportedCases and fail( "The following specified cases are not available:\n" + _caseList(unsupportedCases) + "Try with:\n" + _caseList(availableCases) ) accept(datapath, back2BackCases, architectureSpecific, cases) sys.exit() if "--acceptall" in argv : warn("Accepting any faling case") accept(datapath, back2BackCases, architectureSpecific) sys.exit() passB2BTests(datapath, back2BackCases, extensions=extensions) or fail("Tests not passed")
def __init__(self, documentName, credentialFilename): from oauth2client.service_account import ServiceAccountCredentials try: credentials = ServiceAccountCredentials.from_json_keyfile_name( credentialFilename, scopes=['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive',], ) except Exception as e: fail(str(e)) gc = gspread.authorize(credentials) try: self.doc = gc.open(documentName) except: credentialContent = json.load(io.open(credentialFilename)) error("No s'ha trobat el document, o no li has donat permisos a l'aplicacio") error("Cal compartir el document '{}' amb el següent correu:" .format(documentName,credentialContent['client_email'])) error(str(e)) sys.exit(-1)
def __init__(self, documentName, credentialFilename): from oauth2client.service_account import ServiceAccountCredentials try: credentials = ServiceAccountCredentials.from_json_keyfile_name( credentialFilename, scopes=[ 'https://spreadsheets.google.com/feeds', ], ) except Exception as e: fail(str(e)) gc = gspread.authorize(credentials) try: self.doc = gc.open(documentName) except: error( "No s'ha trobat el document, o no li has donat permisos a l'aplicacio" ) error( "Cal compartir el document '{}' amb el següent correu:".format( documentName, json_key['client_email'])) error(str(e)) sys.exit(-1)
polissa_id = O.GiscedataPolissa.search([('name', '=', pol)])[0] polissas_id.append(polissa_id) except Exception as e: error("ERROR la polissa {} no ha estat trobada l'ERP", pol) error(unicode(e)) return polissas_id step("Cercant polisses endarrerides") polissaEndarrerida_ids = get_polissa_id_from_polissa_name(polissas_file) _polissaEndarrerida_ids = contractOutOfBatchDate() polissaEndarrerida_ids_len = len(polissaEndarrerida_ids) step("Adelantant {} polisses", polissaEndarrerida_ids_len) if polissaEndarrerida_ids_len == 0: fail("Cap polissa per adelantar!") polisses = Contract.read(polissaEndarrerida_ids, [ 'name', 'data_alta', 'tarifa', 'comptadors', 'data_ultima_lectura', 'lot_facturacio', 'pagador', 'cups', ]) result = ns() result.contractsWithPreviousDraftInvoices = [] result.contractsWithError = []
from trello.board import Board import trellovariables import re import sys import argparse from consolemsg import step, out, warn, printStdOut, color, fail from yamlns import namespace as ns #Constants time position in card TIME_SPEND = 1 TIME_ESTIMATED = 2 action = sys.argv[1].lower() if action not in ('prioritize', 'clean'): fail("Action '{}' not allowed, should be either 'priorize' or 'clean'", -1, action) subaction = sys.argv[2].lower() if subaction not in ('show', 'apply'): fail("Subaction shoult be one of 'show' or 'apply', not '{}'", -1, subaction) boardfilter = sys.argv[3].lower() listfilter = sys.argv[4].lower() #SETUP client = TrelloClient( api_key=trellovariables.trello_api['api_key'], api_secret=trellovariables.trello_api['api_secret'], )
def parseargs(): import argparse parser = argparse.ArgumentParser(description='Reimportar els F1') parser.add_argument( '-d', '--date', help="Escull data des de que comencem a fer la cerca", ) return parser.parse_args() args = parseargs() if not args.date: fail("Introdueix una data de descarrega") vals_search = [('state', '=', 'erroni')] if args.date: data_carrega = args.date def valid_date(date_text): from datetime import datetime try: datetime.strptime(date_text, '%Y-%m-%d') except ValueError: raise ValueError("Incorrect data format, should be YYYY-MM-DD") return True data_carrega = data_carrega if data_carrega and valid_date(
print "Corbes obtingudes TM_PROFILE: " + str( mongo_data_tmprofile.count()) print "Les corbes disponibles s'han pujat a " + server def parseargs(): import argparse parser = argparse.ArgumentParser(description='Copiar corbes a Testing') parser.add_argument( '-c', '--cups', help="Escull per cups", ) parser.add_argument( '-s', '--server', help="Escull un servidor destí", ) return parser.parse_args() if __name__ == '__main__': args = parseargs() if not args.cups: fail("Introdueix un cups o el missatge d'error o una data") if not args.server: fail("Introdueix un servidor a on copiar les corbes") main(args.cups, args.server)
data_inicial = mod_actual.data_inici data_inici = datetime.strptime(mod_actual.data_inici, '%Y-%m-%d') data_inici = datetime.strftime(data_inici + timedelta(b), '%Y-%m-%d') try: mod_actual.write({'data_inici': data_inici}) except Exception as e: printTiException( e, "Mod contractual ACTUAL modificant data_final per:" "\n -dies: " + str(dies) + "\n -id:" + str(mod_actual_id) + "\n -data:" + str(data_inici)) args = parseargs() if not args.cups and not args.info and not args.date: fail("Introdueix un cups o el missatge d'error o una data") vals_search = [ ('state', '=', 'erroni'), # ('state','in',['erroni',False]), ] + ([('cups_id.name', '=', args.cups)] if args.cups else []) + ([('info', 'like', args.info)] if args.info else []) if args.date: data_carrega = args.date data_carrega = data_carrega if data_carrega and valid_date( data_carrega) else None if data_carrega: vals_search += [('data_carrega', '>=', data_carrega)] if args.dend:
# @skipIfNoPersonalDataAccess() def test_debugCase_rejected_c1(self) : self._test_debugCase('8091', 'rejected_c1') # @skipIfNoPersonalDataAccess() def test_debugCase_dropout_c1_differentCreationAndActivationMonth(self) : self._test_debugCase('8166', 'dropout_c1_differentCreationAndActivationMonth') # @skipIfNoPersonalDataAccess() def test_debugCase_activated_b1(self) : self._test_debugCase('15235', 'activated_b1') # @skipIfNoPersonalDataAccess() def test_debugCase_cancelpending_c2(self) : self._test_debugCase('4149', 'cancelpending_c2') if __name__ == '__main__' : from dbconfig import psycopg as config db = psycopg2.connect(**config) if len(sys.argv) is not 2: fail("Usage: {} <caseid>".format(sys.argv[0])) print debugCase(db, sys.argv[1]).dump()
args = vars(parser.parse_args()) start_date = args['startdate'] end_date = args['enddate'] contract_name = args['contractname'] def valid_date(date_text): try: datetime.strptime(date_text, '%Y-%m-%d') except ValueError: raise ValueError("Incorrect data format, should be YYYY-MM-DD") return True O = lazyOOOP() if not contract_name: fail("Contracte name missing") start_date = start_date if start_date and valid_date(start_date) else None end_date = end_date if end_date and valid_date(end_date) else None contract_id = O.GiscedataPolissa.search([('name', '=', contract_name)])[0] quarantine = {'kWh': [], 'euro': []} old_measures = get_measures_by_contract(O, contract_id, range(1, 12)) if old_measures == []: fail("El compatador actiu no té mesures") new_measures = load_new_measures(O, contract_id) if old_measures[0]['origen_id'][0] not in [7, 10, 11]: end_date = old_measures[0]['name']
help="Nom de la pòlissa") parser.add_argument( '--from-date', dest='from_date', required=True, help="Data a partir de la qual actualitzar l'adjunt dels F1 de testing" ) parser.add_argument( '-s', '--server', help="Escull un servidor destí", ) parser.add_argument( '--output', dest='output', type=str, help="Output csv file", ) return parser.parse_args() if __name__ == '__main__': args = parseargs() if not args.csv_file and not args.polissa_name: fail( "Introdueix el fitxer amb els números de contracte o bé un número de contracte" ) main(args.csv_file, args.output, args.from_date, args.server, args.polissa_name)