@@ -1,135 +1,135 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on Jul 15, 2014 |
|
3 | 3 | |
|
4 | 4 | @author: roj-idl71 |
|
5 | 5 | ''' |
|
6 | 6 | import time |
|
7 | 7 | import threading |
|
8 | 8 | import cPickle |
|
9 | 9 | |
|
10 | 10 | # try: |
|
11 | 11 | # from gevent import sleep |
|
12 | 12 | # except: |
|
13 | 13 | from time import sleep |
|
14 | 14 | |
|
15 | 15 | SERIALIZER = cPickle |
|
16 | 16 | |
|
17 | 17 | # from schainpy.serializer import DynamicSerializer |
|
18 | 18 | from schainpy.model.io.jroIO_usrp import USRPReader |
|
19 |
from schainpy.serializer. |
|
|
19 | from schainpy.model.serializer.data import obj2Serial | |
|
20 | 20 | |
|
21 | 21 | class USRPReaderAPI(USRPReader, threading.Thread): |
|
22 | 22 | |
|
23 | 23 | # __isBufferEmpty = True |
|
24 | 24 | |
|
25 | 25 | __DATAKEYLIST = ['data','utctime','flagNoData'] |
|
26 | 26 | |
|
27 | 27 | def __init__(self, serializer='msgpack'): |
|
28 | 28 | |
|
29 | 29 | threading.Thread.__init__(self) |
|
30 | 30 | USRPReader.__init__(self) |
|
31 | 31 | |
|
32 | 32 | # self.__serializerObj = DynamicSerializer.DynamicSerializer('msgpack') |
|
33 | 33 | self.__mySerial = None |
|
34 | 34 | self.__isBufferEmpty = True |
|
35 | 35 | |
|
36 | 36 | self.setSerializer(serializer) |
|
37 | 37 | |
|
38 | 38 | def setSerializer(self, serializer): |
|
39 | 39 | |
|
40 | 40 | self.__serializer = serializer |
|
41 | 41 | |
|
42 | 42 | def getSerializer(self): |
|
43 | 43 | |
|
44 | 44 | return self.__serializer |
|
45 | 45 | |
|
46 | 46 | def getProfileIndex(self): |
|
47 | 47 | |
|
48 | 48 | return self.profileIndex |
|
49 | 49 | |
|
50 | 50 | def getSerialMetaData(self): |
|
51 | 51 | |
|
52 | 52 | if self.__isBufferEmpty: |
|
53 | 53 | ini = time.time() |
|
54 | 54 | |
|
55 | 55 | while True: |
|
56 | 56 | |
|
57 | 57 | if not self.__isBufferEmpty: |
|
58 | 58 | break |
|
59 | 59 | |
|
60 | 60 | if time.time() - ini > 20: |
|
61 | 61 | break |
|
62 | 62 | |
|
63 | 63 | sleep(1e-12) |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | # if not self.getData(): |
|
67 | 67 | # self.__isBufferEmpty = False |
|
68 | 68 | # return None |
|
69 | 69 | |
|
70 | 70 | if self.dataOut.flagNoData: |
|
71 | 71 | return None |
|
72 | 72 | |
|
73 | 73 | myMetadataSerial = obj2Serial(self.dataOut, |
|
74 | 74 | serializer = self.__serializer) |
|
75 | 75 | |
|
76 | 76 | return myMetadataSerial |
|
77 | 77 | |
|
78 | 78 | def getSerialData(self): |
|
79 | 79 | |
|
80 | 80 | if self.__isBufferEmpty: |
|
81 | 81 | ini = time.time() |
|
82 | 82 | |
|
83 | 83 | while True: |
|
84 | 84 | |
|
85 | 85 | if not self.__isBufferEmpty: |
|
86 | 86 | break |
|
87 | 87 | |
|
88 | 88 | if time.time() - ini > 20: |
|
89 | 89 | break |
|
90 | 90 | |
|
91 | 91 | sleep(1e-12) |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | # if not self.getData(): |
|
95 | 95 | # self.__isBufferEmpty = False |
|
96 | 96 | # return None |
|
97 | 97 | |
|
98 | 98 | if self.dataOut.flagNoData: |
|
99 | 99 | return None |
|
100 | 100 | |
|
101 | 101 | self.__isBufferEmpty = True |
|
102 | 102 | |
|
103 | 103 | return self.__mySerial |
|
104 | 104 | |
|
105 | 105 | def run(self): |
|
106 | 106 | |
|
107 | 107 | ''' |
|
108 | 108 | This method will be called once when start() is called |
|
109 | 109 | ''' |
|
110 | 110 | |
|
111 | 111 | if not self.isConfig: |
|
112 | 112 | raise RuntimeError, 'setup() method has to be called before start()' |
|
113 | 113 | |
|
114 | 114 | while True: |
|
115 | 115 | |
|
116 | 116 | if not self.__isBufferEmpty: |
|
117 | 117 | sleep(1e-12) |
|
118 | 118 | continue |
|
119 | 119 | |
|
120 | 120 | if not self.getData(): |
|
121 | 121 | break |
|
122 | 122 | |
|
123 | 123 | print ".", |
|
124 | 124 | |
|
125 | 125 | self.__mySerial = obj2Serial(self.dataOut, |
|
126 | 126 | keyList = self.__DATAKEYLIST, |
|
127 | 127 | serializer = self.__serializer) |
|
128 | 128 | self.__isBufferEmpty = False |
|
129 | 129 | |
|
130 | 130 | # print self.profileIndex |
|
131 | 131 | # print 'wait 1 second' |
|
132 | 132 | |
|
133 | 133 | # sleep(0.1) |
|
134 | 134 | |
|
135 | 135 | return No newline at end of file |
|
1 | NO CONTENT: file renamed from schainpy/serializer/__init__.py to schainpy/model/serializer/__init__.py |
@@ -1,115 +1,128 | |||
|
1 | 1 | ''' |
|
2 | 2 | Created on Jul 15, 2014 |
|
3 | 3 | |
|
4 | 4 | @author: Miguel Urco |
|
5 | 5 | ''' |
|
6 |
from |
|
|
6 | from serializer import DynamicSerializer | |
|
7 | 7 | |
|
8 | 8 | PICKLE_SERIALIZER = DynamicSerializer('cPickle') |
|
9 | 9 | MSGPACK_SERIALIZER = DynamicSerializer('msgpack') |
|
10 | 10 | |
|
11 | 11 | from schainpy.model.data.jrodata import * |
|
12 | 12 | |
|
13 | 13 | CLASSNAME_KEY = 'classname__' |
|
14 | 14 | |
|
15 | 15 | def isNotClassVar(myObj): |
|
16 | 16 | |
|
17 | 17 | return not hasattr(myObj,'__dict__') |
|
18 | 18 | |
|
19 | 19 | def isDictFormat(thisValue): |
|
20 | 20 | |
|
21 | 21 | if type(thisValue) != type({}): |
|
22 | 22 | return False |
|
23 | 23 | |
|
24 | 24 | if CLASSNAME_KEY not in thisValue.keys(): |
|
25 | 25 | return False |
|
26 | 26 | |
|
27 | 27 | return True |
|
28 | 28 | |
|
29 | 29 | def obj2Dict(myObj, keyList=[]): |
|
30 | 30 | |
|
31 | 31 | if not keyList: |
|
32 | 32 | keyList = myObj.__dict__.keys() |
|
33 | 33 | |
|
34 | 34 | myDict = {} |
|
35 | 35 | |
|
36 | 36 | myDict[CLASSNAME_KEY] = myObj.__class__.__name__ |
|
37 | 37 | |
|
38 | 38 | for thisKey, thisValue in myObj.__dict__.items(): |
|
39 | 39 | |
|
40 | 40 | if thisKey not in keyList: |
|
41 | 41 | continue |
|
42 | 42 | |
|
43 | 43 | if isNotClassVar(thisValue): |
|
44 | 44 | myDict[thisKey] = thisValue |
|
45 | 45 | continue |
|
46 | 46 | |
|
47 | 47 | ## If this value is another class instance |
|
48 | 48 | myNewDict = obj2Dict(thisValue) |
|
49 | 49 | myDict[thisKey] = myNewDict |
|
50 | 50 | |
|
51 | 51 | return myDict |
|
52 | 52 | |
|
53 | 53 | def dict2Obj(myDict): |
|
54 | 54 | ''' |
|
55 | 55 | ''' |
|
56 | 56 | |
|
57 | 57 | if CLASSNAME_KEY not in myDict.keys(): |
|
58 | 58 | return None |
|
59 | 59 | |
|
60 | 60 | className = eval(myDict[CLASSNAME_KEY]) |
|
61 | 61 | |
|
62 | 62 | myObj = className() |
|
63 | 63 | |
|
64 | 64 | for thisKey, thisValue in myDict.items(): |
|
65 | 65 | |
|
66 | 66 | if thisKey == CLASSNAME_KEY: |
|
67 | 67 | continue |
|
68 | 68 | |
|
69 | 69 | if not isDictFormat(thisValue): |
|
70 | 70 | setattr(myObj, thisKey, thisValue) |
|
71 | 71 | continue |
|
72 | 72 | |
|
73 | 73 | myNewObj = dict2Obj(thisValue) |
|
74 | 74 | setattr(myObj, thisKey, myNewObj) |
|
75 | 75 | |
|
76 | 76 | return myObj |
|
77 | 77 | |
|
78 |
def |
|
|
78 | def dict2Serial(myDict, serializer='msgpack'): | |
|
79 | 79 | |
|
80 | 80 | if serializer == 'cPickle': |
|
81 | 81 | SERIALIZER = PICKLE_SERIALIZER |
|
82 | 82 | else: |
|
83 | 83 | SERIALIZER = MSGPACK_SERIALIZER |
|
84 |
|
|
|
85 | myDict = obj2Dict(myObj, **kwargs) | |
|
84 | ||
|
86 | 85 | mySerial = SERIALIZER.dumps(myDict) |
|
87 | 86 | |
|
88 | 87 | return mySerial |
|
89 | 88 | |
|
90 | 89 | def serial2Dict(mySerial, serializer='msgpack'): |
|
91 | 90 | |
|
92 | 91 | if serializer == 'cPickle': |
|
93 | 92 | SERIALIZER = PICKLE_SERIALIZER |
|
94 | 93 | else: |
|
95 | 94 | SERIALIZER = MSGPACK_SERIALIZER |
|
95 | ||
|
96 | myDict = SERIALIZER.loads(mySerial) | |
|
97 | ||
|
98 | return myDict | |
|
99 | ||
|
100 | def obj2Serial(myObj, serializer='msgpack', **kwargs): | |
|
101 | ||
|
102 | if serializer == 'cPickle': | |
|
103 | SERIALIZER = PICKLE_SERIALIZER | |
|
104 | else: | |
|
105 | SERIALIZER = MSGPACK_SERIALIZER | |
|
96 | 106 | |
|
97 | return SERIALIZER.loads(mySerial) | |
|
107 | myDict = obj2Dict(myObj, **kwargs) | |
|
108 | mySerial = dict2Serial(myDict, serializer) | |
|
109 | ||
|
110 | return mySerial | |
|
98 | 111 | |
|
99 | 112 | def serial2Obj(mySerial, metadataDict = {}, serializer='msgpack'): |
|
100 | 113 | |
|
101 | 114 | if serializer == 'cPickle': |
|
102 | 115 | SERIALIZER = PICKLE_SERIALIZER |
|
103 | 116 | else: |
|
104 | 117 | SERIALIZER = MSGPACK_SERIALIZER |
|
105 | 118 | |
|
106 |
myDataDict = |
|
|
119 | myDataDict = serial2Dict(mySerial, serializer) | |
|
107 | 120 | |
|
108 | 121 | if not metadataDict: |
|
109 | 122 | myObj = dict2Obj(myDataDict) |
|
110 | 123 | return myObj |
|
111 | 124 | |
|
112 | 125 | metadataDict.update(myDataDict) |
|
113 | 126 | myObj = dict2Obj(metadataDict) |
|
114 | 127 | |
|
115 | return myObj No newline at end of file | |
|
128 | return myObj |
|
1 | NO CONTENT: file renamed from schainpy/serializer/JROSerializer.py to schainpy/model/serializer/serializer.py |
|
1 | NO CONTENT: file renamed from schainpy/serializer/test/DynamicObject.py to schainpy/model/serializer/test/DynamicObject.py |
|
1 | NO CONTENT: file renamed from schainpy/serializer/test/DynamicSerializer.py to schainpy/model/serializer/test/DynamicSerializer.py |
|
1 | NO CONTENT: file renamed from schainpy/serializer/test/DynamicYAML.py to schainpy/model/serializer/test/DynamicYAML.py |
|
1 | NO CONTENT: file renamed from schainpy/serializer/test/Lookup.py to schainpy/model/serializer/test/Lookup.py |
|
1 | NO CONTENT: file renamed from schainpy/serializer/test/OrderedYAML.py to schainpy/model/serializer/test/OrderedYAML.py |
|
1 | NO CONTENT: file renamed from schainpy/serializer/test/PrecisionTime.py to schainpy/model/serializer/test/PrecisionTime.py |
|
1 | NO CONTENT: file renamed from schainpy/serializer/test/Serializer.py to schainpy/model/serializer/test/Serializer.py |
|
1 | NO CONTENT: file renamed from schainpy/serializer/test/__init__.py to schainpy/model/serializer/test/__init__.py |
|
1 | NO CONTENT: file renamed from schainpy/serializer/test/serialtest.py to schainpy/model/serializer/test/serialtest.py |
|
1 | NO CONTENT: file renamed from schainpy/serializer/test/test _serializer_speed.py to schainpy/model/serializer/test/test _serializer_speed.py |
General Comments 0
You need to be logged in to leave comments.
Login now