summaryrefslogtreecommitdiff
path: root/talersurvey/talerconfig.py
blob: 1a33294ed445294f68e7f789f6187e8b2f0a7b5b (plain)
1
2
3
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
##
# This file is part of TALER
# (C) 2016 INRIA
#
# TALER is free software; you can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free Software
# Foundation; either version 3, or (at your option) any later version.
#
# TALER is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
#
# @author Florian Dold
# @author Marcello Stanisci
# @brief Parse GNUnet-style configurations in pure Python

import logging
import collections
import os
import weakref
import sys
import re
from typing import Callable, Any

LOGGER = logging.getLogger(__name__)

__all__ = ["TalerConfig"]

TALER_DATADIR = None

try:
    # not clear if this is a good idea ...
    from talerpaths import TALER_DATADIR as t
    TALER_DATADIR = t
except ImportError:
    pass


##
# Exception class for a any configuration error.
class ConfigurationError(Exception):
    pass


##
# Exception class for malformed strings having with parameter
# expansion.
class ExpansionSyntaxError(Exception):
    pass


##
# Do shell-style parameter expansion.
# Supported syntax:
#  - ${X}
#  - ${X:-Y}
#  - $X
#
# @param var entire config value that might contain a parameter
#        to expand.
# @param getter function that is in charge of returning _some_
#        value to be used in place of the parameter to expand.
#        Typically, the replacement is searched first under the
#        PATHS section of the current configuration, or (if not
#        found) in the environment.
#
# @return the expanded config value.
def expand(var: str, getter: Callable[[str], str]) -> str:
    pos = 0
    result = ""
    while pos != -1:
        start = var.find("$", pos)
        if start == -1:
            break
        if var[start:].startswith("${"):
            balance = 1
            end = start + 2
            while balance > 0 and end < len(var):
                balance += {"{": 1, "}": -1}.get(var[end], 0)
                end += 1
            if balance != 0:
                raise ExpansionSyntaxError("unbalanced parentheses")
            piece = var[start + 2:end - 1]
            if piece.find(":-") > 0:
                varname, alt = piece.split(":-", 1)
                replace = getter(varname)
                if replace is None:
                    replace = expand(alt, getter)
            else:
                varname = piece
                replace = getter(varname)
                if replace is None:
                    replace = var[start:end]
        else:
            end = start + 2
            while end < len(var) and var[start + 1:end + 1].isalnum():
                end += 1
            varname = var[start + 1:end]
            replace = getter(varname)
            if replace is None:
                replace = var[start:end]
        result = result + replace
        pos = end

    return result + var[pos:]


