~landscape/landscape-client/landscape-client-12.04-0ubuntu0.12.04.0

« back to all changes in this revision

Viewing changes to landscape/ui/controller/configuration.py

  • Committer: Andreas Hasenack
  • Date: 2012-03-19 12:39:10 UTC
  • Revision ID: andreas@canonical.com-20120319123910-xfzd1k5skb2c2emu
Merged with trunk r527, updated changelog from Thomas

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from landscape.ui.constants import NOT_MANAGED, CANONICAL_MANAGED
 
2
import logging
 
3
 
 
4
from landscape.ui.model.registration.proxy import RegistrationProxy
 
5
from landscape.ui.model.configuration.state import StateError
 
6
 
 
7
 
 
8
class ConfigControllerLockError(Exception):
 
9
    pass
 
10
 
 
11
 
 
12
class ConfigController(object):
 
13
    """
 
14
    L{ConfigContoller} defines actions to take against a configuration object,
 
15
    providing starting values from the file, allowing them to be changed
 
16
    transiently, reverted or committed.
 
17
    """
 
18
 
 
19
    DEFAULT_DEDICATED_ACCOUNT_NAME = "standalone"
 
20
 
 
21
    def __init__(self, configuration):
 
22
        self._observers = []
 
23
        self._configuration = configuration
 
24
        self._initialised = True
 
25
 
 
26
    def __getattr__(self, name):
 
27
        if name in self.__dict__:
 
28
            return self.__dict__[name]
 
29
        else:
 
30
            return getattr(self._configuration, name)
 
31
 
 
32
    def __setattr__(self, name, value):
 
33
        # this test allows attributes to be set in the __init__ method
 
34
        if not '_initialised' in self.__dict__:
 
35
            return object.__setattr__(self, name, value)
 
36
        if name in ConfigController.__dict__:
 
37
            return object.__setattr__(self, name, value)
 
38
        else:
 
39
            try:
 
40
                setattr(self._configuration, name, value)
 
41
                self._configuration.modify()
 
42
            except AttributeError:
 
43
                return object.__setattr__(self, name, value)
 
44
            else:
 
45
                self._configuration.modify()
 
46
 
 
47
    def load(self):
 
48
        """
 
49
        Load the initial data from the configuration.
 
50
        """
 
51
        return self._configuration.load_data()
 
52
 
 
53
    def revert(self):
 
54
        """
 
55
        Revert settings to those the configuration object originally found.
 
56
        """
 
57
        try:
 
58
            self._configuration.revert()
 
59
        except StateError:
 
60
            # We probably don't care.
 
61
            logging.info("landscape-client-settings-ui reverted with no "
 
62
                         "changes to revert.")
 
63
 
 
64
    def persist(self, on_notify, on_error, on_succeed, on_fail):
 
65
        "Persist settings via the configuration object."
 
66
        try:
 
67
            self._configuration.persist()
 
68
        except StateError:
 
69
            # We probably don't care.
 
70
            logging.info("landscape-client-settings-ui committed with no "
 
71
                         "changes to commit.")
 
72
        if self._configuration.management_type == NOT_MANAGED:
 
73
            self.disable(on_notify, on_succeed, on_fail)
 
74
        else:
 
75
            self.register(on_notify, on_error, on_succeed, on_fail)
 
76
 
 
77
    def register(self, notify_method, error_method, succeed_method,
 
78
                 fail_method):
 
79
        """
 
80
        Perform registration using the L{RegistrationProxy}.
 
81
        """
 
82
 
 
83
        def registration_fail_wrapper():
 
84
            fail_method(action="Registering client")
 
85
 
 
86
        def registration_succeed_wrapper():
 
87
            succeed_method(action="Registering client")
 
88
 
 
89
        registration = RegistrationProxy(
 
90
            on_register_notify=notify_method,
 
91
            on_register_error=error_method,
 
92
            on_register_succeed=registration_succeed_wrapper,
 
93
            on_register_fail=registration_fail_wrapper)
 
94
        if self._configuration.management_type == CANONICAL_MANAGED:
 
95
            notify_method("Attempting to register at %s" %
 
96
                          self._configuration.hosted_landscape_host)
 
97
        else:
 
98
            notify_method("Attempting to register at %s" %
 
99
                          self._configuration.local_landscape_host)
 
100
        registration.register(self._configuration.get_config_filename())
 
101
        registration.exit()
 
102
 
 
103
    def disable(self, notify_method, succeed_method, fail_method):
 
104
        """
 
105
        Disable landscape client via the L{RegistrationProxy}.
 
106
        """
 
107
 
 
108
        def disabling_fail_wrapper():
 
109
            fail_method(action="Disabling client")
 
110
 
 
111
        def disabling_succeed_wrapper():
 
112
            succeed_method(action="Disabling client")
 
113
 
 
114
        registration = RegistrationProxy(
 
115
            on_disable_succeed=disabling_succeed_wrapper,
 
116
            on_disable_fail=disabling_fail_wrapper)
 
117
        notify_method("Attempting to disable landscape client.")
 
118
        registration.disable()
 
119
        registration.exit()