Passed
Pull Request — master (#974)
by Konstantin
02:57
created

ocrd_network.deployment_config   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 88
Duplicated Lines 25 %

Importance

Changes 0
Metric Value
wmc 9
eloc 55
dl 22
loc 88
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A MongoConfig.__init__() 7 7 1
A HostConfig.__init__() 0 10 2
A ProcessingServerConfig.__init__() 0 14 4
A QueueConfig.__init__() 7 7 1
A WorkerConfig.__init__() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from typing import Dict
2
from yaml import safe_load
3
from ocrd_validators import ProcessingServerConfigValidator
4
from .deployment_utils import DeployType
5
6
__all__ = [
7
    'ProcessingServerConfig',
8
    'HostConfig',
9
    'WorkerConfig',
10
    'MongoConfig',
11
    'QueueConfig',
12
]
13
14
15
class ProcessingServerConfig:
16
    def __init__(self, config_path: str) -> None:
17
        # Load and validate the config
18
        with open(config_path) as fin:
19
            config = safe_load(fin)
20
        report = ProcessingServerConfigValidator.validate(config)
21
        if not report.is_valid:
22
            raise Exception(f'Processing-Server configuration file is invalid:\n{report.errors}')
23
24
        # Split the configurations
25
        self.mongo = MongoConfig(config['database'])
26
        self.queue = QueueConfig(config['process_queue'])
27
        self.hosts = []
28
        for host in config['hosts']:
29
            self.hosts.append(HostConfig(host))
30
31
32
class HostConfig:
33
    """Class to wrap information for all processing-worker-hosts.
34
35
    Config information and runtime information is stored here. This class
36
    should not do much but hold config information and runtime information. I
37
    hope to make the code better understandable this way. Deployer should still
38
    be the class who does things and this class here should be mostly passive
39
    """
40
41
    def __init__(self, config: dict) -> None:
42
        self.address = config['address']
43
        self.username = config['username']
44
        self.password = config.get('password', None)
45
        self.keypath = config.get('path_to_privkey', None)
46
        self.processors = []
47
        for worker in config['workers']:
48
            deploy_type = DeployType.from_str(worker['deploy_type'])
49
            self.processors.append(
50
                WorkerConfig(worker['name'], worker['number_of_instance'], deploy_type)
51
            )
52
53
54
class WorkerConfig:
55
    """
56
    Class wrapping information from config file for an OCR-D processor
57
    """
58
    def __init__(self, name: str, count: int, deploy_type: DeployType) -> None:
59
        self.name = name
60
        self.count = count
61
        self.deploy_type = deploy_type
62
63
64 View Code Duplication
class MongoConfig:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
65
    """ Class to hold information for Mongodb-Docker container
66
    """
67
68
    def __init__(self, config: Dict) -> None:
69
        self.address = config['address']
70
        self.port = int(config['port'])
71
        self.username = config['ssh']['username']
72
        self.keypath = config['ssh'].get('path_to_privkey', None)
73
        self.password = config['ssh'].get('password', None)
74
        self.credentials = (config['credentials']['username'], config['credentials']['password'])
75
76
77 View Code Duplication
class QueueConfig:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
78
    """ Class to hold information for RabbitMQ-Docker container
79
    """
80
81
    def __init__(self, config: Dict) -> None:
82
        self.address = config['address']
83
        self.port = int(config['port'])
84
        self.username = config['ssh']['username']
85
        self.keypath = config['ssh'].get('path_to_privkey', None)
86
        self.password = config['ssh'].get('password', None)
87
        self.credentials = (config['credentials']['username'], config['credentials']['password'])
88