##
# A configuration entry.
class Entry:

    ##
    # Init constructor.
    #
    # @param self the object itself.
    # @param config reference to a configuration object - FIXME
    #        define "configuration object".
    # @param section name of the config section where this entry
    #        got defined.
    # @param option name of the config option associated with this
    #        entry.
    # @param kwargs keyword arguments that hold the value / filename
    #        / line number of this current option.
    def __init__(self, config, section: str, option: str, **kwargs) -> None:
        self.value = kwargs.get("value")
        self.filename = kwargs.get("filename")
        self.lineno = kwargs.get("lineno")
        self.section = section
        self.option = option
        self.config = weakref.ref(config)

    ##
    # XML representation of this entry.
    #
    # @param self the object itself.
    # @return XML string holding all the relevant information
    #         for this entry.
    def __repr__(self) -> str:
        return "<Entry section=%s, option=%s, value=%s>" \
               % (self.section, self.option, repr(self.value),)

    ##
    # Return the value for this entry, as is.
    #
    # @param self the object itself.
    # @return the config value.
    def __str__(self) -> Any:
        return self.value

    ##
    # Return entry value, accepting defaults.
    #
    # @param self the object itself
    # @param default default value to return if none was found.
    # @param required indicate whether the value was required or not.
    #        If the value was required, but was not found, an exception
    #        is found.
    # @param warn if True, outputs a warning message if the value was
    #        not found -- regardless of it being required or not.
    # @return the value, or the given @a default, if not found.
    def value_string(self, default=None, required=False, warn=False) -> str:
        if required and self.value is None:
            raise ConfigurationError("Missing required option '%s' in section '%s'" \
                                     % (self.option.upper(), self.section.upper()))
        if self.value is None:
            if warn:
                if default is not None:
                    LOGGER.warning(
                        "Configuration is missing option '%s' in section '%s',\
                                   falling back to '%s'", self.option,
                        self.section, default
                    )
                else:
                    LOGGER.warning(
                        "Configuration ** is missing option '%s' in section '%s'",
                        self.option.upper(), self.section.upper()
                    )
            return default
        return self.value

    ##
    # Return entry value as a _int_.  Raise exception if the
    # value is not convertible to a integer.
    #
    # @param self the object itself
    # @param default currently ignored.
    # @param required currently ignored.
    # @param warn currently ignored.
    # @return the value, or the given @a default, if not found.
    def value_int(self, default=None, required=False, warn=False) -> int:
        value = self.value_string(default, warn, required)
        if value is None:
            return None
        try:
            return int(value)
        except ValueError:
            raise ConfigurationError("Expected number for option '%s' in section '%s'" \
                                     % (self.option.upper(), self.section.upper()))

    ##
    # Fetch value to substitute to expansion variables.
    #
    # @param self the object itself.
    # @param key the value's name to lookup.
    # @return the value, if found, None otherwise.
    def _getsubst(self, key: str) -> Any:
        value = self.config()["paths"][key].value
        if value is not None:
            return value
        value = os.environ.get(key)
        if value is not None:
            return value
        return None

    ##
    # Fetch the config value that should be a filename,
    # taking care of invoking the variable-expansion logic first.
    #
    # @param self the object itself.
    # @param default currently ignored.
    # @param required currently ignored.
    # @param warn currently ignored.
    # @return the (expanded) filename.
    def value_filename(self, default=None, required=False, warn=False) -> str:
        value = self.value_string(default, required, warn)
        if value is None:
            return None
        return expand(value, self._getsubst)

    ##
    # Give the filename and line number of this config entry.
    #
    # @param self this object.
    # @return <filename>:<linenumber>, or "<unknown>" if one
    #         is not known.
    def location(self) -> str:
        if self.filename is None or self.lineno is None:
            return "<unknown>"
        return "%s:%s" % (self.filename, self.lineno)


##
# Represent a section by inheriting from 'defaultdict'.
class OptionDict(collections.defaultdict):

    ##
    # Init constructor.
    #
    # @param self the object itself
    # @param config the "config" object -- typically a @a TalerConfig instance.
    # @param section_name the section name to assign to this object.
    def __init__(self, config, section_name: str) -> None:
        self.config = weakref.ref(config)
        self.section_name = section_name
        super().__init__()

    ##
    # Logic to run when a non-existent key is dereferenced.
    # Just create and return a empty config @a Entry.  Note
    # that the freshly created entry will nonetheless put
    # under the accessed key (that *does* become existent
    # afterwards).
    #
    # @param self the object itself.
    # @param key the key attempted to be accessed.
    # @return the no-value entry.
    def __missing__(self, key: str) -> Entry:
        entry = Entry(self.config(), self.section_name, key)
        self[key] = entry
        return entry

    ##
    # Attempt to fetch one value from the object.
    #
    # @param self the object itself.
    # @param chunk the key (?) that is tried to access.
    # @return the object, if it exists, or a freshly created
    #         (empty) one, if it doesn't exist.
    def __getitem__(self, chunk: str) -> Entry:
        return super().__getitem__(chunk.lower())

    ##
    # Set one value into the object.
    #
    # @param self the object itself.
    # @param chunk key under which the value is going to be set.
    # @param value value to set the @a chunk to.
    def __setitem__(self, chunk: str, value: Entry) -> None:
        super().__setitem__(chunk.lower(), value)


##
# Collection of all the (@a OptionDict) sections.
class SectionDict(collections.defaultdict):

    ##
    # Automatically invoked when a missing section is
    # dereferenced.  It creates the missing - empty - section.
    #
    # @param self the object itself.
    # @param key the dereferenced section name.
    # @return the freshly created section.
    def __missing__(self, key):
        value = OptionDict(self, key)
        self[key] = value
        return value

    ##
    # Attempt to retrieve a section.
    #
    # @param self the object itself.
    # @param chunk the section name.
    def __getitem__(self, chunk: str) -> OptionDict:
        return super().__getitem__(chunk.lower())

    ##
    # Set a section.
    #
    # @param self the object itself.
    # @param chunk the section name to set.
    # @param value the value to set under that @a chunk.
    def __setitem__(self, chunk: str, value: OptionDict) -> None:
        super().__setitem__(chunk.lower(), value)


