Refactor line finding (somewhat)
This commit is contained in:
		
							parent
							
								
									2d2895ce14
								
							
						
					
					
						commit
						ed3c994421
					
				@ -2,6 +2,27 @@ import exceptions
 | 
				
			|||||||
import re
 | 
					import re
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
 | 
					import sty
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# TODO Avoid extra fname argument??
 | 
				
			||||||
 | 
					def find_lines(lines: list, pattern: str, fname: str, is_regexp=False) -> list:
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    Distinguish between regexp or not (default is False)
 | 
				
			||||||
 | 
					    """
 | 
				
			||||||
 | 
					    found_lines = []
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for l in lines:
 | 
				
			||||||
 | 
					        if is_regexp:
 | 
				
			||||||
 | 
					            matches = re.findall(pattern, l)
 | 
				
			||||||
 | 
					            FIND_COND = len(matches) > 0
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            FIND_COND = l.find(pattern) != -1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if FIND_COND:
 | 
				
			||||||
 | 
					            l = l.strip()
 | 
				
			||||||
 | 
					            found_lines.append(fname + l)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return found_lines
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def print_usage():
 | 
					def print_usage():
 | 
				
			||||||
    usage = """
 | 
					    usage = """
 | 
				
			||||||
@ -120,20 +141,12 @@ def process_grep(args_tree: dict) -> list:
 | 
				
			|||||||
                pattern = pattern.lower()
 | 
					                pattern = pattern.lower()
 | 
				
			||||||
            # Check if regexp flag
 | 
					            # Check if regexp flag
 | 
				
			||||||
            if 'E' in options:
 | 
					            if 'E' in options:
 | 
				
			||||||
 | 
					                # pattern is regexp
 | 
				
			||||||
                if not check_pattern(pattern):
 | 
					                if not check_pattern(pattern):
 | 
				
			||||||
                    raise exceptions.InvalidPattern(pattern)
 | 
					                    raise exceptions.InvalidPattern(pattern)
 | 
				
			||||||
                # Match regexp
 | 
					                found_lines.extend(find_lines(lines, pattern, f, True))
 | 
				
			||||||
                # This isn't DRY... decorator?
 | 
					 | 
				
			||||||
                for l in lines:
 | 
					 | 
				
			||||||
                    matches = re.findall(pattern, l)
 | 
					 | 
				
			||||||
                    if len(matches) > 0:
 | 
					 | 
				
			||||||
                        l = l.strip()
 | 
					 | 
				
			||||||
                        found_lines.append(f + l)
 | 
					 | 
				
			||||||
            else:
 | 
					            else:
 | 
				
			||||||
                for l in lines:
 | 
					                found_lines.extend(find_lines(lines, pattern, f))
 | 
				
			||||||
                    if l.find(pattern) != -1:
 | 
					 | 
				
			||||||
                        l = l.strip()
 | 
					 | 
				
			||||||
                        found_lines.append(f + l)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    except FileNotFoundError as e:
 | 
					    except FileNotFoundError as e:
 | 
				
			||||||
        print(e)
 | 
					        print(e)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user