basic features
This commit is contained in:
parent
844209e7b6
commit
b8451b3171
1 changed files with 85 additions and 34 deletions
|
|
@ -11,7 +11,6 @@ from configparser import ConfigParser
|
||||||
# LIBRARY
|
# LIBRARY
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
logger = logging.getLogger("forthlift")
|
logger = logging.getLogger("forthlift")
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -26,7 +25,7 @@ class stream:
|
||||||
|
|
||||||
class consume:
|
class consume:
|
||||||
class Consume:
|
class Consume:
|
||||||
def __call__(self):
|
def __call__(self, stream):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
class lines(Consume):
|
class lines(Consume):
|
||||||
|
|
@ -35,12 +34,46 @@ class consume:
|
||||||
|
|
||||||
class format:
|
class format:
|
||||||
class Format:
|
class Format:
|
||||||
def __call__(self, lines):
|
def __call__(self, items):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
class as_is(Format):
|
class as_is(Format):
|
||||||
def __call__(self, lines):
|
def __call__(self, items):
|
||||||
return lines
|
return items
|
||||||
|
|
||||||
|
class trim(Format):
|
||||||
|
def __init__(self, max = 14):
|
||||||
|
self.max = max
|
||||||
|
|
||||||
|
def trim(self, line):
|
||||||
|
if len(line) > self.max:
|
||||||
|
yield line[:self.max]
|
||||||
|
for subline in self.trim(line[self.max:]):
|
||||||
|
yield subline
|
||||||
|
else:
|
||||||
|
yield line
|
||||||
|
|
||||||
|
def __call__(self, items):
|
||||||
|
trimmed = []
|
||||||
|
for item in items:
|
||||||
|
for subline in self.trim(item):
|
||||||
|
trimmed.append(subline)
|
||||||
|
return trimmed
|
||||||
|
|
||||||
|
class eol(Format):
|
||||||
|
def __call__(self, items):
|
||||||
|
eoled = []
|
||||||
|
for item in items:
|
||||||
|
eoled.append( item + "\n" )
|
||||||
|
return eoled
|
||||||
|
|
||||||
|
class strip(Format):
|
||||||
|
def __call__(self, items):
|
||||||
|
stripped = []
|
||||||
|
for item in items:
|
||||||
|
stripped.append( item.strip() )
|
||||||
|
return stripped
|
||||||
|
|
||||||
|
|
||||||
class lift:
|
class lift:
|
||||||
class Lift:
|
class Lift:
|
||||||
|
|
@ -50,7 +83,10 @@ class lift:
|
||||||
class stdout(Lift):
|
class stdout(Lift):
|
||||||
def __call__(self, items):
|
def __call__(self, items):
|
||||||
for item in items:
|
for item in items:
|
||||||
print(item, end="")
|
if __debug__:
|
||||||
|
print(item, end = '', file = sys.stdout, flush = True)
|
||||||
|
else:
|
||||||
|
print(item, end = '')
|
||||||
|
|
||||||
|
|
||||||
class Forthlifter:
|
class Forthlifter:
|
||||||
|
|
@ -76,22 +112,21 @@ class Forthlifter:
|
||||||
if not isinstance(lifters, list):
|
if not isinstance(lifters, list):
|
||||||
lifters = [lifters]
|
lifters = [lifters]
|
||||||
self.lifters = lifters
|
self.lifters = lifters
|
||||||
logger.debug(f"└lifters: {', '.join(type(i).__name__ for i in self.lifters)}")
|
logger.debug(f"└ lifters: {', '.join(type(i).__name__ for i in self.lifters)}")
|
||||||
|
|
||||||
def consume(self):
|
def consume(self):
|
||||||
logger.debug("│ ├ consume")
|
logger.debug("│ ├ consume")
|
||||||
lines = []
|
items = []
|
||||||
for streamer in self.streamers:
|
for streamer in self.streamers:
|
||||||
logger.debug(f"│ │ ├ {type(self.consumer).__name__}({type(streamer).__name__})")
|
logger.debug(f"│ │ ├ {type(self.consumer).__name__}({type(streamer).__name__})")
|
||||||
# Concatenate
|
# Concatenate
|
||||||
lines += self.consumer(streamer())
|
items += self.consumer(streamer())
|
||||||
logger.debug(f"│ │ ├ {len(lines)} lines")
|
logger.debug(f"│ │ ├ {len(items)} items")
|
||||||
logger.debug("│ │ └OK")
|
logger.debug(f"│ │ └OK {len(items)} items")
|
||||||
return lines
|
return items
|
||||||
|
|
||||||
def format(self, lines):
|
def format(self, items):
|
||||||
logger.debug(f"│ ├ format {len(lines)} lines")
|
logger.debug(f"│ ├ format {len(items)} items")
|
||||||
items = lines
|
|
||||||
for formatter in self.formatters:
|
for formatter in self.formatters:
|
||||||
logger.debug(f"│ │ ├ {type(formatter).__name__}")
|
logger.debug(f"│ │ ├ {type(formatter).__name__}")
|
||||||
# Replace
|
# Replace
|
||||||
|
|
@ -106,7 +141,7 @@ class Forthlifter:
|
||||||
logger.debug(f"│ │ ├ {type(lifter).__name__}")
|
logger.debug(f"│ │ ├ {type(lifter).__name__}")
|
||||||
# Call
|
# Call
|
||||||
lifter(items)
|
lifter(items)
|
||||||
logger.debug("│ │ └OK")
|
logger.debug(f"│ │ └OK {len(items)} items")
|
||||||
|
|
||||||
def __call__(self):
|
def __call__(self):
|
||||||
logger.debug("├ call")
|
logger.debug("├ call")
|
||||||
|
|
@ -118,9 +153,10 @@ class Forthlifter:
|
||||||
#
|
#
|
||||||
|
|
||||||
def classes_of(namespace):
|
def classes_of(namespace):
|
||||||
name = namespace.__name__[0].upper()+namespace.__name__[1:]
|
itf_name = namespace.__name__[0].upper()+namespace.__name__[1:]
|
||||||
itf = getattr(namespace, name)
|
itf = getattr(namespace, itf_name)
|
||||||
return [cls.__name__ for cls in itf.__subclasses__()]
|
subs = {cls.__name__:cls for cls in itf.__subclasses__()}
|
||||||
|
return subs
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
@ -130,34 +166,35 @@ def main():
|
||||||
|
|
||||||
parser = argparse.ArgumentParser( description=usage,
|
parser = argparse.ArgumentParser( description=usage,
|
||||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||||
|
|
||||||
streamers = classes_of(stream)
|
streamers = classes_of(stream)
|
||||||
parser.add_argument("-s", "--stream",
|
parser.add_argument("-s", "--stream",
|
||||||
choices = streamers,
|
choices = streamers.keys(),
|
||||||
metavar = "STREAMER(S)",
|
# metavar = "STREAMER(S)",
|
||||||
default = ["stdin"],
|
default = [],
|
||||||
action="append",
|
action="append",
|
||||||
help="Where to get items.")
|
help="Where to get items.")
|
||||||
|
|
||||||
consumers = classes_of(consume)
|
consumers = classes_of(consume)
|
||||||
parser.add_argument("-c", "--consume",
|
parser.add_argument("-c", "--consume",
|
||||||
choices = consumers,
|
choices = consumers.keys(),
|
||||||
metavar = "CONSUMER",
|
# metavar = "CONSUMER",
|
||||||
default = "lines",
|
default = "lines",
|
||||||
help="How to get the content form items.")
|
help="How to get the content form items.")
|
||||||
|
|
||||||
formaters = classes_of(format)
|
formaters = classes_of(format)
|
||||||
parser.add_argument("-f", "--format",
|
parser.add_argument("-f", "--format",
|
||||||
choices = formaters,
|
choices = formaters.keys(),
|
||||||
metavar = "FORMATER(S)",
|
# metavar = "FORMATER(S)",
|
||||||
default = "as_is",
|
default = [],
|
||||||
action="append",
|
action="append",
|
||||||
help="How to format items.")
|
help="How to format items.")
|
||||||
|
|
||||||
lifters = classes_of(lift)
|
lifters = classes_of(lift)
|
||||||
parser.add_argument("-l", "--lift",
|
parser.add_argument("-l", "--lift",
|
||||||
choices = lifters,
|
choices = lifters.keys(),
|
||||||
metavar = "LIFTER(S)",
|
# metavar = "LIFTER(S)",
|
||||||
default = ["stdout"],
|
default = [],
|
||||||
action="append",
|
action="append",
|
||||||
help="How to lift items.")
|
help="How to lift items.")
|
||||||
|
|
||||||
|
|
@ -173,9 +210,23 @@ def main():
|
||||||
logger.debug(f"├ formaters: {', '.join(formaters)}")
|
logger.debug(f"├ formaters: {', '.join(formaters)}")
|
||||||
logger.debug(f"└ lifters: {', '.join(lifters)}")
|
logger.debug(f"└ lifters: {', '.join(lifters)}")
|
||||||
|
|
||||||
logger.debug("instantiate")
|
if not asked.stream:
|
||||||
forthlift = Forthlifter()
|
asked.stream = ["stdin"]
|
||||||
logger.debug("run")
|
|
||||||
|
if not asked.format:
|
||||||
|
asked.format = ["as_is"]
|
||||||
|
|
||||||
|
if not asked.lift:
|
||||||
|
asked.lift = ["stdout"]
|
||||||
|
|
||||||
|
logger.debug("Chosen operators:")
|
||||||
|
forthlift = Forthlifter(
|
||||||
|
consumer = consumers[asked.consume](),
|
||||||
|
streamers = [streamers[k]() for k in asked.stream],
|
||||||
|
formatters = [formaters[k]() for k in asked.format],
|
||||||
|
lifters = [ lifters[k]() for k in asked.lift ],
|
||||||
|
)
|
||||||
|
logger.debug("Run:")
|
||||||
forthlift()
|
forthlift()
|
||||||
logger.debug("└OK")
|
logger.debug("└OK")
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue