def _get_keys_folder(jdir, key_path=None, in_memory=True,
                     ignore_prefix=('.', '_')):
    """ get json keys from directory structure

    e.g.

    jdir
        sub_dir1
            data1.json
            data2.json
        sub_dir2
            data.json

    _get_keys_folder(jdir)
    => ['sub_dir1', 'sub_dir2']
    _get_keys_folder(jdir,['sub_dir1'])
    => ['data1', 'data2']

    NB: json files are identified with .json extension
        files/directories beginning with '.' are ignored

    """
    if not hasattr(jdir, 'iterdir'):
        raise ValueError('jdir is not a path object; {}'.format(jdir))

    key_path = [] if key_path is None else key_path

    keys = []

    key_found = False if key_path else True
    search_key = key_path[0] if len(key_path) > 0 else None

    for jsub in jdir.iterdir():
        if jsub.is_file() and jsub.name[-5:] == '.json':

            name, ext = os.path.splitext(jsub.name)
            if name == search_key or not key_path:
                key_found = True
                if key_path:
                    return jkeys(jsub, key_path[1:], in_memory, ignore_prefix)
                else:
                    keys.append(name)

        elif (jsub.is_dir()
              and not jsub.name.startswith(ignore_prefix)
              and (jsub.name == search_key or not key_path)):

            key_found = True
            if jsub.name in keys:
                raise IOError(
                    'directory has a sub-dir and file with same name: '
                    '{1} and {1}.json in {0}'.format(jdir, jsub.name))
            if key_path:
                return jkeys(jsub, key_path[1:], in_memory, ignore_prefix)
            else:
                keys.append(jsub.name)

    if not key_found:
        raise KeyError('key not found: {0}'.format(search_key))

    return sorted(keys)
def _get_keys_folder ( jdir , key_path = None , in_memory = True , ignore_prefix = ( '.' , '_' ) ) : if not hasattr ( jdir , 'iterdir' ) : raise ValueError ( 'jdir is not a path object; {}' . format ( jdir ) ) key_path = [ ] if key_path is None else key_path keys = [ ] key_found = False if key_path else True search_key = key_path [ 0 ] if len ( key_path ) > 0 else None for jsub in jdir . iterdir ( ) : if jsub . is_file ( ) and jsub . name [ - 5 : ] == '.json' : name , ext = os . path . splitext ( jsub . name ) if name == search_key or not key_path : key_found = True if key_path : return jkeys ( jsub , key_path [ 1 : ] , in_memory , ignore_prefix ) else : keys . append ( name ) elif ( jsub . is_dir ( ) and not jsub . name . startswith ( ignore_prefix ) and ( jsub . name == search_key or not key_path ) ) : key_found = True if jsub . name in keys : raise IOError ( 'directory has a sub-dir and file with same name: ' '{1} and {1}.json in {0}' . format ( jdir , jsub . name ) ) if key_path : return jkeys ( jsub , key_path [ 1 : ] , in_memory , ignore_prefix ) else : keys . append ( jsub . name ) if not key_found : raise KeyError ( 'key not found: {0}' . format ( search_key ) ) return sorted ( keys )
get json keys from directory structure

    e.g.

    jdir
        sub_dir1
            data1.json
            data2.json
        sub_dir2
            data.json

    _get_keys_folder(jdir)
    => ['sub_dir1', 'sub_dir2']
    _get_keys_folder(jdir,['sub_dir1'])
    => ['data1', 'data2']

    NB: json files are identified with .json extension
        files/directories beginning with '.' are ignored