author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 28 Apr 2011 15:24:37 +0200 | |
branch | oldstable |
changeset 7266 | f625097d1e99 |
parent 7118 | e094b3d4eb95 |
child 7237 | 9f619715665b |
permissions | -rw-r--r-- |
6944
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
1 |
# copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5124
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
5824 | 18 |
"""cubicweb server sources support""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
22 |
import itertools |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
23 |
from os.path import join, splitext |
1263 | 24 |
from datetime import datetime, timedelta |
0 | 25 |
from logging import getLogger |
26 |
||
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
27 |
from logilab.common import configuration |
0 | 28 |
|
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
29 |
from yams.schema import role_name |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
30 |
|
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
31 |
from cubicweb import ValidationError, set_log_methods, server |
2596
d02eed70937f
[R repo, schema] use VIRTUAL_RTYPES const
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2493
diff
changeset
|
32 |
from cubicweb.schema import VIRTUAL_RTYPES |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
33 |
from cubicweb.server.sqlutils import SQL_PREFIX |
7118
e094b3d4eb95
[server] move EditedEntity class to its own module, to avoid cyclic dependency when needed from e.g. session.py
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
7040
diff
changeset
|
34 |
from cubicweb.server.edition import EditedEntity |
1251
af40e615dc89
introduce a 'cw_' prefix on entity table and column names so we don't conflict with sql or DBMS specific keywords
sylvain.thenault@logilab.fr
parents:
1250
diff
changeset
|
35 |
|
0 | 36 |
|
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
37 |
def dbg_st_search(uri, union, varmap, args, cachekey=None, prefix='rql for'): |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
38 |
if server.DEBUG & server.DBG_RQL: |
2719
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
39 |
print ' %s %s source: %s' % (prefix, uri, union.as_string()) |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
40 |
if varmap: |
2719
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
41 |
print ' using varmap', varmap |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
42 |
if server.DEBUG & server.DBG_MORE: |
2719
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
43 |
print ' args', args |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
44 |
print ' cache key', cachekey |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
45 |
print ' solutions', ','.join(str(s.solutions) |
6df328b47361
[server debug] indent for more readability
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2650
diff
changeset
|
46 |
for s in union.children) |
2625
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
47 |
# return true so it can be used as assertion (and so be killed by python -O) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
48 |
return True |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
49 |
|
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
50 |
def dbg_results(results): |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
51 |
if server.DEBUG & server.DBG_RQL: |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
52 |
if len(results) > 10: |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
53 |
print ' -->', results[:10], '...', len(results) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
54 |
else: |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
55 |
print ' -->', results |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
56 |
# return true so it can be used as assertion (and so be killed by python -O) |
d6012db7b93e
R [server debug] more server side debugging tweaks
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2596
diff
changeset
|
57 |
return True |
2493
9806571ea790
major refactoring of database dump/restore:
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2476
diff
changeset
|
58 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
59 |
class TimedCache(dict): |
5642
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
60 |
def __init__(self, ttl): |
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
61 |
# time to live in seconds |
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
62 |
if ttl <= 0: |
6582
8eb7883b4223
[pylint] fix a bug of pylint detected errors and i18n pb (calling builtins._ instead of req._)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6427
diff
changeset
|
63 |
raise ValueError('TimedCache initialized with a ttl of %ss' % ttl.seconds) |
5642
6a90357b9769
TimedCache now only accepts values expressed in seconds
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5637
diff
changeset
|
64 |
self.ttl = timedelta(seconds=ttl) |
1792 | 65 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
66 |
def __setitem__(self, key, value): |
1263 | 67 |
dict.__setitem__(self, key, (datetime.now(), value)) |
1792 | 68 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
69 |
def __getitem__(self, key): |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
70 |
return dict.__getitem__(self, key)[1] |
1792 | 71 |
|
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
72 |
def clear_expired(self): |
1263 | 73 |
now_ = datetime.now() |
1238
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
74 |
ttl = self.ttl |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
75 |
for key, (timestamp, value) in self.items(): |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
76 |
if now_ - timestamp > ttl: |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
77 |
del self[key] |
fa29b5b60107
set 30sec query cache on pyro source, important speedup for pages generating multiple time the same external query
sylvain.thenault@logilab.fr
parents:
386
diff
changeset
|
78 |
|
0 | 79 |
|
80 |
class AbstractSource(object): |
|
81 |
"""an abstract class for sources""" |
|
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
82 |
# does the source copy data into the system source, or is it a *true* source |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
83 |
# (i.e. entities are not stored physically here) |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
84 |
copy_based_source = False |
0 | 85 |
|
86 |
# boolean telling if modification hooks should be called when something is |
|
87 |
# modified in this source |
|
88 |
should_call_hooks = True |
|
89 |
# boolean telling if the repository should connect to this source during |
|
90 |
# migration |
|
91 |
connect_for_migration = True |
|
1792 | 92 |
|
0 | 93 |
# mappings telling which entities and relations are available in the source |
94 |
# keys are supported entity/relation types and values are boolean indicating |
|
95 |
# wether the support is read-only (False) or read-write (True) |
|
96 |
support_entities = {} |
|
97 |
support_relations = {} |
|
98 |
# a global identifier for this source, which has to be set by the source |
|
99 |
# instance |
|
100 |
uri = None |
|
101 |
# a reference to the system information helper |
|
102 |
repo = None |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
103 |
# a reference to the instance'schema (may differs from the source'schema) |
0 | 104 |
schema = None |
1792 | 105 |
|
3040
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
106 |
# multi-sources planning control |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
107 |
dont_cross_relations = () |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
108 |
cross_relations = () |
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
109 |
|
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
110 |
# force deactivation (configuration error for instance) |
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
111 |
disabled = False |
3040
e4dc22040f5f
[multi-sources] move [dont_]cross_relations to AbstractSource
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2759
diff
changeset
|
112 |
|
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
113 |
# source configuration options |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
114 |
options = () |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
115 |
|
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
116 |
def __init__(self, repo, source_config, eid=None): |
0 | 117 |
self.repo = repo |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
118 |
self.set_schema(repo.schema) |
0 | 119 |
self.support_relations['identity'] = False |
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
120 |
self.eid = eid |
6722
3341521d857b
[repo source] rename attribute so it's much easier to grasp its role
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
121 |
self.public_config = source_config.copy() |
3341521d857b
[repo source] rename attribute so it's much easier to grasp its role
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6582
diff
changeset
|
122 |
self.remove_sensitive_information(self.public_config) |
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
123 |
self.uri = source_config.pop('uri') |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
124 |
set_log_methods(self, getLogger('cubicweb.sources.'+self.uri)) |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
125 |
source_config.pop('type') |
1792 | 126 |
|
0 | 127 |
def __repr__(self): |
6724
24bf6f181d0e
[pyro source] store pyro source mapping file into the database
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6722
diff
changeset
|
128 |
return '<%s source %s @%#x>' % (self.uri, self.eid, id(self)) |
0 | 129 |
|
130 |
def __cmp__(self, other): |
|
131 |
"""simple comparison function to get predictable source order, with the |
|
132 |
system source at last |
|
133 |
""" |
|
134 |
if self.uri == other.uri: |
|
135 |
return 0 |
|
136 |
if self.uri == 'system': |
|
137 |
return 1 |
|
138 |
if other.uri == 'system': |
|
139 |
return -1 |
|
140 |
return cmp(self.uri, other.uri) |
|
1792 | 141 |
|
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
142 |
def backup(self, backupfile, confirm): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
143 |
"""method called to create a backup of source's data""" |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
144 |
pass |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
145 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
146 |
def restore(self, backupfile, confirm, drop): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
147 |
"""method called to restore a backup of source's data""" |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
148 |
pass |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
149 |
|
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
150 |
@classmethod |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
151 |
def check_conf_dict(cls, eid, confdict, _=unicode, fail_if_unknown=True): |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
152 |
"""check configuration of source entity. Return config dict properly |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
153 |
typed with defaults set. |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
154 |
""" |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
155 |
processed = {} |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
156 |
for optname, optdict in cls.options: |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
157 |
value = confdict.pop(optname, optdict.get('default')) |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
158 |
if value is configuration.REQUIRED: |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
159 |
if not fail_if_unknown: |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
160 |
continue |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
161 |
msg = _('specifying %s is mandatory' % optname) |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
162 |
raise ValidationError(eid, {role_name('config', 'subject'): msg}) |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
163 |
elif value is not None: |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
164 |
# type check |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
165 |
try: |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
166 |
value = configuration.convert(value, optdict, optname) |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
167 |
except Exception, ex: |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
168 |
msg = unicode(ex) # XXX internationalization |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
169 |
raise ValidationError(eid, {role_name('config', 'subject'): msg}) |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
170 |
processed[optname] = value |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
171 |
# cw < 3.10 bw compat |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
172 |
try: |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
173 |
processed['adapter'] = confdict['adapter'] |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
174 |
except: |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
175 |
pass |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
176 |
# check for unknown options |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
177 |
if confdict and not confdict.keys() == ['adapter']: |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
178 |
if fail_if_unknown: |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
179 |
msg = _('unknown options %s') % ', '.join(confdict) |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
180 |
raise ValidationError(eid, {role_name('config', 'subject'): msg}) |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
181 |
else: |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
182 |
logger = getLogger('cubicweb.sources') |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
183 |
logger.warning('unknown options %s', ', '.join(confdict)) |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
184 |
# add options to processed, they may be necessary during migration |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
185 |
processed.update(confdict) |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
186 |
return processed |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
187 |
|
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
188 |
@classmethod |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
189 |
def check_config(cls, source_entity): |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
190 |
"""check configuration of source entity""" |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
191 |
return cls.check_conf_dict(source_entity.eid, source_entity.host_config, |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
192 |
_=source_entity._cw._) |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
193 |
|
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
194 |
def update_config(self, source_entity, typedconfig): |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
195 |
"""update configuration from source entity. `typedconfig` is config |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
196 |
properly typed with defaults set |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
197 |
""" |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
198 |
pass |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
199 |
|
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
200 |
# source initialization / finalization ##################################### |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
201 |
|
0 | 202 |
def set_schema(self, schema): |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
203 |
"""set the instance'schema""" |
0 | 204 |
self.schema = schema |
1792 | 205 |
|
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
206 |
def init_creating(self): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
207 |
"""method called by the repository once ready to create a new instance""" |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
208 |
pass |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
209 |
|
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
210 |
def init(self, activated, source_entity): |
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
211 |
"""method called by the repository once ready to handle request. |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
212 |
`activated` is a boolean flag telling if the source is activated or not. |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
213 |
""" |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
214 |
pass |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
215 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
216 |
PUBLIC_KEYS = ('type', 'uri') |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
217 |
def remove_sensitive_information(self, sourcedef): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
218 |
"""remove sensitive information such as login / password from source |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
219 |
definition |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
220 |
""" |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
221 |
for key in sourcedef.keys(): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
222 |
if not key in self.PUBLIC_KEYS: |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
223 |
sourcedef.pop(key) |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
224 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
225 |
# connections handling ##################################################### |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
226 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
227 |
def get_connection(self): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
228 |
"""open and return a connection to the source""" |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
229 |
raise NotImplementedError() |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
230 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
231 |
def check_connection(self, cnx): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
232 |
"""Check connection validity, return None if the connection is still |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
233 |
valid else a new connection (called when the pool using the given |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
234 |
connection is being attached to a session). Do nothing by default. |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
235 |
""" |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
236 |
pass |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
237 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
238 |
def close_pool_connections(self): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
239 |
for pool in self.repo.pools: |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
240 |
pool._cursors.pop(self.uri, None) |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
241 |
pool.source_cnxs[self.uri][1].close() |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
242 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
243 |
def open_pool_connections(self): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
244 |
for pool in self.repo.pools: |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
245 |
pool.source_cnxs[self.uri] = (self, self.get_connection()) |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
246 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
247 |
def pool_reset(self, cnx): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
248 |
"""the pool using the given connection is being reseted from its current |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
249 |
attached session |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
250 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
251 |
do nothing by default |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
252 |
""" |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
253 |
pass |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
254 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
255 |
# cache handling ########################################################### |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
256 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
257 |
def reset_caches(self): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
258 |
"""method called during test to reset potential source caches""" |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
259 |
pass |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
260 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
261 |
def clear_eid_cache(self, eid, etype): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
262 |
"""clear potential caches for the given eid""" |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
263 |
pass |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
264 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
265 |
# external source api ###################################################### |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
266 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
267 |
def eid2extid(self, eid, session=None): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
268 |
return self.repo.eid2extid(self, eid, session) |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
269 |
|
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
270 |
def extid2eid(self, value, etype, session=None, **kwargs): |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
271 |
return self.repo.extid2eid(self, value, etype, session, **kwargs) |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
272 |
|
0 | 273 |
def support_entity(self, etype, write=False): |
274 |
"""return true if the given entity's type is handled by this adapter |
|
275 |
if write is true, return true only if it's a RW support |
|
276 |
""" |
|
277 |
try: |
|
278 |
wsupport = self.support_entities[etype] |
|
279 |
except KeyError: |
|
280 |
return False |
|
281 |
if write: |
|
282 |
return wsupport |
|
283 |
return True |
|
1792 | 284 |
|
0 | 285 |
def support_relation(self, rtype, write=False): |
286 |
"""return true if the given relation's type is handled by this adapter |
|
287 |
if write is true, return true only if it's a RW support |
|
288 |
||
1792 | 289 |
current implementation return true if the relation is defined into |
290 |
`support_relations` or if it is a final relation of a supported entity |
|
0 | 291 |
type |
292 |
""" |
|
293 |
try: |
|
294 |
wsupport = self.support_relations[rtype] |
|
295 |
except KeyError: |
|
296 |
rschema = self.schema.rschema(rtype) |
|
6057
cb5c8852cbda
[ms] cleanups
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5849
diff
changeset
|
297 |
if not rschema.final or rschema.type == 'has_text': |
0 | 298 |
return False |
299 |
for etype in rschema.subjects(): |
|
300 |
try: |
|
301 |
wsupport = self.support_entities[etype] |
|
302 |
break |
|
303 |
except KeyError: |
|
304 |
continue |
|
305 |
else: |
|
306 |
return False |
|
307 |
if write: |
|
308 |
return wsupport |
|
1792 | 309 |
return True |
310 |
||
3041
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
311 |
def may_cross_relation(self, rtype): |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
312 |
"""return True if the relation may be crossed among sources. Rules are: |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
313 |
|
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
314 |
* if this source support the relation, can't be crossed unless explicitly |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
315 |
specified in .cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
316 |
|
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
317 |
* if this source doesn't support the relation, can be crossed unless |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
318 |
explicitly specified in .dont_cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
319 |
""" |
3633
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
320 |
# XXX find a way to have relation such as state_of in dont cross |
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
321 |
# relation (eg composite relation without both end type available? |
2d81178bea5f
[multi-source] add test case for an encountered bug, fixed by adding state_of to dont_cross_relations. Added a note to remind me that something should be done to be smarter in handling this
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
322 |
# card 1 relation ? ...) |
3041
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
323 |
if self.support_relation(rtype): |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
324 |
return rtype in self.cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
325 |
return rtype not in self.dont_cross_relations |
782fa7566a22
[multi-sources] new may_cross_relation method on sources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3040
diff
changeset
|
326 |
|
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
327 |
def before_entity_insertion(self, session, lid, etype, eid, sourceparams): |
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
328 |
"""called by the repository when an eid has been attributed for an |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
329 |
entity stored here but the entity has not been inserted in the system |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
330 |
table yet. |
0 | 331 |
|
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
332 |
This method must return the an Entity instance representation of this |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
333 |
entity. |
0 | 334 |
""" |
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
335 |
entity = self.repo.vreg['etypes'].etype_class(etype)(session) |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
336 |
entity.eid = eid |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
337 |
entity.cw_edited = EditedEntity(entity) |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
338 |
return entity |
1792 | 339 |
|
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
340 |
def after_entity_insertion(self, session, lid, entity, sourceparams): |
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
341 |
"""called by the repository after an entity stored here has been |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
342 |
inserted in the system table. |
0 | 343 |
""" |
344 |
pass |
|
1792 | 345 |
|
6957
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
346 |
def _load_mapping(self, session=None, **kwargs): |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
347 |
if not 'CWSourceSchemaConfig' in self.schema: |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
348 |
self.warning('instance is not mapping ready') |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
349 |
return |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
350 |
if session is None: |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
351 |
_session = self.repo.internal_session() |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
352 |
else: |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
353 |
_session = session |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
354 |
try: |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
355 |
for schemacfg in _session.execute( |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
356 |
'Any CFG,CFGO,S WHERE ' |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
357 |
'CFG options CFGO, CFG cw_schema S, ' |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
358 |
'CFG cw_for_source X, X eid %(x)s', {'x': self.eid}).entities(): |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
359 |
self.add_schema_config(schemacfg, **kwargs) |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
360 |
finally: |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
361 |
if session is None: |
ffda12be2e9f
[repository] #1460066: backport datafeed cube as cubicweb source
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6945
diff
changeset
|
362 |
_session.close() |
0 | 363 |
|
6944
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
364 |
def add_schema_config(self, schemacfg, checkonly=False): |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
365 |
"""added CWSourceSchemaConfig, modify mapping accordingly""" |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
366 |
msg = schemacfg._cw._("this source doesn't use a mapping") |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
367 |
raise ValidationError(schemacfg.eid, {None: msg}) |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
368 |
|
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
369 |
def del_schema_config(self, schemacfg, checkonly=False): |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
370 |
"""deleted CWSourceSchemaConfig, modify mapping accordingly""" |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
371 |
msg = schemacfg._cw._("this source doesn't use a mapping") |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
372 |
raise ValidationError(schemacfg.eid, {None: msg}) |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
373 |
|
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
374 |
def update_schema_config(self, schemacfg, checkonly=False): |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
375 |
"""updated CWSourceSchemaConfig, modify mapping accordingly""" |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
376 |
self.del_schema_config(schemacfg, checkonly) |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
377 |
self.add_schema_config(schemacfg, checkonly) |
0cf10429ad39
[sources] rewrite the way pyrorql mapping are stored in the database so it can be reused for other sources (eg datafeed+cwxml)
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6943
diff
changeset
|
378 |
|
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
379 |
# user authentication api ################################################## |
1792 | 380 |
|
3647
2941f4a0aab9
refactor repo authentication to allow pluggable authentifier to login with something else than a password
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3041
diff
changeset
|
381 |
def authenticate(self, session, login, **kwargs): |
6152
6824f8b61098
use is_instance in a number of places (esp. documentation) rather than the deprecated implements
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6057
diff
changeset
|
382 |
"""if the source support CWUser entity type, it should implement |
1398
5fe84a5f7035
rename internal entity types to have CW prefix instead of E
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
383 |
this method which should return CWUser eid for the given login/password |
0 | 384 |
if this account is defined in this source and valid login / password is |
385 |
given. Else raise `AuthenticationError` |
|
386 |
""" |
|
387 |
raise NotImplementedError() |
|
1792 | 388 |
|
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
389 |
# RQL query api ############################################################ |
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
390 |
|
0 | 391 |
def syntax_tree_search(self, session, union, |
392 |
args=None, cachekey=None, varmap=None, debug=0): |
|
1792 | 393 |
"""return result from this source for a rql query (actually from a rql |
394 |
syntax tree and a solution dictionary mapping each used variable to a |
|
0 | 395 |
possible type). If cachekey is given, the query necessary to fetch the |
396 |
results (but not the results themselves) may be cached using this key. |
|
397 |
""" |
|
398 |
raise NotImplementedError() |
|
1792 | 399 |
|
0 | 400 |
def flying_insert(self, table, session, union, args=None, varmap=None): |
401 |
"""similar as .syntax_tree_search, but inserts data in the temporary |
|
402 |
table (on-the-fly if possible, eg for the system source whose the given |
|
403 |
cursor come from). If not possible, inserts all data by calling |
|
404 |
.executemany(). |
|
405 |
""" |
|
406 |
res = self.syntax_tree_search(session, union, args, varmap=varmap) |
|
2627
d710278e0c1c
manual_insert is a public method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2625
diff
changeset
|
407 |
session.pool.source('system').manual_insert(res, table, session) |
1792 | 408 |
|
6943
406a41c25e13
[sources] reorganize abstract source code, drop no more used cleanup_entities_info and _cleanup_system_relations methods
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6931
diff
changeset
|
409 |
# write modification api ################################################### |
0 | 410 |
# read-only sources don't have to implement methods below |
411 |
||
412 |
def get_extid(self, entity): |
|
413 |
"""return the external id for the given newly inserted entity""" |
|
414 |
raise NotImplementedError() |
|
1792 | 415 |
|
0 | 416 |
def add_entity(self, session, entity): |
417 |
"""add a new entity to the source""" |
|
418 |
raise NotImplementedError() |
|
1792 | 419 |
|
0 | 420 |
def update_entity(self, session, entity): |
421 |
"""update an entity in the source""" |
|
422 |
raise NotImplementedError() |
|
423 |
||
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
424 |
def delete_entities(self, session, entities): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
425 |
"""delete several entities from the source""" |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
426 |
for entity in entities: |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
427 |
self.delete_entity(session, entity) |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
428 |
|
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
429 |
def delete_entity(self, session, entity): |
0 | 430 |
"""delete an entity from the source""" |
431 |
raise NotImplementedError() |
|
432 |
||
433 |
def add_relation(self, session, subject, rtype, object): |
|
434 |
"""add a relation to the source""" |
|
435 |
raise NotImplementedError() |
|
1792 | 436 |
|
0 | 437 |
def delete_relation(self, session, subject, rtype, object): |
438 |
"""delete a relation from the source""" |
|
439 |
raise NotImplementedError() |
|
440 |
||
441 |
# system source interface ################################################# |
|
442 |
||
443 |
def eid_type_source(self, session, eid): |
|
444 |
"""return a tuple (type, source, extid) for the entity with id <eid>""" |
|
445 |
raise NotImplementedError() |
|
1792 | 446 |
|
0 | 447 |
def create_eid(self, session): |
448 |
raise NotImplementedError() |
|
449 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
450 |
def add_info(self, session, entity, source, extid): |
0 | 451 |
"""add type and source info for an eid into the system table""" |
452 |
raise NotImplementedError() |
|
453 |
||
4913
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
454 |
def update_info(self, session, entity, need_fti_update): |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
455 |
"""mark entity as being modified, fulltext reindex if needed""" |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
456 |
raise NotImplementedError() |
083b4d454192
server/web api for accessing to deleted_entites
Katia Saurfelt <katia.saurfelt@logilab.fr>
parents:
4807
diff
changeset
|
457 |
|
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
458 |
def delete_info(self, session, entity, uri, extid): |
0 | 459 |
"""delete system information on deletion of an entity by transfering |
460 |
record from the entities table to the deleted_entities table |
|
461 |
""" |
|
462 |
raise NotImplementedError() |
|
1792 | 463 |
|
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
464 |
def delete_info_multi(self, session, entities, uri, extids): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
465 |
"""ame as delete_info but accepts a list of entities with |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
466 |
the same etype and belinging to the same source. |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
467 |
""" |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
468 |
for entity, extid in itertools.izip(entities, extids): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
469 |
self.delete_info(session, entity, uri, extid) |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
470 |
|
4807
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
471 |
def modified_entities(self, session, etypes, mtime): |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
472 |
"""return a 2-uple: |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
473 |
* list of (etype, eid) of entities of the given types which have been |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
474 |
modified since the given timestamp (actually entities whose full text |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
475 |
index content has changed) |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
476 |
* list of (etype, eid) of entities of the given types which have been |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
477 |
deleted since the given timestamp |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
478 |
""" |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
479 |
raise NotImplementedError() |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
480 |
|
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
481 |
def index_entity(self, session, entity): |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
482 |
"""create an operation to [re]index textual content of the given entity |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
483 |
on commit |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
484 |
""" |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
485 |
raise NotImplementedError() |
5642bfa43236
[cleanup] add index_entity to abstract source, add docstring
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
486 |
|
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
487 |
def fti_unindex_entities(self, session, entities): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
488 |
"""remove text content for entities from the full text index |
0 | 489 |
""" |
490 |
raise NotImplementedError() |
|
1792 | 491 |
|
6889
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
492 |
def fti_index_entities(self, session, entities): |
37668bf302f5
improve massive deletion performance
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
6722
diff
changeset
|
493 |
"""add text content of created/modified entities to the full text index |
0 | 494 |
""" |
495 |
raise NotImplementedError() |
|
1792 | 496 |
|
0 | 497 |
# sql system source interface ############################################# |
498 |
||
499 |
def sqlexec(self, session, sql, args=None): |
|
500 |
"""execute the query and return its result""" |
|
501 |
raise NotImplementedError() |
|
1792 | 502 |
|
0 | 503 |
def temp_table_def(self, selection, solution, table, basemap): |
504 |
raise NotImplementedError() |
|
1792 | 505 |
|
0 | 506 |
def create_index(self, session, table, column, unique=False): |
507 |
raise NotImplementedError() |
|
1792 | 508 |
|
0 | 509 |
def drop_index(self, session, table, column, unique=False): |
510 |
raise NotImplementedError() |
|
511 |
||
512 |
def create_temp_table(self, session, table, schema): |
|
513 |
raise NotImplementedError() |
|
514 |
||
515 |
def clean_temp_data(self, session, temptables): |
|
516 |
"""remove temporary data, usually associated to temporary tables""" |
|
517 |
pass |
|
518 |
||
1792 | 519 |
|
0 | 520 |
class TrFunc(object): |
521 |
"""lower, upper""" |
|
522 |
def __init__(self, trname, index, attrname=None): |
|
523 |
self._tr = trname.lower() |
|
524 |
self.index = index |
|
525 |
self.attrname = attrname |
|
1792 | 526 |
|
0 | 527 |
def apply(self, resdict): |
528 |
value = resdict.get(self.attrname) |
|
529 |
if value is not None: |
|
530 |
return getattr(value, self._tr)() |
|
531 |
return None |
|
532 |
||
533 |
||
534 |
class GlobTrFunc(TrFunc): |
|
535 |
"""count, sum, max, min, avg""" |
|
536 |
funcs = { |
|
537 |
'count': len, |
|
538 |
'sum': sum, |
|
539 |
'max': max, |
|
540 |
'min': min, |
|
541 |
# XXX avg |
|
542 |
} |
|
543 |
def apply(self, result): |
|
544 |
"""have to 'groupby' manually. For instance, if we 'count' for index 1: |
|
545 |
>>> self.apply([(1, 2), (3, 4), (1, 5)]) |
|
546 |
[(1, 7), (3, 4)] |
|
547 |
""" |
|
548 |
keys, values = [], {} |
|
549 |
for row in result: |
|
550 |
key = tuple(v for i, v in enumerate(row) if i != self.index) |
|
551 |
value = row[self.index] |
|
552 |
try: |
|
553 |
values[key].append(value) |
|
554 |
except KeyError: |
|
555 |
keys.append(key) |
|
556 |
values[key] = [value] |
|
557 |
result = [] |
|
558 |
trfunc = self.funcs[self._tr] |
|
559 |
for key in keys: |
|
560 |
row = list(key) |
|
561 |
row.insert(self.index, trfunc(values[key])) |
|
562 |
result.append(row) |
|
563 |
return result |
|
564 |
||
565 |
||
566 |
class ConnectionWrapper(object): |
|
567 |
def __init__(self, cnx=None): |
|
568 |
self.cnx = cnx |
|
569 |
def commit(self): |
|
570 |
pass |
|
571 |
def rollback(self): |
|
572 |
pass |
|
573 |
def cursor(self): |
|
574 |
return None # no actual cursor support |
|
7037
6a8235456fe1
[multi-sources] add missing close method on ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
575 |
def close(self): |
6a8235456fe1
[multi-sources] add missing close method on ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
576 |
if hasattr(self.cnx, 'close'): |
6a8235456fe1
[multi-sources] add missing close method on ConnectionWrapper
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6889
diff
changeset
|
577 |
self.cnx.close() |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
578 |
|
0 | 579 |
from cubicweb.server import SOURCE_TYPES |
580 |
||
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
581 |
def source_adapter(source_type): |
0 | 582 |
try: |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
583 |
return SOURCE_TYPES[source_type] |
0 | 584 |
except KeyError: |
6427
c8a5ac2d1eaa
[schema / sources] store data sources as cubicweb entities
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6162
diff
changeset
|
585 |
raise RuntimeError('Unknown source type %r' % source_type) |
1792 | 586 |
|
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
587 |
def get_source(type, source_config, repo, eid): |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
588 |
"""return a source adapter according to the adapter field in the source's |
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
589 |
configuration |
0 | 590 |
""" |
6945
28bf94d062a9
[sources] refactor source creation and options handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
6944
diff
changeset
|
591 |
return source_adapter(type)(repo, source_config, eid) |