config.py 8.28 KB
Newer Older
André Anjos's avatar
André Anjos committed
1
2
3
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

Samuel GAIST's avatar
Samuel GAIST committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
###################################################################################
#                                                                                 #
# Copyright (c) 2019 Idiap Research Institute, http://www.idiap.ch/               #
# Contact: beat.support@idiap.ch                                                  #
#                                                                                 #
# Redistribution and use in source and binary forms, with or without              #
# modification, are permitted provided that the following conditions are met:     #
#                                                                                 #
# 1. Redistributions of source code must retain the above copyright notice, this  #
# list of conditions and the following disclaimer.                                #
#                                                                                 #
# 2. Redistributions in binary form must reproduce the above copyright notice,    #
# this list of conditions and the following disclaimer in the documentation       #
# and/or other materials provided with the distribution.                          #
#                                                                                 #
# 3. Neither the name of the copyright holder nor the names of its contributors   #
# may be used to endorse or promote products derived from this software without   #
# specific prior written permission.                                              #
#                                                                                 #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND #
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   #
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE          #
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE    #
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL      #
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR      #
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER      #
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,   #
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE   #
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.            #
#                                                                                 #
###################################################################################


37
"""Configuration manipulation and display"""
André Anjos's avatar
André Anjos committed
38
39
40


import copy
André Anjos's avatar
André Anjos committed
41
import getpass
Samuel GAIST's avatar
Samuel GAIST committed
42
43
44
45
import logging
import os
import sys

46
47
48
import click
import simplejson

49
from .click_helper import AliasedGroup
Samuel GAIST's avatar
Samuel GAIST committed
50
from .decorators import verbosity_option
51

52
logger = logging.getLogger(__name__)
André Anjos's avatar
André Anjos committed
53

Samuel GAIST's avatar
Samuel GAIST committed
54
#  Default values for the command-line utility
André Anjos's avatar
André Anjos committed
55
DEFAULTS = {
Samuel GAIST's avatar
Samuel GAIST committed
56
57
58
59
60
61
62
    "platform": "https://www.beat-eu.org/platform/",
    "user": getpass.getuser(),
    "token": None,
    "prefix": os.path.realpath(os.path.join(os.curdir, "prefix")),
    "cache": "cache",
    "editor": None,
}
André Anjos's avatar
André Anjos committed
63

André Anjos's avatar
André Anjos committed
64

Samuel GAIST's avatar
Samuel GAIST committed
65
#  Documentation for the configuration parameters
André Anjos's avatar
André Anjos committed
66
DOC = {
Samuel GAIST's avatar
Samuel GAIST committed
67
68
69
70
71
72
73
    "platform": "Web address of the BEAT platform",
    "user": "User name for operations that create, delete or edit objects",
    "token": "Secret key of the user on the BEAT platform",
    "prefix": "Directory containing BEAT objects",
    "cache": "Directory to use for data caching (relative to prefix)",
    "editor": "Editor to be used to edit local files",
}
André Anjos's avatar
André Anjos committed
74
75
76


class Configuration(object):
Samuel GAIST's avatar
Samuel GAIST committed
77
    """Keeps track of configuration elements"""
André Anjos's avatar
André Anjos committed
78

79
    def __init__(self, args):
André Anjos's avatar
André Anjos committed
80

81
        self.files = [
Samuel GAIST's avatar
Samuel GAIST committed
82
83
            os.path.expanduser("~/.beatrc"),
            os.path.realpath("./.beatrc"),
André Anjos's avatar
André Anjos committed
84
        ]
André Anjos's avatar
André Anjos committed
85

86
87
88
        self.__data = copy.deepcopy(DEFAULTS)
        for k in self.files:
            if os.path.exists(k):
Samuel GAIST's avatar
Samuel GAIST committed
89
90
                with open(k, "rt") as f:
                    tmp = simplejson.load(f)
91
92
                self.__data.update(tmp)
                logger.info("Loaded configuration file `%s'", k)
André Anjos's avatar
André Anjos committed
93

94
        for key in DEFAULTS:
Samuel GAIST's avatar
Samuel GAIST committed
95
            self.__data[key] = args.get("--%s" % key) or self.__data[key]
André Anjos's avatar
André Anjos committed
96

97
98
    @property
    def path(self):
Samuel GAIST's avatar
Samuel GAIST committed
99
        """The directory for the prefix"""
André Anjos's avatar
André Anjos committed
100

Samuel GAIST's avatar
Samuel GAIST committed
101
        return self.__data["prefix"]
André Anjos's avatar
André Anjos committed
102

103
104
    @property
    def cache(self):
Samuel GAIST's avatar
Samuel GAIST committed
105
        """The directory for the cache"""
André Anjos's avatar
André Anjos committed
106

Samuel GAIST's avatar
Samuel GAIST committed
107
108
109
        if os.path.isabs(self.__data["cache"]):
            return self.__data["cache"]
        return os.path.join(self.__data["prefix"], self.__data["cache"])
André Anjos's avatar
André Anjos committed
110

111
112
    @property
    def database_paths(self):
Samuel GAIST's avatar
Samuel GAIST committed
113
        """A dict of paths for databases"""
Jaden's avatar
Jaden committed
114

Samuel GAIST's avatar
Samuel GAIST committed
115
        return dict((k, self.__data[k]) for k in self.__data if self.is_database_key(k))
Jaden's avatar
Jaden committed
116

117
    def set(self, key, value, local=False):
