1
#!/usr/bin/env python
2
# Copyright 2019-2020 AstroLab Software
3
# Author: Julien Peloton
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16 4
import yaml
17 4
import os
18

19 4
from fink_client.tester import regular_unit_tests
20

21 4
_ROOTDIR = os.path.join(os.environ['HOME'], ".finkclient")
22 4
_CREDNAME = "credentials.yml"
23

24 4
def write_credentials(dict_file: dict, verbose: bool = False, tmp: bool = False):
25
    """ Store user credentials on the computer.
26

27
    To get your credentials, contact Fink admins or fill the registration form:
28
        https://forms.gle/2td4jysT4e9pkf889
29

30
    Parameters
31
    ----------
32
    dict_file: dict
33
        Dictionnary containing user credentials.
34
    verbose: bool, optional
35
        If True, print the credentials location. Default is False.
36
    tmp: bool, optional
37
        If True, store the credentials under /tmp. Default is False.
38

39
    Examples
40
    ----------
41
    >>> conf = {
42
    ...   'username': 'test',
43
    ...   'password': None,
44
    ...   'mytopics': ['rrlyr'],
45
    ...   'servers': 'localhost:9093',
46
    ...   'group_id': 'test_group',
47
    ...   'maxtimeout': 10
48
    ... }
49
    >>> write_credentials(conf, verbose=False, tmp=True)
50
    """
51 4
    if tmp:
52 4
        ROOTDIR = "/tmp"
53
    else:
54 0
        ROOTDIR = _ROOTDIR
55

56
    # check there are no missing information
57 4
    mandatory_keys = [
58
        'username', 'password', 'group_id',
59
        'mytopics', 'servers',
60
        'maxtimeout'
61
    ]
62 4
    for k in mandatory_keys:
63 4
        assert k in dict_file.keys(), 'You need to specify {}'.format(k)
64

65
    # Create the folder if it does not exist
66 4
    os.makedirs(ROOTDIR, exist_ok=True)
67

68
    # Store data into yml file
69 4
    with open(os.path.join(ROOTDIR, _CREDNAME), 'w') as f:
70 4
        yaml.dump(dict_file, f)
71

72 4
    if verbose:
73 0
        print('Credentials stored at {}/{}'.format(ROOTDIR, _CREDNAME))
74

75 4
def load_credentials(tmp: bool = False) -> dict:
76
    """ Load fink-client credentials.
77

78
    Parameters
79
    ----------
80
    tmp: bool, optional
81
        If True, load the credentials from /tmp. Default is False.
82

83
    Returns
84
    --------
85
    creds: dict
86
        Dictionnary containing user credentials.
87

88
    Examples
89
    ----------
90
    >>> conf_in = {
91
    ...   'username': 'test',
92
    ...   'password': None,
93
    ...   'mytopics': ['rrlyr'],
94
    ...   'servers': 'localhost:9093',
95
    ...   'group_id': 'test_group',
96
    ...   'maxtimeout': 10
97
    ... }
98
    >>> write_credentials(conf_in, verbose=False, tmp=True)
99
    >>> conf_out = load_credentials(tmp=True)
100

101
    If, however the credentials do not exist yet
102
    >>> os.remove('/tmp/credentials.yml')
103
    >>> conf = load_credentials(tmp=True) # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
104
    Traceback (most recent call last):
105
     ...
106
    OSError: No credentials found, did you register?
107
    To get your credentials, and use fink-client you need to:
108
      1. subscribe to one or more Fink streams at
109
        https://forms.gle/2td4jysT4e9pkf889
110
      2. run `fink_client_registration` to register
111

112
    """
113 4
    if tmp:
114 4
        ROOTDIR = "/tmp"
115
    else:
116 0
        ROOTDIR = _ROOTDIR
117

118 4
    path = os.path.join(ROOTDIR, _CREDNAME)
119

120 4
    if not os.path.exists(path):
121 4
        msg = """
122
        No credentials found, did you register?
123
        To get your credentials, and use fink-client you need to:
124
          1. subscribe to one or more Fink streams at
125
            https://forms.gle/2td4jysT4e9pkf889
126
          2. run `fink_client_registration` to register
127
        """
128 4
        raise IOError(msg)
129

130 4
    with open(path) as f:
131 4
        creds = yaml.load(f, Loader=yaml.FullLoader)
132

133 4
    return creds
134

135

136 4
if __name__ == "__main__":
137
    """ Run the test suite """
138

139 4
    regular_unit_tests(globals())

Read our documentation on viewing source code .

Loading