Exemple #1
0
class Nginx(Plugin):
    configuration_schema: Schema = Schema({
        Required('www_directory_path', default=None):
        Maybe(str),
        Required('https', default=None):
        Maybe(bool),
    })

    def __init__(self,
                 site: Site,
                 www_directory_path: Optional[str] = None,
                 https: Optional[bool] = None):
        self._https = https
        self._www_directory_path = www_directory_path
        self._site = site

    @classmethod
    def for_site(cls, site: Site, configuration: Dict):
        return cls(site, configuration['www_directory_path'],
                   configuration['https'])

    def subscribes_to(self) -> List[Tuple[Type[Event], Callable]]:
        return [
            (PostGenerateEvent, self._generate_config),
        ]

    @property
    def assets_directory_path(self) -> Optional[str]:
        return '%s/assets' % os.path.dirname(__file__)

    @property
    def https(self) -> bool:
        if self._https is None:
            return self._site.configuration.base_url.startswith('https')
        return self._https

    @property
    def www_directory_path(self) -> str:
        if self._www_directory_path is None:
            return self._site.configuration.www_directory_path
        return self._www_directory_path

    async def _generate_config(self, event: PostGenerateEvent) -> None:
        output_directory_path = os.path.join(
            self._site.configuration.output_directory_path, 'nginx')
        makedirs(output_directory_path)

        # Render the ngnix configuration.
        file_name = 'nginx.conf.j2'
        destination_file_path = os.path.join(output_directory_path, file_name)
        await self._site.assets.copy2(file_name, destination_file_path)
        await self._site.renderer.render_file(destination_file_path)

        # Render the Dockerfile.
        copyfile(os.path.join(DOCKER_PATH, 'Dockerfile'),
                 os.path.join(output_directory_path, 'Dockerfile'))
Exemple #2
0
def test_maybe():
    s = Schema(Maybe(int))
    assert s(1) == 1
    assert s(None) is None
    assert_raises(Invalid, s, 'foo')

    s = Schema(Maybe({str: Coerce(int)}))
    assert s({'foo': '100'}) == {'foo': 100}
    assert s(None) is None
    assert_raises(Invalid, s, {'foo': 'bar'})
class Output(JSONSerialisableClass):
    """
    Represents a model output in the pyESG configuration.
    Attributes:
        id (str): The id (or name) for the output.
        type (str): The type of the output.
        initial_value (float): The initial value for the output.
        parameters (List[Parameter]): A list of the parameters associated with the output.
    """
    _serialisable_attrs = {
        'parameters': Parameters,
    }

    _validation_schema = Schema({
        Required('id'):
        str,
        Required('type'):
        str,
        Required('initial_value'):
        Maybe(Coerce(float)),
        Required('parameters'):
        Parameters._validation_schema,
    })

    def __init__(self, **kwargs):
        self.id = None  # type: str
        self.type = None  # type: str
        self.initial_value = None  # type: float
        self.parameters = Parameters()  # type: Parameters
        super().__init__(**kwargs)
Exemple #4
0
def test_maybe():
    assert_raises(TypeError, Maybe, lambda x: x)

    s = Schema(Maybe(int))
    assert s(1) == 1
    assert s(None) is None

    assert_raises(Invalid, s, 'foo')
Exemple #5
0
def test_maybe_returns_custom_subvalidator_error():
    schema = Schema(Maybe(validator_value_not_3))

    # The following should be valid
    schema(None)
    schema(1)

    with raises(MultipleInvalid, "Should not be 3"):
        assert schema(3)
Exemple #6
0
def test_maybe_returns_subvalidator_error():
    schema = Schema(Maybe(Range(1, 2)))

    # The following should be valid
    schema(None)
    schema(1)
    schema(2)

    with raises(MultipleInvalid, "value must be at most 2"):
        assert schema(3)
Exemple #7
0
def test_maybe_returns_subvalidator_error():
    schema = Schema(Maybe(Range(1, 2)))

    # The following should be valid
    schema(None)
    schema(1)
    schema(2)

    try:
        # Should trigger a MultipleInvalid exception
        schema(3)
    except MultipleInvalid as e:
        assert_equal(str(e), "value must be at most 2")
    else:
        assert False, "Did not raise correct Invalid"
