-
Notifications
You must be signed in to change notification settings - Fork 328
/
_registry.py
321 lines (282 loc) · 9.68 KB
/
_registry.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#! /usr/bin/env python
r"""Registry of landlab components being used.
The landlab registry keeps track of landlab components that have
be instantiated by a user. A user can then get a list of all
the components in use and then print a list of citations for
all of the components they have used.
Examples
--------
>>> from landlab import registry
>>> registry.registered
('landlab',)
>>> print(registry.format_citations())
# Citations
## landlab
@article{hobley2017creative,
title={Creative computing with Landlab: an open-source toolkit
for building, coupling, and exploring two-dimensional
numerical models of Earth-surface dynamics},
author={Hobley, Daniel EJ and Adams, Jordan M and Nudurupati,
Sai Siddhartha and Hutton, Eric WH and Gasparini, Nicole M and
Istanbulluoglu, Erkan and Tucker, Gregory E},
journal={Earth Surface Dynamics},
volume={5},
number={1},
pages={21},
year={2017},
publisher={Copernicus GmbH}
}
When a component contains citation information, and the component has been
instantiated (not just imported) the component citation is also included.
>>> from landlab import RasterModelGrid
>>> from landlab.components import Flexure
>>> grid = RasterModelGrid((4, 5))
>>> _ = grid.add_zeros("lithosphere__overlying_pressure_increment", at="node")
>>> _ = grid.add_zeros("lithosphere_surface__elevation_increment", at="node")
>>> flexure = Flexure(grid)
>>> print(registry.format_citations())
# Citations
## landlab
@article{hobley2017creative,
title={Creative computing with Landlab: an open-source toolkit
for building, coupling, and exploring two-dimensional
numerical models of Earth-surface dynamics},
author={Hobley, Daniel EJ and Adams, Jordan M and Nudurupati,
Sai Siddhartha and Hutton, Eric WH and Gasparini, Nicole M and
Istanbulluoglu, Erkan and Tucker, Gregory E},
journal={Earth Surface Dynamics},
volume={5},
number={1},
pages={21},
year={2017},
publisher={Copernicus GmbH}
}
<BLANKLINE>
## Flexure
@article{hutton2008sedflux,
title={Sedflux 2.0: An advanced process-response model that
generates three-dimensional stratigraphy},
author={Hutton, Eric WH and Syvitski, James PM},
journal={Computers \& Geosciences},
volume={34},
number={10},
pages={1319--1337},
year={2008},
publisher={Pergamon}
}
Finally, the component's citation information is accessible through an
attribute called ``cite_as``:
>>> print(Flexure.cite_as)
@article{hutton2008sedflux,
title={Sedflux 2.0: An advanced process-response model that
generates three-dimensional stratigraphy},
author={Hutton, Eric WH and Syvitski, James PM},
journal={Computers \& Geosciences},
volume={34},
number={10},
pages={1319--1337},
year={2008},
publisher={Pergamon}
}
"""
import os
from . import _info
from .core.messages import indent_and_wrap
class ComponentRegistry(object):
"""A registry for instantiated landlab components."""
def __init__(self, objs=None):
self._registered = []
if objs is not None:
try:
[self.add(obj) for obj in objs]
except TypeError:
self.add(objs)
def add(self, cls):
"""Add a class to the registry.
Parameters
----------
cls : Component
A landlab component to register as used.
"""
if cls not in self._registered:
self._registered.append(cls)
@property
def registered(self):
"""All registered classes.
Returns
-------
tuple
The components in the registry.
Examples
--------
>>> from landlab._registry import ComponentRegistry
>>> registry = ComponentRegistry()
>>> registry.registered
()
>>> class FooBar(object):
... pass
>>> registry.add(FooBar)
>>> registry.registered
('FooBar',)
"""
return tuple([ComponentRegistry.get_name(obj) for obj in self._registered])
@staticmethod
def format_citation(obj):
"""Format a single citation.
Parameters
----------
obj : Component
A landlab component class or instance.
Returns
-------
str
The formatted citation, or "None" if there is no citation
given.
Examples
--------
>>> from landlab._registry import ComponentRegistry
>>> registry = ComponentRegistry()
>>> class DoNothingComponent(object):
... pass
>>> print(registry.format_citation(DoNothingComponent))
## DoNothingComponent
None
>>> class SorterAndSearcher(object):
... _cite_as = '''
... @book{knuth1998art,
... title={The art of computer programming: sorting and searching},
... author={Knuth, Donald Ervin},
... volume={3},
... year={1998},
... publisher={Pearson Education}
... }'''
>>> print(registry.format_citation(SorterAndSearcher))
## SorterAndSearcher
@book{knuth1998art,
title={The art of computer programming: sorting and searching},
author={Knuth, Donald Ervin},
volume={3},
year={1998},
publisher={Pearson Education}
}
"""
name = ComponentRegistry.get_name(obj)
header = ["## {name}".format(name=name)]
cite_as = ComponentRegistry.get_citations(obj)
body = []
for citation in cite_as:
body.append(indent_and_wrap(citation, indent=" " * 4))
return os.linesep.join(header + body)
@staticmethod
def get_name(obj):
"""Get the display name for an object.
Examples
--------
>>> from landlab._registry import ComponentRegistry
>>> class MontyPython(object):
... name = "Eric Idle"
>>> ComponentRegistry.get_name(MontyPython)
'Eric Idle'
>>> class MontyPython(object):
... _name = "Graham Chapman"
>>> ComponentRegistry.get_name(MontyPython)
'Graham Chapman'
>>> class MontyPython(object):
... pass
>>> ComponentRegistry.get_name(MontyPython)
'MontyPython'
"""
name = "Unknown"
for attr in ("name", "_name", "__name__"):
try:
name = getattr(obj, attr)
except AttributeError:
pass
else:
break
return name
@staticmethod
def get_citations(obj):
"""Get a list of citations from an object."""
citations = "None"
for attr in ("cite_as", "_cite_as"):
try:
citations = getattr(obj, attr)
except AttributeError:
pass
else:
break
if isinstance(citations, str):
citations = [citations]
return citations
def format_citations(self):
"""Format citations for all registered components.
Returns
-------
str
The formatted citations.
Examples
--------
>>> from landlab._registry import ComponentRegistry
>>> registry = ComponentRegistry()
>>> class HolyGrailFinder(object):
... _name = 'Monty Python'
... _cite_as = ['''@book{python2000holy,
... title={the Holy Grail},
... author={Python, Monty and Chapman, Graham and Cleese, John and Gilliam, Terry and Jones, Terry and Idle, Eric and Palin, Michael},
... year={2000},
... publisher={EMI Records}
... }''',
... '''@book{chapman1989complete,
... title={The Complete Monty Python's Flying Circus: All the Words. Volume one},
... author={Chapman, Graham and Python, Monty},
... volume={1},
... year={1989},
... publisher={Pantheon}
... }''']
>>> class Evolution(object):
... _cite_as = '''
... @book{darwin1859origin,
... title={On the origin of species},
... author={Darwin, Charles},
... year={1859},
... publisher={Lulu. com}
... }'''
>>> registry.add(HolyGrailFinder)
>>> registry.add(Evolution)
>>> print(registry.format_citations())
# Citations
## Monty Python
@book{python2000holy,
title={the Holy Grail},
author={Python, Monty and Chapman, Graham and Cleese, John and
Gilliam, Terry and Jones, Terry and Idle, Eric and Palin,
Michael},
year={2000},
publisher={EMI Records}
}
@book{chapman1989complete,
title={The Complete Monty Python's Flying Circus: All the Words.
Volume one},
author={Chapman, Graham and Python, Monty},
volume={1},
year={1989},
publisher={Pantheon}
}
<BLANKLINE>
## Evolution
@book{darwin1859origin,
title={On the origin of species},
author={Darwin, Charles},
year={1859},
publisher={Lulu. com}
}
"""
header = ["# Citations"]
body = []
for cls in self._registered:
body.append(self.format_citation(cls))
return os.linesep.join(header + [(2 * os.linesep).join(body)])
def __repr__(self):
return "ComponentRegistry({0})".format(repr(self.registered))
registry = ComponentRegistry(_info)