File: GCodeSocket.py

package info (click to toggle)
libcharon 5.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 576 kB
  • sloc: python: 1,575; sh: 388; makefile: 3
file content (81 lines) | stat: -rw-r--r-- 2,347 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Copyright (c) 2021 Ultimaker B.V.
# libCharon is released under the terms of the LGPLv3 or higher.

import socket
import struct
from io import BytesIO, SEEK_SET, SEEK_CUR

from typing import Any, Dict, IO, Optional, List

from Charon.filetypes.GCodeFile import GCodeFile
from urllib.parse import urlparse


## This class is used to read GCode stream that are served
#  dynamically over a TCP connection.
class SocketFileStream(BytesIO):
    def __init__(self, sock_object: socket.socket) -> None:
        super().__init__()
        self.current_line = 0
        self.__socket = sock_object

    def seekable(self) -> bool:
        return True

    def seek(self, offset: int, whence: Optional[int] = None) -> int:
        if whence is None or whence == SEEK_SET:
            self.current_line = offset
        elif whence == SEEK_CUR:
            self.current_line += offset
        else:
            raise ValueError('Unsupported whence mode in seek: %d' % whence)
        return offset

    def readline(self, _size: int = -1) -> bytes:
        self.__socket.send(struct.pack('>I', self.current_line))
        line = b''
        char = b''

        while char != b'\n':
            char = self.__socket.recv(1)
            line += char

        self.current_line += 1
        return line

    def read(self, _size: int = -1) -> bytes:
        raise NotImplementedError("Only readline has been implemented")

    def readlines(self, _hint: int = -1) -> List[bytes]:
        raise NotImplementedError("Only readline has been implemented")

    def tell(self) -> int:
        raise NotImplementedError("Only readline has been implemented")

    def close(self) -> None:
        self.__socket.close()

    def __iter__(self):
        return self

    def __next__(self):
        return self.readline()


class GCodeSocket(GCodeFile):
    mime_type = "text/x-gcode-socket"

    MaximumHeaderLength = 100

    def __init__(self) -> None:
        super().__init__()
        self.__stream = None  # type: Optional[IO[bytes]]
        self.__metadata = {}  # type: Dict[str, Any]
        self.__sock = None

    @staticmethod
    def stream_handler(path: str, mode: str) -> IO:
        url = urlparse(path)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((url.hostname, 1337))
        return SocketFileStream(sock)