vfind, supporting shell expansions and regexp

This commit is contained in:
Amber 2024-04-05 14:22:11 +02:00
parent d892c03bfa
commit 82aebaf869
3 changed files with 23 additions and 2 deletions

View File

@ -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

View File

@ -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 = {}

View File

@ -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)