# Path: raxcli/models.py
# class Attribute(object):
#     """
#     Generic attribute that represents a field on a resource.
#     """
#     _creation_count = 0
# 
#     def __init__(self, src=None, transform_func=None, view_single=True,
#                  view_list=True):
#         """
#         @keyword src: Optional name of the field on the object which acts as a
#                       source for this attribute.
#         @keyword src: C{str}
#         """
#         self.src = src
#         self.transform_func = transform_func
#         self.view_single = view_single
#         self.view_list = view_list
# 
#         # Keep a count that is incremented on instantiation so we can iterate
#         # in the same order that attributes are delcared on an Object.
#         self._creation_count = Attribute._creation_count + 1
#         Attribute._creation_count = self._creation_count
# 
#     def get_value(self):
#         if self.transform_func:
#             return self.transform_func(self.value)
#         else:
#             return self.value
# 
# class Model(object):
#     """
#     Generic object that allows you to declaratively define how resources
#     should be presented.
#     """
#     def get_attrs(self, view_type=None):
#         """
#         Get model attributes for the provided view_type.
#         """
#         attrs = []
#         for attr, _ in getmembers(self):
#             field = getattr(self, attr)
# 
#             if not isinstance(field, Attribute):
#                 continue
# 
#             if not view_type or getattr(field, view_type):
#                 src = field.src if field.src else attr
#                 attrs.append((field._creation_count, [attr, src]))
# 
#         return [attr for field, attr in sorted(attrs, key=itemgetter(0))]
# 
#     def __init__(self, obj):
#         self._obj_type = 'dict' if isinstance(obj, dict) else 'cls'
# 
#         for name, source in self.get_attrs():
#             field = getattr(self, name)
# 
#             if self._obj_type == 'dict':
#                 field.value = obj[source]
#             elif self._obj_type == 'cls':
#                 field.value = getattr(obj, source)
# 
#             setattr(self, name, copy.copy(field))
# 
#     def generate_output(self):
#         columns = [attr for attr, _ in self.get_attrs(view_type='view_single')]
#         data = [getattr(self, attr).get_value() for attr in columns]
#         return (columns, data)
# 
# class Collection(object):
#     """
#     A collection of resources for outputting to a list.
#     """
#     def __init__(self, objs):
#         self.objs = objs
# 
#     def generate_output(self):
#         columns = []
#         data = []
#         for obj in self.objs:
#             if not columns:
#                 columns = [attr for attr, _ in
#                            obj.get_attrs(view_type='view_list')]
# 
#             values = []
#             for key in columns:
#                 attr = getattr(obj, key)
#                 value = attr.get_value()
#                 values.append(value)
# 
#             data.append(values)
# 
#         return (columns, data)


import sys
import unittest2 as unittest
from raxcli.models import Attribute, Model, Collection
# limitations under the License.




class Entity(Model):
    id = Attribute(src='key')
    label = Attribute()
    state = Attribute(transform_func=lambda value: value + '-foo')


class FakeEntity1(object):
    key = 'key1'
    ip = '127.0.0.1'
    label = 'test label'
    state = 'unknown1'


class FakeEntity2(object):
    key = 'key2'
    ip = '127.0.0.1'
    label = 'label2'
    state = 'unknown2'


class TestModels(unittest.TestCase):
    def test_generate_output(self):
        en1 = Entity(FakeEntity1())
        en2 = Entity(FakeEntity2())


# Path: raxcli/models.py
# class Attribute(object):
#     """
#     Generic attribute that represents a field on a resource.
#     """
#     _creation_count = 0
# 
#     def __init__(self, src=None, transform_func=None, view_single=True,
#                  view_list=True):
#         """
#         @keyword src: Optional name of the field on the object which acts as a
#                       source for this attribute.
#         @keyword src: C{str}
#         """
#         self.src = src
#         self.transform_func = transform_func
#         self.view_single = view_single
#         self.view_list = view_list
# 
#         # Keep a count that is incremented on instantiation so we can iterate
#         # in the same order that attributes are delcared on an Object.
#         self._creation_count = Attribute._creation_count + 1
#         Attribute._creation_count = self._creation_count
# 
#     def get_value(self):
#         if self.transform_func:
#             return self.transform_func(self.value)
#         else:
#             return self.value
# 
# class Model(object):
#     """
#     Generic object that allows you to declaratively define how resources
#     should be presented.
#     """
#     def get_attrs(self, view_type=None):
#         """
#         Get model attributes for the provided view_type.
#         """
#         attrs = []
#         for attr, _ in getmembers(self):
#             field = getattr(self, attr)
# 
#             if not isinstance(field, Attribute):
#                 continue
# 
#             if not view_type or getattr(field, view_type):
#                 src = field.src if field.src else attr
#                 attrs.append((field._creation_count, [attr, src]))
# 
#         return [attr for field, attr in sorted(attrs, key=itemgetter(0))]
# 
#     def __init__(self, obj):
#         self._obj_type = 'dict' if isinstance(obj, dict) else 'cls'
# 
#         for name, source in self.get_attrs():
#             field = getattr(self, name)
# 
#             if self._obj_type == 'dict':
#                 field.value = obj[source]
#             elif self._obj_type == 'cls':
#                 field.value = getattr(obj, source)
# 
#             setattr(self, name, copy.copy(field))
# 
#     def generate_output(self):
#         columns = [attr for attr, _ in self.get_attrs(view_type='view_single')]
#         data = [getattr(self, attr).get_value() for attr in columns]
#         return (columns, data)
# 
# class Collection(object):
#     """
#     A collection of resources for outputting to a list.
#     """
#     def __init__(self, objs):
#         self.objs = objs
# 
#     def generate_output(self):
#         columns = []
#         data = []
#         for obj in self.objs:
#             if not columns:
#                 columns = [attr for attr, _ in
#                            obj.get_attrs(view_type='view_list')]
# 
#             values = []
#             for key in columns:
#                 attr = getattr(obj, key)
#                 value = attr.get_value()
#                 values.append(value)
# 
#             data.append(values)
# 
#         return (columns, data)


# Path: tests/test_models.py
import sys
import unittest2 as unittest
from raxcli.models import Attribute, Model, Collection

# limitations under the License.




class Entity(Model):
    id = Attribute(src='key')
    label = Attribute()
    state = Attribute(transform_func=lambda value: value + '-foo')


class FakeEntity1(object):
    key = 'key1'
    ip = '127.0.0.1'
    label = 'test label'
    state = 'unknown1'


class FakeEntity2(object):
    key = 'key2'
    ip = '127.0.0.1'
    label = 'label2'
    state = 'unknown2'


class TestModels(unittest.TestCase):
    def test_generate_output(self):
        en1 = Entity(FakeEntity1())
        en2 = Entity(FakeEntity2())


        collection = Collection([en1, en2])