1 | """ |
---|
2 | Loading thread |
---|
3 | """ |
---|
4 | import time |
---|
5 | import sys |
---|
6 | import os |
---|
7 | |
---|
8 | from sas.data_util.calcthread import CalcThread |
---|
9 | |
---|
10 | |
---|
11 | EXTENSIONS = ['.svs', '.prv', '.inv', '.fitv'] |
---|
12 | |
---|
13 | class DataReader(CalcThread): |
---|
14 | """ |
---|
15 | Load a data given a filename |
---|
16 | """ |
---|
17 | def __init__(self, path, loader, |
---|
18 | flag=True, |
---|
19 | transform_data=None, |
---|
20 | completefn=None, |
---|
21 | updatefn=None, |
---|
22 | yieldtime=0.01, |
---|
23 | worktime=0.01): |
---|
24 | CalcThread.__init__(self, completefn, |
---|
25 | updatefn, |
---|
26 | yieldtime, |
---|
27 | worktime) |
---|
28 | self.load_state_flag = flag |
---|
29 | self.transform_data = transform_data |
---|
30 | self.list_path = path |
---|
31 | # Instantiate a loader |
---|
32 | self.loader = loader |
---|
33 | self.message = "" |
---|
34 | self.starttime = 0 |
---|
35 | self.updatefn = updatefn |
---|
36 | |
---|
37 | def isquit(self): |
---|
38 | """ |
---|
39 | :raise KeyboardInterrupt: when the thread is interrupted |
---|
40 | """ |
---|
41 | try: |
---|
42 | CalcThread.isquit(self) |
---|
43 | except KeyboardInterrupt: |
---|
44 | raise KeyboardInterrupt |
---|
45 | |
---|
46 | def compute(self): |
---|
47 | """ |
---|
48 | read some data |
---|
49 | """ |
---|
50 | self.starttime = time.time() |
---|
51 | output = [] |
---|
52 | error_message = "" |
---|
53 | for path in self.list_path: |
---|
54 | basename = os.path.basename(path) |
---|
55 | _, extension = os.path.splitext(basename) |
---|
56 | if self.load_state_flag: |
---|
57 | if extension.lower() in EXTENSIONS: |
---|
58 | pass |
---|
59 | else: |
---|
60 | if extension.lower() not in EXTENSIONS: |
---|
61 | pass |
---|
62 | try: |
---|
63 | temp = self.loader.load(path) |
---|
64 | if temp.__class__.__name__ == "list": |
---|
65 | for item in temp: |
---|
66 | data = self.transform_data(item, path) |
---|
67 | output.append(data) |
---|
68 | else: |
---|
69 | data = self.transform_data(temp, path) |
---|
70 | output.append(data) |
---|
71 | message = "Loading ..." + str(path) + "\n" |
---|
72 | if self.updatefn is not None: |
---|
73 | self.updatefn(output=output, message=message) |
---|
74 | except: |
---|
75 | error_message = "Error while loading: %s\n" % str(path) |
---|
76 | error_message += str(sys.exc_value) + "\n" |
---|
77 | self.updatefn(output=output, message=error_message) |
---|
78 | |
---|
79 | message = "Loading Complete!" |
---|
80 | self.complete(output=output, error_message=error_message, |
---|
81 | message=message, path=self.list_path) |
---|