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
|
||||
#
|
||||
|
||||
|
||||
logger = logging.getLogger("forthlift")
|
||||
|
||||
|
||||
|
|
@ -26,7 +25,7 @@ class stream:
|
|||
|
||||
class consume:
|
||||
class Consume:
|
||||
def __call__(self):
|
||||
def __call__(self, stream):
|
||||
raise NotImplementedError
|
||||
|
||||
class lines(Consume):
|
||||
|
|
@ -35,12 +34,46 @@ class consume:
|
|||
|
||||
class format:
|
||||
class Format:
|
||||
def __call__(self, lines):
|
||||
def __call__(self, items):
|
||||
raise NotImplementedError
|
||||
|
||||
class as_is(Format):
|
||||
def __call__(self, lines):
|
||||
return lines
|
||||
def __call__(self, items):
|
||||
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:
|
||||
|
|
@ -50,7 +83,10 @@ class lift:
|
|||
class stdout(Lift):
|
||||
def __call__(self, items):
|
||||
for item in items:
|
||||
print(item, end="")
|
||||
if __debug__:
|
||||
print(item, end = '', file = sys.stdout, flush = True)
|
||||
else:
|
||||
print(item, end = '')
|
||||
|
||||
|
||||
class Forthlifter:
|
||||
|
|
@ -76,22 +112,21 @@ class Forthlifter:
|
|||
if not isinstance(lifters, list):
|
||||
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):
|
||||
logger.debug("│ ├ consume")
|
||||
lines = []
|
||||
items = []
|
||||
for streamer in self.streamers:
|
||||
logger.debug(f"│ │ ├ {type(self.consumer).__name__}({type(streamer).__name__})")
|
||||
# Concatenate
|
||||
lines += self.consumer(streamer())
|
||||
logger.debug(f"│ │ ├ {len(lines)} lines")
|
||||
logger.debug("│ │ └OK")
|
||||
return lines
|
||||
items += self.consumer(streamer())
|
||||
logger.debug(f"│ │ ├ {len(items)} items")
|
||||
logger.debug(f"│ │ └OK {len(items)} items")
|
||||
return items
|
||||
|
||||
def format(self, lines):
|
||||
logger.debug(f"│ ├ format {len(lines)} lines")
|
||||
items = lines
|
||||
def format(self, items):
|
||||
logger.debug(f"│ ├ format {len(items)} items")
|
||||
for formatter in self.formatters:
|
||||
logger.debug(f"│ │ ├ {type(formatter).__name__}")
|
||||
# Replace
|
||||
|
|
@ -106,7 +141,7 @@ class Forthlifter:
|
|||
logger.debug(f"│ │ ├ {type(lifter).__name__}")
|
||||
# Call
|
||||
lifter(items)
|
||||
logger.debug("│ │ └OK")
|
||||
logger.debug(f"│ │ └OK {len(items)} items")
|
||||
|
||||
def __call__(self):
|
||||
logger.debug("├ call")
|
||||
|
|
@ -118,9 +153,10 @@ class Forthlifter:
|
|||
#
|
||||
|
||||
def classes_of(namespace):
|
||||
name = namespace.__name__[0].upper()+namespace.__name__[1:]
|
||||
itf = getattr(namespace, name)
|
||||
return [cls.__name__ for cls in itf.__subclasses__()]
|
||||
itf_name = namespace.__name__[0].upper()+namespace.__name__[1:]
|
||||
itf = getattr(namespace, itf_name)
|
||||
subs = {cls.__name__:cls for cls in itf.__subclasses__()}
|
||||
return subs
|
||||
|
||||
|
||||
def main():
|
||||
|
|
@ -130,34 +166,35 @@ def main():
|
|||
|
||||
parser = argparse.ArgumentParser( description=usage,
|
||||
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
||||
|
||||
streamers = classes_of(stream)
|
||||
parser.add_argument("-s", "--stream",
|
||||
choices = streamers,
|
||||
metavar = "STREAMER(S)",
|
||||
default = ["stdin"],
|
||||
choices = streamers.keys(),
|
||||
# metavar = "STREAMER(S)",
|
||||
default = [],
|
||||
action="append",
|
||||
help="Where to get items.")
|
||||
|
||||
consumers = classes_of(consume)
|
||||
parser.add_argument("-c", "--consume",
|
||||
choices = consumers,
|
||||
metavar = "CONSUMER",
|
||||
choices = consumers.keys(),
|
||||
# metavar = "CONSUMER",
|
||||
default = "lines",
|
||||
help="How to get the content form items.")
|
||||
|
||||
formaters = classes_of(format)
|
||||
parser.add_argument("-f", "--format",
|
||||
choices = formaters,
|
||||
metavar = "FORMATER(S)",
|
||||
default = "as_is",
|
||||
choices = formaters.keys(),
|
||||
# metavar = "FORMATER(S)",
|
||||
default = [],
|
||||
action="append",
|
||||
help="How to format items.")
|
||||
|
||||
lifters = classes_of(lift)
|
||||
parser.add_argument("-l", "--lift",
|
||||
choices = lifters,
|
||||
metavar = "LIFTER(S)",
|
||||
default = ["stdout"],
|
||||
choices = lifters.keys(),
|
||||
# metavar = "LIFTER(S)",
|
||||
default = [],
|
||||
action="append",
|
||||
help="How to lift items.")
|
||||
|
||||
|
|
@ -173,9 +210,23 @@ def main():
|
|||
logger.debug(f"├ formaters: {', '.join(formaters)}")
|
||||
logger.debug(f"└ lifters: {', '.join(lifters)}")
|
||||
|
||||
logger.debug("instantiate")
|
||||
forthlift = Forthlifter()
|
||||
logger.debug("run")
|
||||
if not asked.stream:
|
||||
asked.stream = ["stdin"]
|
||||
|
||||
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()
|
||||
logger.debug("└OK")
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue