shellnotes/util/notegrep/m.py
2021-07-02 18:43:27 +03:00

56 lines
1.8 KiB
Python

#!/usr/bin/env python3
#shellnotes - m.py
#(C) Dimitris Marakomihelakis
#Released under the "All rights reserved" category. See the RIGHTS.txt file
#in /docs/github/ for its full text.
import sys
import os.path
from os import path
import subprocess
from subprocess import check_output
import re
class Multi():
def __init__(self, patterns, files):
self.patterns = patterns
self.files = files
def remove_duplicates(self):
self.patterns, self.files = list(dict.fromkeys(self.patterns)), list(dict.fromkeys(self.files))
def print_matches(self):
for file in self.files:
try:
if file == "": return 0
print("\n"+file if path.exists('Notes/'+file) else f"{file} (not found):\n-")
print("=" * len(file) if path.exists('Notes/'+file) else "")
for pattern in self.patterns:
if pattern == "": return 0
with open('Notes/'+file, 'r') as f:
for line in f:
if re.search(pattern, line):
out = os.system(f"""echo -n "'{pattern}'": && grep -n --color=always {pattern} Notes/{file}""")
#bug: pattern is generated multiple times
except FileNotFoundError: pass
def main():
try:
newMulti = Multi(sys.argv[1].split(','), sys.argv[2].split(','))
newMulti.remove_duplicates()
newMulti.print_matches()
except IndexError:
patterns = input("Enter patterns, separated by a comma: ")
files = input("Enter files, separated by a comma: ")
newMulti = Multi(patterns.split(','), files.split(','))
newMulti.remove_duplicates()
newMulti.print_matches()
if __name__ == "__main__":
main()