1 """Storage and retrieval of qubx.table info into qubx.tree format.
2
3 Copyright 2008-2011 Research Foundation State University of New York
4 This file is part of QUB Express.
5
6 QUB Express is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 QUB Express is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License,
17 named LICENSE.txt, in the QUB Express program directory. If not, see
18 <http://www.gnu.org/licenses/>.
19
20 """
21
22 import numbers
23 import numpy
24 import traceback
25
26 import qubx.table
27 import qubx.tree
28
29 from itertools import izip, count
30
42
68
69
70
79 for field in table.fields:
80 if field == 'Index':
81 continue
82 child = tree.insert(field, child)
83 x = table[0, field]
84 if isinstance(x, numbers.Integral) or isinstance(x, numpy.integer):
85 child.data.setup(qubx.tree.QTR_TYPE_INT, table.size, 1)
86 copy_numbers(child.data, field)
87 elif isinstance(x, numbers.Real) or isinstance(x, numpy.floating):
88 child.data.setup(qubx.tree.QTR_TYPE_DOUBLE, table.size, 1)
89 copy_numbers(child.data, field)
90 else:
91 sub = qubx.tree.NullNode()
92 for i in xrange(table.size):
93 sub = child.insert('Row', sub)
94 sub.data = table.format[field](table[i, field])
95 return tree
96
98
99 skip_from_to = False
100 child_from = tree.find('From')
101 child_to = tree.find('To')
102 if not (child_from.isNull or child_to.isNull):
103 skip_from_to = True
104 for i, f, t in izip(count(), child_from.data, child_to.data):
105 if i == table.size:
106 table.append({'From' : f, 'To' : t})
107 else:
108 table[i,'From'] = f
109 table[i,'To'] = t
110 for child in qubx.tree.children(tree):
111 field = child.name
112 if skip_from_to and (field in ('From', 'To')):
113 continue
114 if child.data.type in (qubx.tree.QTR_TYPE_INT, qubx.tree.QTR_TYPE_DOUBLE):
115 for i, x in enumerate(child.data):
116 if i == table.size:
117 table.append({})
118 try:
119 table[i, field] = x
120 except KeyError:
121 pass
122 else:
123 for i, sub in enumerate(qubx.tree.children(child)):
124 if i == table.size:
125 table.append({})
126 sdata = str(sub.data)
127 try:
128 try:
129 try:
130 accept = table.accept[field]
131 except KeyError:
132 table[i, field] = table.auto_accept(sdata)
133 table[i, field] = accept(sdata)
134 except KeyError:
135 pass
136 except:
137 try:
138 table[i, field] = sdata
139 except KeyError:
140 pass
141