interactive: Move interactive_setup to utils
... and split it in hope to make the parts more reusable.
This commit is contained in:
parent
d532359ffe
commit
8dce33f9b3
@ -43,12 +43,9 @@ You can also enjoy autocompletion with <TAB>.
|
|||||||
|
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
|
||||||
import atexit
|
|
||||||
import json
|
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import readline
|
|
||||||
import rlcompleter # noqa
|
import rlcompleter # noqa
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
@ -177,7 +174,7 @@ g = gspi # noqa
|
|||||||
|
|
||||||
|
|
||||||
def list_spi(pkgs, colors=None, to=print):
|
def list_spi(pkgs, colors=None, to=print):
|
||||||
if isinstance(pkgs, basestring):
|
if isinstance(pkgs, (str, bytes)):
|
||||||
pkgs = pkgs.split()
|
pkgs = pkgs.split()
|
||||||
pset = frozenset(pkgs)
|
pset = frozenset(pkgs)
|
||||||
lines = _spi_by_predicate(pset.__contains__, colors)
|
lines = _spi_by_predicate(pset.__contains__, colors)
|
||||||
@ -298,8 +295,10 @@ def stats(names=None, to=dump):
|
|||||||
def inapt_reporter():
|
def inapt_reporter():
|
||||||
import inapt
|
import inapt
|
||||||
d = inapt.DependencyInfo.load(CONFIG['repos'][CURRENT['base']])
|
d = inapt.DependencyInfo.load(CONFIG['repos'][CURRENT['base']])
|
||||||
|
|
||||||
def _reporter(name, to=pager):
|
def _reporter(name, to=pager):
|
||||||
to(d.recursive_srpm_report(name, _colorizer()))
|
to(d.recursive_srpm_report(name, _colorizer()))
|
||||||
|
|
||||||
return _reporter
|
return _reporter
|
||||||
|
|
||||||
|
|
||||||
@ -307,35 +306,6 @@ def doc(to=dump):
|
|||||||
to(__doc__)
|
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__':
|
if __name__ == '__main__':
|
||||||
interactive_setup()
|
CONFIG = utils.interactive_setup()
|
||||||
doc()
|
doc()
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
|
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def format_timestamp(ts, fmt='%Y-%m-%d %H:%M:%S'):
|
def format_timestamp(ts, fmt='%Y-%m-%d %H:%M:%S'):
|
||||||
dt = datetime.datetime.fromtimestamp(ts)
|
dt = datetime.datetime.fromtimestamp(ts)
|
||||||
@ -44,3 +49,35 @@ def format_dict(data, indent=False):
|
|||||||
indent=2, separators=(',', ': '))
|
indent=2, separators=(',', ': '))
|
||||||
else:
|
else:
|
||||||
return json.dumps(out, sort_keys=True)
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user