Skip to content

Commit

Permalink
add possibility to load resources list data directly (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jupe authored Apr 14, 2021
1 parent 5242026 commit 0325c66
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
26 changes: 19 additions & 7 deletions lockable/lockable.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,37 @@ class Lockable:
"""
def __init__(self, hostname=socket.gethostname(),
resource_list_file=None,
resource_list=None,
lock_folder=tempfile.gettempdir()):
self._allocations = dict()
self.logger = logging.getLogger('lockable.Lockable')
self.logger.debug('Initialized lockable')
self._hostname = hostname
self._lock_folder = lock_folder
self._resource_list = None
if resource_list_file:
self.load_resources_list(resource_list_file)
assert not (isinstance(resource_list, list) and
resource_list_file), 'only one of resource_list or ' \
'resource_list_file is accepted, not both'
if isinstance(resource_list_file, str):
self.load_resources_list_file(resource_list_file)
elif isinstance(resource_list, list):
self.load_resources_list(resource_list)
else:
self.logger.warning('resource_list_file is not configured')
self.logger.warning('resource_list_file or resource_list is not configured')

def load_resources_list(self, filename: str):
def load_resources_list_file(self, filename: str):
""" Load resources list file"""
self.load_resources_list(self._read_resources_list(filename))
self.logger.warning('Use resources from %s file', filename)

def load_resources_list(self, resources_list: list):
""" Load resources list """
self._resource_list = self._read_resources_list(filename)
self.logger.debug('Resources: ')
assert isinstance(resources_list, list), 'resources_list is not an list'
self._resource_list = resources_list
self.logger.debug('Resources loaded: ')
for resource in self._resource_list:
self.logger.debug(json.dumps(resource))
self.logger.warning('Use resources from %s file', filename)


@staticmethod
def _read_resources_list(filename):
Expand Down
10 changes: 6 additions & 4 deletions tests/test_Lockable.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
def create_lockable(data=[{"id": 1, "hostname": "myhost", "online": True}], lock_folder=None):
with TemporaryDirectory() as tmpdirname:
lock_folder = lock_folder or tmpdirname
list_file = os.path.join(tmpdirname, 'test.json')
with open(list_file, 'w') as fp:
fp.write(json.dumps(data))
yield Lockable(hostname='myhost', resource_list_file=list_file, lock_folder=lock_folder)
yield Lockable(hostname='myhost', resource_list=data, lock_folder=lock_folder)


class LockableTests(TestCase):
Expand All @@ -32,6 +29,11 @@ def test_constructor(self):
fp.write('[]')
Lockable(hostname='myhost', resource_list_file=list_file, lock_folder=tmpdirname)

def test_invalid_constructor(self):
with self.assertRaises(AssertionError):
Lockable(hostname='myhost', resource_list_file='asdf',
resource_list=[], lock_folder='.')

def test_lock_require_resources_json_loaded(self):
lockable = Lockable()
with self.assertRaises(AssertionError) as error:
Expand Down

0 comments on commit 0325c66

Please sign in to comment.