# Path: pgctl/configsearch.py
# def search_parent_directories(path='.'):
#     from os.path import abspath, dirname
#     path = abspath(path)
#     original_fs_id = fs_id = get_filesystem_id(path)
#     previous_path = None
# 
#     while original_fs_id == fs_id and path != previous_path:
#         yield path
#         previous_path = path
#         path = dirname(path)
#         fs_id = get_filesystem_id(path)


import configparser
import json
import logging
import yaml
from os import environ
from os.path import join
from pgctl.configsearch import search_parent_directories
        from pgctl.configsearch import glob
    from collections import deque
    from collections.abc import Mapping
    from sys import argv
        if environ.get('PGCTL_NO_GLOBAL_CONFIG') == 'true':
            return {}
        etc = join(environ.get('PREFIX', '/'), 'etc', '')
        return self.from_path_prefix(etc)

    def from_homedir(self):
        if environ.get('PGCTL_NO_GLOBAL_CONFIG') == 'true':
            return {}
        home = environ.get('HOME', '$HOME')
        return self.from_path_prefix(home + '/.')

    def from_environ(self, env=None):
        if env is None:
            env = environ

        var_prefix = self.projectname.upper() + '_'
        config = {}
        for varname, value in env.items():
            if varname.startswith(var_prefix):
                varname = varname.replace(var_prefix, '', 1).lower()
                if varname.endswith('_list'):
                    varname = varname.rsplit('_list', 1)[0]
                    value = value.split()
                config[varname] = value
        return config

    def from_app(self, path='.'):
        pattern = self.projectname + '.*'
        return merge(
            self.from_glob(join(parentdir, pattern))

# Path: pgctl/configsearch.py
# def search_parent_directories(path='.'):
#     from os.path import abspath, dirname
#     path = abspath(path)
#     original_fs_id = fs_id = get_filesystem_id(path)
#     previous_path = None
# 
#     while original_fs_id == fs_id and path != previous_path:
#         yield path
#         previous_path = path
#         path = dirname(path)
#         fs_id = get_filesystem_id(path)


# Path: pgctl/config.py
import configparser
import json
import logging
import yaml
from os import environ
from os.path import join
from pgctl.configsearch import search_parent_directories
        from pgctl.configsearch import glob
    from collections import deque
    from collections.abc import Mapping
    from sys import argv

        if environ.get('PGCTL_NO_GLOBAL_CONFIG') == 'true':
            return {}
        etc = join(environ.get('PREFIX', '/'), 'etc', '')
        return self.from_path_prefix(etc)

    def from_homedir(self):
        if environ.get('PGCTL_NO_GLOBAL_CONFIG') == 'true':
            return {}
        home = environ.get('HOME', '$HOME')
        return self.from_path_prefix(home + '/.')

    def from_environ(self, env=None):
        if env is None:
            env = environ

        var_prefix = self.projectname.upper() + '_'
        config = {}
        for varname, value in env.items():
            if varname.startswith(var_prefix):
                varname = varname.replace(var_prefix, '', 1).lower()
                if varname.endswith('_list'):
                    varname = varname.rsplit('_list', 1)[0]
                    value = value.split()
                config[varname] = value
        return config

    def from_app(self, path='.'):
        pattern = self.projectname + '.*'
        return merge(
            self.from_glob(join(parentdir, pattern))

            for parentdir in reversed(tuple(search_parent_directories(path)))