Skip to content

Commit

Permalink
fixing up state interface a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
toumorokoshi committed Feb 16, 2015
1 parent 7cf6f23 commit 7e6e39e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 41 deletions.
6 changes: 0 additions & 6 deletions uranium/part.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,4 @@ def __eq__(self, other):
if self.name != other.name:
return False

if self.type != other.type:
return False

if self.entry_point != other.entry_point:
return False

return super(Part, self).__eq__(other)
19 changes: 9 additions & 10 deletions uranium/state.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import yaml
from .part import Part

PART_STATE_KEY = 'parts'

Expand All @@ -14,30 +15,28 @@ def __init__(self, state_file_path=None):
PART_STATE_KEY: {}
}

def store(self):
def save(self):
if hasattr(self, '_state_file_path'):
with open(self._state_file_path, 'w+') as fh:
fh.write(yaml.dump(self._state,
default_flow_style=False))
return True
return False

def retrieve(self):
def load(self):
if hasattr(self, '_state_file_path'):
with open(self._state_file_path, 'r') as fh:
self._state = yaml.load(fh.read())
return True
return False

def set_is_installed(self, part):
def set_part(self, part):
""" let state know the part is installed """
self._state[PART_STATE_KEY][part.name] = {
'type': part.type,
'entry_point': part.entry_point
}
self._state[PART_STATE_KEY][part.name] = dict(part.items())

def is_part_installed(self, part_name):
def has_part(self, part_name):
return part_name in self._state[PART_STATE_KEY]

def get_installed_part(self, part_name):
return self._state[PART_STATE_KEY].get(part_name)
def get_part(self, part_name):
return Part(part_name,
self._state[PART_STATE_KEY].get(part_name))
30 changes: 12 additions & 18 deletions uranium/tests/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,21 @@ def setUp(self):
def tearDown(self):
os.unlink(self.temp_file)

def test_set_is_installed(self):
def test_set_part(self):
part = Part('foo', {'_plugin': 'foo'})
self.state.set_is_installed(part)
ok_(self.state.is_part_installed(part.name))
self.state.set_part(part)
ok_(self.state.has_part(part.name))

def get_installed_part(self):
def test_get_part(self):
part = Part('foo', {'_plugin': 'foo'})
self.state.set_is_installed(part)
eq_(self.state.get_installed_part(part.name), {
'type': part.type,
'entry_point': part.entry_point
})
self.state.set_part(part)
eq_(self.state.get_part(part.name), part)

def test_store(self):
def test_save(self):
part = Part('foo', {'_plugin': 'foo'})
self.state.set_is_installed(part)
self.state.store()
self.state.set_part(part)
self.state.save()
new_state = State(self.temp_file)
new_state.retrieve()
ok_(new_state.is_part_installed(part.name))
eq_(new_state.get_installed_part(part.name), {
'type': 'plugin',
'entry_point': 'foo'
})
new_state.load()
ok_(new_state.has_part(part.name))
eq_(new_state.get_part(part.name), part)
13 changes: 6 additions & 7 deletions uranium/uranium.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .phases import (AFTER_EGGS, BEFORE_EGGS)
from .pip_manager import PipManager
from .plugin_runner import PluginRunner
from .store import Store
from .state import State
LOGGER = logging.getLogger(__name__)

PARTS_DIRECTORY = "parts"
Expand All @@ -22,7 +22,7 @@ class UraniumException(Exception):

class Uranium(object):

def __init__(self, config, root, store_file=None):
def __init__(self, config, root, state_file=None):
# well cast the dict to a config for people
# to make it easier
if type(config) == dict:
Expand All @@ -37,7 +37,7 @@ def __init__(self, config, root, store_file=None):
self._classloader = ClassLoader(self._pip)
self._buildout = BuildoutAdapter(self, self._classloader)
self._plugin_runner = PluginRunner(self, self._classloader)
self._store = Store(store_file)
self._state = State(state_file)
self._validate_config()

@property
Expand Down Expand Up @@ -78,13 +78,12 @@ def run_part(self, name):

part_instance = runner.get_part_instance(part)

if self._store.is_part_installed(name):
if not self._state.has_part(name):
runner.install_part(part_instance)

else:
part_info = self._store.get_installed_part(name)
if part_info['type'] == part.type and \
part_info['entry_point'] == part.entry_point:
old_part = self._state.get_part(name)
if part == old_part:
runner.update_part(part_instance)
else:
# todo: add a delete part
Expand Down

0 comments on commit 7e6e39e

Please sign in to comment.