# Path: dxfgrabber/tablessection.py
# class TablesSection(object):
#     name = 'tables'
# 
#     def __init__(self):
#         self._tables = dict()
#         self._create_default_tables()
# 
#     def _create_default_tables(self):
#         for cls in TABLESMAP.values():
#             table = cls()
#             self._tables[table.name] = table
# 
#     @staticmethod
#     def from_tags(tags, drawing):
#         tables_section = TablesSection()
#         tables_section._setup_tables(tags)
#         return tables_section
# 
#     def _setup_tables(self, tags):
#         def name(table):
#             return table[1].value
# 
#         def skiptags(tags, count):
#             for i in range(count):
#                 next(tags)
#             return tags
# 
#         itertags = skiptags(iter(tags), 2)  # (0, 'SECTION'), (2, 'TABLES')
#         for table in iterchunks(itertags, stoptag='ENDSEC', endofchunk='ENDTAB'):
#             table_class = table_factory(name(table))
#             if table_class is not None:
#                 new_table = table_class.from_tags(table)
#                 self._tables[new_table.name] = new_table
# 
#     def __getattr__(self, key):
#         try:
#             return self._tables[key]
#         except KeyError:
#             raise AttributeError(key)
# 
# Path: dxfgrabber/entitysection.py
# class EntitySection(object):
#     name = 'entities'
# 
#     def __init__(self):
#         self._entities = list()
# 
#     @classmethod
#     def from_tags(cls, tags, drawing):
#         entity_section = cls()
#         entity_section._build(tags)
#         return entity_section
# 
#     def get_entities(self):
#         return self._entities
# 
#     def __len__(self):
#         return len(self._entities)
# 
#     def __iter__(self):
#         return iter(self._entities)
# 
#     def __getitem__(self, index):
#         return self._entities[index]
# 
#     def _build(self, tags):
#         if len(tags) == 3:  # empty entities section
#             return
#         groups = TagGroups(islice(tags, 2, len(tags)-1))
#         self._entities = build_entities(groups)
# 
# class ObjectsSection(EntitySection):
#     name = 'objects'
# 
# Path: dxfgrabber/blockssection.py
# 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])
#                 self._add(block)
#                 groups = list()
# 
#     def _add(self, block):
#         self._blocks[block.name] = block
# 
#     # start of public interface
#     def __len__(self):
#         return len(self._blocks)
# 
#     def __iter__(self):
#         return iter(self._blocks.values())
# 
#     def __contains__(self, name):
#         return name in self._blocks
# 
#     def __getitem__(self, name):
#         return self._blocks[name]
# 
#     def get(self, name, default=None):
#         return self._blocks.get(name, default)


from .codepage import toencoding
from .defaultchunk import DefaultChunk, iterchunks
from .headersection import HeaderSection
from .tablessection import TablesSection
from .entitysection import EntitySection, ObjectsSection
from .blockssection import BlocksSection
from .acdsdata import AcDsDataSection

# Copyright (C) 2012, Manfred Moitzi
# License: MIT License
from __future__ import unicode_literals
__author__ = "mozman <mozman@gmx.at>"


class Sections(object):
    def __init__(self, tagreader, drawing):
        self._sections = {}
        self._create_default_sections()
        self._setup_sections(tagreader, drawing)

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

    def _create_default_sections(self):
        self._sections['header'] = HeaderSection()
        for cls in SECTIONMAP.values():
            section = cls()
            self._sections[section.name] = section

    def _setup_sections(self, tagreader, drawing):
        def name(section):
            return section[1].value

        for section in iterchunks(tagreader, stoptag='EOF', endofchunk='ENDSEC'):
            if name(section) == 'HEADER':
                new_section = HeaderSection.from_tags(section)
                drawing.dxfversion = new_section.get('$ACADVER', 'AC1009')
                codepage = new_section.get('$DWGCODEPAGE', 'ANSI_1252')

