make XONSH_HISTORY_BACKEND support instance too

This commit is contained in:
Hugo Wang 2017-02-05 21:48:52 +08:00
parent bc2d32096e
commit 5b114d70a3
3 changed files with 25 additions and 6 deletions

View file

@ -38,7 +38,7 @@ from xonsh.tools import (
is_dynamic_cwd_width, to_dynamic_cwd_tuple, dynamic_cwd_tuple_to_str,
is_logfile_opt, to_logfile_opt, logfile_opt_to_str, executables_in,
is_nonstring_seq_of_strings, pathsep_to_upper_seq,
seq_to_upper_pathsep, print_color
seq_to_upper_pathsep, print_color, is_history_backend, to_itself,
)
import xonsh.prompt.base as prompt
@ -176,7 +176,7 @@ def DEFAULT_ENSURERS():
'XONSH_DEBUG': (always_false, to_debug, bool_or_int_to_str),
'XONSH_ENCODING': (is_string, ensure_string, ensure_string),
'XONSH_ENCODING_ERRORS': (is_string, ensure_string, ensure_string),
'XONSH_HISTORY_BACKEND': (is_string_or_callable, ensure_string, ensure_string),
'XONSH_HISTORY_BACKEND': (is_history_backend, to_itself, ensure_string),
'XONSH_HISTORY_FILE': (is_string, ensure_string, ensure_string),
'XONSH_HISTORY_SIZE': (is_history_tuple, to_history_tuple, history_tuple_to_str),
'XONSH_LOGIN': (is_bool, to_bool, bool_to_str),
@ -937,6 +937,7 @@ class Env(cabc.MutableMapping):
if not ensurer.validate(val):
val = ensurer.convert(val)
# existing envvars can have any value including None
old_value = self._d[key] if key in self._d else self._no_value
self._d[key] = val
if self.detypeable(val):

View file

@ -9,6 +9,7 @@ import json
import os
import sys
from xonsh.history.base import History
from xonsh.history.dummy import DummyHistory
from xonsh.history.json import JsonHistory
from xonsh.history.sqlite import SqliteHistory
@ -27,14 +28,16 @@ def construct_history(**kwargs):
"""Construct the history backend object."""
env = builtins.__xonsh_env__
backend = env.get('XONSH_HISTORY_BACKEND')
if inspect.isclass(backend):
if isinstance(backend, str) and backend in HISTORY_BACKENDS:
kls_history = HISTORY_BACKENDS[backend]
elif inspect.isclass(backend):
kls_history = backend
elif backend not in HISTORY_BACKENDS:
elif isinstance(backend, History):
return backend
else:
print('Unknown history backend: {}. Using JSON version'.format(
backend), file=sys.stderr)
kls_history = JsonHistory
else:
kls_history = HISTORY_BACKENDS[backend]
return kls_history(**kwargs)

View file

@ -819,6 +819,11 @@ def is_string_or_callable(x):
return is_string(x) or is_callable(x)
def is_class(x):
"""Tests if something is a class"""
return isinstance(x, type)
def always_true(x):
"""Returns True"""
return True
@ -915,6 +920,11 @@ def to_bool(x):
return bool(x)
def to_itself(x):
"""Do not do conversion, returns to itself."""
return x
def bool_to_str(x):
"""Converts a bool to an empty string if False and the string '1' if
True.
@ -1240,6 +1250,11 @@ def is_history_tuple(x):
return False
def is_history_backend(x):
"""Tests if something is a valid history backend."""
return is_string(x) or is_class(x) or isinstance(x, object)
def is_dynamic_cwd_width(x):
""" Determine if the input is a valid input for the DYNAMIC_CWD_WIDTH
environement variable.