Skip to content

Commit 8694098

Browse files
committed
BUILD: vendor tempita from Cython
1 parent aab644b commit 8694098

File tree

6 files changed

+1278
-3
lines changed

6 files changed

+1278
-3
lines changed

LICENSES_bundled.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ Name: spin
2929
Files: .spin/cmds.py
3030
License: BSD-3
3131
For license text, see .spin/LICENSE
32+
33+
Name: tempita
34+
Files: numpy/_build_utils/tempita/*
35+
License: MIT
36+
For details, see numpy/_build_utils/tempita/LICENCE.txt

numpy/_build_utils/tempita.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import os
44
import argparse
55

6-
from Cython import Tempita as tempita
7-
8-
# XXX: If this import ever fails (does it really?), vendor cython.tempita
6+
import tempita
97

108

119
def process_tempita(fromfile, outfile=None):
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2008 Ian Bicking and Contributors
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# The original Tempita implements all of its templating code here.
2+
# Moved it to _tempita.py to make the compilation portable.
3+
4+
from ._tempita import *

numpy/_build_utils/tempita/_looper.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
"""
2+
Helper for looping over sequences, particular in templates.
3+
4+
Often in a loop in a template it's handy to know what's next up,
5+
previously up, if this is the first or last item in the sequence, etc.
6+
These can be awkward to manage in a normal Python loop, but using the
7+
looper you can get a better sense of the context. Use like::
8+
9+
>>> for loop, item in looper(['a', 'b', 'c']):
10+
... print loop.number, item
11+
... if not loop.last:
12+
... print '---'
13+
1 a
14+
---
15+
2 b
16+
---
17+
3 c
18+
19+
"""
20+
21+
basestring_ = (bytes, str)
22+
23+
__all__ = ['looper']
24+
25+
26+
class looper:
27+
"""
28+
Helper for looping (particularly in templates)
29+
30+
Use this like::
31+
32+
for loop, item in looper(seq):
33+
if loop.first:
34+
...
35+
"""
36+
37+
def __init__(self, seq):
38+
self.seq = seq
39+
40+
def __iter__(self):
41+
return looper_iter(self.seq)
42+
43+
def __repr__(self):
44+
return '<%s for %r>' % (
45+
self.__class__.__name__, self.seq)
46+
47+
48+
class looper_iter:
49+
50+
def __init__(self, seq):
51+
self.seq = list(seq)
52+
self.pos = 0
53+
54+
def __iter__(self):
55+
return self
56+
57+
def __next__(self):
58+
if self.pos >= len(self.seq):
59+
raise StopIteration
60+
result = loop_pos(self.seq, self.pos), self.seq[self.pos]
61+
self.pos += 1
62+
return result
63+
64+
65+
class loop_pos:
66+
67+
def __init__(self, seq, pos):
68+
self.seq = seq
69+
self.pos = pos
70+
71+
def __repr__(self):
72+
return '<loop pos=%r at %r>' % (
73+
self.seq[self.pos], self.pos)
74+
75+
def index(self):
76+
return self.pos
77+
index = property(index)
78+
79+
def number(self):
80+
return self.pos + 1
81+
number = property(number)
82+
83+
def item(self):
84+
return self.seq[self.pos]
85+
item = property(item)
86+
87+
def __next__(self):
88+
try:
89+
return self.seq[self.pos + 1]
90+
except IndexError:
91+
return None
92+
__next__ = property(__next__)
93+
94+
def previous(self):
95+
if self.pos == 0:
96+
return None
97+
return self.seq[self.pos - 1]
98+
previous = property(previous)
99+
100+
def odd(self):
101+
return not self.pos % 2
102+
odd = property(odd)
103+
104+
def even(self):
105+
return self.pos % 2
106+
even = property(even)
107+
108+
def first(self):
109+
return self.pos == 0
110+
first = property(first)
111+
112+
def last(self):
113+
return self.pos == len(self.seq) - 1
114+
last = property(last)
115+
116+
def length(self):
117+
return len(self.seq)
118+
length = property(length)
119+
120+
def first_group(self, getter=None):
121+
"""
122+
Returns true if this item is the start of a new group,
123+
where groups mean that some attribute has changed. The getter
124+
can be None (the item itself changes), an attribute name like
125+
``'.attr'``, a function, or a dict key or list index.
126+
"""
127+
if self.first:
128+
return True
129+
return self._compare_group(self.item, self.previous, getter)
130+
131+
def last_group(self, getter=None):
132+
"""
133+
Returns true if this item is the end of a new group,
134+
where groups mean that some attribute has changed. The getter
135+
can be None (the item itself changes), an attribute name like
136+
``'.attr'``, a function, or a dict key or list index.
137+
"""
138+
if self.last:
139+
return True
140+
return self._compare_group(self.item, self.__next__, getter)
141+
142+
def _compare_group(self, item, other, getter):
143+
if getter is None:
144+
return item != other
145+
elif (isinstance(getter, basestring_)
146+
and getter.startswith('.')):
147+
getter = getter[1:]
148+
if getter.endswith('()'):
149+
getter = getter[:-2]
150+
return getattr(item, getter)() != getattr(other, getter)()
151+
else:
152+
return getattr(item, getter) != getattr(other, getter)
153+
elif hasattr(getter, '__call__'):
154+
return getter(item) != getter(other)
155+
else:
156+
return item[getter] != other[getter]

0 commit comments

Comments
 (0)