def gen_manually_selected(all_devices, scrtype, template):
     """Used to generate a list of screens for screentypes that require 
     the devices to be manually set to a particular screen rather than
     filling them up until full and creating a new screen."""
     sheet = scrtype.sheet
     headings = sheet.row_values(0)
     scr_names = headings.index('Screen Name')
     screen_names = sheet.col_values(scr_names)
     unique_names = [] 
     all_names = []
     for name in screen_names:
         if name not in unique_names:
             all_names.append(name)
     # Now we have a list of unique names. Now select all devices for that
     # screen and generate them one at a time
     all_screens = []
     while unique_names:
         for name in unique_names:
             for device in all_devices:
                 if device.cells['Screen Name'] is name:
                     all_screens.append(screengen.gen_screen(scrtype, 
                     screen_devices, template, name))
             unique_names.pop()
     return all_screens
def batch_by_scrtype(scrtype, template):
    """Pass a ScreenType and a template, and return a list all of screens for
    that ScreenType.

    A screen is defined as a list of FactoryTalkXML instances which can be
    sequentially rendered into valid XML text for importing to Factory Talk.
    """
    
    def get_max_screen_rows(layout_cols):
        """Pass a list of LayoutColumns and return the sum all all max_rows,
        which represents the maximum number of rows that a screen can
        accomodate."""
        max_screen_rows = 0
        for layout_col in layout_cols:
            max_screen_rows += layout_col.max_rows
        return max_screen_rows
    
    def get_required_screens(all_devices, max_screen_rows):
        """Figures out the number of screens that will be required to handle 
        the number of devices."""
        required_screens = len(all_devices) / max_screen_rows
        if len(all_devices) % max_screen_rows != 0:
            required_screens += 1 
        return required_screens
        
    def gen_manually_selected(all_devices, scrtype, template):
        """Used to generate a list of screens for screentypes that require 
        the devices to be manually set to a particular screen rather than
        filling them up until full and creating a new screen."""
        sheet = scrtype.sheet
        headings = sheet.row_values(0)
        scr_names = headings.index('Screen Name')
        screen_names = sheet.col_values(scr_names)
        unique_names = [] 
        all_names = []
        for name in screen_names:
            if name not in unique_names:
                all_names.append(name)
        # Now we have a list of unique names. Now select all devices for that
        # screen and generate them one at a time
        all_screens = []
        while unique_names:
            for name in unique_names:
                for device in all_devices:
                    if device.cells['Screen Name'] is name:
                        all_screens.append(screengen.gen_screen(scrtype, 
                        screen_devices, template, name))
                unique_names.pop()
        return all_screens

    all_devices = DeviceCollection.get_all(scrtype.sheet)
    max_screen_rows = get_max_screen_rows(scrtype.layout_cols)
    required_screens = get_required_screens(all_devices, max_screen_rows)
    if scrtype.select_manually: # Settings which belong on a specific page
        assert required_screens == 1, ('Manually selected screens must fit ' 
                                       'all devices on one screen')
        all_screens = gen_manually_selected(all_devices, scrtype, template) 
        return all_screens
    else: # Automatic device selection (e.g. servos, VFDs, I/O)
        device_index = 0 # Next unplaced device
        all_screens = []
        for screen_count in range(0, required_screens):    
            last_device = device_index + max_screen_rows
            screen_devices = all_devices[device_index:last_device+1] 
            new_screen = screengen.gen_screen(scrtype, screen_devices, 
                                              template)
            all_screens.append(new_screen)
            device_index = last_device + 1
        return all_screens