def rel_path_to_abs(rel_path, rel_path_start): """ Will turn a relative path of a file to an absolute one (if this is an absolute path, the function will return it). Args: param rel_path: the relative path you want to turn to abs param rel_path_start: the path from which the rel path will be calculated """ import os_file_handler.file_handler as fh # if that's a relative source if rel_path.startswith('./'): return rel_path_to_abs(os.path.join(fh.get_parent_path(rel_path_start), rel_path[2:]), rel_path_start) else: parent_num = rel_path.count('../') if parent_num > 0: parent_dir = fh.get_parent_path(rel_path_start) for i in range(0, parent_num): parent_dir = fh.get_parent_path(parent_dir) rest_of_path_idx = rel_path.rindex('../') + 3 rest_of_path = rel_path[rest_of_path_idx:] return os.path.join(parent_dir, rest_of_path) else: return rel_path
def replace_text_in_file(file_src, file_dst, old_expression, new_expression, replace_whole_line=False, cancel_if_exists=False): lines = read_text_file(file_src, drop_new_lines=True) if cancel_if_exists and is_line_exists_in_text(new_expression, lines=lines): return with open(file_dst, 'w') as f: from os_file_handler import file_handler as fh parent_dir = fh.get_parent_path(file_dst) if not fh.is_dir_exists(parent_dir): fh.create_dir(parent_dir) for line in lines: if old_expression in line: if replace_whole_line: if new_expression == '': continue else: line = new_expression else: line = line.replace(old_expression, new_expression) f.write(f'{line}\n')
def delete_text_range_in_file(file_src, file_dst, from_text, to_text, include_bundaries=False): lines = read_text_file(file_src, drop_new_lines=True) with open(file_dst, 'w') as f: from os_file_handler import file_handler as fh parent_dir = fh.get_parent_path(file_dst) if not fh.is_dir_exists(parent_dir): fh.create_dir(parent_dir) from_text_found = False done = False for i in range(0, len(lines)): if done: f.write(f'{lines[i]}\n') continue if from_text in lines[i]: from_text_found = True if include_bundaries: f.write(f'{lines[i]}\n') if from_text_found and to_text in lines[i]: done = True if include_bundaries: f.write(f'{lines[i]}\n') if not from_text_found: f.write(f'{lines[i]}\n')
def create_xml_file(root_node_tag, output_file): xml = etree.Element(root_node_tag) tree = etree.ElementTree(xml) # create dir if not exists from os_file_handler import file_handler parent_dir = file_handler.get_parent_path(output_file) if not file_handler.is_dir_exists(parent_dir): file_handler.create_dir(parent_dir) save_xml_file(tree, output_file) # tree = read_xml_file(output_file) # maybe to read the xml again, to prevent the comments from being removed? return tree
def write_file(file_path, content): with open(file_path, 'w') as f: from os_file_handler import file_handler as fh parent_dir = fh.get_parent_path(file_path) if not fh.is_dir_exists(parent_dir): fh.create_dir(parent_dir) if isinstance(content, str): f.write(content) if isinstance(content, list): for line in content: if not str(line).endswith('\n'): line += '\n' f.write(line)
def create_launcher_icons(self): self.print_ln() self.logger.info( f'Preparing to work on {str(len(self.launcher_obj_list))} icons') time.sleep(1) tools.ask_for_input( 'Please open your custom project in Android Studio and inform me when you are done [done]' ) self.logger.info( f'Now go back to the project and wait {str(self.SECS_COOLDOWN_UNTIL_PROJECT_OPEN)} seconds...' ) time.sleep(self.SECS_COOLDOWN_UNTIL_PROJECT_OPEN) for i in range(0, len(self.launcher_obj_list)): curr_launcher_obj = self.launcher_obj_list[i] self.print_ln() launcher_name = fh.get_dir_name( fh.get_parent_path(curr_launcher_obj.icon_path)) self.logger.info(f'Working on: "{launcher_name}"...') # skip if exists if self.skip_if_output_dir_exists: if fh.is_dir_exists(curr_launcher_obj.output_path): self.logger.info( f'Launcher "{launcher_name}" output path already exists. Skipping!' ) continue # run current launcher res = self.run_cycle(self.launcher_obj_list[i]) # do something with the response if res: self.logger.info(f'Launcher {launcher_name} done!') else: tools.ask_for_input( "ERROR: It seems like the process failed for " + self.launcher_obj_list[i].icon_path + ". Let me know when you are ready to run on the same file [Enter]" ) i -= 1 self.print_ln()
def append_text_above_line_in_file(file_src, file_dst, above_line, new_expression, cancel_if_exists=False): lines = read_text_file(file_src, drop_new_lines=True) if cancel_if_exists and is_line_exists_in_text(new_expression, lines=lines): return with open(file_dst, 'w') as f: from os_file_handler import file_handler as fh parent_dir = fh.get_parent_path(file_dst) if not fh.is_dir_exists(parent_dir): fh.create_dir(parent_dir) for i in range(0, len(lines)): if above_line in lines[i]: f.write(f'{new_expression}\n') f.write(f'{lines[i]}\n')