def genCollectionRpt(optiondict, debug=False): # message what we are running if optiondict['verbose']: print('genCollectionRpt') ### WINE COLLECTION ### winecollection = loadWineCollection(optiondict, debug=debug) # messaging if optiondict['verbose']: print('genCollectionRpt:winecollection:', len(winecollection)) ### COLLECTION_XREF ### collection_xref = loadCollectionXrefDict( optiondict['collection_xref_file'], skipIgnore=True, debug=debug) # messaging if optiondict['verbose']: print('genCollectionRpt:collection_xref:', len(collection_xref)) ### WINE_XREF ### # wine_xref = kvcsv.readcsv2dict( optiondict['wine_xref_file'], ['winedescr'] ) ### WINE_INV ### wine_inv = calcWineSummary(winecollection, collection_xref, optiondict, debug=debug) # set filename if not set if not optiondict['collection_out_file']: optiondict['collection_out_file'] = optiondict['collection_file'] # save this data out fldlist = [ 'name', 'cnt', 'price_min', 'price_avg', 'value', 'consumed', 'in_inv', 'wine_xref', 'total_spend', 'wine_type' ] kvcsv.writedict2csv(optiondict['collection_out_file'], wine_inv, fldlist) if optiondict['verbose']: print('genCollectionRpt:File created:', optiondict['collection_out_file'])
def migrate_stays_to_history( occupy_filename, occupy_history_filename, fldDate, debug=False ): # log that we are doing this work # load stay history if os.path.isfile( occupy_history_filename ): logger.info('migrate_stays_to_history:load file:%s', occupy_history_filename) stays_history = villaecobee.load_villa_calendar( occupy_history_filename, fldDate, debug=False ) else: logger.info('migrate_stays_to_history:file does not exist:%s', occupy_history_filename) stays_history = dict() # load current stay information stays = villaecobee.load_villa_calendar( occupy_filename, fldDate, debug=False ) logger.info('migrate_stays_to_history:load file:%s', occupy_filename) # capture today today = datetime.datetime.today() # and capture the number of records add to the stay history records_added=0 # step through the stays file and look for past due dates for staydate in stays: if datetime.datetime.strptime(staydate, datefmt) < today: # this date is in the past - see if this date is in the history already if staydate not in stays_history: # add this date to stays history stays_history[staydate] = stays[staydate] # increment the counter records_added += 1 # debugging message logger.debug('migrate_stays_to_history:date added:%s', staydate) else: # debugging message logger.debug('migrate_stays_to_history:date skipped:%s', staydate) # loop through - now if we added records we need to save stay history data if records_added: logger.info('migrate_stays_to_history:records added to history:%d', records_added) kvcsv.writedict2csv( occupy_history_filename, stays_history ) else: logger.info('migrate_stays_to_history:no records added to history')
def updateCollectionXrefDict(optiondict, debug=False): # message what we are running if optiondict['verbose']: print('updateCollectionXrefDict') ### COLLECTION_XREF ### collection_xref = loadCollectionXrefDict( optiondict['collection_xref_file'], debug=debug) # messaging if optiondict['verbose']: print('updateCollectionXrefDict:collection_xref:', len(collection_xref)) ### COLLECTION #### collection = kvcsv.readcsv2dict(optiondict['collection_file'], ['name']) for wine, value in collection.items(): if value['wine_xref'][:1] == '*': if debug: print('collection - destarred:', wine) collection[wine]['wine_xref'] = value['wine_xref'][1:] # messaging if optiondict['verbose']: print('updateCollectionXrefDict:collection:', len(collection)) ### UPDATE ### updates = updateCollectionXref(collection_xref, collection, debug=debug) ### OUTPUT ### fldlist = ['name', 'wine_xref'] kvcsv.writedict2csv(optiondict['collection_xref_file'], collection_xref, fldlist) if optiondict['verbose']: print('updateCollectionXrefDict:Records updated:', updates) print('updateCollectionXrefDict:File created:', optiondict['collection_xref_file'])
def load_and_process_files( file_input, file_del, del_flag, delreckey, delencoding, inputreckey, inputencoding, dellastseenfld, inputdatefld, inputheader=None, headerlc=False, debug=False ): if debug: print('file_del:', file_del) print('delreckey:', delreckey) # first read in the file_del - into a dictionary that is wine_store+wine_name keyed delRecs, delDictKeys, delDupCount = kvcsv.readcsv2dict_with_header( file_del, delreckey, headerlc=headerlc, encoding=delencoding, debug=debug) if debug: #print('delRecs:', delRecs) print('delDictKeys:', delDictKeys) print('delDupCount:', delDupCount) if debug: print('file_input:', file_input) print('inputreckey:', inputreckey) print('inputheader:', inputheader) # load the input file keyed by the process_date, wine_store, wine_name if inputheader: inputRecs, inputDictKeys, inputDupCount = kvcsv.readcsv2dict_with_noheader( file_input, inputreckey, header=inputheader, encoding=inputencoding, debug=debug) else: inputRecs, inputDictKeys, inputDupCount = kvcsv.readcsv2dict_with_header( file_input, inputreckey, headerlc=headerlc, encoding=inputencoding, debug=debug) # calc the number of input records we processed inputRecCount = len(inputRecs) + inputDupCount if debug: # print('inputRecs:', inputRecs) print('inputDictKeys:', inputDictKeys) print('inputDupCount:', inputDupCount) # set the variables delRecCount = 0 # step through input records and see if the match a delete entry for inputkey in list(inputRecs.keys()): # built up the store + name key from the process_date + store + name key storename = '|'.join(inputkey.split('|')[1:]) # debugging if 0 and debug: print('storename:', storename, ':inputkey:', inputkey) # if this is a match if storename in delRecs: if 0 and debug: print('match to be deleted:', storename, ':', inputkey) # update the last seen field in delRecs delRecs[storename][dellastseenfld] = inputRecs[inputkey][inputdatefld] # count this delete delRecCount += 1 # remove the record del inputRecs[inputkey] # if we deleted records - then we updated file_del and we need to save it if delRecCount: if 0 and debug: print('saving updates into:', file_del) kvcsv.writedict2csv( file_del, delRecs, delDictKeys, encoding=delencoding, debug=debug ) ### TODO - replace dict returned with a list of values - not sure why i had to return it this way # return what we just extracted return {'inputRecs' : inputRecs, 'inputDictKeys' : inputDictKeys, 'inputDupCount' : inputDupCount, 'delRecCount' : delRecCount, 'inputRecCount' : inputRecCount }
if test: print ('---------------TEST FLAG ENABLED---------------------------') # check to see if we passed in srchlist instead of srchstring if optiondict['file_append']: print('file_append was passed in INSTEAD of append_flag - substituting') optiondict['append_flag'] = optiondict['file_append'] # load and process the files results = load_and_process_files( optiondict['file_input'], optiondict['file_del'], optiondict['del_flag'], optiondict['delreckey'], optiondict['delencoding'], optiondict['inputreckey'], optiondict['inputencoding'], optiondict['dellastseenfld'], optiondict['inputdatefld'], inputheader=optiondict['inputheader'], headerlc=optiondict['headerlc'], debug=False ) # output what we just got in if optiondict['append_flag']: kvcsv.writedict2csv( optiondict['file_output'], results['inputRecs'], results['inputDictKeys'], mode='a', header=False, encoding=optiondict['inputencoding'], debug=False ) else: kvcsv.writedict2csv( optiondict['file_output'], results['inputRecs'], results['inputDictKeys'], encoding=optiondict['inputencoding'], debug=False ) if optiondict['rename_flag']: rename_file(optiondict['file_input'], optiondict['rename_ext'], optiondict['rename_file']) # final summary print('wine_dedupe.py: Results--------------------------') print('Input file....:', optiondict['file_input']) print('Del file......:', optiondict['file_del']) print('Output file...:', optiondict['file_output']) print('Append flag...:', optiondict['append_flag']) print('Input records.:', results['inputRecCount']) print('Dup records...:', results['inputDupCount']) print('Del records...:', results['delRecCount'])