Exemple #8
0
def test_repr():
    """Verify that __repr__ returns valid Python expressions"""
    match = Match('a pattern', msg='message')
    replace = Replace('you', 'I', msg='you and I')
    range_ = Range(min=0, max=42, min_included=False,
                   max_included=False, msg='number not in range')
    coerce_ = Coerce(int, msg="moo")
    all_ = All('10', Coerce(int), msg='all msg')
    maybe_int = Maybe(int)

    assert_equal(repr(match), "Match('a pattern', msg='message')")
    assert_equal(repr(replace), "Replace('you', 'I', msg='you and I')")
    assert_equal(
        repr(range_),
        "Range(min=0, max=42, min_included=False, max_included=False, msg='number not in range')"
    )
    assert_equal(repr(coerce_), "Coerce(int, msg='moo')")
    assert_equal(repr(all_), "All('10', Coerce(int, msg=None), msg='all msg')")
    assert_equal(repr(maybe_int), "Any(%s, None, msg=None)" % str(int))
Exemple #9
0
def test_maybe_accepts_msg():
    s = Schema(Maybe(int, msg='int or None expected'))
    with raises(MultipleInvalid, 'int or None expected'):
        assert s([])
Exemple #10
0
        "pci" : Use any PCIe Edge TPU
        "pci:<N>" : Use N-th PCIe Edge TPU
        "cpu" : Run on the CPU
    """
    for regex in DEVICE_REGEXES:
        if regex.match(device):
            return device
    raise Invalid(
        f"EdgeTPU device {device} is invalid. Please check your configuration")


SCHEMA = AbstractDetectorConfig.schema.extend({
    Required("model_path", default=MODEL_PATH):
    str,
    Optional("model_width", default=MODEL_WIDTH):
    Maybe(int),
    Optional("model_height", default=MODEL_HEIGHT):
    Maybe(int),
    Required("label_path", default=LABEL_PATH):
    str,
    Optional("device", default=DEVICE):
    All(str, edgetpu_device_validator),
})


class ObjectDetection(AbstractObjectDetection):
    """Performs object detection."""
    def __init__(self, config):
        self.labels = self.read_labels(config.label_path)
        LOGGER.debug(f"Available devices: {list_edge_tpus()}")
        LOGGER.debug(f"Loading interpreter with device {config.device}")
Exemple #11
0
from voluptuous import (All, Any, Coerce, Length, Match, Maybe,
                        MultipleInvalid, Required, Schema)

# JSON Validation Schemas
ID_VALIDATOR = All(
    Coerce(str, msg='Invalid variable type, expected str'),
    Length(min=8, max=30, msg='Invalid Length, expected 8-30 char'),
    Match(r'^[A-Za-z_][A-Za-z0-9_]{7,29}$'))

DEVICE_SCHEMA = Schema({
    Required('id'):
    ID_VALIDATOR,
    Required('type', default=None):
    Coerce(str),
    Required('cluster', default=None):
    Any(Maybe(str), ID_VALIDATOR),
    Required('gateway'):
    All(Coerce(str), Match(r'^0x[0-9A-F]{8}$'))
})

CLUSTER_SCHEMA = Schema({
    Required('id'):
    ID_VALIDATOR,
    Required('type', default=None):
    Maybe(str, msg='Invalid "type" variable, expected str or None'),
    Required('devices', default=list):
    list,
    Required('gateways', default=list):
    list,
})
Exemple #12
0
    DNN_TARGET_CPU,
    DNN_TARGET_CUDA,
    DNN_TARGET_OPENCL,
)
from voluptuous import All, Any, Coerce, Maybe, Optional, Range

from viseron.const import ENV_CUDA_SUPPORTED, ENV_OPENCL_SUPPORTED
from viseron.detector import AbstractDetectorConfig

from .defaults import LABEL_PATH, MODEL_CONFIG, MODEL_PATH

SCHEMA = AbstractDetectorConfig.SCHEMA.extend(
    {
        Optional("model_path", default=MODEL_PATH): str,
        Optional("model_config", default=MODEL_CONFIG): str,
        Optional("model_width", default=None): Maybe(int),
        Optional("model_height", default=None): Maybe(int),
        Optional("label_path", default=LABEL_PATH): str,
        Optional("suppression", default=0.4): All(
            Any(0, 1, All(float, Range(min=0.0, max=1.0))), Coerce(float)
        ),
    }
)


class Config(AbstractDetectorConfig):
    """Darknet object detection config."""

    def __init__(self, detector_config):
        super().__init__(detector_config)
        self._model_path = detector_config["model_path"]
Exemple #13
0
class Nginx(Plugin, PostGenerator, ServerProvider):
    configuration_schema: Schema = Schema({
        Required('www_directory_path', default=None):
        Maybe(str),
        Required('https', default=None):
        Maybe(bool),
    })

    def __init__(self,
                 site: Site,
                 www_directory_path: Optional[str] = None,
                 https: Optional[bool] = None):
        self._https = https
        self._www_directory_path = www_directory_path
        self._site = site

    @classmethod
    def for_site(cls, site: Site, configuration: Any = NO_CONFIGURATION):
        return cls(site, configuration['www_directory_path'],
                   configuration['https'])

    @property
    def servers(self) -> Iterable[Server]:
        from betty.plugin.nginx.serve import DockerizedNginxServer

        if DockerizedNginxServer.is_available():
            return [DockerizedNginxServer(self._site)]
        return []

    async def post_generate(self) -> None:
        await self.generate_configuration_file()
        await self._generate_dockerfile_file()

    @property
    def assets_directory_path(self) -> Optional[str]:
        return '%s/assets' % path.dirname(__file__)

    @property
    def https(self) -> bool:
        if self._https is None:
            return self._site.configuration.base_url.startswith('https')
        return self._https

    @property
    def www_directory_path(self) -> str:
        if self._www_directory_path is None:
            return self._site.configuration.www_directory_path
        return self._www_directory_path

    async def generate_configuration_file(
            self,
            destination_file_path: Optional[str] = None,
            **kwargs) -> None:
        kwargs = dict(
            {
                'content_negotiation':
                self._site.configuration.content_negotiation,
                'https': self._site.plugins[Nginx].https,
                'locale': self._site.locale,
                'locales': self._site.configuration.locales,
                'multilingual': self._site.configuration.multilingual,
                'server_name': urlparse(
                    self._site.configuration.base_url).netloc,
                'www_directory_path':
                self._site.configuration.www_directory_path,
            }, **kwargs)
        if destination_file_path is None:
            destination_file_path = path.join(
                self._site.configuration.output_directory_path, 'nginx',
                'nginx.conf')
        await generate_configuration_file(destination_file_path,
                                          self._site.jinja2_environment,
                                          **kwargs)

    async def _generate_dockerfile_file(self) -> None:
        await generate_dockerfile_file(
            path.join(self._site.configuration.output_directory_path, 'nginx',
                      'docker', 'Dockerfile'))
Exemple #14
0
def test_voluptuous_maybe_checks_none_first():
    schema = Maybe(validator_that_raises_typeerror)
    assert schema(None) is None
Exemple #15
0
        Coerce(str),
        Match(
            r'^(22[4-9]|230)(\.([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])){3}:\d{3,4}$'
        )),
    Optional('max_log_size', default='2'):
    All(Coerce(str), Match(r'^\d{1,2}$')),
    Optional('max_log_count', default='5'):
    All(Coerce(str), Match(r'^\d{1,2}$')),
})

END_DEVICE_CONF_VALIDATOR = Schema({
    Required('code'):
    Coerce(str),
    Required('id'):
    All(Coerce(str, msg='Invalid variable type, expected str'),
        Length(min=8, max=30, msg='Invalid Length, expected 8-30 char'),
        Match(r'^[A-Za-z_][A-Za-z0-9_]{7,29}$')),
    Required('cluster'):
    Any(
        Maybe(str),
        All(Coerce(str, msg='Invalid variable type, expected str'),
            Length(min=8, max=30, msg='Invalid Length, expected 8-30 char'),
            Match(r'^[A-Za-z_][A-Za-z0-9_]{7,29}$'))),
    Required('type'):
    str
})

SERVER_RESPONSE_VALIDATOR = Schema({
    Required('status'): Coerce(str),
    Required('message'): Coerce(str)
})
Exemple #16
0
    # Dont enable VA-API if CUDA is available
    if (os.getenv(ENV_VAAPI_SUPPORTED) == "true"
            and os.getenv(ENV_CUDA_SUPPORTED) != "true"):
        return HWACCEL_VAAPI
    return hwaccel_args


STREAM_SCEHMA = Schema({
    Required("stream_format", default="rtsp"):
    Any("rtsp", "rtmp", "mjpeg"),
    Required("path"):
    All(str, Length(min=1)),
    Required("port"):
    All(int, Range(min=1)),
    Optional("width", default=None):
    Maybe(int),
    Optional("height", default=None):
    Maybe(int),
    Optional("fps", default=None):
    Maybe(All(int, Range(min=1))),
    Optional("input_args", default=None):
    Maybe(list),
    Optional("hwaccel_args", default=CAMERA_HWACCEL_ARGS):
    check_for_hwaccels,
    Optional("codec", default=""):
    str,
    Optional("audio_codec", default="unset"):
    Maybe(str),
    Optional("rtsp_transport", default="tcp"):
    Any("tcp", "udp", "udp_multicast", "http"),
    Optional("filter_args", default=[]):