From 82aebaf86942b511142e4406e8b93533777fcf70 Mon Sep 17 00:00:00 2001 From: Amber Date: Fri, 5 Apr 2024 14:22:11 +0200 Subject: [PATCH] vfind, supporting shell expansions and regexp --- src/cmdline_parser.py | 3 +++ src/tree_find.py | 20 ++++++++++++++++++-- vfind.py | 2 ++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/cmdline_parser.py b/src/cmdline_parser.py index a69e672..47a3ce2 100644 --- a/src/cmdline_parser.py +++ b/src/cmdline_parser.py @@ -16,6 +16,9 @@ cmd_parser.add_argument("-S", "--show-subtree-info", action="store_true") ## more than one occurrencies of -e option are appended in a list cmd_parser.add_argument("-e", "--exclude", action="append") +# supporting regular expression otherwise use shell expansions +cmd_parser.add_argument("-re", "--regular-expression", action="store_true") + cmd_parser.add_argument("-i", "--include", action="append") # start the command line parsing diff --git a/src/tree_find.py b/src/tree_find.py index d893cf1..8f35186 100644 --- a/src/tree_find.py +++ b/src/tree_find.py @@ -1,6 +1,7 @@ import time import re +import fnmatch from src.colors import RED, YELLOW, GREEN, CYAN , BLUE, PURPLE from src.tree import NerdTree @@ -15,6 +16,11 @@ class NerdTreeFind(NerdTree): def __init__(self, startpath, opts={}, find_opts={}): self.item_name = '' self.re_item_name = None + ''' + re_item_name is a regexp python object, can be + 1. a regular expression, specifying in the command line -re option + 2. a string that supports the shell expansion + ''' self.results = [] self.start_time = time.time() self.end_time = None @@ -137,14 +143,24 @@ class NerdTreeFind(NerdTree): self.compute_aggregate_recursively(tree_struct) return tree_struct + def get_reobj_find(self, item_name): + if not self.find_opts['use_regular_expression']: + regex = fnmatch.translate(item_name) + reobj = re.compile(regex) + return reobj + + reobj = re.compile(r'^%s$' % (item_name,)) + return reobj + def reset(self, new_item_name, new_opts): self.find_opts = new_opts self.results = [] self.item_name = new_item_name print('Starting from path: %s' % CYAN(self.startpath,)) - print('Regexp: %s' % CYAN(new_item_name,)) + print('Use regexp: %s' % (CYAN(self.find_opts['use_regular_expression']))) + print('pattern: %s' % CYAN(new_item_name,)) try: - self.re_item_name = re.compile(r'^%s$' % (new_item_name,)) + self.re_item_name = self.get_reobj_find(new_item_name) except re.error: raise Exception('Can\'t compile regexp %s, exit' % (new_item_name,)) self.json_tree_find = {} diff --git a/vfind.py b/vfind.py index 89dfc2a..4d8c9d0 100755 --- a/vfind.py +++ b/vfind.py @@ -11,6 +11,7 @@ if __name__ == '__main__': pattern = cmd_args.pattern show_children_nodes = cmd_args.show_children_nodes show_subtree_info = cmd_args.show_subtree_info + use_re = cmd_args.regular_expression # opts for generatign the tree opts = { @@ -27,6 +28,7 @@ if __name__ == '__main__': 'show_subtree_info': show_subtree_info, 'type' : cmd_args.type, 'items_to_include' : cmd_args.include or [], + 'use_regular_expression' : use_re, } ntree = NerdTreeFind(path, opts=opts)