-
Notifications
You must be signed in to change notification settings - Fork 2k
/
interfaces.py
680 lines (526 loc) · 19.3 KB
/
interfaces.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
"""
Interfaces for plugins system
See doc/plugins.rst for more information
"""
__all__ = [
'Interface',
'IGenshiStreamFilter', 'IRoutes',
'IMapper', 'ISession',
'IMiddleware',
'IAuthFunctions',
'IDomainObjectModification', 'IGroupController',
'IPackageController', 'IPluginObserver',
'IConfigurable', 'IConfigurer', 'IAuthorizer',
'IActions', 'IResourceUrlChange', 'IDatasetForm',
'IGroupForm',
'ITagController',
'ITemplateHelpers',
]
from inspect import isclass
from pyutilib.component.core import Interface as _pca_Interface
class Interface(_pca_Interface):
@classmethod
def provided_by(cls, instance):
return cls.implemented_by(instance.__class__)
@classmethod
def implemented_by(cls, other):
if not isclass(other):
raise TypeError("Class expected", other)
try:
return cls in other._implements
except AttributeError:
return False
class IMiddleware(Interface):
'''Hook into Pylons middleware stack
'''
def make_middleware(self, app, config):
'''Return an app configured with this middleware
'''
return app
class IGenshiStreamFilter(Interface):
'''
Hook into template rendering.
See ckan.lib.base.py:render
'''
def filter(self, stream):
"""
Return a filtered Genshi stream.
Called when any page is rendered.
:param stream: Genshi stream of the current output document
:returns: filtered Genshi stream
"""
return stream
class IRoutes(Interface):
"""
Plugin into the setup of the routes map creation.
"""
def before_map(self, map):
"""
Called before the routes map is generated. ``before_map`` is before any
other mappings are created so can override all other mappings.
:param map: Routes map object
:returns: Modified version of the map object
"""
return map
def after_map(self, map):
"""
Called after routes map is set up. ``after_map`` can be used to
add fall-back handlers.
:param map: Routes map object
:returns: Modified version of the map object
"""
return map
class IMapper(Interface):
"""
A subset of the SQLAlchemy mapper extension hooks.
See http://www.sqlalchemy.org/docs/05/reference/orm/interfaces.html#sqlalchemy.orm.interfaces.MapperExtension
Example::
>>> class MyPlugin(SingletonPlugin):
...
... implements(IMapper)
...
... def after_update(self, mapper, connection, instance):
... log("Updated: %r", instance)
"""
def before_insert(self, mapper, connection, instance):
"""
Receive an object instance before that instance is INSERTed into
its table.
"""
def before_update(self, mapper, connection, instance):
"""
Receive an object instance before that instance is UPDATEed.
"""
def before_delete(self, mapper, connection, instance):
"""
Receive an object instance before that instance is DELETEed.
"""
def after_insert(self, mapper, connection, instance):
"""
Receive an object instance after that instance is INSERTed.
"""
def after_update(self, mapper, connection, instance):
"""
Receive an object instance after that instance is UPDATEed.
"""
def after_delete(self, mapper, connection, instance):
"""
Receive an object instance after that instance is DELETEed.
"""
class ISession(Interface):
"""
A subset of the SQLAlchemy session extension hooks.
"""
def after_begin(self, session, transaction, connection):
"""
Execute after a transaction is begun on a connection
"""
def before_flush(self, session, flush_context, instances):
"""
Execute before flush process has started.
"""
def after_flush(self, session, flush_context):
"""
Execute after flush has completed, but before commit has been called.
"""
def before_commit(self, session):
"""
Execute right before commit is called.
"""
def after_commit(self, session):
"""
Execute after a commit has occured.
"""
def after_rollback(self, session):
"""
Execute after a rollback has occured.
"""
class IDomainObjectModification(Interface):
"""
Receives notification of new, changed and deleted datesets.
"""
def notify(self, entity, operation):
pass
class IResourceUrlChange(Interface):
"""
Receives notification of changed urls.
"""
def notify(self, resource):
pass
class ITagController(Interface):
'''
Hook into the Tag controller. These will usually be called just before
committing or returning the respective object, i.e. all validation,
synchronization and authorization setup are complete.
'''
def before_view(self, tag_dict):
'''
Extensions will recieve this before the tag gets displayed. The
dictionary passed will be the one that gets sent to the template.
'''
return tag_dict
class IGroupController(Interface):
"""
Hook into the Group controller. These will
usually be called just before committing or returning the
respective object, i.e. all validation, synchronization
and authorization setup are complete.
"""
def read(self, entity):
pass
def create(self, entity):
pass
def edit(self, entity):
pass
def authz_add_role(self, object_role):
pass
def authz_remove_role(self, object_role):
pass
def delete(self, entity):
pass
def before_view(self, pkg_dict):
'''
Extensions will recieve this before the group gets
displayed. The dictionary passed will be the one that gets
sent to the template.
'''
return pkg_dict
class IPackageController(Interface):
"""
Hook into the package controller.
(see IGroupController)
"""
def read(self, entity):
pass
def create(self, entity):
pass
def edit(self, entity):
pass
def authz_add_role(self, object_role):
pass
def authz_remove_role(self, object_role):
pass
def delete(self, entity):
pass
def before_search(self, search_params):
'''
Extensions will receive a dictionary with the query parameters,
and should return a modified (or not) version of it.
search_params will include an `extras` dictionary with all values
from fields starting with `ext_`, so extensions can receive user
input from specific fields.
'''
return search_params
def after_search(self, search_results, search_params):
'''
Extensions will receive the search results, as well as the search
parameters, and should return a modified (or not) object with the
same structure:
{'count': '', 'results': '', 'facets': ''}
Note that count and facets may need to be adjusted if the extension
changed the results for some reason.
search_params will include an `extras` dictionary with all values
from fields starting with `ext_`, so extensions can receive user
input from specific fields.
'''
return search_results
def before_index(self, pkg_dict):
'''
Extensions will receive what will be given to the solr for
indexing. This is essentially a flattened dict (except for
multli-valued fields such as tags) of all the terms sent to
the indexer. The extension can modify this by returning an
altered version.
'''
return pkg_dict
def before_view(self, pkg_dict):
'''
Extensions will recieve this before the dataset gets
displayed. The dictionary passed will be the one that gets
sent to the template.
'''
return pkg_dict
def before_search_view(self, pkg_dict):
'''
Extensions will recieve this before the dataset gets
displayed in the search view. The dictionary passed will
be the one that gets sent to the template.
'''
return pkg_dict
class IPluginObserver(Interface):
"""
Plugin to the plugin loading mechanism
"""
def before_load(self, plugin):
"""
Called before a plugin is loaded
This method is passed the plugin class.
"""
def after_load(self, service):
"""
Called after a plugin has been loaded.
This method is passed the instantiated service object.
"""
def before_unload(self, plugin):
"""
Called before a plugin is loaded
This method is passed the plugin class.
"""
def after_unload(self, service):
"""
Called after a plugin has been unloaded.
This method is passed the instantiated service object.
"""
class IConfigurable(Interface):
"""
Pass configuration to plugins and extensions
"""
def configure(self, config):
"""
Called by load_environment
"""
class IConfigurer(Interface):
"""
Configure CKAN (pylons) environment via the ``pylons.config`` object
"""
def update_config(self, config):
"""
Called by load_environment at earliest point when config is
available to plugins. The config should be updated in place.
:param config: ``pylons.config`` object
"""
class IAuthorizer(Interface):
"""
Allow customisation of default Authorization implementation
"""
def get_roles(self, username, domain_obj):
"""
Called by Authorizer to extend the list of roles which a user
has in the context of the supplied object. Should return a
list of strings which are the names of valid UserObjectRoles.
"""
def is_authorized(self, username, action, domain_obj):
"""
Called by Authorizer to assert that a user ```username``` can
perform ``action``` on ```domain_obj```.
Should return True or False. A value of False will allow
other Authorizers to run; True will shortcircuit and return.
"""
class IActions(Interface):
"""
Allow adding of actions to the logic layer.
"""
def get_actions(self):
"""
Should return a dict, the keys being the name of the logic
function and the values being the functions themselves.
By decorating a function with the `ckan.logic.side_effect_free`
decorator, the associated action will be made available by a GET
request (as well as the usual POST request) through the action API.
"""
class IAuthFunctions(Interface):
"""
Allow customisation of default Authorization implementation
"""
def get_auth_functions(self):
"""
Returns a dict of all the authorization functions which the
implementation overrides
"""
class ITemplateHelpers(Interface):
"""
Allow adding extra template functions available via h variable
"""
def get_helpers(self):
"""
Should return a dict, the keys being the name of the helper
function and the values being the functions themselves.
"""
class IDatasetForm(Interface):
"""
Allows customisation of the package controller as a plugin.
The behaviour of the plugin is determined by 5 method hooks:
- package_form(self)
- form_to_db_schema(self)
- db_to_form_schema(self)
- check_data_dict(self, data_dict, schema=None)
- setup_template_variables(self, context, data_dict)
Furthermore, there can be many implementations of this plugin registered
at once. With each instance associating itself with 0 or more package
type strings. When a package controller action is invoked, the package
type determines which of the registered plugins to delegate to. Each
implementation must implement two methods which are used to determine the
package-type -> plugin mapping:
- is_fallback(self)
- package_types(self)
Implementations might want to consider mixing in
ckan.lib.plugins.DefaultDatasetForm which provides
default behaviours for the 5 method hooks.
"""
##### These methods control when the plugin is delegated to #####
def is_fallback(self):
"""
Returns true iff this provides the fallback behaviour, when no other
plugin instance matches a package's type.
There must be exactly one fallback controller defined, any attempt to
register more than one will throw an exception at startup. If there's
no fallback registered at startup the
ckan.lib.plugins.DefaultDatasetForm is used as the fallback.
"""
def package_types(self):
"""
Returns an iterable of package type strings.
If a request involving a package of one of those types is made, then
this plugin instance will be delegated to.
There must only be one plugin registered to each package type. Any
attempts to register more than one plugin instance to a given package
type will raise an exception at startup.
"""
##### End of control methods
##### Hooks for customising the PackageController's behaviour #####
##### TODO: flesh out the docstrings a little more. #####
def new_template(self):
"""
Returns a string representing the location of the template to be
rendered for the new page
"""
def comments_template(self):
"""
Returns a string representing the location of the template to be
rendered for the comments page
"""
def search_template(self):
"""
Returns a string representing the location of the template to be
rendered for the search page (if present)
"""
def read_template(self):
"""
Returns a string representing the location of the template to be
rendered for the read page
"""
def history_template(self):
"""
Returns a string representing the location of the template to be
rendered for the history page
"""
def package_form(self):
"""
Returns a string representing the location of the template to be
rendered. e.g. "package/new_package_form.html".
"""
def form_to_db_schema(self):
"""
Returns the schema for mapping package data from a form to a format
suitable for the database.
"""
def db_to_form_schema(self):
"""
Returns the schema for mapping package data from the database into a
format suitable for the form (optional)
"""
def check_data_dict(self, data_dict, schema=None):
"""
Check if the return data is correct.
raise a DataError if not.
"""
def setup_template_variables(self, context, data_dict):
"""
Add variables to c just prior to the template being rendered.
"""
##### End of hooks #####
class IGroupForm(Interface):
"""
Allows customisation of the group controller as a plugin.
The behaviour of the plugin is determined by 5 method hooks:
- package_form(self)
- form_to_db_schema(self)
- db_to_form_schema(self)
- check_data_dict(self, data_dict)
- setup_template_variables(self, context, data_dict)
Furthermore, there can be many implementations of this plugin registered
at once. With each instance associating itself with 0 or more package
type strings. When a package controller action is invoked, the package
type determines which of the registered plugins to delegate to. Each
implementation must implement two methods which are used to determine the
package-type -> plugin mapping:
- is_fallback(self)
- package_types(self)
Implementations might want to consider mixing in
ckan.lib.plugins.DefaultGroupForm which provides
default behaviours for the 5 method hooks.
"""
##### These methods control when the plugin is delegated to #####
def is_fallback(self):
"""
Returns true iff this provides the fallback behaviour, when no other
plugin instance matches a package's type.
There must be exactly one fallback controller defined, any attempt to
register more than one will throw an exception at startup. If there's
no fallback registered at startup the
ckan.lib.plugins.DefaultGroupForm used as the fallback.
"""
def group_types(self):
"""
Returns an iterable of group type strings.
If a request involving a package of one of those types is made, then
this plugin instance will be delegated to.
There must only be one plugin registered to each group type. Any
attempts to register more than one plugin instance to a given group
type will raise an exception at startup.
"""
##### End of control methods
##### Hooks for customising the PackageController's behaviour #####
##### TODO: flesh out the docstrings a little more. #####
def new_template(self):
"""
Returns a string representing the location of the template to be
rendered for the 'new' page. Uses the default_group_type configuration
option to determine which plugin to use the template from.
"""
def index_template(self):
"""
Returns a string representing the location of the template to be
rendered for the index page. Uses the default_group_type configuration
option to determine which plugin to use the template from.
"""
def read_template(self):
"""
Returns a string representing the location of the template to be
rendered for the read page
"""
def history_template(self):
"""
Returns a string representing the location of the template to be
rendered for the history page
"""
def edit_template(self):
"""
Returns a string representing the location of the template to be
rendered for the edit page
"""
def package_form(self):
"""
Returns a string representing the location of the template to be
rendered. e.g. "group/new_group_form.html".
"""
def form_to_db_schema(self):
"""
Returns the schema for mapping group data from a form to a format
suitable for the database.
"""
def db_to_form_schema(self):
"""
Returns the schema for mapping group data from the database into a
format suitable for the form (optional)
"""
def check_data_dict(self, data_dict):
"""
Check if the return data is correct.
raise a DataError if not.
"""
def setup_template_variables(self, context, data_dict):
"""
Add variables to c just prior to the template being rendered.
"""
##### End of hooks #####