from output import basic_gds_output
from inspect import ismethod
DO = set(dir(list()))

__all__ = ["gds_output_object"]

class gds_output_object(basic_gds_output):

    def __str_object__(self, item):
        s = []
        s.append("TYPE: " + str(type(item)))
        if hasattr(item, "name"):
            s.append("NAME: " + str(item.name))
        d = set(dir(item)) - DO
        for i in d:
            I = getattr(item,i)
            if ismethod(I): continue
            try:
                s.append("---" + str(i) + " = " + str(I))
            except:
                s.append("---" + str(i) + " : " + str(type(I)))
        s.append("-----------------------------------------------------\n")
        return "\n".join(s)

    def __str_library_header__(self, library):
        self.init_library_header(library)
        return self.__str_object__(library)

    def __str_library_footer__(self):
        return "END OF LIBRARY\n\n"

    def __str_structure_header__(self, item):
        print >>sys.stderr, "STRUCTURE: " , item.name
        return self.__str_object__(item)

    #generate the footer for any structure
    def __str_structure_footer__(self, item):
        return "END OF STRUCTURE : " + item.name + "\n\n"

    def str_shape_element(self, item):
        return "\n".join([self.__str_object__(item),
                          self.__str_object__(item.shape),
                          basic_gds_output.str_shape_element(self, item),
                          ]
                          )

    def str_path_element (self, layer, coordinates, line_width, path_type):
        return "\n".join([ "   PATHSHAPE = " + str(type(shape)),
                           self.__str_object__(coordinates)])

    def str_boundary_element (self, layer,coordinates):
        return "\n".join([ "   BOUNDARYSHAPE = " + self.__str_object__(coordinates)])

    def str_label_element (self, item):
        return self.__str_object__(item)

    def str_box_element (self, item):
        return self.__str_object__(item)

    def str_sref_element (self, item):
        return self.__str_object__(item)

    def str_aref_element (self, item):
        return self.__str_object__(item)

