# Path: pyoko/conf.py
# class Settings(object):
#     def __init__(self):
# 
# Path: pyoko/db/connection.py
# class FoundInCache(Exception):
# class NotFound(Exception):
# class PyokoMG(MultiGetPool):
#     def _worker_method(self):
# 
# Path: pyoko/manage.py
# class BaseThreadedCommand(object):
#     def find_models(self):
#         import_module(settings.MODELS_MODULE)
#         registry = import_module('pyoko.model').model_registry
#         model_name = self.manager.args.model
# 
#         params = [prm['name'] for prm in self.PARAMS]
#         if model_name != 'all':
#             models = [registry.get_model(name) for name in model_name.split(',')]
#         else:
#             excluded_models = []
#             if 'exclude' in params and self.manager.args.exclude:
#                 excluded_models = [registry.get_model(name) for name in
#                                    self.manager.args.exclude.split(',')]
# 
#             models = [model for model in registry.get_base_models() if model not in excluded_models]
#         return models
# 
#     def do_with_submit(self, func, iterables, *args, **kwargs):
#         """
# 
#         Args:
#             func: function name to execute
#             iterables: iterables list to execute with given function
# 
#         Returns:
# 
#         """
#         threads = kwargs.get('threads', None) or 1
#         with con.ThreadPoolExecutor(max_workers=int(threads)) as executor:
#             for item in iterables:
#                 executor.submit(func, item, *args)
# 
# Path: pyoko/manage.py
# class ReIndex(Command, BaseThreadedCommand):
#     CMD_NAME = 'reindex'
#     HELP = 'Re-indexes model objects'
#     PARAMS = [{'name': 'model', 'required': True,
#                'help': 'Models name(s) to be cleared. Say "all" to clear all models'},
#               {'name': 'exclude',
#                'help': 'Models name(s) to be excluded, comma separated'},
#               {'name': 'include_deleted', 'action': 'store_true',
#                'help': 'Reindex object even if it was deleted'},
#               {'name': 'threads', 'default': 1, 'help': 'Max number of threads. Defaults to 1'}
#               ]
# 
#     def run(self):
#         models = self.find_models()
#         self.do_with_submit(self.reindex_model, models, threads=self.manager.args.threads)
# 
#     def reindex_model(self, mdl):
#         stream = mdl.objects.adapter.bucket.stream_keys()
#         i = 0
#         t = 0
#         unsaved_keys = []
#         for key_list in stream:
#             for key in key_list:
#                 t += 1
#                 # time.sleep(0.4)
#                 try:
#                     mdl.objects.get(key).save()
#                     i += 1
#                 except ObjectDoesNotExist:
#                     if self.manager.args.include_deleted:
#                         o = mdl.objects.filter(key=key, deleted=True)[0]
#                         o.save()
#                         i += 1
#                         print("Deleted object found: %s " % o.key)
# 
#                 except ConflictError:
#                     unsaved_keys.append(key)
#                     print("Error on save. Record in conflict: %s > %s" % (mdl.__name__, key))
#                 except:
#                     unsaved_keys.append(key)
#                     print("Error on save! %s > %s" % (mdl.__name__, key))
#                     import traceback
# 
#                     traceback.print_exc()
#         stream.close()
#         print("Re-indexed %s records of %s of %s" % (i, t, mdl.__name__))
#         if unsaved_keys:
#             print("\nThese keys cannot be updated:\n\n", unsaved_keys)


import codecs
import time
import six
import os
import copy
                import traceback
                import traceback
from sys import stdout
from riak import RiakError
from pyoko.conf import settings
from pyoko.db.connection import client
from pyoko.manage import BaseThreadedCommand
from pyoko.manage import ReIndex
    from urllib.request import urlopen
    from urllib.error import HTTPError
    from urllib2 import urlopen, HTTPError
class SchemaUpdater(object):
    """
    traverses trough all models, collects fields marked for index or store in solr
    then creates a solr schema for these fields.
    """

    FIELD_TEMPLATE = '<field    type="{type}" name="{name}"  indexed="{index}" ' \
                     'stored="{store}" multiValued="{multi}" />'

    def __init__(self, models, threads, force):
        self.report = []
        self.models = models
        self.force = force
        self.client = client
        self.threads = int(threads)
        self.n_val = client.bucket_type(settings.DEFAULT_BUCKET_TYPE).get_property('n_val')
        self.base_thread = BaseThreadedCommand()

    def run(self, check_only=False):
        """

        Args:
            check_only:  do not migrate, only report migration is needed or not if True

        Returns:

        """
        reindex = ReIndex().reindex_model
        models = copy.deepcopy(self.models)
        num_models = len(models)

