# Path: pulp_auto/namespace.py
# def load_ns(d, leaf_processor=lambda x: x):
#     '''a recursive dict-to-Namespace loader'''
#     if not isinstance(d, dict):
#         if isinstance(d, list):
#             return [load_ns(item) for item in d]
#         if isinstance(d, tuple):
#             return tuple([load_ns(item) for item in d])
#         return leaf_processor(d)
#     ns = Namespace()
#     for k, v in d.items():
#         ns[k] = load_ns(v, leaf_processor)
#     return ns
# 
# class Namespace(dict):
#     '''an attribure-access dictionary'''
#     def __init__(self, *args, **kvargs):
#         super(dict, self).__init__(*args, **kvargs)
#         # a little magic
#         self.__dict__ = self
# 
#     def copy(self):
#         '''return a namespace made out of self'''
#         return load_ns(super(Namespace, self).copy())
# 
#     def __getitem__(self, key):
#         def locate(ns, key):
#             if not type(key) in (unicode, str):
#                 # no point in splitting; try directly
#                 return  dict.__getitem__(ns, key)
# 
#             if key == '':
#                 # item found
#                 return ns
# 
#             if type(ns) not in (Namespace, dict):
#                 # item not found
#                 raise KeyError(repr(key))
# 
#             for item_prefix, item_suffix in key_parts(key):
#                 if item_prefix in ns.keys():
#                     return locate(dict.__getitem__(ns, item_prefix), item_suffix, )
# 
#             # item not found
#             raise KeyError(repr(key))
#         return locate(self.__dict__, key)
# 
#     def __contains__(self, key):
#         try:
#             self[key]
#             return True
#         except KeyError:
#             return False


from pulp_auto.namespace import load_ns, Namespace


class Rpm(object):
    '''rpm-specific content type actions'''
    @staticmethod
    def unit_to_nevra(request, epoch=0, arch='x86_64', vendor='Fedora'):
        # unit key as in https://pulp.readthedocs.org/en/latest/dev-guide/integration/rest-api/consumer/content.html#install-content-on-a-consumer
        assert type(request.unit_key) is Namespace, 'unsupported unit key type: %s' % type(request.unit_key)
        if 'version' in request.unit_key:
            v, r  = request.unit_key.version.split("-")
        else:
            v, r = None, None

        return load_ns({
            'name':  request.unit_key.name,
            'epoch': epoch,
            'version': v,
            'release': r,
            'arch':  arch,
            'vendor': vendor
        })

    @staticmethod
    def nevra_to_unit(nevra):

# Path: pulp_auto/namespace.py
# def load_ns(d, leaf_processor=lambda x: x):
#     '''a recursive dict-to-Namespace loader'''
#     if not isinstance(d, dict):
#         if isinstance(d, list):
#             return [load_ns(item) for item in d]
#         if isinstance(d, tuple):
#             return tuple([load_ns(item) for item in d])
#         return leaf_processor(d)
#     ns = Namespace()
#     for k, v in d.items():
#         ns[k] = load_ns(v, leaf_processor)
#     return ns
# 
# class Namespace(dict):
#     '''an attribure-access dictionary'''
#     def __init__(self, *args, **kvargs):
#         super(dict, self).__init__(*args, **kvargs)
#         # a little magic
#         self.__dict__ = self
# 
#     def copy(self):
#         '''return a namespace made out of self'''
#         return load_ns(super(Namespace, self).copy())
# 
#     def __getitem__(self, key):
#         def locate(ns, key):
#             if not type(key) in (unicode, str):
#                 # no point in splitting; try directly
#                 return  dict.__getitem__(ns, key)
# 
#             if key == '':
#                 # item found
#                 return ns
# 
#             if type(ns) not in (Namespace, dict):
#                 # item not found
#                 raise KeyError(repr(key))
# 
#             for item_prefix, item_suffix in key_parts(key):
#                 if item_prefix in ns.keys():
#                     return locate(dict.__getitem__(ns, item_prefix), item_suffix, )
# 
#             # item not found
#             raise KeyError(repr(key))
#         return locate(self.__dict__, key)
# 
#     def __contains__(self, key):
#         try:
#             self[key]
#             return True
#         except KeyError:
#             return False


# Path: pulp_auto/handler/rpm_unit.py
from pulp_auto.namespace import load_ns, Namespace



class Rpm(object):
    '''rpm-specific content type actions'''
    @staticmethod
    def unit_to_nevra(request, epoch=0, arch='x86_64', vendor='Fedora'):
        # unit key as in https://pulp.readthedocs.org/en/latest/dev-guide/integration/rest-api/consumer/content.html#install-content-on-a-consumer
        assert type(request.unit_key) is Namespace, 'unsupported unit key type: %s' % type(request.unit_key)
        if 'version' in request.unit_key:
            v, r  = request.unit_key.version.split("-")
        else:
            v, r = None, None

        return load_ns({
            'name':  request.unit_key.name,
            'epoch': epoch,
            'version': v,
            'release': r,
            'arch':  arch,
            'vendor': vendor
        })

    @staticmethod
    def nevra_to_unit(nevra):

        return load_ns({