from pacman103.lib import data_spec_gen, graph, lib_map
from pacman103.front.common import enums

from ..utils import bins, filters, vertices


class TransmitVertex(graph.Vertex, filters.VertexWithFilters):
    """PACMAN Vertex for an object which receives input for a Node and
    transmits it to the host.
    """

    REGIONS = enums.enum1(
        'SYSTEM',
        'FILTERS',
        'FILTER_ROUTING'
    )
    model_name = "nengo_tx"

    def __init__(self, node, dt=0.001, output_period=100, time_step=1000,
                 constraints=None, label=None):
        self.node = node

        self.dt = dt
        self.time_step = time_step
        self.output_period = output_period

        self.filters = bins.FilterCollection()

        # Create the vertex
        super(TransmitVertex, self).__init__(
            1, constraints=constraints, label=label
        )

    def get_maximum_atoms_per_core(self):
        return 1

    def get_resources_for_atoms(self, lo_atom, hi_atom, n_machine_time_steps,
                                machine_time_step_us, partition_data_object):
        return lib_map.Resources(1, 1, 1)

    def sizeof_region_system(self):
        """Get the size (in bytes) of the SYSTEM region."""
        # 5 words
        return 4 * 5

    def generateDataSpec(self, processor, subvertex, dao):
        # Get the executable
        x, y, p = processor.get_coordinates()
        executable_target = lib_map.ExecutableTarget(
            vertices.resource_filename("nengo_spinnaker",
                                       "binaries/%s.aplx" % self.model_name),
            x, y, p
        )

        # Generate the spec
        subvertex.spec = data_spec_gen.DataSpec(processor, dao)
        subvertex.spec.initialise(0xABCE, dao)
        subvertex.spec.comment("# Nengo Tx Component")

        # Fill in the spec
        self.reserve_regions(subvertex)
        self.write_region_system(subvertex)

        if len(self.filters) > 0:
            self.write_region_filters(subvertex)
            self.write_region_filter_keys(subvertex)

        # Close the spec
        subvertex.spec.endSpec()
        subvertex.spec.closeSpecFile()

        return (executable_target, list(), list())

    def reserve_regions(self, subvertex):
        """Reserve sufficient space for the regions in the spec."""
        # TODO Modify the following functions to use write_array rather than
        #  lots of writes.
        subvertex.spec.reserveMemRegion(
            self.REGIONS.SYSTEM,
            self.sizeof_region_system()
        )
        if len(self.filters) > 0:
            subvertex.spec.reserveMemRegion(
                self.REGIONS.FILTERS,
                self.sizeof_region_filters()
            )
            subvertex.spec.reserveMemRegion(
                self.REGIONS.FILTER_ROUTING,
                self.sizeof_region_filter_keys(subvertex)
            )

    def write_region_system(self, subvertex):
        """Write the system region for the given subvertex."""
        subvertex.spec.switchWriteFocus(self.REGIONS.SYSTEM)
        subvertex.spec.comment("""# System Region
        # -------------
        # 1. Number of dimensions
        # 2. Machine time step in us
        # 3. Output period in ticks
        # 4. Number of filters
        # 5. Number of filter keys
        """)
        subvertex.spec.write(data=self.node.size_in)
        subvertex.spec.write(data=self.time_step)
        subvertex.spec.write(data=self.output_period)

        if len(self.in_edges) > 0:
            subvertex.spec.write(data=len(self.filters))
            subvertex.spec.write(data=self.filters.num_keys(subvertex))
        else:
            subvertex.spec.write(data=0, repeats=2)

@@ -2,207 +2,118 @@
 rom 
