vfind, supporting shell expansions and regexp
This commit is contained in:
parent
d892c03bfa
commit
82aebaf869
@ -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
|
## more than one occurrencies of -e option are appended in a list
|
||||||
cmd_parser.add_argument("-e", "--exclude", action="append")
|
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")
|
cmd_parser.add_argument("-i", "--include", action="append")
|
||||||
|
|
||||||
# start the command line parsing
|
# start the command line parsing
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import time
|
import time
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
import fnmatch
|
||||||
|
|
||||||
from src.colors import RED, YELLOW, GREEN, CYAN , BLUE, PURPLE
|
from src.colors import RED, YELLOW, GREEN, CYAN , BLUE, PURPLE
|
||||||
from src.tree import NerdTree
|
from src.tree import NerdTree
|
||||||
@ -15,6 +16,11 @@ class NerdTreeFind(NerdTree):
|
|||||||
def __init__(self, startpath, opts={}, find_opts={}):
|
def __init__(self, startpath, opts={}, find_opts={}):
|
||||||
self.item_name = ''
|
self.item_name = ''
|
||||||
self.re_item_name = None
|
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.results = []
|
||||||
self.start_time = time.time()
|
self.start_time = time.time()
|
||||||
self.end_time = None
|
self.end_time = None
|
||||||
@ -137,14 +143,24 @@ class NerdTreeFind(NerdTree):
|
|||||||
self.compute_aggregate_recursively(tree_struct)
|
self.compute_aggregate_recursively(tree_struct)
|
||||||
return 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):
|
def reset(self, new_item_name, new_opts):
|
||||||
self.find_opts = new_opts
|
self.find_opts = new_opts
|
||||||
self.results = []
|
self.results = []
|
||||||
self.item_name = new_item_name
|
self.item_name = new_item_name
|
||||||
print('Starting from path: %s' % CYAN(self.startpath,))
|
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:
|
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:
|
except re.error:
|
||||||
raise Exception('Can\'t compile regexp %s, exit' % (new_item_name,))
|
raise Exception('Can\'t compile regexp %s, exit' % (new_item_name,))
|
||||||
self.json_tree_find = {}
|
self.json_tree_find = {}
|
||||||
|
2
vfind.py
2
vfind.py
@ -11,6 +11,7 @@ if __name__ == '__main__':
|
|||||||
pattern = cmd_args.pattern
|
pattern = cmd_args.pattern
|
||||||
show_children_nodes = cmd_args.show_children_nodes
|
show_children_nodes = cmd_args.show_children_nodes
|
||||||
show_subtree_info = cmd_args.show_subtree_info
|
show_subtree_info = cmd_args.show_subtree_info
|
||||||
|
use_re = cmd_args.regular_expression
|
||||||
|
|
||||||
# opts for generatign the tree
|
# opts for generatign the tree
|
||||||
opts = {
|
opts = {
|
||||||
@ -27,6 +28,7 @@ if __name__ == '__main__':
|
|||||||
'show_subtree_info': show_subtree_info,
|
'show_subtree_info': show_subtree_info,
|
||||||
'type' : cmd_args.type,
|
'type' : cmd_args.type,
|
||||||
'items_to_include' : cmd_args.include or [],
|
'items_to_include' : cmd_args.include or [],
|
||||||
|
'use_regular_expression' : use_re,
|
||||||
}
|
}
|
||||||
|
|
||||||
ntree = NerdTreeFind(path, opts=opts)
|
ntree = NerdTreeFind(path, opts=opts)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user