# Path: pyoko/conf.py
# class Settings(object):
#     def __init__(self):
# 
# Path: pyoko/db/connection.py
# class FoundInCache(Exception):
# class NotFound(Exception):
# class PyokoMG(MultiGetPool):
#     def _worker_method(self):
# 
# Path: pyoko/manage.py
# class BaseThreadedCommand(object):
#     def find_models(self):
#         import_module(settings.MODELS_MODULE)
#         registry = import_module('pyoko.model').model_registry
#         model_name = self.manager.args.model
# 
#         params = [prm['name'] for prm in self.PARAMS]
#         if model_name != 'all':
#             models = [registry.get_model(name) for name in model_name.split(',')]
#         else:
#             excluded_models = []
#             if 'exclude' in params and self.manager.args.exclude:
#                 excluded_models = [registry.get_model(name) for name in
#                                    self.manager.args.exclude.split(',')]
# 
#             models = [model for model in registry.get_base_models() if model not in excluded_models]
#         return models
# 
#     def do_with_submit(self, func, iterables, *args, **kwargs):
#         """
# 
#         Args:
#             func: function name to execute
#             iterables: iterables list to execute with given function
# 
#         Returns:
# 
#         """
#         threads = kwargs.get('threads', None) or 1
#         with con.ThreadPoolExecutor(max_workers=int(threads)) as executor:
#             for item in iterables:
#                 executor.submit(func, item, *args)
# 
# Path: pyoko/manage.py
# class ReIndex(Command, BaseThreadedCommand):
#     CMD_NAME = 'reindex'
#     HELP = 'Re-indexes model objects'
#     PARAMS = [{'name': 'model', 'required': True,
#                'help': 'Models name(s) to be cleared. Say "all" to clear all models'},
#               {'name': 'exclude',
#                'help': 'Models name(s) to be excluded, comma separated'},
#               {'name': 'include_deleted', 'action': 'store_true',
#                'help': 'Reindex object even if it was deleted'},
#               {'name': 'threads', 'default': 1, 'help': 'Max number of threads. Defaults to 1'}
#               ]
# 
#     def run(self):
#         models = self.find_models()
#         self.do_with_submit(self.reindex_model, models, threads=self.manager.args.threads)
# 
#     def reindex_model(self, mdl):
#         stream = mdl.objects.adapter.bucket.stream_keys()
#         i = 0
#         t = 0
#         unsaved_keys = []
#         for key_list in stream:
#             for key in key_list:
#                 t += 1
#                 # time.sleep(0.4)
#                 try:
#                     mdl.objects.get(key).save()
#                     i += 1
#                 except ObjectDoesNotExist:
#                     if self.manager.args.include_deleted:
#                         o = mdl.objects.filter(key=key, deleted=True)[0]
#                         o.save()
#                         i += 1
#                         print("Deleted object found: %s " % o.key)
# 
#                 except ConflictError:
#                     unsaved_keys.append(key)
#                     print("Error on save. Record in conflict: %s > %s" % (mdl.__name__, key))
#                 except:
#                     unsaved_keys.append(key)
#                     print("Error on save! %s > %s" % (mdl.__name__, key))
#                     import traceback
# 
#                     traceback.print_exc()
#         stream.close()
#         print("Re-indexed %s records of %s of %s" % (i, t, mdl.__name__))
#         if unsaved_keys:
#             print("\nThese keys cannot be updated:\n\n", unsaved_keys)


# Path: pyoko/db/schema_update.py
import codecs
import time
import six
import os
import copy
                import traceback
                import traceback
from sys import stdout
from riak import RiakError
from pyoko.conf import settings
from pyoko.db.connection import client
from pyoko.manage import BaseThreadedCommand
from pyoko.manage import ReIndex
    from urllib.request import urlopen
    from urllib.error import HTTPError
    from urllib2 import urlopen, HTTPError

class SchemaUpdater(object):
    """
    traverses trough all models, collects fields marked for index or store in solr
    then creates a solr schema for these fields.
    """

    FIELD_TEMPLATE = '<field    type="{type}" name="{name}"  indexed="{index}" ' \
                     'stored="{store}" multiValued="{multi}" />'

    def __init__(self, models, threads, force):
        self.report = []
        self.models = models
        self.force = force
        self.client = client
        self.threads = int(threads)
        self.n_val = client.bucket_type(settings.DEFAULT_BUCKET_TYPE).get_property('n_val')
        self.base_thread = BaseThreadedCommand()

    def run(self, check_only=False):
        """

        Args:
            check_only:  do not migrate, only report migration is needed or not if True

        Returns:

        """
        reindex = ReIndex().reindex_model
        models = copy.deepcopy(self.models)
        num_models = len(models)

        self.client.create_search_index('foo_index', '_yz_default', n_val=self.n_val)