Skip to content

Commit

Permalink
Enables history cross sessions (storing in a file)
Browse files Browse the repository at this point in the history
  • Loading branch information
TizianoPerrucci committed Mar 14, 2015
1 parent 58ed6a4 commit d134c3e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
*.iml
*.pyc
p-env
tests/env
tests/envs
dist
prudentia.egg-info
build
.history.txt
7 changes: 3 additions & 4 deletions bin/prudentia
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import logging
import os
from os import path

from prudentia.cli import CLI


if __name__ == "__main__":
os.environ['PYTHONUNBUFFERED'] = '1'
PRUDENTIA_USER_DIR = path.join(path.expanduser('~'), 'prudentia')
os.environ['PRUDENTIA_USER_DIR'] = PRUDENTIA_USER_DIR = path.join(path.expanduser('~'), 'prudentia')

if not os.path.exists(PRUDENTIA_USER_DIR):
os.makedirs(PRUDENTIA_USER_DIR)
Expand All @@ -22,6 +19,8 @@ if __name__ == "__main__":
level=logging.WARNING
)

from prudentia.cli import CLI

cli = CLI()

exit_error = 0
Expand Down
21 changes: 20 additions & 1 deletion prudentia/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
__version__ = '0.8.1'
__author__ = 'Tiziano Perrucci'


import readline
import atexit
import os

if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind ^I rl_complete")
else:
readline.parse_and_bind("tab: complete")


historyPath = os.path.join(os.environ['PRUDENTIA_USER_DIR'], '.history.txt')


def save_history(p=historyPath):
import readline
readline.write_history_file(p)


if os.path.exists(historyPath):
try:
readline.read_history_file(historyPath)
except:
pass

atexit.register(save_history)
del os, atexit, readline, save_history, historyPath
2 changes: 1 addition & 1 deletion prudentia/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


class Environment(object):
DEFAULT_ENVS_PATH = path.join(path.expanduser('~'), 'prudentia', 'envs')
DEFAULT_ENVS_PATH = path.join(os.environ['PRUDENTIA_USER_DIR'], 'envs')
DEFAULT_ENV_FILE_NAME = 'boxes.json'

def __init__(self, id_env, general_type=None, box_extra_type=None, envs_path=DEFAULT_ENVS_PATH,
Expand Down
3 changes: 3 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import os

os.environ['PRUDENTIA_USER_DIR'] = '.'
6 changes: 3 additions & 3 deletions tests/domain_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

class TestEnvironment(unittest.TestCase):
def setUp(self):
self.env = Environment(id_env='test', envs_path='./env')
self.env = Environment(id_env='test')
self.test_box = Box('box-name', 'dev.yml', 'box-host', '0.0.0.0')

def test_add(self):
self.env.add(self.test_box)
box = json.load(open('./env/test/' + Environment.DEFAULT_ENV_FILE_NAME, 'r'))[0]
box = json.load(open(Environment.DEFAULT_ENVS_PATH + '/test/' + Environment.DEFAULT_ENV_FILE_NAME, 'r'))[0]
self.assertEqual(box['name'], self.test_box.name)
self.assertFalse('remote_user' in box)
self.assertFalse('remote_pwd' in box)
Expand All @@ -31,4 +31,4 @@ def test_get_not_valid_box(self):

def test_remove(self):
self.env.remove('box-name')
self.assertEqual(json.load(open('./env/test/' + Environment.DEFAULT_ENV_FILE_NAME, 'r')), [])
self.assertEqual(json.load(open(Environment.DEFAULT_ENVS_PATH + '/test/' + Environment.DEFAULT_ENV_FILE_NAME, 'r')), [])

0 comments on commit d134c3e

Please sign in to comment.