# Path: pybombs/config_file.py
# class PBConfigFile(object):
#     """
#     Abstraction layer for our config and other files
#     """
#     def __init__(self, filename):
#         # Store normalized path, in case someone chdirs after calling the ctor
#         self._filename = os.path.abspath(os.path.expanduser(os.path.normpath(filename)))
#         self.data = None
#         self.yaml = AbstractYaml()
#         touch_file(filename)
#         with open(filename) as fn:
#             try:
#                 # TODO: Recursively turn this into an OrderedDict, not just at
#                 # top level. In a nested dict, some key elements will still be
#                 # ruamel.ordereddict.
#                 self.data = OrderedDict(self.yaml.load(fn.read()) or {})
#             except (IOError, OSError):
#                 self.data = OrderedDict()
#             except Exception as e:
#                 raise PBException("Error loading {0}: {1}".format(filename, str(e)))
#         assert isinstance(self.data, OrderedDict)
# 
#     def get(self, key=None, default=None):
#         " Return the data from the config file as a dict "
#         if key is None:
#             return self.data
#         if default is None:
#             default = OrderedDict()
#         # TODO Again, this is not recursive and sub-values may be non-vanilla
#         # ordered dicts
#         return OrderedDict(self.data.get(key, default))
# 
#     def save(self, newdata=None):
#         " Write the contents of the data cache to the file. "
#         if newdata is not None:
#             assert isinstance(newdata, dict)
#             self.data = newdata
#         fpath = os.path.split(self._filename)[0]
#         if len(fpath):
#             sysutils.mkdirp_writable(fpath)
#         with open(self._filename, 'w') as fn:
#             self.yaml.dump(self.data, fn)
# 
#     def update(self, newdata):
#         " Overwrite the data with newdata recursively. Updates the file. "
#         self.data = dict_merge(self.data, newdata)
#         self.save()
#         return self.data


from pybombs.commands import CommandBase
from pybombs.config_file import PBConfigFile

    def _run_env(self):
        " Handle `pybombs config --env` "
        cfg_file = PBConfigFile(self.cfg_file)
        keys = [self.args.key]
        verb = "Getting"
        config_target = 'env'
        if self.args.key is None:
            keys = cfg_file.data.get(config_target, {}).keys()
        elif self.args.value is not None:
            cfg_file.update({config_target: {self.args.key: self.args.value}})
            verb = "Setting"
        print("{verb} environment variables:".format(verb=verb))
        for key in keys:
            print("{key}: {value}".format(
                key=key,
                value=cfg_file.data.get(config_target, {}).get(key))
            )

    def _run_pkg_or_cat(self):
        " Handle `pybombs config --package or --category' "
        if self.args.package is not None and self.args.category is not None:
            self.log.error("Cannot provide both --package and --category!")
            return -1
        if self.args.package is not None:
            config_target = 'packages'
            pkg_cat = self.args.package
        elif self.args.category is not None:
            config_target = 'categories'
            pkg_cat = self.args.category

# Path: pybombs/config_file.py
# class PBConfigFile(object):
#     """
#     Abstraction layer for our config and other files
#     """
#     def __init__(self, filename):
#         # Store normalized path, in case someone chdirs after calling the ctor
#         self._filename = os.path.abspath(os.path.expanduser(os.path.normpath(filename)))
#         self.data = None
#         self.yaml = AbstractYaml()
#         touch_file(filename)
#         with open(filename) as fn:
#             try:
#                 # TODO: Recursively turn this into an OrderedDict, not just at
#                 # top level. In a nested dict, some key elements will still be
#                 # ruamel.ordereddict.
#                 self.data = OrderedDict(self.yaml.load(fn.read()) or {})
#             except (IOError, OSError):
#                 self.data = OrderedDict()
#             except Exception as e:
#                 raise PBException("Error loading {0}: {1}".format(filename, str(e)))
#         assert isinstance(self.data, OrderedDict)
# 
#     def get(self, key=None, default=None):
#         " Return the data from the config file as a dict "
#         if key is None:
#             return self.data
#         if default is None:
#             default = OrderedDict()
#         # TODO Again, this is not recursive and sub-values may be non-vanilla
#         # ordered dicts
#         return OrderedDict(self.data.get(key, default))
# 
#     def save(self, newdata=None):
#         " Write the contents of the data cache to the file. "
#         if newdata is not None:
#             assert isinstance(newdata, dict)
#             self.data = newdata
#         fpath = os.path.split(self._filename)[0]
#         if len(fpath):
#             sysutils.mkdirp_writable(fpath)
#         with open(self._filename, 'w') as fn:
#             self.yaml.dump(self.data, fn)
# 
#     def update(self, newdata):
#         " Overwrite the data with newdata recursively. Updates the file. "
#         self.data = dict_merge(self.data, newdata)
#         self.save()
#         return self.data


# Path: pybombs/commands/config.py
from pybombs.commands import CommandBase
from pybombs.config_file import PBConfigFile


    def _run_env(self):
        " Handle `pybombs config --env` "
        cfg_file = PBConfigFile(self.cfg_file)
        keys = [self.args.key]
        verb = "Getting"
        config_target = 'env'
        if self.args.key is None:
            keys = cfg_file.data.get(config_target, {}).keys()
        elif self.args.value is not None:
            cfg_file.update({config_target: {self.args.key: self.args.value}})
            verb = "Setting"
        print("{verb} environment variables:".format(verb=verb))
        for key in keys:
            print("{key}: {value}".format(
                key=key,
                value=cfg_file.data.get(config_target, {}).get(key))
            )

    def _run_pkg_or_cat(self):
        " Handle `pybombs config --package or --category' "
        if self.args.package is not None and self.args.category is not None:
            self.log.error("Cannot provide both --package and --category!")
            return -1
        if self.args.package is not None:
            config_target = 'packages'
            pkg_cat = self.args.package
        elif self.args.category is not None:
            config_target = 'categories'
            pkg_cat = self.args.category

        cfg_file = PBConfigFile(self.cfg_file)