PythonOlogClientLibrary
OlogDataTypes.py
1 '''
2 Copyright (c) 2010 Brookhaven National Laboratory
3 All rights reserved. Use is subject to license terms and conditions.
4 
5 Created on Jan 10, 2013
6 
7 @author: shroffk
8 '''
9 
10 class LogEntry(object):
11  '''
12  A LogEntry consists of some Text description, an owner and an associated logbook
13  It can optionally be associated with one or more logbooks and contain one or more tags, properties and attachments
14  '''
15 
16  def __init__(self, text, owner, logbooks, tags=[], attachments=[], properties=[], id=None, createTime=None, modifyTime=None):
17  '''
18  Constructor for log Entry
19  Simple LogEntry
20  >> LogEntry('test log entry', 'controls', logbooks=[Logbook('commissioning', owner='controls')])
21 
22  Comprehensive logEntry
23  >> LogEntry('test log entry', 'controls',
24  logbooks=[Logbook('commissioning', owner='controls')],
25  tags=[Tag('TimingSystem')]
26  properties=[Property('Ticket', attributes={'Id':'1234','URL':'http://trac.nsls2.bnl.gov/trac/1234'}]
27  attachments=[Attachment(open('databrowser.plt'))]
28  )
29  '''
30  self.Text = str(text).strip();
31  self.Owner = str(owner).strip();
32  self.logbooks = logbooks
33  self.tags = tags
34  self.attachments = attachments
35  self.properties = properties
36  self.__id = id
37  self.__createTime = createTime
38  self.__modifytime = modifyTime
39 
40  def getId(self):
41  return self.__id
42 
43  def getCreateTime(self):
44  return self.__createTime
45 
46  def getModifyTime(self):
47  return self.__modifytime
48 
49  def getText(self):
50  return self.Text
51 
52  def getOwner(self):
53  return self.Owner
54 
55  def getLogbooks(self):
56  return self.logbooks
57 
58  def getTags(self):
59  return self.tags
60 
61  def getAttachments(self):
62  return self.attachments
63 
64  def getProperties(self):
65  return self.properties
66 
67  def __cmp__(self, *arg, **kwargs):
68  if arg[0] == None:
69  return 1
70  if self.__id:
71  return cmp((self.__id),(arg[0].__id))
72  else:
73  raise Exception, 'Invalid LogEntry: id cannot be None'
74 
75 
76 class Logbook(object):
77  '''
78  A Logbook consist of an unique name and an owner,
79  logentries can be added to a logbook so long as the user either the owner
80  or a member of the owner group
81  '''
82 
83  def __init__(self, name, owner):
84  '''
85  Create a logbook
86  >> Logbook('commissioning', 'controls')
87  '''
88  self.__Name = str(name).strip();
89  self.__Owner = str(owner).strip();
90 
91  def getName(self):
92  return self.__Name
93 
94  def getOwner(self):
95  return self.__Owner
96 
97  def __cmp__(self, *arg, **kwargs):
98  if arg[0] == None:
99  return 1
100  return cmp((self.__Name, self.__Owner), (arg[0].__Name, arg[0].__Owner))
101 
102 class Tag(object):
103  '''
104  A Tag consists of a unique name, it is used to tag logEntries to enable querying and organizing log Entries
105  '''
106 
107  def __init__(self, name, state="Active"):
108  '''
109  Create a Tag object
110  >> Tag('TimingSystem')
111  '''
112  self.__Name = str(name).strip()
113  self.__State = str(state).strip()
114 
115  def getName(self):
116  return self.__Name
117 
118  def getState(self):
119  return self.__State
120 
121  def __cmp__(self, *arg, **kwargs):
122  if arg[0] == None:
123  return 1
124  return cmp((self.__Name, self.__State), (arg[0].__Name, arg[0].__State))
125 
126 class Attachment(object):
127  '''
128  A Attachment, a file associated with the log entry
129  TODO this is not thread safe
130  '''
131 
132  def __init__(self, file):
133  '''
134  Create Attachment
135  >> Attachment(file=open('/home/usr/databrowser.plt')
136  >> Attachment(file=open('test.jpg','rb')
137  '''
138  self.__file=file
139 
140  def getFile(self):
141  return self.__file
142 
143 class Property(object):
144  '''
145  A property consists of a unique name and a set of attributes consisting of key value pairs
146  '''
147 
148  def __init__(self, name, attributes=None):
149  '''
150  Create a property with a unique name and attributes
151  >> Property('Ticket', attributes={'Id':'1234','URL':'http://trac.nsls2.bnl.gov/trac/1234'}
152  >> Property('Scan', attributes={'Number':'run-1234', 'script':'scan_20130117.py'}
153  '''
154  self.__Name = str(name).strip()
155  self.Attributes = attributes
156 
157  def getName(self):
158  return self.__Name
159 
160  def getAttributes(self):
161  return self.Attributes;
162 
163  def getAttributeNames(self):
164  return self.Attributes.keys()
165 
166  def getAttributeValue(self, attributeName):
167  return self.Attributes.get(attributeName)
168 
169  def __cmp__(self, *arg, **kwargs):
170  if arg[0] == None:
171  return 1
172  return cmp((self.__Name, set(self.Attributes)), (arg[0].__Name, set(arg[0].Attributes)))
173