Samuel GAIST's avatar
Samuel GAIST committed
118
        """Sets or resets a field in the configuration"""
André Anjos's avatar
André Anjos committed
119

120
121
122
        if not self._is_valid_key(key):
            logger.error("Don't know about parameter `%s'", key)
            sys.exit(1)
André Anjos's avatar
André Anjos committed
123

124
125
126
127
        if value is not None:
            self.__data[key] = value
        elif key in DEFAULTS:
            self.__data[key] = DEFAULTS[key]
André Anjos's avatar
André Anjos committed
128

129
        self.save(local)
André Anjos's avatar
André Anjos committed
130

131
    def save(self, local=False):
Samuel GAIST's avatar
Samuel GAIST committed
132
        """Saves contents to configuration file
André Anjos's avatar
André Anjos committed
133

134
        Parameters:
André Anjos's avatar
André Anjos committed
135

Theophile GENTILHOMME's avatar
Theophile GENTILHOMME committed
136
137
138
139
            local: bool, Optional
                if set to ``True``, then save
                configuration values to local configuration file (typically
                ``.beatrc``)
Samuel GAIST's avatar
Samuel GAIST committed
140
        """
André Anjos's avatar
André Anjos committed
141

142
143
144
        path = self.files[0]
        if local:
            path = self.files[1]
André Anjos's avatar
André Anjos committed
145

146
        flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC
Samuel GAIST's avatar
Samuel GAIST committed
147
148
149
150
151
152
        with os.fdopen(os.open(path, flags, 0o600), "wt") as f:
            f.write(
                simplejson.dumps(
                    self.__data, sort_keys=True, indent=4, separators=(",", ": ")
                )
            )
André Anjos's avatar
André Anjos committed
153

154
155
    def _is_valid_key(self, key):
        return key in DEFAULTS or self.is_database_key(key)
Jaden's avatar
Jaden committed
156

157
    def is_database_key(self, key):
Samuel GAIST's avatar
Samuel GAIST committed
158
        return key.startswith("database/")
André Anjos's avatar
André Anjos committed
159

160
161
    def __str__(self):
        return simplejson.dumps(
Samuel GAIST's avatar
Samuel GAIST committed
162
            self.__data, sort_keys=True, indent=4, separators=(",", ": ")
163
        )
André Anjos's avatar
André Anjos committed
164

165
166
    def as_dict(self):
        return copy.copy(self.__data)
André Anjos's avatar
André Anjos committed
167

168
169
    def __getattr__(self, key):
        return self.__data[key]
170
171


172
@click.group(cls=AliasedGroup)
173
174
175
176
177
@verbosity_option()
@click.pass_context
def config(ctx):
    """The manager for beat cmdline configuration."""
    pass
André Anjos's avatar
André Anjos committed
178

Samuel GAIST's avatar
Samuel GAIST committed
179

180
181
182
183
@config.command()
@click.pass_context
def show(ctx):
    """Shows the configuration.
André Anjos's avatar
André Anjos committed
184

185
186
    Lists the configuration after resolving defaults and saved variables
    """
Samuel GAIST's avatar
Samuel GAIST committed
187
    click.echo(ctx.meta["config"])
André Anjos's avatar
André Anjos committed
188
189


190
@config.command()
Samuel GAIST's avatar
Samuel GAIST committed
191
@click.argument("key")
192
193
194
@click.pass_context
def get(ctx, key):
    """Prints out the contents of a single field.
André Anjos's avatar
André Anjos committed
195

196
197
198
199
    To query for a specific parameter:

    $ %(prog)s config get token
    1234567890abcdef1234567890abcde
André Anjos's avatar
André Anjos committed
200
201


202
203
204
205
206
207
208
209
210
211
212
    \b
    Arguments
    ---------
    key : str
        The key to return its value from the configuration.

    \b
    Fails
    -----
    * If the key is not found.
    """
Samuel GAIST's avatar
Samuel GAIST committed
213
    value = getattr(ctx.meta["config"], key)
214
215
    if value is None:
        # Exit the command line with ClickException in case of errors.
Samuel GAIST's avatar
Samuel GAIST committed
216
        raise click.ClickException("The requested key `{}' does not exist".format(key))
217
218
219
220
    click.echo(value)


@config.command()
Samuel GAIST's avatar
Samuel GAIST committed
221
222
223
224
225
226
227
228
@click.argument("args", nargs=-1)
@click.option(
    "--local/--not-local",
    default=False,
    help="Save values on the "
    "local configuration file (.beatrc) instead of using the global "
    "file (~/.beatrc)",
)
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
@click.pass_context
def set(ctx, args, local):
    """Sets the value for a key.

    Sets a specific known field to a value
    To save a different user name token to a file and save results locally - i.e.
    doesn't override the global configuration file (notice you can pass multiple
    parameters at once using key-value pairs):

    \b
    Arguments
    ---------
    key : str
        The key to set the value for.
    value : str
        The value of the key.
    local : bool
        Save locally or not

    \b
    Fails
    -----
    * If something goes wrong.
    """

    if len(args) % 2 != 0:
Samuel GAIST's avatar
Samuel GAIST committed
255
        raise click.BadParameter("You must provide pair(s) of key/value")
256
257
258

    try:
        for idx in range(0, len(args), 2):
Samuel GAIST's avatar
Samuel GAIST committed
259
            ctx.meta["config"].set(args[idx], args[idx + 1], local)
260
261
    except Exception:
        raise click.ClickException("Failed to change the configuration.")