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 __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 __get_catagories__(verbose=True): config = __validate_config__() data_dir = config[Data.SECTION][Data.ALL][config[Data.SECTION][Data.CURRENT]] project = config[Project.SECTION][Project.CURRENT] tools_path = emel_tools_file_path() files = [ root for root, _, files in os.walk(tools_path) ] files = [ os.path.split(f)[-1] for f in files[1:] ] if verbose: print 'Catagories:' for f in files: print '\t', f return files
def __list_tools__(verbose=True): config = __validate_config__() data_dir = config[Data.SECTION][Data.ALL][config[Data.SECTION][Data.CURRENT]] project = config[Project.SECTION][Project.CURRENT] tools_path = emel_tools_file_path() print "Catagories" for root, d, files in os.walk(tools_path): catagory = os.path.split(root)[-1] if catagory != 'tools': print '\t', catagory files = [ f for f in files if not (f.startswith('__') or f.endswith('.pyc')) ] for f in files: print '\t\t', f
def __edit_tool__(pattern): config = __validate_config__() data_dir = config[Data.SECTION][Data.ALL][config[Data.SECTION][Data.CURRENT]] project = config[Project.SECTION][Project.CURRENT] tools_path = emel_tools_file_path() regex = re.compile(pattern) tools = [] append_tools = tools.append for root, d, files in os.walk(tools_path): files = [f for f in files if not f.startswith('__')] for f in files: if regex.search(f): append_tools(os.path.join(root, f)) if not tools: print 'Did not find any tools matching the pattern', pattern else: tools = [tool for tool in tools if tool.endswith('.py')] if len(tools) == 1: __run_editor__(tools) else: print 'Found (', len(tools), ') matching the pattern "', pattern, '"' print 'Please choose:' print 'a - all' print 'q - cancel' for i, j in enumerate(tools): print i,'-', j choice = '' while choice not in ['a', 'q'] + [str(i) for i in range(len(tools))]: choice = raw_input('[ ') if choice == 'q': print 'User aborted.' exit() elif choice == 'a': __run_editor__(tools) elif choice in [str(i) for i in range(len(tools))]: __run_editor__([tools[int(choice)]])