Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_settings_defaults(load_source, settings):
load_source.return_value = object()
settings.init()
for key, val in const.DEFAULT_SETTINGS.items():
assert getattr(settings, key) == val
def test_create_if_doesnt_exists(self, settings):
settings_file = six.StringIO()
settings_path_mock = Mock(
is_file=Mock(return_value=False),
open=Mock(return_value=Mock(
__exit__=lambda *args: None, __enter__=lambda *args: settings_file)))
settings.user_dir = Mock(joinpath=Mock(return_value=settings_path_mock))
settings._init_settings_file()
settings_file_contents = settings_file.getvalue()
assert settings_path_mock.is_file.call_count == 1
assert settings_path_mock.open.call_count == 1
assert const.SETTINGS_HEADER in settings_file_contents
for setting in const.DEFAULT_SETTINGS.items():
assert '# {} = {}\n'.format(*setting) in settings_file_contents
settings_file.close()
def _rules_from_env(self, val):
"""Transforms rules list from env-string to python."""
val = val.split(':')
if 'DEFAULT_RULES' in val:
val = const.DEFAULT_RULES + [rule for rule in val if rule != 'DEFAULT_RULES']
return val
def _group_by_calls(log):
ps1 = os.environ['PS1']
ps1_newlines = ps1.count('\\n') + ps1.count('\n')
ps1_counter = 0
script_line = None
lines = []
for line in log:
if const.USER_COMMAND_MARK in line or ps1_counter > 0:
if script_line and ps1_counter == 0:
yield script_line, lines
if ps1_newlines > 0:
if ps1_counter <= 0:
ps1_counter = ps1_newlines
else:
ps1_counter -= 1
script_line = line
lines = [line]
elif script_line is not None:
lines.append(line)
if script_line:
yield script_line, lines
def get_key():
ch = getch()
if ch in const.KEY_MAPPING:
return const.KEY_MAPPING[ch]
elif ch == '\x1b':
next_ch = getch()
if next_ch == '[':
last_ch = getch()
if last_ch == 'A':
return const.KEY_UP
elif last_ch == 'B':
return const.KEY_DOWN
return ch
def _settings_from_file(self):
"""Loads settings from file."""
settings = load_source(
'settings', text_type(self.user_dir.joinpath('settings.py')))
return {key: getattr(settings, key)
for key in const.DEFAULT_SETTINGS.keys()
if hasattr(settings, key)}
def _init_settings_file(self):
settings_path = self.user_dir.joinpath('settings.py')
if not settings_path.is_file():
with settings_path.open(mode='w') as settings_file:
settings_file.write(const.SETTINGS_HEADER)
for setting in const.DEFAULT_SETTINGS.items():
settings_file.write(u'# {} = {}\n'.format(*setting))
def _read(f, fd):
data = os.read(fd, 1024)
try:
f.write(data)
except ValueError:
position = const.LOG_SIZE_IN_BYTES - const.LOG_SIZE_TO_CLEAN
f.move(0, const.LOG_SIZE_TO_CLEAN, position)
f.seek(position)
f.write(b'\x00' * const.LOG_SIZE_TO_CLEAN)
f.seek(position)
return data
def _read(f, fd):
data = os.read(fd, 1024)
try:
f.write(data)
except ValueError:
position = const.LOG_SIZE_IN_BYTES - const.LOG_SIZE_TO_CLEAN
f.move(0, const.LOG_SIZE_TO_CLEAN, position)
f.seek(position)
f.write(b'\x00' * const.LOG_SIZE_TO_CLEAN)
f.seek(position)
return data
def show_corrected_command(corrected_command):
sys.stderr.write(u'{prefix}{bold}{script}{reset}{side_effect}\n'.format(
prefix=const.USER_COMMAND_MARK,
script=corrected_command.script,
side_effect=u' (+side effect)' if corrected_command.side_effect else u'',
bold=color(colorama.Style.BRIGHT),
reset=color(colorama.Style.RESET_ALL)))