From 8dce33f9b306cb3a39fae88c302e69c48fa67421 Mon Sep 17 00:00:00 2001 From: "Ivan A. Melnikov" Date: Thu, 2 Sep 2021 16:02:33 +0400 Subject: [PATCH] interactive: Move interactive_setup to utils ... and split it in hope to make the parts more reusable. --- port_stats/interactive.py | 38 ++++---------------------------------- port_stats/utils.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/port_stats/interactive.py b/port_stats/interactive.py index 1645191..634b740 100644 --- a/port_stats/interactive.py +++ b/port_stats/interactive.py @@ -43,12 +43,9 @@ You can also enjoy autocompletion with . from __future__ import print_function -import atexit -import json import logging import os import re -import readline import rlcompleter # noqa import sys import time @@ -177,7 +174,7 @@ g = gspi # noqa def list_spi(pkgs, colors=None, to=print): - if isinstance(pkgs, basestring): + if isinstance(pkgs, (str, bytes)): pkgs = pkgs.split() pset = frozenset(pkgs) lines = _spi_by_predicate(pset.__contains__, colors) @@ -298,8 +295,10 @@ def stats(names=None, to=dump): def inapt_reporter(): import inapt d = inapt.DependencyInfo.load(CONFIG['repos'][CURRENT['base']]) + def _reporter(name, to=pager): to(d.recursive_srpm_report(name, _colorizer())) + return _reporter @@ -307,35 +306,6 @@ def doc(to=dump): to(__doc__) -def interactive_setup(): - # Bind ‘TAB’ to complete - readline.parse_and_bind('tab:complete') - - # Set history file – ~\.pythonhistory - histfile = os.path.join(os.environ['HOME'], '.pythonhistory') - - # Attempt read of histfile - try: - readline.read_history_file(histfile) - except IOError: - pass - - # Write history file at shell exit - atexit.register(readline.write_history_file, histfile) - - # Configure logging - logging.basicConfig( - format='%(asctime)s %(levelname)-5s %(name)s - %(message)s', - datefmt='%Y-%m-%d %H:%M:%S', - stream=sys.stderr, level=logging.INFO) - - config = sys.argv[1] - LOG.info("Loading configuraition file: %s", config) - with open(config, 'r') as f: - global CONFIG - CONFIG = json.load(f) - - if __name__ == '__main__': - interactive_setup() + CONFIG = utils.interactive_setup() doc() diff --git a/port_stats/utils.py b/port_stats/utils.py index b2ec1c7..4dfa171 100644 --- a/port_stats/utils.py +++ b/port_stats/utils.py @@ -1,8 +1,13 @@ import datetime import json +import logging +import os +import sys import time +LOG = logging.getLogger(__name__) + def format_timestamp(ts, fmt='%Y-%m-%d %H:%M:%S'): dt = datetime.datetime.fromtimestamp(ts) @@ -44,3 +49,35 @@ def format_dict(data, indent=False): indent=2, separators=(',', ': ')) else: return json.dumps(out, sort_keys=True) + + +def readline_setup(): + import atexit + import readline + + readline.parse_and_bind('tab:complete') + histfile = os.path.join(os.environ['HOME'], '.pythonhistory') + try: + readline.read_history_file(histfile) + except IOError: + pass + atexit.register(readline.write_history_file, histfile) + + +def logging_setup(loglevel=logging.INFO): + logging.basicConfig( + format='%(asctime)s %(levelname)-5s %(name)s - %(message)s', + datefmt='%Y-%m-%d %H:%M:%S', + stream=sys.stderr, level=loglevel) + + +def interactive_setup(readline=True, loglevel=logging.INFO): + logging_setup() + + if readline: + readline_setup() + + config = sys.argv[1] + LOG.info("Loading configuraition file: %s", config) + with open(config, 'r') as f: + return json.load(f)