Exemplo n.º 1
0
 def test_missing_gcode_file_throws_IOError(self):
     """Loading a missing gcode file throws an IOError."""
     gcode_dir = tempfile.mkdtemp()
     with patch.object(utils.options.mockable(), 'gcode_dir', gcode_dir):
         with self.assertRaises(IOError):
             utils.load_gcode_file(id)
     shutil.rmtree(gcode_dir)
Exemplo n.º 2
0
 def test_failed_gcode_file_throws_IOError(self):
     """Failure while loading a gcode file throws an IOError."""
     mopen = mock_open()
     mopen.side_effect = IOError
     with patch('builtins.open', mopen):
         with self.assertRaises(IOError):
             utils.load_gcode_file(123)
Exemplo n.º 3
0
    def print_gcode_file(self, id):
        """
        Print gcode file with given `id`.

        :param id: ID of the gcode file to get.
        :type id: :class:`int`
        """
        if PRINTER['state'] != opengb.printer.State.READY:
            raise IndexError('Printer not ready')
        try:
            OGD.GCodeFile.get(OGD.GCodeFile.id == id)
        except OGD.GCodeFile.DoesNotExist:
            raise IndexError('No gcode file found with id {0}'.format(id))
        try:
            gcode = OGU.prepare_gcode(OGU.load_gcode_file(id))
        except IOError as err:
            LOGGER.error('Error reading gcode file with id {0}: '
                         '{1}'.format(id, err))
            raise IndexError('Error reading gcode file with '
                             'id {0}'.format(id))
        self._to_printer.put({
            'method': 'execute_gcode',
            'params': {
                'gcode_sequence': gcode,
            }
        })
        return True
Exemplo n.º 4
0
 def test_gcode_file_loaded(self):
     """Gcode file with given id is loaded from the filesystem."""
     gcode_dir = tempfile.mkdtemp()
     id = 123
     with open(os.path.join(gcode_dir, str(id)), 'wb') as p:
         p.write(self.gcode.encode())
     with patch.object(utils.options.mockable(), 'gcode_dir', gcode_dir):
         gcode = utils.load_gcode_file(id)
     self.assertEqual(gcode, self.gcode)
     shutil.rmtree(gcode_dir)
Exemplo n.º 5
0
    def get_gcode_file(self, id, content=False):
        """
        Get details of a single gcode file with the given `id`.

        Optionally include the gcode file content.

        :param id: ID of the gcode file to get.
        :type id: :class:`int`
        :param content: Include the gcode file content in the results.
        :type content: :class:`bool` (default False)
        """
        try:
            result = OGD.GCodeFile.get(OGD.GCodeFile.id == id)
            gcode_file = {
                'id': result.id,
                'name': result.name,
                'size': result.size,
                'uploaded': str(result.uploaded),
                'print_material': result.print_material,
                'print_quality': result.print_quality,
                'print_extruders': result.print_extruders,
                'print_time_sec': result.print_time_sec,
                'print_filament_mm': result.print_filament_mm,
                'print_material_gm': result.print_material_gm,
                'thumbnail_png_base64': result.thumbnail_png_base64,
            }
        except OGD.GCodeFile.DoesNotExist:
            raise IndexError('No gcode file found with id {0}'.format(id))
        if content:
            try:
                gcode_file['content'] = OGU.load_gcode_file(id)
            except IOError as err:
                LOGGER.error('Error reading gcode file with id {0}: '
                             '{1}'.format(id, err))
                raise IndexError('Error reading gcode file with '
                                 'id {0}'.format(id))
        return gcode_file