-
Notifications
You must be signed in to change notification settings - Fork 75
/
cell.py
222 lines (190 loc) · 7.83 KB
/
cell.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright 2016-2020 European Commission (JRC);
# Licensed under the EUPL (the 'Licence');
# You may not use this work except in compliance with the Licence.
# You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
"""
It provides Cell class.
"""
import copy
import collections
import functools
import numpy as np
import schedula as sh
from .parser import Parser
from .tokens.operand import Error, XlError, range2parts
from .ranges import Ranges, _assemble_values, _shape, _get_indices_intersection
CELL = sh.Token('Cell')
class CellWrapper(sh.add_args):
def __init__(self, func, parse_args, parse_kwargs):
super(CellWrapper, self).__init__(func, n=0)
self.parse_args = parse_args
self.parse_kwargs = parse_kwargs
def __call__(self, *args, **kwargs):
try:
return self.func(*self.parse_args(*args), **self.parse_kwargs(**kwargs))
except sh.DispatcherError as ex:
if isinstance(ex.ex, NotImplementedError):
return Error.errors['#NAME?']
raise ex
def check_cycles(self, cycle):
from .excel.cycle import simple_cycles
fn, k, cells = self.func, 'solve_cycle', set()
f_nodes, o, inputs = fn.dsp.function_nodes, fn.outputs[0], fn.inputs
dmap = {v: set(nbrs) for v, nbrs in fn.dsp.dmap.succ.items()}
dmap[o] = set(cycle).intersection(inputs)
for c in map(set, simple_cycles(dmap, False)):
for n in map(f_nodes.get, c.intersection(f_nodes)):
if k in n and n[k](*(i in c for i in n['inputs'])):
cells.update(c.intersection(inputs))
break
else:
return set()
return cells
def wrap_cell_func(func, parse_args=lambda *a: a, parse_kwargs=lambda **kw: kw):
wrapper = CellWrapper(func, parse_args, parse_kwargs)
return functools.update_wrapper(wrapper, func)
def format_output(rng, value):
return Ranges().set_value(rng, value)
class Cell:
parser = Parser()
def __init__(self, reference, value, context=None):
self.func = self.range = self.inputs = self.output = None
if reference is not None:
self.range = Ranges().push(reference, context=context)
self.output = self.range.ranges[0]['name']
self.tokens, self.builder, self.value = (), None, sh.EMPTY
if isinstance(value, str) and self.parser.is_formula(value):
self.tokens, self.builder = self.parser.ast(value, context=context)
elif value is not None:
self.value = value
@property
def __name__(self):
if self.func:
return self.func.__name__
return self.output
def compile(self, references=None):
if self.builder:
func = self.builder.compile(
references=references, **{CELL: self.range}
)
self.func = wrap_cell_func(func, self._args)
self.update_inputs(references=references)
return self
def _missing_ref(self, inp, k):
sh.get_nested_dicts(inp, Error.errors['#REF!'], default=list).append(k)
def update_inputs(self, references=None):
if not self.builder:
return
self.inputs = inp = collections.OrderedDict()
references, get = references or set(), sh.get_nested_dicts
for k, rng in self.func.inputs.items():
if k in references:
get(inp, k, default=list).append(k)
else:
try:
for r in rng.ranges:
get(inp, r['name'], default=list).append(k)
except AttributeError:
self._missing_ref(inp, k)
def _args(self, *args):
assert len(args) == len(self.inputs)
inputs = copy.deepcopy(self.func.inputs)
for links, v in zip(self.inputs.values(), args):
for k in links:
try:
inputs[k].values.update(v.values)
except AttributeError: # Reference.
inputs[k] = v
return inputs.values()
def _output_filters(self):
return functools.partial(format_output, self.range.ranges[0]),
def add(self, dsp, context=None):
nodes = set()
if self.func or self.value is not sh.EMPTY:
directory = context and context.get('directory') or '.'
output = self.output
nodes.add(dsp.add_data(
output, filters=self._output_filters(),
default_value=self.value, directory=directory
))
if self.func:
inputs = self.inputs
nodes.update(inputs)
for k in inputs or ():
if k not in dsp.nodes:
if isinstance(k, XlError):
val = Ranges().push(
'A1:', np.asarray([[k]], object)
)
dsp.add_data(k, val, directory=directory)
else:
try:
rng = Ranges.get_range(
Ranges.format_range, k, context
)
f = functools.partial(format_output, rng),
except ValueError:
f = ()
dsp.add_data(k, filters=f, directory=directory)
nodes.add(dsp.add_function(
self.__name__, self.func, inputs or None, [output]
))
return nodes
class Ref(Cell):
def __init__(self, reference, value, context=None):
super(Ref, self).__init__(None, value, context)
self.output = range2parts(None, ref=reference, **context)['name']
def _missing_ref(self, inp, k):
sh.get_nested_dicts(inp, k, default=list).append(k)
def _output_filters(self):
return ()
def compile(self, references=None):
super(Ref, self).compile()
if self.inputs:
self.func.dsp.nodes[self.func.outputs[0]].pop('filters', None)
else:
self.value, self.func = self.func(), None
return self
class RangesAssembler:
def __init__(self, ref, context=None):
self.missing = self.range = Ranges().push(ref, context=context)
self.inputs = collections.OrderedDict()
@property
def output(self):
return self.range.ranges[0]['name']
def push(self, cell):
for b in self.missing.intersect(cell.range):
if b:
self.missing = self.missing - cell.range
self.inputs[cell.output] = None
break
def add(self, dsp):
base = self.range.ranges[0]
for rng in self.missing.ranges:
ctx = sh.selector(('excel', 'sheet'), rng)
for r in range(int(rng['r1']), int(rng['r2']) + 1):
for n in range(rng['n1'], rng['n2'] + 1):
ist = Ranges.format_range(
('name', 'n1', 'n2'), n1=n, r1=r, **ctx
)
k = ist['name']
self.inputs[k] = _get_indices_intersection(base, ist)
f = functools.partial(format_output, ist),
dsp.add_data(k, [[sh.EMPTY]], filters=f)
if list(self.inputs) != [self.output]:
dsp.add_function(None, self, self.inputs or None, [self.output])
@property
def __name__(self):
return '=%s' % self.output
def __call__(self, *cells):
base = self.range.ranges[0]
out = np.empty(_shape(**base), object)
for c, ind in zip(cells, self.inputs.values()):
if ind:
out[ind[0], ind[1]] = c.value
else:
_assemble_values(base, c.values, out)
return out