# Path: dxfgrabber/tags.py
# class TagGroups(list):
#     """
#     Group of tags starting with a SplitTag and ending before the next SplitTag.
# 
#     A SplitTag is a tag with code == splitcode, like (0, 'SECTION') for splitcode=0.
# 
#     """
#     def __init__(self, tags, split_code=0):
#         super(TagGroups, self).__init__()
#         self._build_groups(tags, split_code)
# 
#     def _build_groups(self, tags, splitcode):
#         def append(tag):  # first do nothing, skip tags in front of the first split tag
#             pass
#         group = None
#         for tag in tags:  # has to work with iterators/generators
#             if tag.code == splitcode:
#                 if group is not None:
#                     self.append(group)
#                 group = Tags([tag])
#                 append = group.append  # redefine append: add tags to this group
#             else:
#                 append(tag)
#         if group is not None:
#             self.append(group)
# 
#     def get_name(self, index):
#         return self[index][0].value
# 
#     @staticmethod
#     def from_text(text, split_code=0):
#         return TagGroups(Tags.from_text(text), split_code)
# 
# Path: dxfgrabber/tags.py
# class Tags(list):
#     """ DXFTag() chunk as flat list. """
#     def find_all(self, code):
#         """ Returns a list of DXFTag(code, ...). """
#         return [tag for tag in self if tag.code == code]
# 
#     def tag_index(self, code, start=0, end=None):
#         """Return first index of DXFTag(code, value).
#         """
#         if end is None:
#             end = len(self)
#         index = start
#         while index < end:
#             if self[index].code == code:
#                 return index
#             index += 1
#         raise ValueError(code)
# 
#     def get_value(self, code):
#         for tag in self:
#             if tag.code == code:
#                 return tag.value
#         raise ValueError(code)
# 
#     @staticmethod
#     def from_text(text):
#         return Tags(string_tagger(text))
# 
#     def get_type(self):
#         return self.__getitem__(0).value
# 
#     def plain_tags(self):  # yield no app data and no xdata
#         is_app_data = False
#         for tag in self:
#             if tag.code >= 1000:  # skip xdata
#                 continue
#             if tag.code == APP_DATA_MARKER:
#                 if is_app_data:  # tag.value == '}'
#                     is_app_data = False  # end of app data
#                 else:  # value == '{APPID'
#                     is_app_data = True  # start of app data
#                 continue
#             if not is_app_data:
#                 yield tag
# 
#     def xdata(self):
#         index = 0
#         end = len(self)
#         while index < end:
#             if self[index].code > 999:  # all xdata tag codes are >= 1000
#                 return self[index:]  # xdata is always at the end of the DXF entity
#             index += 1
#         return []
# 
#     def app_data(self):
#         app_data = {}
#         app_tags = None
#         for tag in self:
#             if tag.code == APP_DATA_MARKER:
#                 if tag.value == '}':  # end of app data
#                     app_tags.append(tag)
#                     app_data[app_tags[0].value] = app_tags
#                     app_tags = None
#                 else:
#                     app_tags = [tag]
#             else:
#                 if app_tags is not None:  # collection app data
#                     app_tags.append(tag)
#         return app_data
# 
#     def subclasses(self):
#         classes = {}
#         name = 'noname'
#         tags = []
#         for tag in self.plain_tags():
#             if tag.code == SUBCLASS_MARKER:
#                 classes[name] = tags
#                 tags = []
#                 name = tag.value
#             else:
#                 tags.append(tag)
#         classes[name] = tags
#         return classes
# 
#     def get_subclass(self, name):
#         classes = self.subclasses()
#         return classes.get(name, 'noname')


from .tags import TagGroups
from .tags import Tags


    def get(self, name, default=KeyError):
        try:
            return self._table_entries[name]
        except KeyError:
            if default is KeyError:
                raise
            else:
                return default

    def __getitem__(self, item):
        return self.get(item)

    def __contains__(self, name):
        return name in self._table_entries

    def __iter__(self):
        return iter(self._table_entries.values())

    def __len__(self):
        return len(self._table_entries)

    def names(self):
        return sorted(self._table_entries.keys())

    def entry_tags(self, tags):
        groups = TagGroups(tags)
        assert groups.get_name(0) == 'TABLE'
        assert groups.get_name(-1) == 'ENDTAB'
        for entrytags in groups[1:-1]:

