def new_page_object(root_path, project, parents, page_name, add_parents=False): """Create a new page object. Parents is a list of directories and subdirectories where the page should be placed. if add_parents is true, the parent directories will be added if they do not exist.""" errors = [] full_page_path = os.path.join(root_path, 'projects', project, 'pages', os.sep.join(parents), '{}.py'.format(page_name)) if os.path.isfile(full_page_path): errors.append('A page file with that name already exists') if not errors: page_path = os.path.join(root_path, 'projects', project, 'pages', os.sep.join(parents)) if not os.path.exists(page_path): if add_parents: base_path = os.path.join(root_path, 'projects', project, 'pages') for parent in parents: base_path = os.path.join(base_path, parent) if not os.path.exists(base_path): utils.create_new_directory(path=base_path, add_init=True) else: errors.append('Directory for new page does not exist') if not errors: with open(full_page_path, 'w') as po_file: po_file.write('') return errors
def new_directory_test_case(root_path, project, parents, test_name): parents = os.sep.join(parents) errors = [] if directory_already_exists(root_path, project, 'tests', parents, test_name): errors.append('A directory with that name already exists') else: path_list = [ root_path, 'projects', project, 'tests', parents, test_name ] utils.create_new_directory(path_list=path_list, add_init=True) return errors
def new_directory(root_path, project, parents, dir_name, dir_type): """Creates a new directory for suites, tests or pages. if the directory is inside other directories, these should already exist. parents is a list of parent directories. dir_type should be in: ['tests', 'suites', 'pages']""" parents = os.sep.join(parents) path = os.path.join(root_path, 'projects', project, dir_type, parents, dir_name) errors = [] if os.path.exists(path): errors.append('A directory with that name already exists') else: utils.create_new_directory(path=path, add_init=True) return errors
def new_test_case(root_path, project, parents, tc_name): test_case_content = ( "\n" "description = ''\n\n" "pages = []\n\n" "def setup(data):\n" " pass\n\n" "def test(data):\n" " pass\n\n" "def teardown(data):\n" " pass\n\n") errors = [] # check if a file already exists path = os.path.join(root_path, 'projects', project, 'tests', os.sep.join(parents), '{}.py'.format(tc_name)) if os.path.isfile(path): errors.append('A test with that name already exists') if not errors: parents_joined = os.sep.join(parents) base_path = os.path.join(root_path, 'projects', project, 'tests') test_case_path = os.path.join(base_path, parents_joined) # create the directory structure if it does not exist if not os.path.exists(test_case_path): for parent in parents: base_path = os.path.join(base_path, parent) utils.create_new_directory(path=base_path, add_init=True) test_case_full_path = os.path.join(test_case_path, tc_name + '.py') # TODO remove create data file on test creation # data_path = os.path.join(root_path, 'projects', project, 'data', parents_joined) # if not os.path.exists(data_path): # os.makedirs(data_path) # data_full_path = os.path.join(data_path, tc_name + '.csv') with open(test_case_full_path, 'w') as test_file: test_file.write(test_case_content) # TODO remove create data file on test creation # with open(data_full_path, 'w') as data_file: # data_file.write('') print('Test {} created for project {}'.format(tc_name, project)) return errors
def new_page_object(root_path, project, parents, po_name): errors = [] path = os.path.join(root_path, 'projects', project, 'pages', os.sep.join(parents), '{}.py'.format(po_name)) if os.path.isfile(path): errors.append('A file with that name already exists') if not errors: parents_joined = os.sep.join(parents) base_path = os.path.join(root_path, 'projects', project, 'pages') page_object_path = os.path.join(base_path, parents_joined) # create the directory structure (with __init__.py files) if it does not exist if not os.path.exists(page_object_path): for parent in parents: base_path = os.path.join(base_path, parent) utils.create_new_directory(path=base_path, add_init=True) page_object_full_path = os.path.join(page_object_path, po_name + '.py') with open(page_object_full_path, 'w') as f: f.write('') return errors
def rename_element(): if request.method == 'POST': project = request.form['project'] elem_type = request.form['elemType'] full_filename = request.form['fullFilename'] new_full_filename = request.form['newFullFilename'] error = '' old_filename, old_parents = utils.separate_file_from_parents( full_filename) new_filename, new_parents = utils.separate_file_from_parents( new_full_filename) if len(new_filename) == 0: error = 'File name cannot be empty' else: for c in new_full_filename.replace('.', ''): if not c.isalnum() and not c in ['-', '_']: error = 'Only letters, numbers, \'-\' and \'_\' are allowed' break dir_type_name = '' if not error: if elem_type == 'test': dir_type_name = 'tests' elif elem_type == 'page': dir_type_name = 'pages' elif elem_type == 'suite': dir_type_name = 'suites' old_full_path = os.path.join(root_path, 'projects', project, dir_type_name, os.sep.join(old_parents), old_filename + '.py') new_dir_path = os.path.join(root_path, 'projects', project, dir_type_name, os.sep.join(new_parents)) new_full_path = os.path.join(new_dir_path, new_filename + '.py') if os.path.exists(new_full_path): error = 'File {} already exists'.format(new_full_filename) if not error: # create new parents if they do not exist if not os.path.exists(new_dir_path): base_path = os.path.join(root_path, 'projects', project, dir_type_name) for parent in new_parents: base_path = os.path.join(base_path, parent) if not os.path.exists(base_path): utils.create_new_directory(path=base_path, add_init=True) # try: os.rename(old_full_path, new_full_path) # except: # error = 'There was an error renaming \'{}\' to \'{}\''.format( # full_filename, new_full_filename) if not error and elem_type == 'test': try: old_data_full_path = os.path.join(root_path, 'projects', project, 'data', os.sep.join(old_parents), old_filename + '.csv') new_data_full_path = os.path.join(new_dir_path, new_filename + '.csv') if os.path.exists(old_data_full_path): shutil.move(old_data_full_path, new_data_full_path) except: pass return json.dumps(error)