-pacman103.lib import data_spec_gen, graph, lib_map%0Afrom pacman103.front.common import enums%0A%0Afrom ..utils import bins, filters, vertices%0A%0A%0Aclass TransmitVertex(graph.Vertex, filters.VertexWithFilters
+..utils import bins, filters, vertices%0A%0A%0A@filters.with_filters(2, 3)%0Aclass TransmitVertex(vertices.NengoVertex
 ):%0A 
@@ -220,17 +220,16 @@
     %22%22%22%0A
-%0A
     REGI
@@ -238,103 +238,57 @@
 S = 
-enums.enum1(%0A        'SYSTEM',%0A        'FILTERS',%0A        'FILTER_ROUTING'%0A    )%0A    model_name
+vertices.ordered_regions('SYSTEM')%0A    MODEL_NAME
  = %22
@@ -803,1960 +803,253 @@
 def 
-get_resources_for_atoms(self, lo_atom, hi_atom, n_machine_time_steps,%0A                                machine_time_step_us, partition_data_object):%0A        return lib_map.Resources(1, 1,
+cpu_usage(self, n_atoms):%0A        return
  1
-)
 %0A%0A    
-def sizeof_region_system(self):%0A        %22%22%22Get the size (in bytes) of the SYSTEM region.%22%22%22%0A        # 5 words%0A        return 4 * 5%0A%0A    def generateDataSpec(self, processor, subvertex, dao):%0A        # Get the executable%0A        x, y, p = processor.get_coordinates()%0A        executable_target = lib_map.ExecutableTarget(%0A            vertices.resource_filename(%22nengo_spinnaker%22,%0A                                       %22binaries/%25s.aplx%22 %25 self.model_name),%0A            x, y, p%0A        )%0A%0A        # Generate the spec%0A        subvertex.spec = data_spec_gen.DataSpec(processor, dao)%0A        subvertex.spec.initialise(0xABCE, dao)%0A        subvertex.spec.comment(%22# Nengo Tx Component%22)%0A%0A        # Fill in the spec%0A        self.reserve_regions(subvertex)%0A        self.write_region_system(subvertex)%0A%0A        if len(self.filters) %3E 0:%0A            self.write_region_filters(subvertex)%0A            self.write_region_filter_keys(subvertex)%0A%0A        # Close the spec%0A        subvertex.spec.endSpec()%0A        subvertex.spec.closeSpecFile()%0A%0A        return (executable_target, list(), list())%0A%0A    def reserve_regions(self, subvertex):%0A        %22%22%22Reserve sufficient space for the regions in the spec.%22%22%22%0A        # TODO Modify the following functions to use write_array rather than%0A        #  lots of writes.%0A        subvertex.spec.reserveMemRegion(%0A            self.REGIONS.SYSTEM,%0A            self.sizeof_region_system()%0A        )%0A        if len(self.filters) %3E 0:%0A            subvertex.spec.reserveMemRegion(%0A                self.REGIONS.FILTERS,%0A                self.sizeof_region_filters()%0A            )%0A            subvertex.spec.reserveMemRegion(%0A                self.REGIONS.FILTER_ROUTING,%0A                self.sizeof_region_filter_keys(subvertex)%0A            )%0A
+@vertices.region_pre_sizeof('SYSTEM')%0A    def sizeof_region_system(self, n_atoms):%0A        %22%22%22Get the size (in words) of the SYSTEM region.%22%22%22%0A        return 5%0A%0A    @vertices.region_write('SYSTEM')
 %0A   
@@ -1084,24 +1084,30 @@
 f, subvertex
+, spec
 ):%0A        %22
@@ -1164,143 +1164,8 @@
 %22%22%22%0A
-        subvertex.spec.switchWriteFocus(self.REGIONS.SYSTEM)%0A        subvertex.spec.comment(%22%22%22# System Region%0A        # -------------%0A
     
@@ -1345,31 +1345,8 @@
     
-%22%22%22)%0A        subvertex.
 spec
@@ -1380,34 +1380,24 @@
 in)%0A        
-subvertex.
 spec.write(d
@@ -1420,34 +1420,24 @@
 ep)%0A        
-subvertex.
 spec.write(d
@@ -1504,34 +1504,24 @@
             
-subvertex.
 spec.write(d
@@ -1551,34 +1551,24 @@
             
-subvertex.
 spec.write(d
@@ -1631,26 +1631,16 @@
         
-subvertex.
 spec.wri

Move `TransmitVertex` over to `NengoVertex` style, using `@with_filters`.