Commit 7b86aa50 authored by Lauri Himanen's avatar Lauri Himanen

Modified the CacheService, updated installation instructions.

parent 3f243d57
......@@ -18,46 +18,30 @@ The simplest way to have this is to check out nomad-lab-base recursively:
then this will be in python-common within it.
# Quick Install
# Local Install
The following instructions were tested on Ubuntu 14.04.
The modules used for parser development are located under the 'nomadcore' package. You can setup this package in any way you like, but a simple installation script 'setup.py' is provided for ease of use. It will install the nomadcore package along with the needed dependencies. You can install the package in development mode by calling the terminal command
The modules used for parser development are located under the 'nomadcore'
package. If you wish to setup this package locally, you can do so by first
installing the required dependencies:
```sh
python setup.py develop --user
```
in the folder where setup.py is located. After this the package will be available to import by simply calling
```python
import nomadcore
pip install -r requirements.txt
```
in python. The development mode also means that the latest version of the code is used, so any updates from git will automatically be available. You can install a static snapshot by using 'install' instead of 'develop'.
# Manual install
This package depends on other python libraries which are declared in 'requirements.txt'. The requirements can be installed simply by calling the terminal command
and then installing the package itself with the provided installation script:
```sh
pip install -r requirements.txt
python setup.py develop --user
```
in the folder where the file is located.
In order to use the nomadcore package you have to add the directory to PYTHONPATH so that python knows where to look for it. This can be achieved temporarily by using a script like this
After this the package will be available to import by simply calling
```python
import sys
dir = os.path.normpath("path/to/python-common/python"))
if not commonDir in sys.path:
sys.path.insert(0, commonDir)
```
or the addition can be made permanent by adding the line
```sh
export PYTHONPATH="${PYTHONPATH}:/path/to/python-common/python"
import nomadcore
```
to your ~/.bashrc file.
in python. The development mode also means that the latest version of the code
is used, so any updates from git will automatically be available. You can
install a static snapshot by using 'install' instead of 'develop'.
......@@ -380,44 +380,25 @@ class ParserContext(object):
self.cache_service = None
#===============================================================================
class CacheMode(object):
"""Enumeration for different types of cache modes for CacheObjects.
"""
MULTI_IN_MULTI_OUT = 1
MULTI_OUT_SINGLE_IN = 2
SINGLE_IN_MULTI_OUT = 3
SINGLE_IN_SINGLE_OUT = 4
#===============================================================================
class CacheObject(object):
"""Wraps an intermediate value stored during parsing.
"""
def __init__(self, name, mode, value):
def __init__(self, name, value, single=True, update=True):
self.name = name
self.cache_mode = mode
self._value = value
# self._reset = False
self._in = False
self._out = False
self._single = single
self._update = update
self._pushed = False
self._updated = value is not None
@property
def value(self):
if self.cache_mode == CacheMode.SINGLE_IN_SINGLE_OUT:
if self._out:
raise Exception("Cannot get the value for '{}' multiple times because of the cache mode.")
self._out = True
return self._value
@value.setter
def value(self, value):
if self.cache_mode == CacheMode.SINGLE_IN_MULTI_OUT or self.cache_mode == CacheMode.SINGLE_IN_SINGLE_OUT:
if self._in:
raise Exception("Cannot set the value for '{}' multiple times because of the cache mode.")
self._value = value
self._in = True
#===============================================================================
......@@ -440,12 +421,16 @@ class CacheService(object):
raises:
"""
cache_object = self.get_cache_object(name)
return cache_object.value
def get_cache_object(self, name):
cache_object = self._cache.get(name)
if cache_object is None:
logger.warning("The cache object for '{}' could not be found".format(name))
return None
value = cache_object.value
return value
return cache_object
def __setitem__(self, name, value):
"""Used to set the value for an item. The CacheObject corresponding to
......@@ -454,28 +439,45 @@ class CacheService(object):
"""
cache_object = self._cache[name]
cache_object.value = value
cache_object._updated = True
def add_cache_object(self, name, mode, value=None):
def add_cache_object(self, name, value=None, single=True, update=True):
"""Used to add a cache object. Two cache objects with the same name are
not allowed.
"""
if name in self._cache:
raise LookupError("There already exists a cached value with the name '{}'. All keys in the CacheService should be unique.".format(name))
cache_object = CacheObject(name, mode, value)
cache_object = CacheObject(name, value, single, update)
self._cache[name] = cache_object
def check_push_allowed(self, cache_object):
if cache_object._single:
if cache_object._pushed:
raise LookupError("The CacheService value '{}' has already been output to the backend. The CacheOutputMode does not allow this.".format(cache_object.name))
if cache_object._update:
if not cache_object._updated:
raise LookupError("The CacheService value '{}' has not been update since it was last output. The CacheUpdateMode does not allow this.".format(cache_object.name))
def push_value(self, name, metaname=None):
"""Pushes the scalar value stored with the given name to the backend.
If the name cannot be found, nothing is pushed. If the metaname
property is not defined the value is pushed with the name that was used
as key.
"""
cache_object = self.get_cache_object(name)
if cache_object is None or cache_object._value is None:
logger.warning("The value for metaname '{}' was not set in the CacheService, and could not be pushed".format(name))
return
self.check_push_allowed(cache_object)
if metaname is None:
metaname = name
value = self[name]
if value is not None:
self.backend.addValue(metaname, value)
# del self._cache[name]
self.backend.addValue(metaname, cache_object.value)
cache_object._pushed = True
cache_object._updated = False
def push_real_value(self, name, unit=None, metaname=None):
"""Pushes the scalar value stored with the given name to the backend.
......@@ -483,12 +485,22 @@ class CacheService(object):
property is not defined the value is pushed with the name that was used
as key.
"""
cache_object = self.get_cache_object(name)
if cache_object is None or cache_object._value is None:
logger.warning("The value for metaname '{}' was not set in the CacheService, and could not be pushed".format(name))
return
self.check_push_allowed(cache_object)
if metaname is None:
metaname = name
value = self[name]
if value is not None:
self.backend.addRealValue(metaname, value, unit=unit)
# del self._cache[name]
if cache_object.value is not None:
self.backend.addRealValue(metaname, cache_object.value, unit=unit)
cache_object._pushed = True
cache_object._updated = False
else:
logger.warning("The value for metaname '{}' was not set in the CacheService, and could not be pushed".format(name))
def push_array_values(self, name, unit=None, metaname=None):
"""Pushes the scalar value stored with the given name to the backend.
......@@ -496,12 +508,22 @@ class CacheService(object):
property is not defined the value is pushed with the name that was used
as key.
"""
cache_object = self.get_cache_object(name)
if cache_object is None or cache_object._value is None:
logger.warning("The value for metaname '{}' was not set in the CacheService, and could not be pushed".format(name))
return
self.check_push_allowed(cache_object)
if metaname is None:
metaname = name
value = self[name]
if value is not None:
self.backend.addArrayValues(metaname, np.array(value), unit=unit)
# del self._cache[name]
if cache_object.value is not None:
self.backend.addArrayValues(metaname, np.array(cache_object.value), unit=unit)
cache_object._pushed = True
cache_object._updated = False
else:
logger.warning("The value for metaname '{}' was not set in the CacheService, and could not be pushed".format(name))
#===============================================================================
......
......@@ -18,10 +18,6 @@ def main():
package_data={
'nomadcore.unit_conversion': ['*.txt'],
},
install_requires=[
'pint',
'numpy',
],
zip_safe=False
)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment