def parse_and_print(path=".", style_name="org", include=[], exclude=[], include_backup_files=False, include_hidden_files=False): """Parses what is at path (it can be a file or a directory) and outputs the items found using the format given, sorting them by name. """ if (os.path.isdir(path)): itemUtils.parse_directory(include=include, exclude=exclude, include_backup_files=include_backup_files, include_hidden_files=include_hidden_files, path=path) else: itemUtils.parse_file(path) db = itemDb.MeuporgItemDB(itemUtils.MeuporgItem.__item_list__) print(fileFormat.output(db.select_and_sort_by_name(), 2, style_name))
def update_main_file(include=[], exclude=[], include_backup_files=False, include_hidden_files=False ): """Parses the main file ruling the directory and updates all the "Items" nodes in it. If there is no "meup.org" or "meuporg.md" file in the parent directories (i.e. if main_file() returns the empty string), exits violently. The name of the header containing the "Items" sub-header is used to modify the "include only" array of pattern: ".*<header name>.*" is added to it. Then, the parse_directory is called on "." and its output org-formatted is placed just below the Item sub-header, at the correct depth. """ # opening old main file and cd-ing to its directory path_to_old_file = main_file() print("updating {}".format(path_to_old_file)) if (path_to_old_file == ""): print("Could not find a main file. Aborting.") exit(1) dir_old_file, file_name = os.path.split(path_to_old_file) os.chdir(dir_old_file) # find the file format from the file name for potential_style in fileFormat.Factory.get_format_list(): if (potential_style.get_main_file_name() == file_name): style = potential_style # reading old file a first time to get configuration include, exclude, include_backup_files, include_hidden_files = get_configuration(file_name) # getting items items = itemUtils.parse_directory(path=".", include=include, exclude=exclude, include_backup_files=include_backup_files, include_hidden_files=include_hidden_files) # setting up variables with open(file_name, 'r') as f_old: new_content = "" depth = 0 recording = True local_include = [] # updating the content for line in f_old.readlines(): line = line.rstrip() if (style.line_to_header(line) != False): old_depth = depth depth, heading = style.line_to_header(line) if (old_depth > depth): recording = True for i in range(0, old_depth-depth+1): local_include.pop() elif (old_depth == depth): local_include.pop() if (heading != "Items"): local_include += [heading_to_patterns(heading)] else: # updating "Items" header. If an item name is in # local_include, we do not sort the items by name. use_sort_by_name = True for pattern in flatten_to_list(local_include): if pattern in MeuporgItem.item_names: use_sort_by_name = False if use_sort_by_name: items_to_print = sort_by_name(pop_item_by_patterns( items, flatten_to_list(local_include) )) else: items_to_print = pop_item_by_patterns( items, flatten_to_list(local_include) ) new_content += line + "\n" + output( items_to_print, depth+1, style.get_name()) # stopping copying the file (to remove the items previously stored) recording = False local_include.append("Items") # will be poped in next iteration if recording: new_content += line + "\n" # writing the new file with open(file_name, 'w') as f_new: f_new.write(new_content) print "[DONE]"
def select_and_sort_by_directories(self, criteria=None, name=[], file_name=[], sections=[], description=[]): item_list = self.select( criteria=criteria, name=name, file_name=file_name, sections=sections, description=description ) return self.sort_by_directories(item_list) # !SECTION! Tests # =============== if __name__ == "__main__": itemUtils.parse_directory( path="..", include=["org$", "el$", "md$", "py$"], exclude=["readme"], include_backup_files=False, include_hidden_files=False, ) crit = Criteria("(and (file_name el$ py$ md$))") print crit print Or( [ And([Attribute("file_name", ["el"]), Attribute("name", ["TODO"])]), Not(Or([Attribute("description", ["test", "explication"]), Attribute("section", ["test", "explication"])])), ] ) db = MeuporgItemDB(itemUtils.MeuporgItem.__item_list__) item_dict = db.select_and_sort_by_name() for key in item_dict.keys():