Пример #1
0
 def load_node_storage(cls, storage_payload: dict, federated_only: bool):
     from nucypher.config.storages import NodeStorage
     node_storage_subclasses = {storage._name: storage for storage in NodeStorage.__subclasses__()}
     storage_type = storage_payload[NodeStorage._TYPE_LABEL]
     storage_class = node_storage_subclasses[storage_type]
     node_storage = storage_class.from_payload(payload=storage_payload, federated_only=federated_only)
     return node_storage
Пример #2
0
    def from_configuration_file(cls, filepath: str = None, **overrides) -> 'NodeConfiguration':
        """Initialize a NodeConfiguration from a JSON file."""

        from nucypher.config.storages import NodeStorage
        node_storage_subclasses = {storage._name: storage for storage in NodeStorage.__subclasses__()}

        if filepath is None:
            filepath = cls.DEFAULT_CONFIG_FILE_LOCATION

        # Read from disk
        payload = cls._read_configuration_file(filepath=filepath)

        # Initialize NodeStorage subclass from file (sub-configuration)
        storage_payload = payload['node_storage']
        storage_type = storage_payload[NodeStorage._TYPE_LABEL]
        storage_class = node_storage_subclasses[storage_type]
        node_storage = storage_class.from_payload(payload=storage_payload,
                                                  # character_class=cls._CHARACTER_CLASS,  # TODO: Do not pass this here - Always Use Ursula
                                                  federated_only=payload['federated_only'],
                                                  serializer=cls.NODE_SERIALIZER,
                                                  deserializer=cls.NODE_DESERIALIZER)

        # Deserialize domains to UTF-8 bytestrings
        domains = list(domain.encode() for domain in payload['domains'])
        payload.update(dict(node_storage=node_storage, domains=domains))

        # Filter out Nones from overrides to detect, well, overrides
        overrides = {k: v for k, v in overrides.items() if v is not None}

        # Instantiate from merged params
        node_configuration = cls(**{**payload, **overrides})

        return node_configuration
Пример #3
0
    def from_configuration_file(cls, filepath,
                                **overrides) -> 'NodeConfiguration':
        """Initialize a NodeConfiguration from a JSON file."""
        from nucypher.config.storages import NodeStorage  # TODO: move
        NODE_STORAGES = {
            storage_class._name: storage_class
            for storage_class in NodeStorage.__subclasses__()
        }

        with open(filepath, 'r') as file:
            payload = cls.__CONFIG_FILE_DESERIALIZER(file.read())

        # Make NodeStorage
        storage_payload = payload['node_storage']
        storage_type = storage_payload[NodeStorage._TYPE_LABEL]
        storage_class = NODE_STORAGES[storage_type]
        node_storage = storage_class.from_payload(
            payload=storage_payload,
            character_class=cls._Character,
            federated_only=payload['federated_only'],
            serializer=cls.NODE_SERIALIZER,
            deserializer=cls.NODE_DESERIALIZER)

        payload.update(dict(node_storage=node_storage))
        return cls(**{**payload, **overrides})
Пример #4
0
 def from_storage(cls,
                  node_storage: NodeStorage,
                  checksum_adress: str,
                  federated_only: bool = False,
                  *args,
                  **kwargs) -> 'Ursula':
     return node_storage.get(checksum_address=checksum_adress)
Пример #5
0
    def from_configuration_file(cls,
                                filepath: str = None,
                                **overrides) -> 'NodeConfiguration':
        """Initialize a NodeConfiguration from a JSON file."""

        from nucypher.config.storages import NodeStorage
        node_storage_subclasses = {
            storage._name: storage
            for storage in NodeStorage.__subclasses__()
        }

        if filepath is None:
            filepath = cls.DEFAULT_CONFIG_FILE_LOCATION

        # Read from disk
        payload = cls._read_configuration_file(filepath=filepath)

        # Sanity check
        try:
            checksum_address = payload['checksum_public_address']
        except KeyError:
            raise cls.ConfigurationError(
                f"No checksum address specified in configuration file {filepath}"
            )
        else:
            if not eth_utils.is_checksum_address(checksum_address):
                raise cls.ConfigurationError(
                    f"Address: '{checksum_address}', specified in {filepath} is not a valid checksum address."
                )

        # Initialize NodeStorage subclass from file (sub-configuration)
        storage_payload = payload['node_storage']
        storage_type = storage_payload[NodeStorage._TYPE_LABEL]
        storage_class = node_storage_subclasses[storage_type]
        node_storage = storage_class.from_payload(
            payload=storage_payload,
            federated_only=payload['federated_only'],
            serializer=cls.NODE_SERIALIZER,
            deserializer=cls.NODE_DESERIALIZER)

        # Deserialize domains to UTF-8 bytestrings
        domains = set(domain.encode() for domain in payload['domains'])
        payload.update(dict(node_storage=node_storage, domains=domains))

        # Filter out Nones from overrides to detect, well, overrides
        overrides = {k: v for k, v in overrides.items() if v is not None}

        # Instantiate from merged params
        node_configuration = cls(config_file_location=filepath,
                                 **{
                                     **payload,
                                     **overrides
                                 })

        return node_configuration