Example #1
0
 def update_profiles(self):
     '''
     Gets the list of available profiles from the server.
     '''
     if self.comms._protocol_version < 2:
         raise utils.SDKVersionError("Your version of OpenRGB doesn't support SDK profile controls")
     self.comms.requestProfileList()
Example #2
0
 def __init__(self,
              update_callback: Callable,
              address: str = "127.0.0.1",
              port: int = 6742,
              name: str = "openrgb-python",
              protocol_version: int = None):
     '''
     :param update_callback: the function to call when data is received
     :param address: the ip address of the SDK server
     :param port: the port of the SDK server
     :param name: the string that will be displayed on the OpenRGB SDK tab's list of clients
     '''
     self.lock = threading.Lock()
     self.callback = update_callback
     self.sock = None
     self.max_protocol_version = OPENRGB_PROTOCOL_VERSION
     if protocol_version is not None:
         if protocol_version > self.max_protocol_version:
             raise utils.SDKVersionError(
                 f"Requested protocol version {protocol_version} is greater than maximum supported version {self.max_protocol_version}"
             )
         self._protocol_version = protocol_version
     else:
         self._protocol_version = OPENRGB_PROTOCOL_VERSION
     self.address = address
     self.port = port
     self.name = name
     self.start_connection()
Example #3
0
    def check_version(self, packet_type: utils.PacketType):
        '''
        Verifies that the packet type is supported on the version we are using.

        :param packet_type: What kind of packet is going to be sent
        :raises utils.SDKVersionError: When a packet is unsupported
        '''
        if self._protocol_version < 2 and packet_type in (
                utils.PacketType.REQUEST_PROFILE_LIST,
                utils.PacketType.REQUEST_SAVE_PROFILE,
                utils.PacketType.REQUEST_LOAD_PROFILE,
                utils.PacketType.REQUEST_DELETE_PROFILE):
            raise utils.SDKVersionError(
                "Profile controls not supported on protoocl versions < 2.  You probably need to update OpenRGB"
            )
        elif self._protocol_version < 3 and packet_type == utils.PacketType.RGBCONTROLLER_SAVEMODE:
            raise utils.SDKVersionError(
                "Saving modes not supported on protoocl versions < 3.  You probably need to update OpenRGB"
            )
Example #4
0
    def load_profile(self,
                     name: Union[str, int, utils.Profile],
                     local: bool = False,
                     directory: str = ''):
        '''
        Loads an OpenRGB profile

        :param name: Can be a profile's name, index, or even the Profile itself
        :param local: Whether to load a local file or a profile from the server.
        :param directory: what directory the profile is in.  Defaults to HOME/.config/OpenRGB
        '''
        if local:
            assert type(name) is str
            if directory == '':
                directory = environ['HOME'].rstrip("/") + "/.config/OpenRGB"
            with open(f'{directory}/{name}.orp', 'rb') as f:
                controllers = utils.LocalProfile.unpack(f).controllers
                pairs = []
                for device in self.devices:
                    for new_controller in controllers:
                        if new_controller.name == device.name \
                                and new_controller.device_type == device.type \
                                and new_controller.metadata.description == device.metadata.description:
                            controllers.remove(new_controller)
                            pairs.append((new_controller, device))
                # print("Pairs:")
                for new_controller, device in pairs:
                    # print(device.name, new_controller.name)
                    if new_controller.colors != device.colors:
                        device.set_colors(new_controller.colors)
                    # print(new_controller.active_mode)
                    if new_controller.active_mode != device.active_mode:
                        device.set_mode(new_controller.active_mode)
        else:
            if self.comms._protocol_version < 2:
                raise utils.SDKVersionError(
                    "Your version of OpenRGB doesn't support SDK profile controls"
                )
            if type(name) is str:
                try:
                    name = next(p for p in self.profiles
                                if p.name.lower() == name.lower())
                except StopIteration as e:
                    raise ValueError(
                        f"`{name}` is not an existing profile") from e
            elif type(name) is int:
                name = self.profiles[name]
            elif type(name) is utils.Profile:
                pass
            name = name.pack()
            self.comms.send_header(0, utils.PacketType.REQUEST_LOAD_PROFILE,
                                   len(name))
            self.comms.send_data(name)
Example #5
0
    def delete_profile(self, name: Union[str, int, utils.Profile]):
        '''
        Deletes the selected profile

        :param name: Can be a profile's name, index, or even the Profile itself
        '''
        if self.comms._protocol_version < 2:
            raise utils.SDKVersionError("Your version of OpenRGB doesn't support SDK profile controls")
        if type(name) is str:
            try:
                name = next(p for p in self.profiles if p.name.lower() == name.lower())
            except StopIteration as e:
                raise ValueError(f"`{name}` is not an existing profile") from e
        elif type(name) is int:
            name = self.profiles[name]
        elif type(name) is utils.Profile:
            pass
        name = name.pack()
        self.comms.send_header(0, utils.PacketType.REQUEST_DELETE_PROFILE, len(name))
        self.comms.send_data(name)
        self.update_profiles()
Example #6
0
    def save_profile(self,
                     name: Union[str, int, utils.Profile],
                     local: bool = False,
                     directory: str = ''):
        '''
        Saves the current state of all of your devices to a new or existing
        OpenRGB profile

        :param name: Can be a profile's name, index, or even the Profile itself
        :param local: Whether to load a local file or a profile on the server.
        :param directory: what directory to save the profile in.  Defaults to HOME/.config/OpenRGB
        '''
        if local:
            self.update()
            if directory == '':
                directory = environ['HOME'].rstrip("/") + "/.config/OpenRGB"
            with open(f'{directory.rstrip("/")}/{name}.orp', 'wb') as f:
                f.write(
                    utils.Profile([dev.data for dev in self.devices]).pack())
        else:
            if self.comms._protocol_version < 2:
                raise utils.SDKVersionError(
                    "Your version of OpenRGB doesn't support SDK profile controls"
                )
            if type(name) is str:
                try:
                    name = next(p for p in self.profiles
                                if p.name.lower() == name.lower())
                except StopIteration:
                    name = utils.Profile(name)
            elif type(name) is int:
                name = self.profiles[name]
            elif type(name) is utils.Profile:
                pass
            name = name.pack()
            self.comms.send_header(0, utils.PacketType.REQUEST_SAVE_PROFILE,
                                   len(name))
            self.comms.send_data(name)
            self.update_profiles()