def __create_directory__(data_tuple, setAsCurrent=True, create=True, islocation=True): ''' Sets the location of the data directory in the emel.config file. ''' config = __validate_config__() data_tuple = data_tuple if isinstance(data_tuple, list) else [data_tuple] if not data_tuple: print '[ERROR] Please supply a lable for this data directory.' exit() lable = data_tuple[0] location = create_path([data_tuple[1],lable]) if len(data_tuple) > 1 else create_path([os.getcwd(), lable]) if create: print 'Attempting to create', lable, 'at location', location, current = create_dir(location, True, True) if current: create_marker(current, Data.MARKER) create_marker(current, INIT_MARKER) if not os.path.isdir(location): print '\n[ERROR] "{0}" is not a valid path.'.format(location) exit() if current and setAsCurrent: config[Data.SECTION][Data.CURRENT] = lable if current and lable not in config[Data.SECTION][Data.ALL]: config[Data.SECTION][Data.ALL][lable] = location print 'Done.' config.write()
def __create_tool__(catagory, tool_name): config = __validate_config__() data_dir = config[Data.SECTION][Data.ALL][config[Data.SECTION][Data.CURRENT]] project = config[Project.SECTION][Project.CURRENT] new_cat_path = create_path([emel_tools_file_path(), catagory]) if catagory not in __get_catagories__(False): choice = yes_no_option('"{0}" is not a catagory. Would you like to create it ?'.format(catagory)) if choice: create_dir(new_cat_path, verbose=True) create_marker(new_cat_path, '__init__.py') else: print '[WARNING] emel did not create {0}->{1}. user aborted.'.format(catagory, tool_name) exit() if os.path.exists(create_path([new_cat_path, tool_name + '.py'])): print '[WARNING] {0}->{1} all ready exists.'.format(catagory, tool_name) exit() fp = open( create_path([new_cat_path, tool_name+'.py']), 'w' ) fp.write( TOOL_TEMPLATE.format(EMEL_UTILS_FILE, tool_name, re.sub('\\\\', '/', new_cat_path)) ) fp.close() # add tool to the __init__.py in the tools folder. fp = open( create_path([emel_tools_file_path(), '__init__.py']), 'a') fp.write( 'import {}.{}\n'.format(catagory, tool_name) ) fp.close() print 'Created a new tool at {0}/{1}.py'.format(re.sub('\\\\', '/',new_cat_path), tool_name) if yes_no_option('Would you like to edit the tool now?'): __run_editor__([os.path.join(new_cat_path, tool_name + '.py')])
def __edit_dependency__(): depFile = create_path([emel_project_path(), DEPENDENCY_FILE]) if not os.path.exists(depFile): print 'dependency file not created.' print 'please run emel.py dependency -h for more help' exit() __run_editor__([depFile])
def __new_project__(newProject): ''' Creates a new project in the current data directory. ''' config = __validate_config__() if newProject in __list_projects__(): print '"', newProject, '" allready exists' exit() config[Project.SECTION][Project.CURRENT] = newProject config.write() dataDir = config[Data.SECTION][Data.CURRENT] projectDir = create_path([dataDir, newProject]) create_dir(projectDir) create_marker( projectDir, Project.MARKER ) create_marker( projectDir, INIT_MARKER ) # Create all nessesary folders. create_dir(emel_raw_file_path(), verbose=True) create_dir(emel_processed_file_path(), verbose=True) create_dir(emel_train_file_path(), verbose=True) create_dir(emel_tools_file_path(), verbose=True) # Create nessesary tool dirs in the project/tools folder. create_marker(emel_tools_file_path(), '__init__.py') print '"{0}" Successfully created.'.format(newProject)
def __create_train__(): message = ('[WARNING] This will destroy any existing train objects. Continue?') go = yes_no_option(message) if go: trainPath = emel_train_file_path() train_object = TRAIN_TEMPLATE.format(EMEL_UTILS_FILE) print 'Creating default train order ...', with open(create_path([trainPath, TRAIN_ORDER_NAME]), 'w') as fp: fp.write(DEFAULT_TRAIN_LIST) print 'Done.' print 'Creating train object ...', with open( create_path([trainPath, TRAIN_OBJECT_NAME]), 'w' ) as fp: fp.write(train_object) create_marker( trainPath, INIT_MARKER ) print 'Done.' else: print 'Aborting'
def __get_list__(): the_list = DEFULT if check_project_status(): depFile = create_path([emel_project_path(), DEPENDENCY_FILE]) if os.path.exists(depFile): the_list = [] with open(depFile, 'r') as f: for line in f.readlines(): if not line.startswith('#'): line = line.split('#') the_list.append(line[0].strip()) return the_list
def __list_current_train__(): trainPath = emel_train_file_path() trainOrderPath = create_path([trainPath, TRAIN_ORDER_NAME]) if not os.path.exists(trainOrderPath): print '[ERROR] the train object is not properly set up.' print 'please run train -n' exit() train_order = imp.load_source('train_order', trainOrderPath) print '"{}"'.format(train_order.description) for function in train_order.order: print '\t', function
def __edit_current_train__(show='both'): trainPath = emel_train_file_path() trainOrder = create_path([trainPath, TRAIN_ORDER_NAME]) trainObject = create_path([trainPath, TRAIN_OBJECT_NAME]) if not os.path.exists(trainOrder): print '[ERROR] Could not find {}.'.format(TRAIN_ORDER_NAME) print 'please run train -n first.' if not os.path.exists(trainObject): print '[ERROR] Could not find {}.'.format(TRAIN_OBJECT_NAME) print 'please run train -n first.' to_open = [] if show == 'both': to_open = [trainOrder, trainObject] if show == 'order': to_open = [trainOrder] if show == 'object': to_open = [trainObject] __run_editor__(to_open)
def __run_current_train__(): config = __validate_config__() trainPath = emel_train_file_path() # Create a backup train folder folderName = datetime.now().strftime(TRAIN_BKUP_TEMPlATE) backupPath = create_dir(create_path([trainPath, folderName])) print 'Creating backup folder "{}" ...'.format(backupPath), print 'Done.' if backupPath: config[BACKUP] = backupPath trainOrderPath = create_path([trainPath, TRAIN_ORDER_NAME]) trainObjectPath = create_path([trainPath, TRAIN_OBJECT_NAME]) # copy over the train order and train object files. print 'Backing up the train order ...', subprocess.call(['cp', trainOrderPath, backupPath]) print 'Done.' print 'Backing up train object ...', subprocess.call(['cp', trainObjectPath, backupPath]) print 'Done.' train_order = imp.load_source('train_order', trainOrderPath) train_object = imp.load_source('train', trainObjectPath) train = train_object.Train() f = open(create_path([backupPath, 'time.txt']), 'w') for function in train_order.order: print 'Running', function start = datetime.now() getattr(train, function)(**train_order.args[function]) f.write('Function "{}" (Took: {})\n'.format(function, datetime.now() - start)) f.close() else: print '[ERROR] Could not create backup train folder.'
def __create_dep_list__(check_first=True): if not check_directory_status(True): exit() if not check_project_status(True): exit() create = True if check_first: message = 'Creatint dependency file for project.\n [WARNING] will overrite existing file. Continue?' create = yes_no_option(message) if create: depFile = create_path([emel_project_path(), DEPENDENCY_FILE]) with open(depFile, 'w') as f: f.write('# emel default packages #\n') for module in DEFULT: f.write(module) f.write('\n') f.write('# End emel defaults #')