# Path: dxfgrabber/tablessection.py
# class TablesSection(object):
#     name = 'tables'
# 
#     def __init__(self):
#         self._tables = dict()
#         self._create_default_tables()
# 
#     def _create_default_tables(self):
#         for cls in TABLESMAP.values():
#             table = cls()
#             self._tables[table.name] = table
# 
#     @staticmethod
#     def from_tags(tags, drawing):
#         tables_section = TablesSection()
#         tables_section._setup_tables(tags)
#         return tables_section
# 
#     def _setup_tables(self, tags):
#         def name(table):
#             return table[1].value
# 
#         def skiptags(tags, count):
#             for i in range(count):
#                 next(tags)
#             return tags
# 
#         itertags = skiptags(iter(tags), 2)  # (0, 'SECTION'), (2, 'TABLES')
#         for table in iterchunks(itertags, stoptag='ENDSEC', endofchunk='ENDTAB'):
#             table_class = table_factory(name(table))
#             if table_class is not None:
#                 new_table = table_class.from_tags(table)
#                 self._tables[new_table.name] = new_table
# 
#     def __getattr__(self, key):
#         try:
#             return self._tables[key]
#         except KeyError:
#             raise AttributeError(key)
# 
# Path: dxfgrabber/entitysection.py
# class EntitySection(object):
#     name = 'entities'
# 
#     def __init__(self):
#         self._entities = list()
# 
#     @classmethod
#     def from_tags(cls, tags, drawing):
#         entity_section = cls()
#         entity_section._build(tags)
#         return entity_section
# 
#     def get_entities(self):
#         return self._entities
# 
#     def __len__(self):
#         return len(self._entities)
# 
#     def __iter__(self):
#         return iter(self._entities)
# 
#     def __getitem__(self, index):
#         return self._entities[index]
# 
#     def _build(self, tags):
#         if len(tags) == 3:  # empty entities section
#             return
#         groups = TagGroups(islice(tags, 2, len(tags)-1))
#         self._entities = build_entities(groups)
# 
# class ObjectsSection(EntitySection):
#     name = 'objects'
# 
# Path: dxfgrabber/blockssection.py
# 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])
#                 self._add(block)
#                 groups = list()
# 
#     def _add(self, block):
#         self._blocks[block.name] = block
# 
#     # start of public interface
#     def __len__(self):
#         return len(self._blocks)
# 
#     def __iter__(self):
#         return iter(self._blocks.values())
# 
#     def __contains__(self, name):
#         return name in self._blocks
# 
#     def __getitem__(self, name):
#         return self._blocks[name]
# 
#     def get(self, name, default=None):
#         return self._blocks.get(name, default)


# Path: dxfgrabber/sections.py
from .codepage import toencoding
from .defaultchunk import DefaultChunk, iterchunks
from .headersection import HeaderSection
from .tablessection import TablesSection
from .entitysection import EntitySection, ObjectsSection
from .blockssection import BlocksSection
from .acdsdata import AcDsDataSection

# Copyright (C) 2012, Manfred Moitzi
# License: MIT License
from __future__ import unicode_literals
__author__ = "mozman <mozman@gmx.at>"


class Sections(object):
    def __init__(self, tagreader, drawing):
        self._sections = {}
        self._create_default_sections()
        self._setup_sections(tagreader, drawing)

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

    def _create_default_sections(self):
        self._sections['header'] = HeaderSection()
        for cls in SECTIONMAP.values():
            section = cls()
            self._sections[section.name] = section

    def _setup_sections(self, tagreader, drawing):
        def name(section):
            return section[1].value

        for section in iterchunks(tagreader, stoptag='EOF', endofchunk='ENDSEC'):
            if name(section) == 'HEADER':
                new_section = HeaderSection.from_tags(section)
                drawing.dxfversion = new_section.get('$ACADVER', 'AC1009')
                codepage = new_section.get('$DWGCODEPAGE', 'ANSI_1252')

                drawing.encoding = toencoding(codepage)
