from peyotl.utility import get_logger
import json
try:
    import anyjson
except:
    class Wrapper(object):
        pass
    anyjson = Wrapper()
    anyjson.loads = json.loads
from peyotl.phylesystem.helper import _get_phylesystem_parent
import os
from threading import Lock
_LOG = get_logger(__name__)
_study_index_lock = Lock()

def get_repos(par_list=None, **kwargs):
    '''Returns a dictionary of name -> filepath
    `name` is the repo name based on the dir name (not the get repo). It is not
        terribly useful, but it is nice to have so that any mirrored repo directory can
        use the same naming convention.
    `filepath` will be the full path to the repo directory (it will end in `name`)
    '''
    _repos = {} # key is repo name, value repo location
    if par_list is None:
        par_list = _get_phylesystem_parent(**kwargs)
    elif not isinstance(par_list, list):
        par_list = [par_list]
    for p in par_list:
        if not os.path.isdir(p):
            raise ValueError('Docstore parent "{p}" is not a directory'.format(p=p))
        for name in os.listdir(p):
            #TODO: Add an option to filter just phylesystem repos (or any specified type?) here!
            #  - add optional list arg `allowed_repo_names`?
            #  - let the FailedShardCreationError work harmlessly?
            #  - treat this function as truly for phylesystem only?
            if os.path.isdir(os.path.join(p, name + '/.git')):
                _repos[name] = os.path.abspath(os.path.join(p, name))
    if len(_repos) == 0:
        raise ValueError('No git repos in {parent}'.format(parent=str(par_list)))
    return _repos

_CACHE_REGION_CONFIGURED = False
_REGION = None
def _make_phylesystem_cache_region(**kwargs):
    '''Only intended to be called by the Phylesystem singleton.
    '''
    global _CACHE_REGION_CONFIGURED, _REGION
    if _CACHE_REGION_CONFIGURED:
        return _REGION
    _CACHE_REGION_CONFIGURED = True
    try:
        from dogpile.cache import make_region
    except:
        _LOG.debug('dogpile.cache not available')
        return
    region = None
    trial_key = 'test_key'
    trial_val = {'test_val': [4, 3]}
    trying_redis = True
    if trying_redis:
        try:
            a = {
                'host': 'localhost',
                'port': 6379,
                'db': 0, # default is 0
                'redis_expiration_time': 60*60*24*2,   # 2 days
                'distributed_lock': False #True if multiple processes will use redis
            }
            region = make_region().configure('dogpile.cache.redis', arguments=a)
            _LOG.debug('cache region set up with cache.redis.')
            _LOG.debug('testing redis caching...')
            region.set(trial_key, trial_val)
            assert trial_val == region.get(trial_key)
            _LOG.debug('redis caching works')
            region.delete(trial_key)
            _REGION = region
            return region
        except:
            _LOG.debug('redis cache set up failed.')
            region = None
    trying_file_dbm = False
    if trying_file_dbm:
        _LOG.debug('Going to try dogpile.cache.dbm ...')
        first_par = _get_phylesystem_parent(**kwargs)[0]
        cache_db_dir = os.path.split(first_par)[0]
        cache_db = os.path.join(cache_db_dir, 'phylesystem-cachefile.dbm')
        _LOG.debug('dogpile.cache region using "{}"'.format(cache_db))
        try:
            a = {'filename': cache_db}
            region = make_region().configure('dogpile.cache.dbm',
                                             expiration_time=36000,
                                             arguments=a)
            _LOG.debug('cache region set up with cache.dbm.')
            _LOG.debug('testing anydbm caching...')
            region.set(trial_key, trial_val)
            assert trial_val == region.get(trial_key)
            _LOG.debug('anydbm caching works')
            region.delete(trial_key)
            _REGION = region
            return region
        except:
            _LOG.debug('anydbm cache set up failed')
            _LOG.debug('exception in the configuration of the cache.')
    _LOG.debug('Phylesystem will not use caching')
    return None



@@ -1649,2563 +1649,4 @@
 os%0A%0A
-_CACHE_REGION_CONFIGURED = False%0A_REGION = None%0Adef _make_phylesystem_cache_region(**kwargs):%0A    '''Only intended to be called by the Phylesystem singleton.%0A    '''%0A    global _CACHE_REGION_CONFIGURED, _REGION%0A    if _CACHE_REGION_CONFIGURED:%0A        return _REGION%0A    _CACHE_REGION_CONFIGURED = True%0A    try:%0A        from dogpile.cache import make_region%0A    except:%0A        _LOG.debug('dogpile.cache not available')%0A        return%0A    region = None%0A    trial_key = 'test_key'%0A    trial_val = %7B'test_val': %5B4, 3%5D%7D%0A    trying_redis = True%0A    if trying_redis:%0A        try:%0A            a = %7B%0A                'host': 'localhost',%0A                'port': 6379,%0A                'db': 0, # default is 0%0A                'redis_expiration_time': 60*60*24*2,   # 2 days%0A                'distributed_lock': False #True if multiple processes will use redis%0A            %7D%0A            region = make_region().configure('dogpile.cache.redis', arguments=a)%0A            _LOG.debug('cache region set up with cache.redis.')%0A            _LOG.debug('testing redis caching...')%0A            region.set(trial_key, trial_val)%0A            assert trial_val == region.get(trial_key)%0A            _LOG.debug('redis caching works')%0A            region.delete(trial_key)%0A            _REGION = region%0A            return region%0A        except:%0A            _LOG.debug('redis cache set up failed.')%0A            region = None%0A    trying_file_dbm = False%0A    if trying_file_dbm:%0A        _LOG.debug('Going to try dogpile.cache.dbm ...')%0A        first_par = _get_phylesystem_parent(**kwargs)%5B0%5D%0A        cache_db_dir = os.path.split(first_par)%5B0%5D%0A        cache_db = os.path.join(cache_db_dir, 'phylesystem-cachefile.dbm')%0A        _LOG.debug('dogpile.cache region using %22%7B%7D%22'.format(cache_db))%0A        try:%0A            a = %7B'filename': cache_db%7D%0A            region = make_region().configure('dogpile.cache.dbm',%0A                                             expiration_time=36000,%0A                                             arguments=a)%0A            _LOG.debug('cache region set up with cache.dbm.')%0A            _LOG.debug('testing anydbm caching...')%0A            region.set(trial_key, trial_val)%0A            assert trial_val == region.get(trial_key)%0A            _LOG.debug('anydbm caching works')%0A            region.delete(trial_key)%0A            _REGION = region%0A            return region%0A        except:%0A            _LOG.debug('anydbm cache set up failed')%0A            _LOG.debug('exception in the configuration of the cache.')%0A    _LOG.debug('Phylesystem will not use caching')%0A    return None%0A%0A%0A

Remove unused (duplicate) method from collections