# 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/entitysection.py
# def build_entities(tag_groups):
#     def build_entity(group):
#         try:
#             entity = entity_factory(Tags(group))
#         except KeyError:
#             entity = None  # ignore unsupported entities
#         return entity
# 
#     entities = list()
#     collector = None
#     for group in tag_groups:
#         entity = build_entity(group)
#         if entity is not None:
#             if collector:
#                 if entity.dxftype == 'SEQEND':
#                     collector.stop()
#                     entities.append(collector.entity)
#                     collector = None
#                 else:
#                     collector.append(entity)
#             elif entity.dxftype in ('POLYLINE', 'POLYFACE', 'POLYMESH'):
#                 collector = _Collector(entity)
#             elif entity.dxftype == 'INSERT' and entity.attribsfollow:
#                 collector = _Collector(entity)
#             else:
#                 entities.append(entity)
#     return entities


from itertools import islice
from .tags import TagGroups
from .entitysection import build_entities

# Created: 09.08.2012, taken from my package ezdxf
# Copyright (C) 2011, Manfred Moitzi
# License: MIT-License
from __future__ import unicode_literals
__author__ = "mozman <mozman@gmx.at>"



class BlocksSection(object):
    name = 'blocks'

    def __init__(self):
        self._blocks = dict()

    @staticmethod
    def from_tags(tags, drawing):
        blocks_section = BlocksSection()
        if drawing.grab_blocks:
            blocks_section._build(tags)
        return blocks_section

    def _build(self, tags):
        if len(tags) == 3:  # empty block section
            return
        groups = list()
        for group in TagGroups(islice(tags, 2, len(tags)-1)):
            groups.append(group)
            if group[0].value == 'ENDBLK':
                entities = build_entities(groups)
                block = entities[0]

# 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/entitysection.py
# def build_entities(tag_groups):
#     def build_entity(group):
#         try:
#             entity = entity_factory(Tags(group))
#         except KeyError:
#             entity = None  # ignore unsupported entities
#         return entity
# 
#     entities = list()
#     collector = None
#     for group in tag_groups:
#         entity = build_entity(group)
#         if entity is not None:
#             if collector:
#                 if entity.dxftype == 'SEQEND':
#                     collector.stop()
#                     entities.append(collector.entity)
#                     collector = None
#                 else:
#                     collector.append(entity)
#             elif entity.dxftype in ('POLYLINE', 'POLYFACE', 'POLYMESH'):
#                 collector = _Collector(entity)
#             elif entity.dxftype == 'INSERT' and entity.attribsfollow:
#                 collector = _Collector(entity)
#             else:
#                 entities.append(entity)
#     return entities


# Path: dxfgrabber/blockssection.py
from itertools import islice
from .tags import TagGroups
from .entitysection import build_entities

# Created: 09.08.2012, taken from my package ezdxf
# Copyright (C) 2011, Manfred Moitzi
# License: MIT-License
from __future__ import unicode_literals
__author__ = "mozman <mozman@gmx.at>"



class BlocksSection(object):
    name = 'blocks'

    def __init__(self):
        self._blocks = dict()

    @staticmethod
    def from_tags(tags, drawing):
        blocks_section = BlocksSection()
        if drawing.grab_blocks:
            blocks_section._build(tags)
        return blocks_section

    def _build(self, tags):
        if len(tags) == 3:  # empty block section
            return
        groups = list()
        for group in TagGroups(islice(tags, 2, len(tags)-1)):
            groups.append(group)
            if group[0].value == 'ENDBLK':
                entities = build_entities(groups)
                block = entities[0]

                block.set_entities(entities[1:-1])
