""" main data importer module """ import sys import os import os.path import csv import ImporterConst as const import ImporterUtil as util ProcMap = util.readProcMap(const.ProcMapFile) def dstExistsFunc(dst): util.dumpString('WARNING: destination file {} already exists'.format(dst)) return True def ImportClients(dbQuery, dstQuery, directory, **kwargs): sql = const.SqlQueries['clients'] result = dbQuery.query(sql) #util.dumpCsv(file Tmpl, result) util.storeDataDict(dstQuery, result, const.SQL_TEMP_TABLE_CLIENT) def ImportDetails(dbQuery, dstQuery, directory, **kwargs): sql = const.SqlQueries['parts1'] result = dbQuery.query(sql) dst_dir = os.path.join(directory, const.ImageDstDir) def processRow(row, fields): result = row.copy() result['weight'] = util.parseFloat(row['weight']) for fieldName, fieldSizeName, realImg in [('imageThumb', 'imageThumbSize', False), ('imageMain', 'imageMainSize', True)]: imagename = util.findFileInRoot(const.ImageSourceDir, row[fieldName], pathHint=row['pathHint'] if realImg else '') imagesize = 0 if imagename != '': custom = const.ImageMainTmpl.format(row['partId']) if realImg else '' imagename, imagesize = util.copyFile(imagename, dst_dir, partId=custom, dstExists=dstExistsFunc) result[fieldName] = imagename result[fieldSizeName] = imagesize return result result = util.processDataDict(result, processRow) #util.dumpCsv(file Tmpl.format(type='part'), result) util.storeDataDict(dstQuery, result, const.SQL_TEMP_TABLE_PART) ##### sql = const.SqlQueries['parts2'] result = dbQuery.query(sql) result = util.processDataDict(result, lambda r, f: util.processRowDeptProcess(r, f, ['DeptID', 'ProcessID'], ['Dept1', 'Dept2', 'Process'], ProcMap)) #util.dumpCsv(file Tmpl.format(type='process'), result) util.storeDataDict(dstQuery, result, const.SQL_TEMP_TABLE_PROCESS_PARTS) def ImportForms(dbQuery, dstQuery, directory, **kwargs): sql = const.SqlQueries['forms1'] result = dbQuery.query(sql) result = util.processDataDict(result, lambda r, f: util.processRowDeptProcess(r, f, ['DeptID', 'ProcessID'], ['Dept1', 'Dept2', 'Process'], ProcMap)) #util.dumpCsv(file Tmpl.format(type='process'), result) util.storeDataDict(dstQuery, result, const.SQL_TEMP_TABLE_PROCESS) ##### sql = const.SqlQueries['forms2'] result = dbQuery.query(sql) dst_dir = os.path.join(directory, const.ImageDstDir) def processRow(row, fields): result = util.processRowDeptProcess(row, fields, ['DeptID', 'ProcessID'], ['Dept1', 'Dept2', 'Process'], ProcMap) control_entry = const.FormControlMap.get(result['typeId'], const.FormControlEmpty) result['typeId'] = control_entry['new_id'] result = control_entry['processRow'](result, fields, filesDir=dst_dir, dstExists=dstExistsFunc) return result result = util.processDataDict(result, processRow) #util.dumpCsv(file Tmpl.format(type='elements'), result) util.storeDataDict(dstQuery, result, const.SQL_TEMP_TABLE_PROCESS_ELEMENTS) def ImportFormData(dbQuery, dstQuery, directory, **kwargs): sql_list = const.SqlQueries['formdata'] dst_dir = os.path.join(directory, const.ImageDstDir) for entry in sql_list: result = dbQuery.query(entry['query']) check_path_hint = entry['check_path_hint'] aux_key_name = '' key_name = '' aux_dict = {} if entry['aux_query'] != '': aux_key_name = entry['aux_key'] key_name = entry['key'] aux_dict = dbQuery.query(entry['aux_query']) aux_dict = util.transposeDataDict(aux_dict, aux_key_name) def processRow(row, fields): result = util.processRowDeptProcess(row, fields, ['DeptID', 'ProcessID'], ['Dept1', 'Dept2', 'Process'], ProcMap) control_entry = const.FormControlMap.get(result['typeId'], const.FormControlEmpty) result['typeId'] = control_entry['new_id'] if key_name != '': key = result[key_name] aux_row = aux_dict.get(key, None) if aux_row is not None: result['resultValue'] = aux_row.get(result['fieldName'], '') if check_path_hint: result = control_entry['processData'](result, fields, filesDir=dst_dir, checkAnn=True, pathHintName='pathHint', partIdName='partId', dstExists=dstExistsFunc) else: result = control_entry['processData'](result, fields, filesDir=dst_dir, checkAnn=True, dstExists=dstExistsFunc) return result result = util.processDataDict(result, processRow) #util.dumpCsv(file Tmpl.format(source=entry['source']), result, dialect=csv.excel) util.storeDataDict(dstQuery, result, const.SQL_TEMP_TABLE_PROCESS_DATA) ImportFuncs = [ { 'title': 'Cache files: {status}', 'func': lambda q, q2, wd, **kwargs: util.loadFileList(q) }, { 'title': 'Creating temp tables: {status}', 'func': lambda q, q2, wd, **kwargs: util.createTempTables(q2) }, { 'title': 'Clients: {status}', 'func': ImportClients }, { 'title': 'Details: {status}', 'func': ImportDetails, }, { 'title': 'Forms: {status}', 'func': ImportForms }, { 'title': 'Form data: {status}', 'func': ImportFormData }, ] if __name__ == '__main__': if len(sys.argv) < 2: print 'Usage: {} '.format(sys.argv[0]) sys.exit(1) work_dir_name = sys.argv[1] if not os.path.exists(work_dir_name): os.makedirs(work_dir_name) src_query = util.DbQuery(const.ConnectionString, parameter_prefix='@') dst_query = util.DbQuery(const.ConnectionStringDest, parameter_prefix='@') for i, import_func in enumerate(ImportFuncs, 0): func = import_func['func'] #tmpl = import_func['tmpl'] title = import_func['title'] #fileTmpl = os.path.join(work_dir_name, tmpl.format(index=i)) util.dumpString(title, status='start') func(src_query, dst_query, work_dir_name, procMap=ProcMap) util.dumpString(title, status='finish')