Skip to content

Commit

Permalink
reworked to use two classes and adds options for paramode and entire …
Browse files Browse the repository at this point in the history
…file
  • Loading branch information
Tom O'Hara committed Oct 23, 2023
1 parent 11593ac commit 4137ed4
Showing 1 changed file with 64 additions and 21 deletions.
85 changes: 64 additions & 21 deletions mezcla/simple_main_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,85 @@
# "fubar". There is an option to check for lines matching a regular expression.
#

"""Simple illustration of Main class"""
"""Simple illustration of Main class
Sample usage:
echo $'foobar\nfubar' | {script} --check-fubar -
"""

# Standard packages
import re
## TODO: from collections import defaultdict

# Local packages
from mezcla import debug
from mezcla import glue_helpers as gh
from mezcla.main import Main
from mezcla.my_regex import my_re

# Constants
TL = debug.TL
FUBAR_ARG = "check-fubar"
REGEX_ARG = "regex"
ENTIRE_ARG = "entire-file"
PARA_ARG = "para"

class SimpleFilter:
"""Regex filter for text"""

def __init__(self, regex=None, flags=0):
"""Initialize class with PATTERN which can be a REGEX"""
self.regex = regex
self.flags = flags
debug.trace_object(TL.DETAILED, self, label=f"{self.__class__.__name__} instance")

def include(self, line):
"""Output line if filter met"""
include = my_re.search(self.regex, line, flags=self.flags)
debug.trace(TL.QUITE_VERBOSE, "SimpleFilter.include({line!r}) => {include}")
return include


class Script(Main):
"""Input processing class"""
regex = None
check_fubar = None
filter_inst = None

def setup(self):
"""Check results of command line processing"""
debug.trace_fmtd(5, "Script.setup(): self={s}", s=self)
self.regex = self.parsed_args['regex']
self.check_fubar = self.get_parsed_option('check-fubar', not self.regex)
debug.assertion(bool(self.regex) ^ self.check_fubar)
debug.trace_object(5, self, label=f"{self.__class__.__name__} instance")
debug.trace_fmtd(TL.DETAILED, "Script.setup(): self={s}", s=self)
regex = self.get_parsed_option(REGEX_ARG, "")
check_fubar = self.get_parsed_option(FUBAR_ARG, not regex)
slurp_mode = self.get_parsed_option(ENTIRE_ARG, False)
para_mode = self.get_parsed_option(PARA_ARG, False)
debug.assertion(bool(regex) ^ bool(check_fubar))
if check_fubar:
regex = "fubar"
if slurp_mode:
self.file_input_mode = True
if para_mode:
self.paragraph_mode = True
self.filter_inst = SimpleFilter(regex)
debug.trace_object(TL.VERBOSE, self, label=f"{self.__class__.__name__} instance")

def process_line(self, line):
"""Processes current line from input"""
debug.trace_fmtd(6, "Script.process_line({l})", l=line)
if self.check_fubar and "fubar" in line:
debug.trace(4, f"Fubar line {self.line_num}: {line}")
print(line)
elif self.regex and re.search(self.regex, line):
debug.trace(4, f"Regex line {self.line_num}: {line}")
"""Processes current LINE from input
Note: can be a paragraph or an entire file (a la perl)"""
debug.trace(TL.QUITE_DETAILED, f"Script.process_line({line!r})")
if self.filter_inst.include(line):
print(line)
return

if __name__ == '__main__':
## TODO: app => script???
app = Script(description=__doc__,
boolean_options=["check-fubar"],
text_options=[("regex", "Regular expression")])
def main():
"""Entry point"""
app = Script(
description=__doc__.format(script=gh.basename(__file__)),
boolean_options=[(FUBAR_ARG, "Check for 'fubar' in line"),
(PARA_ARG, "Process file in paragraphs (a la Perl)"),
(ENTIRE_ARG, "Apply filter to entire file (a la perl slurping")],
text_options=[(REGEX_ARG, "Regular expression to check")])
app.run()

#-------------------------------------------------------------------------------

if __name__ == '__main__':
debug.trace_current_context(level=TL.QUITE_VERBOSE)
main()

0 comments on commit 4137ed4

Please sign in to comment.