# Path: dxfgrabber/tags.py
# class TagGroups(list):
#     """
#     Group of tags starting with a SplitTag and ending before the next SplitTag.
# 
#     A SplitTag is a tag with code == splitcode, like (0, 'SECTION') for splitcode=0.
# 
#     """
#     def __init__(self, tags, split_code=0):
#         super(TagGroups, self).__init__()
#         self._build_groups(tags, split_code)
# 
#     def _build_groups(self, tags, splitcode):
#         def append(tag):  # first do nothing, skip tags in front of the first split tag
#             pass
#         group = None
#         for tag in tags:  # has to work with iterators/generators
#             if tag.code == splitcode:
#                 if group is not None:
#                     self.append(group)
#                 group = Tags([tag])
#                 append = group.append  # redefine append: add tags to this group
#             else:
#                 append(tag)
#         if group is not None:
#             self.append(group)
# 
#     def get_name(self, index):
#         return self[index][0].value
# 
#     @staticmethod
#     def from_text(text, split_code=0):
#         return TagGroups(Tags.from_text(text), split_code)
# 
# Path: dxfgrabber/tags.py
# class Tags(list):
#     """ DXFTag() chunk as flat list. """
#     def find_all(self, code):
#         """ Returns a list of DXFTag(code, ...). """
#         return [tag for tag in self if tag.code == code]
# 
#     def tag_index(self, code, start=0, end=None):
#         """Return first index of DXFTag(code, value).
#         """
#         if end is None:
#             end = len(self)
#         index = start
#         while index < end:
#             if self[index].code == code:
#                 return index
#             index += 1
#         raise ValueError(code)
# 
#     def get_value(self, code):
#         for tag in self:
#             if tag.code == code:
#                 return tag.value
#         raise ValueError(code)
# 
#     @staticmethod
#     def from_text(text):
#         return Tags(string_tagger(text))
# 
#     def get_type(self):
#         return self.__getitem__(0).value
# 
#     def plain_tags(self):  # yield no app data and no xdata
#         is_app_data = False
#         for tag in self:
#             if tag.code >= 1000:  # skip xdata
#                 continue
#             if tag.code == APP_DATA_MARKER:
#                 if is_app_data:  # tag.value == '}'
#                     is_app_data = False  # end of app data
#                 else:  # value == '{APPID'
#                     is_app_data = True  # start of app data
#                 continue
#             if not is_app_data:
#                 yield tag
# 
#     def xdata(self):
#         index = 0
#         end = len(self)
#         while index < end:
#             if self[index].code > 999:  # all xdata tag codes are >= 1000
#                 return self[index:]  # xdata is always at the end of the DXF entity
#             index += 1
#         return []
# 
#     def app_data(self):
#         app_data = {}
#         app_tags = None
#         for tag in self:
#             if tag.code == APP_DATA_MARKER:
#                 if tag.value == '}':  # end of app data
#                     app_tags.append(tag)
#                     app_data[app_tags[0].value] = app_tags
#                     app_tags = None
#                 else:
#                     app_tags = [tag]
#             else:
#                 if app_tags is not None:  # collection app data
#                     app_tags.append(tag)
#         return app_data
# 
#     def subclasses(self):
#         classes = {}
#         name = 'noname'
#         tags = []
#         for tag in self.plain_tags():
#             if tag.code == SUBCLASS_MARKER:
#                 classes[name] = tags
#                 tags = []
#                 name = tag.value
#             else:
#                 tags.append(tag)
#         classes[name] = tags
#         return classes
# 
#     def get_subclass(self, name):
#         classes = self.subclasses()
#         return classes.get(name, 'noname')


# Path: dxfgrabber/layers.py
from .tags import TagGroups
from .tags import Tags


    def get(self, name, default=KeyError):
        try:
            return self._table_entries[name]
        except KeyError:
            if default is KeyError:
                raise
            else:
                return default

    def __getitem__(self, item):
        return self.get(item)

    def __contains__(self, name):
        return name in self._table_entries

    def __iter__(self):
        return iter(self._table_entries.values())

    def __len__(self):
        return len(self._table_entries)

    def names(self):
        return sorted(self._table_entries.keys())

    def entry_tags(self, tags):
        groups = TagGroups(tags)
        assert groups.get_name(0) == 'TABLE'
        assert groups.get_name(-1) == 'ENDTAB'
        for entrytags in groups[1:-1]:

            yield Tags(entrytags)
