Completed
Push — master ( 8a9714...a444b5 )
by Jace
9s
created

Data.prune_status()   B

Complexity

Conditions 5

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 13
ccs 8
cts 10
cp 0.8
crap 5.2
rs 8.5454
c 0
b 0
f 0
1
"""Data structures that combine all program data."""
2
3 1
import logging
4
5 1
import yorm
0 ignored issues
show
Configuration introduced by Jace Browning
The import yorm could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
6
7 1
from .config import ProgramConfig
8 1
from .status import ProgramStatus
9
10
11 1
log = logging.getLogger(__name__)
12
13
14 1
@yorm.attr(config=ProgramConfig)
15 1
@yorm.attr(status=ProgramStatus)
16
class Data:
17
    """Primary wrapper for all settings."""
18
19 1
    def __init__(self):
20 1
        self.config = ProgramConfig()
21 1
        self.status = ProgramStatus()
22 1
        self._last_counter = self.status.counter
23
24 1
    def __repr__(self):
25 1
        return "settings"
26
27 1
    @property
28
    def modified(self):
29 1
        changed = self.status.counter != self._last_counter
30 1
        self._last_counter = self.status.counter
31 1
        return changed
32
33 1
    @staticmethod
34
    def prune_status(config, status):
35
        """Remove undefined applications and computers."""
36 1
        log.info("Cleaning up applications and computers...")
37 1
        for appstatus in status.applications.copy():
38 1
            if not config.applications.find(appstatus.application):
39
                status.applications.remove(appstatus)
40
                log.info("Removed application: %s", appstatus)
41
            else:
42 1
                for computerstate in appstatus.computers.copy():
43 1
                    if not config.computers.find(computerstate.computer):
44 1
                        appstatus.computers.remove(computerstate)
45 1
                        log.info("Removed computer: %s", computerstate)
46
47 1
    @staticmethod
48
    def queue_all_applications(config, status, computer):
49
        """Queue applications for launch."""
50
        log.info("Queuing applications for launch...")
51
        for application in config.applications:
52
            if application.auto_queue:
53
                log.debug("Queuing %s on %s...", application, computer)
54
                status.queue(application, computer)
55
56 1
    @staticmethod
57
    def launch_queued_applications(config, status, computer, manager):
58
        """Launch applications that have been queued."""
59 1
        log.info("Launching queued applications...")
60 1
        for app_status in status.applications:
61 1
            if app_status.next:
62
                application = config.applications.get(app_status.application)
63
                show_queued(application, app_status.next)
64
                if app_status.next == computer:
65
                    latest = status.get_latest(application)
66
                    if latest in (computer, None) or application.no_wait:
67
                        if not manager.is_running(application):
68
                            manager.start(application)
69
                        app_status.next = None
70
                    else:
71
                        show_waiting(application, latest)
72
                elif manager.is_running(application):
73
                    manager.stop(application)
74
75 1
    @staticmethod
76
    def close_all_applications(config, manager):
77
        """Close all applications running on this computer."""
78
        log.info("Closing all applications on this computer...")
79
        for application in config.applications:
80
            manager.stop(application)
81
82 1
    @staticmethod
83
    def update_status(config, status, computer, manager):
84
        """Update each application's status."""
85 1
        log.info("Recording application status...")
86 1
        for application in config.applications:
87 1
            if manager.is_running(application):
88 1
                latest = status.get_latest(application)
89 1
                if computer != latest:
90 1
                    if status.is_running(application, computer):
91
                        # case 1: application just launched remotely
92 1
                        manager.stop(application)
93 1
                        status.stop(application, computer)
94 1
                        show_running(application, latest)
95 1
                        show_stopped(application, computer)
96
                    else:
97
                        # case 2: application just launched locally
98 1
                        status.start(application, computer)
99 1
                        show_running(application, computer)
100
                else:
101
                    # case 3: application already running locally
102 1
                    pass
103
            else:
104 1
                if status.is_running(application, computer):
105
                    # case 4: application just closed locally
106 1
                    status.stop(application, computer)
107 1
                    show_stopped(application, computer)
108
                else:
109
                    # case 5: application already closed locally
110 1
                    pass
111
112
113 1
def show_queued(application, computer):
114
    """Display the state of a queued application."""
115
    print("{} is queued for {}".format(application, computer))
116
117
118 1
def show_waiting(application, computer):
119
    """Display the old state of a running application."""
120
    print("{} is still running on {}".format(application, computer))
121
122
123 1
def show_running(application, computer):
124
    """Display the new state of a running application."""
125 1
    print("{} is now running on {}".format(application, computer))
126
127
128 1
def show_started(application, computer):
129
    """Display the new state of a started application."""
130
    print("{} is now started on {}".format(application, computer))
131
132
133 1
def show_stopped(application, computer):
134
    """Display the new state of a stopped application."""
135
    print("{} is now stopped on {}".format(application, computer))
136