]> git.evergreen-ils.org Git - working/random.git/blob - contrib/evergreen/eg_data.py
fixed bug in property reading from eg_data module. more python style tweaks
[working/random.git] / contrib / evergreen / eg_data.py
1 from constrictor.properties import Properties
2 from constrictor.script import ScriptThread
3 from constrictor.log import *
4 from oils.utils.utils import unique
5
6 PROP_USERNAME = 'evergreen.username'
7 PROP_PASSWORD = 'evergreen.password'
8 PROP_WORKSTATION = 'evergreen.workstation'
9 PROP_COPY_BARCODE = 'evergreen.copyBarcodes'
10 PROP_TITLE_ID = 'evergreen.titleIDs'
11 PROP_PATRON_BARCODE = 'evergreen.patronBarcodes'
12 PROP_ORG_ID = 'evergreen.orgIDs'
13 PROP_PATRON_ID = 'evergreen.patronIDs'
14
15 PROP_CONSTRICTOR_THREADS = 'constrictor.numThreads'
16
17 class DataManagerException(Exception):
18     pass
19
20 class DataManager(object):
21     ''' This module manages a global cache of test data '''
22
23     def __init__(self):
24         self.data = {}
25         self.props = Properties.get_properties()
26         self.readProps()
27         log_debug(self)
28
29     def __str__(self):
30         s = 'DataManager() read properties:\n'
31         for p in [
32             PROP_PASSWORD,
33             PROP_WORKSTATION,
34             PROP_COPY_BARCODE,
35             PROP_TITLE_ID,
36             PROP_PATRON_BARCODE,
37             PROP_PATRON_ID,
38             PROP_ORG_ID ]:
39
40             s += "\t%s=%s\n" % (p, str(self.data[p]))
41         return s
42
43
44     def readProps(self):
45         self.readProp(PROP_USERNAME)
46         self.readProp(PROP_PASSWORD)
47         self.readProp(PROP_WORKSTATION)
48         self.readProp(PROP_COPY_BARCODE, True)
49         self.readProp(PROP_TITLE_ID, True)
50         self.readProp(PROP_PATRON_BARCODE, True)
51         self.readProp(PROP_PATRON_ID, True)
52         self.readProp(PROP_ORG_ID, True)
53
54     def readProp(self, prop, split=False):
55         v = self.props.get_property(prop)
56         if split and v:
57             v = unique(v.split(','))
58         self.data[prop] = v
59         log_debug("DataManager set property %s => %s" % (prop, str(v)))
60
61     def get_thread_data(self, prop, noSharing=False):
62         ''' If the caller is requesting array-based data, we want to de-multiplex(?) 
63             the requested data based on the currently running thread.  In other
64             words, each thread should have its own view into the array of data
65             so that different threads are not sharing data.  If there are more
66             running threads than unique points of data, then sharing is inevitable,
67             and it's assumed that's OK unless noSharing is set to true, in which
68             case an exception is raised.  For atomic data, the value is returned
69             and no thread data is checked '''
70
71         data = self.data.get(prop)
72         if not isinstance(data, list):
73             return data
74
75         currentThread = ScriptThread.get_thread_id()
76         totalThreads = self.props.get_property(PROP_CONSTRICTOR_THREADS)
77
78         if len(data) > currentThread:
79             return data[currentThread]
80
81         if noSharing:
82             raise DataManagerException(
83                 "Too many threads for unique data.  Thread index is %d, size of dataset is %d" % (
84                     currentThread, len(data)))
85         
86         # data sharing is OK  
87         return data[currentThread % len(data)]
88