Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more objectmodel stuff #38

Merged
merged 3 commits into from
Jul 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ispyb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import ConfigParser as configparser
import logging

__version__ = '4.8.0'
__version__ = '4.9.0'

_log = logging.getLogger('ispyb')

Expand Down
19 changes: 19 additions & 0 deletions ispyb/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,22 @@ def __bool__(self):
def __nonzero__(self):
'''Python 2: value when used in bool() context.'''
return bool(self._value)


def add_properties(objectclass, property_list):
'''Generate class properties for a model that provide read-only access
to elements from the internal ._data data structure.

:param objectclass: The class to which properties should be added
:param property_list: A list of property name + data structure key
+ optional docstring tuples. Property names
then read from the given data structure keys.
'''
for prop_item in property_list:
key = prop_item[0]
internalkey = prop_item[1]
def model_attribute(self, k=internalkey):
return self._data[k]
if len(prop_item) > 2:
model_attribute.__doc__ = prop_item[2]
setattr(objectclass, key, property(model_attribute))
5 changes: 2 additions & 3 deletions ispyb/model/autoprocprogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def __str__(self):
' Last Message : {0.message}',
))).format(self)

for key, internalkey in (
ispyb.model.add_properties(AutoProcProgram, (
('program', 'programs'),
('command', 'commandLine'),
('environment', 'environment'),
Expand All @@ -83,5 +83,4 @@ def __str__(self):
('time_end', 'endTime'),
('status', 'status'),
('message', 'message'),
):
setattr(AutoProcProgram, key, property(lambda self, k=internalkey: self._data[k]))
))
21 changes: 12 additions & 9 deletions ispyb/model/datacollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def dcid(self):

@property
def group(self):
'''Returns a DataCollectionGroup object'''
'''Returns a DataCollectionGroup object.'''
if self._cache_group is None:
self._cache_group = DataCollectionGroup(self.dcgid, self._db.conn)
return self._cache_group
Expand All @@ -54,19 +54,22 @@ def __str__(self):
return 'DataCollection #%d (not yet loaded from database)' % self._dcid
return ('\n'.join((
'DataCollection #{0.dcid}',
' Status : {0.status}',
' Started : {0.time_start}',
' Finished : {0.time_end}',
' DC group : {0.dcgid}',
))).format(self)

for key, internalkey in (
('dcgid', 'groupId'),
('time_start', 'startTime'),
('time_end', 'endTime'),
('image_count', 'noImages'),
('image_start_number', 'startImgNumber'),
):
setattr(DataCollection, key, property(lambda self, k=internalkey: self._data[k]))
ispyb.model.add_properties(DataCollection, (
('dcgid', 'groupId', 'Returns the Data Collection Group ID associated with this data collection. '
'You can use .group to get the data collection group model object instead'),
('time_start', 'startTime', None),
('time_end', 'endTime', None),
('image_count', 'noImages', None),
('image_start_number', 'startImgNumber', None),
('status', 'status', 'Returns a string representing the current data collection status.'),
))


class DataCollectionGroup(ispyb.model.DBCache):
'''An object representing a DataCollectionGroup database entry. The object
Expand Down
5 changes: 2 additions & 3 deletions ispyb/model/processingjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,12 @@ def __str__(self):
' Timestamp : {pj.timestamp}',
))).format(pj=self)

for key, internalkey in (
ispyb.model.add_properties(ProcessingJob, (
('name', 'displayName'),
('comment', 'comments'),
('recipe', 'recipe'),
('timestamp', 'recordTimestamp'),
):
setattr(ProcessingJob, key, property(lambda self, k=internalkey: self._data[k]))
))


class ProcessingJobParameterValue(ispyb.model.EncapsulatedValue):
Expand Down