interactive: Move interactive_setup to utils

... and split it in hope to make the parts more reusable.
This commit is contained in:
Ivan A. Melnikov 2021-09-02 16:02:33 +04:00
parent d532359ffe
commit 8dce33f9b3
2 changed files with 41 additions and 34 deletions

View File

@ -43,12 +43,9 @@ You can also enjoy autocompletion with <TAB>.
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()

View File

@ -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)