##
# One loaded taler configuration, including base configuration
# files and included files.
class TalerConfig:

    ##
    # Init constructor..
    #
    # @param self the object itself.
    def __init__(self) -> None:
        self.sections = SectionDict()  # just plain dict

    ##
    # Load a configuration file, instantiating a config object.
    #
    # @param filename the filename where to load the configuration
    #        from.  If None, it defaults "taler.conf".
    # @param load_defaults if True, then defaults values are loaded
    #        (from canonical directories like "<prefix>/share/config.d/taler/")
    #        before the actual configuration file.  This latter then
    #        can override some/all the defaults.
    # @return the config object.
    @staticmethod
    def from_file(filename=None, load_defaults=True):
        cfg = TalerConfig()
        if filename is None:
            xdg = os.environ.get("XDG_CONFIG_HOME")
            if xdg:
                filename = os.path.join(xdg, "taler.conf")
            else:
                filename = os.path.expanduser("~/.config/taler.conf")
            print("Loading default config: (%s)" % filename)
        if load_defaults:
            cfg.load_defaults()
        cfg.load_file(os.path.expanduser(filename))
        return cfg

    ##
    # Get a string value from the config.
    #
    # @param self the config object itself.
    # @param section the section to fetch the value from.
    # @param option the value's option name.
    # @param kwargs dict argument with instructions about
    #        the value retrieval logic.
    # @return the wanted string (or a default / exception if
    #         a error occurs).
    def value_string(self, section, option, **kwargs) -> str:
        return self.sections[section][option].value_string(
            kwargs.get("default"), kwargs.get("required"), kwargs.get("warn")
        )

    ##
    # Get a value from the config that should be a filename.
    # The variable expansion for the path's components is internally managed.
    #
    # @param self the config object itself.
    # @param section the section to fetch the value from.
    # @param option the value's option name.
    # @param kwargs dict argument with instructions about
    #        the value retrieval logic.
    # @return the wanted filename (or a default / exception if
    #         a error occurs).
    def value_filename(self, section, option, **kwargs) -> str:
        return self.sections[section][option].value_filename(
            kwargs.get("default"), kwargs.get("required"), kwargs.get("warn")
        )

    ##
    # Get a integer value from the config.
    #
    # @param self the config object itself.
    # @param section the section to fetch the value from.
    # @param option the value's option name.
    # @param kwargs dict argument with instructions about
    #        the value retrieval logic.
    # @return the wanted integer (or a default / exception if
    #         a error occurs).
    def value_int(self, section, option, **kwargs) -> int:
        return self.sections[section][option].value_int(
            kwargs.get("default"), kwargs.get("required"), kwargs.get("warn")
        )

    ##
    # Load default values from canonical locations.
    #
    # @param self the object itself.
    def load_defaults(self) -> None:
        base_dir = os.environ.get("TALER_BASE_CONFIG")
        if base_dir:
            self.load_dir(base_dir)
            return
        prefix = os.environ.get("TALER_PREFIX")
        if prefix:
            tmp = os.path.split(os.path.normpath(prefix))
            if re.match("lib", tmp[1]):
                prefix = tmp[0]
            self.load_dir(os.path.join(prefix, "share/taler/config.d"))
            return
        if TALER_DATADIR:
            self.load_dir(os.path.join(TALER_DATADIR, "share/taler/config.d"))
            return
        LOGGER.warning("no base directory found")

    ##
    # Load configuration from environment variable
    # TALER_CONFIG_FILE or from default location if the
    # variable is not set.
    #
    # @param args currently unused.
    # @param kwargs kwargs for subroutine @a from_file.
    # @return freshly instantiated config object.
    @staticmethod
    def from_env(*args, **kwargs):
        filename = os.environ.get("TALER_CONFIG_FILE")
        return TalerConfig.from_file(filename, *args, **kwargs)

    ##
    # Load config values from _each_ file found in a directory.
    #
    # @param self the object itself.
    # @param dirname the directory to crawl in the look for config files.
    def load_dir(self, dirname) -> None:
        try:
            files = os.listdir(dirname)
        except FileNotFoundError:
            LOGGER.warning("can't read config directory '%s'", dirname)
            return
        for file in files:
            if not file.endswith(".conf"):
                continue
            self.load_file(os.path.join(dirname, file))

    ##
    # Load config values from a file.
    #
    # @param filename config file to take the values from.
    def load_file(self, filename) -> None:
        sections = self.sections
        try:
            with open(filename, "r") as file:
                lineno = 0
                current_section = None
                for line in file:
                    lineno += 1
                    line = line.strip()
                    if line == "":
                        # empty line
                        continue
                    if line.startswith("#"):
                        # comment
                        continue
                    if line.startswith("@INLINE@"):
                        pair = line.split()
                        if 2 != len(pair):
                            LOGGER.error(
                                "invalid inlined config filename given ('%s')" %
                                line
                            )
                            continue
                        if pair[1].startswith("/"):
                            self.load_file(pair[1])
                        else:
                            self.load_file(
                                os.path.join(
                                    os.path.dirname(filename), pair[1]
                                )
                            )
                        continue
                    if line.startswith("["):
                        if not line.endswith("]"):
                            LOGGER.error(
                                "invalid section header in line %s: %s", lineno,
                                repr(line)
                            )
                        section_name = line.strip("[]").strip().strip('"')
                        current_section = section_name
                        continue
                    if current_section is None:
                        LOGGER.error(
                            "option outside of section in line %s: %s", lineno,
                            repr(line)
                        )
                        continue
                    pair = line.split("=", 1)
                    if len(pair) != 2:
                        LOGGER.error(
                            "invalid option in line %s: %s", lineno, repr(line)
                        )
                    key = pair[0].strip()
                    value = pair[1].strip()
                    if value.startswith('"'):
                        value = value[1:]
                        if not value.endswith('"'):
                            LOGGER.error(
                                "mismatched quotes in line %s: %s", lineno,
                                repr(line)
                            )
                        else:
                            value = value[:-1]
                    entry = Entry(
                        self.sections,
                        current_section,
                        key,
                        value=value,
                        filename=filename,
                        lineno=lineno
                    )
                    sections[current_section][key] = entry
        except FileNotFoundError:
            # not logging here, as this interests the final user mostly.
            print("Configuration file (%s) not found" % filename)
            sys.exit(3)

    ##
    # Dump the textual representation of a config object.
    #
    # Format:
    #
    # [section]
    # option = value # FIXME (what is location?)
    #
    # @param self the object itself, that will be dumped.
    def dump(self) -> None:
        for kv_section in self.sections.items():
            print("[%s]" % (kv_section[1].section_name, ))
            for kv_option in kv_section[1].items():
                print("%s = %s # %s" % \
                      (kv_option[1].option,
                       kv_option[1].value,
                       kv_option[1].location()))

    ##
    # Return a whole section from this object.
    #
    # @param self the object itself.
    # @param chunk name of the section to return.
    # @return the section - note that if the section is
    #         not found, a empty one will created on the fly,
    #         then set under 'chunk', and returned.
    def __getitem__(self, chunk: str) -> OptionDict:
        if isinstance(chunk, str):
            return self.sections[chunk]
        raise TypeError("index must be string")


if __name__ == "__main__":
    import argparse

    PARSER = argparse.ArgumentParser()
    PARSER.add_argument(
        "--section", "-s", dest="section", default=None, metavar="SECTION"
    )
    PARSER.add_argument(
        "--option", "-o", dest="option", default=None, metavar="OPTION"
    )
    PARSER.add_argument(
        "--config", "-c", dest="config", default=None, metavar="FILE"
    )
    PARSER.add_argument(
        "--filename",
        "-f",
        dest="expand_filename",
        default=False,
        action='store_true'
    )
    ARGS = PARSER.parse_args()

    TC = TalerConfig.from_file(ARGS.config)

    if ARGS.section is not None and ARGS.option is not None:
        if ARGS.expand_filename:
            X = TC.value_filename(ARGS.section, ARGS.option)
        else:
            X = TC.value_string(ARGS.section, ARGS.option)
        if X is not None:
            print(X)
    else:
        TC.dump()