From 715dbab1631d8f65780c59c55701fd586c685a6e Mon Sep 17 00:00:00 2001 From: Matthias Bilger Date: Wed, 2 Dec 2020 13:34:39 +0100 Subject: [PATCH] added base scripts --- base/aoc.egg-info/PKG-INFO | 10 ++++ base/aoc.egg-info/SOURCES.txt | 8 +++ base/aoc.egg-info/dependency_links.txt | 1 + base/aoc.egg-info/top_level.txt | 1 + base/aoc/__init__.py | 2 + base/aoc/input.py | 68 ++++++++++++++++++++++++++ base/aoc/partselector.py | 49 +++++++++++++++++++ base/build/lib/aoc/__init__.py | 3 ++ base/build/lib/aoc/input.py | 68 ++++++++++++++++++++++++++ base/build/lib/aoc/partselector.py | 49 +++++++++++++++++++ base/setup.py | 7 +++ 11 files changed, 266 insertions(+) create mode 100644 base/aoc.egg-info/PKG-INFO create mode 100644 base/aoc.egg-info/SOURCES.txt create mode 100644 base/aoc.egg-info/dependency_links.txt create mode 100644 base/aoc.egg-info/top_level.txt create mode 100644 base/aoc/__init__.py create mode 100644 base/aoc/input.py create mode 100644 base/aoc/partselector.py create mode 100644 base/build/lib/aoc/__init__.py create mode 100644 base/build/lib/aoc/input.py create mode 100644 base/build/lib/aoc/partselector.py create mode 100644 base/setup.py diff --git a/base/aoc.egg-info/PKG-INFO b/base/aoc.egg-info/PKG-INFO new file mode 100644 index 0000000..74a21e8 --- /dev/null +++ b/base/aoc.egg-info/PKG-INFO @@ -0,0 +1,10 @@ +Metadata-Version: 1.0 +Name: aoc +Version: 0.1 +Summary: UNKNOWN +Home-page: UNKNOWN +Author: UNKNOWN +Author-email: UNKNOWN +License: UNKNOWN +Description: UNKNOWN +Platform: UNKNOWN diff --git a/base/aoc.egg-info/SOURCES.txt b/base/aoc.egg-info/SOURCES.txt new file mode 100644 index 0000000..bdc79fc --- /dev/null +++ b/base/aoc.egg-info/SOURCES.txt @@ -0,0 +1,8 @@ +setup.py +aoc/__init__.py +aoc/input.py +aoc/partselector.py +aoc.egg-info/PKG-INFO +aoc.egg-info/SOURCES.txt +aoc.egg-info/dependency_links.txt +aoc.egg-info/top_level.txt \ No newline at end of file diff --git a/base/aoc.egg-info/dependency_links.txt b/base/aoc.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/base/aoc.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/base/aoc.egg-info/top_level.txt b/base/aoc.egg-info/top_level.txt new file mode 100644 index 0000000..ca98b4f --- /dev/null +++ b/base/aoc.egg-info/top_level.txt @@ -0,0 +1 @@ +aoc diff --git a/base/aoc/__init__.py b/base/aoc/__init__.py new file mode 100644 index 0000000..a66db05 --- /dev/null +++ b/base/aoc/__init__.py @@ -0,0 +1,2 @@ +from .partselector import get_logger, part_one, part_two, draw +from .input import get_input, read_single_keypress diff --git a/base/aoc/input.py b/base/aoc/input.py new file mode 100644 index 0000000..57ae48a --- /dev/null +++ b/base/aoc/input.py @@ -0,0 +1,68 @@ +import sys +import argparse + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('-s', '--sample', action='store_true') + parser.add_argument('-f', '--file', type=str, nargs='?') + parser.add_argument('input', type=str, nargs='*') + return parser.parse_known_args() + +def get_input(transform): + p = parse_args()[0] + if p.input: + return transform(p.input) + inp = [] + if p.file: + f = open(p.file) + elif p.sample: + f = open('data/sample.txt') + else: + f = open('data/data.txt') + for line in f.readlines(): + inp.append(transform(line.strip())) + f.close() + if len(inp) == 1: + return inp[0] + return inp + + +def read_single_keypress(): + import termios, fcntl, sys, os + fd = sys.stdin.fileno() + # save old state + flags_save = fcntl.fcntl(fd, fcntl.F_GETFL) + attrs_save = termios.tcgetattr(fd) + # make raw - the way to do this comes from the termios(3) man page. + attrs = list(attrs_save) # copy the stored version to update + # iflag + attrs[0] &= ~(termios.IGNBRK | termios.BRKINT | termios.PARMRK + | termios.ISTRIP | termios.INLCR | termios. IGNCR + | termios.ICRNL | termios.IXON ) + # oflag + attrs[1] &= ~termios.OPOST + # cflag + attrs[2] &= ~(termios.CSIZE | termios. PARENB) + attrs[2] |= termios.CS8 + # lflag + attrs[3] &= ~(termios.ECHONL | termios.ECHO | termios.ICANON + | termios.ISIG | termios.IEXTEN) + termios.tcsetattr(fd, termios.TCSANOW, attrs) + # turn off non-blocking + fcntl.fcntl(fd, fcntl.F_SETFL, flags_save & ~os.O_NONBLOCK) + # read a single keystroke + ret = [] + try: + ret.append(sys.stdin.read(1)) # returns a single character + fcntl.fcntl(fd, fcntl.F_SETFL, flags_save | os.O_NONBLOCK) + c = sys.stdin.read(1) # returns a single character + while len(c) > 0: + ret.append(c) + c = sys.stdin.read(1) + except KeyboardInterrupt: + ret.append('\x03') + finally: + # restore old state + termios.tcsetattr(fd, termios.TCSAFLUSH, attrs_save) + fcntl.fcntl(fd, fcntl.F_SETFL, flags_save) + return tuple(ret) diff --git a/base/aoc/partselector.py b/base/aoc/partselector.py new file mode 100644 index 0000000..5b1e872 --- /dev/null +++ b/base/aoc/partselector.py @@ -0,0 +1,49 @@ +import argparse +import logging + +logging.basicConfig(level=logging.WARNING) +_logger = logging.getLogger(__name__) + +args = None +def parse_args(): + global args + if args is None: + parser = argparse.ArgumentParser() + parser.add_argument('-A', action='store_true') + parser.add_argument('-B', action='store_true') + parser.add_argument('-G', action='store_true') + parser.add_argument('-q', action='store_true') + parser.add_argument('-v', "--verbose", dest="verbose_count", + action="count", default=0, + help="increases log verbosity for each occurence.") + args = parser.parse_known_args() + return args + +def get_logger(*args, **kwargs): + p = parse_args()[0] + log = logging.getLogger(*args, **kwargs) + log.setLevel(max(3 - p.verbose_count, 1) * 10) + return log + +def part_one(): + p = parse_args()[0] + if p.B: + return False + if not p.q: + print('==== PART I ====') + return True + +def part_two(): + p = parse_args()[0] + if p.A: + return False + if not p.q: + print('==== PART II ====') + return True + +def draw(): + p = parse_args()[0] + if p.G: + return True + return False + diff --git a/base/build/lib/aoc/__init__.py b/base/build/lib/aoc/__init__.py new file mode 100644 index 0000000..019e810 --- /dev/null +++ b/base/build/lib/aoc/__init__.py @@ -0,0 +1,3 @@ +from .partselector import get_logger, part_one, part_two, draw +from .input import get_input, read_single_keypress +from .debugger import Debugger diff --git a/base/build/lib/aoc/input.py b/base/build/lib/aoc/input.py new file mode 100644 index 0000000..57ae48a --- /dev/null +++ b/base/build/lib/aoc/input.py @@ -0,0 +1,68 @@ +import sys +import argparse + +def parse_args(): + parser = argparse.ArgumentParser() + parser.add_argument('-s', '--sample', action='store_true') + parser.add_argument('-f', '--file', type=str, nargs='?') + parser.add_argument('input', type=str, nargs='*') + return parser.parse_known_args() + +def get_input(transform): + p = parse_args()[0] + if p.input: + return transform(p.input) + inp = [] + if p.file: + f = open(p.file) + elif p.sample: + f = open('data/sample.txt') + else: + f = open('data/data.txt') + for line in f.readlines(): + inp.append(transform(line.strip())) + f.close() + if len(inp) == 1: + return inp[0] + return inp + + +def read_single_keypress(): + import termios, fcntl, sys, os + fd = sys.stdin.fileno() + # save old state + flags_save = fcntl.fcntl(fd, fcntl.F_GETFL) + attrs_save = termios.tcgetattr(fd) + # make raw - the way to do this comes from the termios(3) man page. + attrs = list(attrs_save) # copy the stored version to update + # iflag + attrs[0] &= ~(termios.IGNBRK | termios.BRKINT | termios.PARMRK + | termios.ISTRIP | termios.INLCR | termios. IGNCR + | termios.ICRNL | termios.IXON ) + # oflag + attrs[1] &= ~termios.OPOST + # cflag + attrs[2] &= ~(termios.CSIZE | termios. PARENB) + attrs[2] |= termios.CS8 + # lflag + attrs[3] &= ~(termios.ECHONL | termios.ECHO | termios.ICANON + | termios.ISIG | termios.IEXTEN) + termios.tcsetattr(fd, termios.TCSANOW, attrs) + # turn off non-blocking + fcntl.fcntl(fd, fcntl.F_SETFL, flags_save & ~os.O_NONBLOCK) + # read a single keystroke + ret = [] + try: + ret.append(sys.stdin.read(1)) # returns a single character + fcntl.fcntl(fd, fcntl.F_SETFL, flags_save | os.O_NONBLOCK) + c = sys.stdin.read(1) # returns a single character + while len(c) > 0: + ret.append(c) + c = sys.stdin.read(1) + except KeyboardInterrupt: + ret.append('\x03') + finally: + # restore old state + termios.tcsetattr(fd, termios.TCSAFLUSH, attrs_save) + fcntl.fcntl(fd, fcntl.F_SETFL, flags_save) + return tuple(ret) diff --git a/base/build/lib/aoc/partselector.py b/base/build/lib/aoc/partselector.py new file mode 100644 index 0000000..5b1e872 --- /dev/null +++ b/base/build/lib/aoc/partselector.py @@ -0,0 +1,49 @@ +import argparse +import logging + +logging.basicConfig(level=logging.WARNING) +_logger = logging.getLogger(__name__) + +args = None +def parse_args(): + global args + if args is None: + parser = argparse.ArgumentParser() + parser.add_argument('-A', action='store_true') + parser.add_argument('-B', action='store_true') + parser.add_argument('-G', action='store_true') + parser.add_argument('-q', action='store_true') + parser.add_argument('-v', "--verbose", dest="verbose_count", + action="count", default=0, + help="increases log verbosity for each occurence.") + args = parser.parse_known_args() + return args + +def get_logger(*args, **kwargs): + p = parse_args()[0] + log = logging.getLogger(*args, **kwargs) + log.setLevel(max(3 - p.verbose_count, 1) * 10) + return log + +def part_one(): + p = parse_args()[0] + if p.B: + return False + if not p.q: + print('==== PART I ====') + return True + +def part_two(): + p = parse_args()[0] + if p.A: + return False + if not p.q: + print('==== PART II ====') + return True + +def draw(): + p = parse_args()[0] + if p.G: + return True + return False + diff --git a/base/setup.py b/base/setup.py new file mode 100644 index 0000000..e40464c --- /dev/null +++ b/base/setup.py @@ -0,0 +1,7 @@ +from setuptools import setup + +setup( + name='aoc', + version='0.1', + packages=['aoc',], +)