1 | |
---|
2 | import time |
---|
3 | import sys |
---|
4 | import os |
---|
5 | |
---|
6 | from data_util.calcthread import CalcThread |
---|
7 | |
---|
8 | |
---|
9 | EXTENSIONS = ['.svs', '.prv', '.inv', '.fitv'] |
---|
10 | |
---|
11 | class DataReader(CalcThread): |
---|
12 | """ |
---|
13 | Load a data given a filename |
---|
14 | """ |
---|
15 | def __init__(self, path, loader, |
---|
16 | flag=True, |
---|
17 | transform_data=None, |
---|
18 | completefn=None, |
---|
19 | updatefn = None, |
---|
20 | yieldtime = 0.01, |
---|
21 | worktime = 0.01 |
---|
22 | ): |
---|
23 | CalcThread.__init__(self, completefn, |
---|
24 | updatefn, |
---|
25 | yieldtime, |
---|
26 | worktime) |
---|
27 | self.load_state_flag = flag |
---|
28 | self.transform_data = transform_data |
---|
29 | self.list_path = path |
---|
30 | #Instantiate a loader |
---|
31 | self.loader = loader |
---|
32 | self.message = "" |
---|
33 | self.starttime = 0 |
---|
34 | self.updatefn = updatefn |
---|
35 | |
---|
36 | def isquit(self): |
---|
37 | """ |
---|
38 | :raise KeyboardInterrupt: when the thread is interrupted |
---|
39 | """ |
---|
40 | try: |
---|
41 | CalcThread.isquit(self) |
---|
42 | except KeyboardInterrupt: |
---|
43 | raise KeyboardInterrupt |
---|
44 | |
---|
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 | root, 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 | elapsed = time.time() - self.starttime |
---|
65 | if temp.__class__.__name__ == "list": |
---|
66 | for item in temp: |
---|
67 | data = self.transform_data(item, path) |
---|
68 | output.append(data) |
---|
69 | else: |
---|
70 | data = self.transform_data(temp, path) |
---|
71 | output.append(data) |
---|
72 | message = "Loading ..." + str(path) + "\n" |
---|
73 | if self.updatefn is not None: |
---|
74 | self.updatefn(output=output, message=message) |
---|
75 | except: |
---|
76 | error_message = "Error while loading: %s\n" % str(path) |
---|
77 | error_message += str(sys.exc_value) + "\n" |
---|
78 | self.updatefn(output=output, message=error_message) |
---|
79 | |
---|
80 | message = "Loading Complete!" |
---|
81 | self.complete(output=output, error_message=error_message, |
---|
82 | message=message, path=self.list_path) |
---|
83 | |
---|
84 | |
---|
85 | |
---|