1
1
import os
2
2
import warnings
3
+ from urlparse import urljoin
3
4
4
5
from unidecode import unidecode
5
6
6
7
from git import Repo
7
8
8
9
from elasticutils import get_es , S as SBase , Q , F
9
10
10
- from elasticgit .storage import StorageManager
11
+ from elasticgit .storage import StorageManager , RemoteStorageManager
11
12
from elasticgit .search import ESManager
12
13
13
14
import logging
@@ -68,8 +69,8 @@ def setup(self, name, email):
68
69
'email' : email ,
69
70
})
70
71
71
- if not self .im .index_exists (self .repo .active_branch . name ):
72
- self .im .create_index (self .repo .active_branch . name )
72
+ if not self .im .index_exists (self .sm .active_branch () ):
73
+ self .im .create_index (self .sm .active_branch () )
73
74
74
75
def exists (self ):
75
76
"""
@@ -79,8 +80,7 @@ def exists(self):
79
80
:returns: bool
80
81
"""
81
82
if self .sm .storage_exists ():
82
- branch = self .sm .repo .active_branch
83
- return self .im .index_exists (branch .name )
83
+ return self .im .index_exists (self .sm .active_branch ())
84
84
85
85
return False
86
86
@@ -90,9 +90,8 @@ def destroy(self):
90
90
Guaranteed to remove things completely, use with caution.
91
91
"""
92
92
if self .sm .storage_exists ():
93
- branch = self .sm .repo .active_branch
94
- if self .im .index_exists (branch .name ):
95
- self .im .destroy_index (branch .name )
93
+ if self .im .index_exists (self .sm .active_branch ()):
94
+ self .im .destroy_index (self .sm .active_branch ())
96
95
self .sm .destroy_storage ()
97
96
98
97
def save (self , model , message , author = None , committer = None ):
@@ -223,9 +222,8 @@ def reindex_iter(self, model_class, refresh_index=True):
223
222
been indexed. Defaults to ``True``
224
223
225
224
"""
226
- branch = self .repo .active_branch
227
- if not self .im .index_exists (branch .name ):
228
- self .im .create_index (branch .name )
225
+ if not self .im .index_exists (self .sm .active_branch ()):
226
+ self .im .create_index (self .sm .active_branch ())
229
227
iterator = self .sm .iterate (model_class )
230
228
for model in iterator :
231
229
yield self .im .index (model )
@@ -246,15 +244,15 @@ def refresh_index(self):
246
244
Manually refresh the Elasticsearch index. In production this is
247
245
not necessary but it is useful when running tests.
248
246
"""
249
- self .im .refresh_indices (self .repo .active_branch . name )
247
+ self .im .refresh_indices (self .sm .active_branch () )
250
248
251
249
def index_ready (self ):
252
250
"""
253
251
Check if the index is ready
254
252
255
253
:returns: bool
256
254
"""
257
- return self .im .index_ready (self .repo .active_branch . name )
255
+ return self .im .index_ready (self .sm .active_branch () )
258
256
259
257
def sync (self , model_class , refresh_index = True ):
260
258
"""
@@ -292,7 +290,7 @@ def setup_mapping(self, model_class):
292
290
:param elasticgit.models.Model model_class:
293
291
:returns: dict, the decoded dictionary from Elasticsearch
294
292
"""
295
- return self .im .setup_mapping (self .repo .active_branch . name , model_class )
293
+ return self .im .setup_mapping (self .sm .active_branch () , model_class )
296
294
297
295
def setup_custom_mapping (self , model_class , mapping ):
298
296
"""
@@ -305,15 +303,15 @@ def setup_custom_mapping(self, model_class, mapping):
305
303
"""
306
304
307
305
return self .im .setup_custom_mapping (
308
- self .repo .active_branch . name , model_class , mapping )
306
+ self .sm .active_branch () , model_class , mapping )
309
307
310
308
def get_mapping (self , model_class ):
311
309
"""
312
310
Get a mapping from Elasticsearch for a model_class
313
311
:param elasticgit.models.Model model_class:
314
312
:returns: dict
315
313
"""
316
- return self .im .get_mapping (self .repo .active_branch . name , model_class )
314
+ return self .im .get_mapping (self .sm .active_branch () , model_class )
317
315
318
316
def S (self , model_class ):
319
317
"""
@@ -331,6 +329,40 @@ def S(self, model_class):
331
329
self .im .get_mapping_type (model_class )).es (** self .es_settings )
332
330
333
331
332
+ class RemoteWorkspace (Workspace ):
333
+ """
334
+ A workspace that connects to a unicore.distribute server hosted
335
+ somewhere on the network.
336
+
337
+ This is a read only version of the :py:class:`Workspace`
338
+ """
339
+ def __init__ (self , url , es = None ):
340
+ """
341
+ :param str url:
342
+ The URL of the unicore.distribute server.
343
+ :param es dict:
344
+ The parameters for connecting to Elasticsearch to. If not specified
345
+ then the default unicore.distribute ES proxy would be used.
346
+ This defaults to ``/esapi`` on the host of the ``url`` parameter
347
+ provided.
348
+ """
349
+ self .es_settings = es or {'urls' : urljoin (url , '/esapi' )}
350
+ self .sm = RemoteStorageManager (url )
351
+ self .im = ESManager (
352
+ self .sm ,
353
+ es = get_es (** self .es_settings ),
354
+ index_prefix = self .sm .repo_name )
355
+
356
+ def pull (self , branch_name = 'master' , remote_name = 'origin' ):
357
+ # TOOD: In the local storage we're diffing the changes pulled in
358
+ # So that we can re-index those, unicore.distribute doesn't
359
+ # expose that diff yet and so we cannot yet reindex.
360
+ import warnings
361
+ warnings .warn ('Pulling without updating the index!' )
362
+ self .sm .pull (branch_name = branch_name ,
363
+ remote_name = remote_name )
364
+
365
+
334
366
class EG (object ):
335
367
336
368
"""
@@ -384,5 +416,6 @@ def init_repo(cls, workdir, bare=False):
384
416
def clone_repo (cls , repo_url , workdir ):
385
417
return Repo .clone_from (repo_url , workdir )
386
418
419
+
387
420
Q
388
421
F
0 commit comments