GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 8fa80d...f4cf2c )
by dup
01:36
created

Conf.extract_variable_from_site()   A

Complexity

Conditions 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nop 4
dl 0
loc 17
rs 9.95
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# -*- coding: utf8 -*-
3
#
4
#  configuration.py : configuration related class and fonctions for
5
#                     versions.py modules.
6
#
7
#  (C) Copyright 2016 - 2018 Olivier Delhomme
8
#  e-mail : [email protected]
9
#
10
#  This program is free software; you can redistribute it and/or modify
11
#  it under the terms of the GNU General Public License as published by
12
#  the Free Software Foundation; either version 3, or (at your option)
13
#  any later version.
14
#
15
#  This program is distributed in the hope that it will be useful,
16
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
#  GNU General Public License for more details.
19
#
20
#  You should have received a copy of the GNU General Public License
21
#  along with this program; if not, write to the Free Software Foundation,
22
#  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23
#
24
25
import codecs
26
import argparse
27
import os
28
import errno
29
import yaml
30
31
__author__ = "Olivier Delhomme <[email protected]>"
32
__date__ = "23.04.2019"
33
__version__ = "1.5.4-rc1"
34
35
36
def make_directories(path):
37
    """
38
    Makes all directories in path if possible. It is not an error if
39
    path already exists.
40
    """
41
42
    try:
43
        os.makedirs(path)
44
45
    except OSError as exc:
46
47
        if exc.errno != errno.EEXIST or os.path.isdir(path) is not True:
48
            raise
49
50
# End of make_directories() function
51
52
class Conf:
53
    """
54
    Class to store configuration of the program and check version.
55
    """
56
57
    config_dir = ''
58
    local_dir = ''
59
    config_filename = ''
60
    description = {}
61
    options = None
62
63
    def __init__(self):
64
        """
65
        Inits the class
66
        """
67
        self.config_dir = os.path.expanduser("~/.config/versions")
68
        self.local_dir = os.path.expanduser("~/.local/versions")
69
        self.config_filename = ''  # At this stage we do not know if a filename has been set on the command line
70
        self.description = {}
71
        self.options = None
72
73
        # Make sure that the directories exists
74
        make_directories(self.config_dir)
75
        make_directories(self.local_dir)
76
77
        self._get_command_line_arguments()
78
79
    # End of init() function
80
81
82
    def load_yaml_from_config_file(self, filename):
83
        """
84
        Loads definitions from the YAML config file filename
85
        >>> conf = Conf()
86
        >>> conf.load_yaml_from_config_file('./bad_formatted.yaml')
87
        Error in configuration file ./bad_formatted.yaml at position: 9:1
88
        """
89
90
        config_file = codecs.open(filename, 'r', encoding='utf-8')
91
92
        try:
93
            self.description = yaml.safe_load(config_file)
94
        except yaml.YAMLError as err:
95
            if hasattr(err, 'problem_mark'):
96
                mark = err.problem_mark
97
                print(u'Error in configuration file {} at position: {}:{}'.format(filename, mark.line+1, mark.column+1))
98
            else:
99
                print(u'Error in configuration file {}'.format(filename))
100
101
        config_file.close()
102
103
    # End of load_yaml_from_config_file() function
104
105
106
    def _get_command_line_arguments(self):
107
        """
108
        Defines and gets all the arguments for the command line using
109
        argparse module. This function is called in the __init__ function
110
        of this class.
111
        """
112
        str_version = 'versions.py - %s' % __version__
113
114
        parser = argparse.ArgumentParser(description='This program checks releases and versions of programs through RSS or Atom feeds')
115
116
        parser.add_argument('-v', '--version', action='version', version=str_version)
117
        parser.add_argument('-f', '--file', action='store', dest='filename', help='Configuration file with projects to check', default='')
118
        parser.add_argument('-l', '--list-cache', action='store_true', dest='list_cache', help='Lists all projects and their version in cache', default=False)
119
        parser.add_argument('-d', '--debug', action='store_true', dest='debug', help='Starts in debug mode and prints things that may help', default=False)
120
121
        self.options = parser.parse_args()
122
123
        if self.options.filename != '':
124
            self.config_filename = self.options.filename
125
        else:
126
            self.config_filename = os.path.join(self.config_dir, 'versions.yaml')
127
128
    # End of get_command_line_arguments() function
129
130
131
    def extract_site_definition(self, site_name):
132
        """
133
        extracts whole site definition
134
        """
135
136
        if site_name in self.description:
137
            return self.description[site_name]
138
        else:
139
            return dict()
140
141
    # End of extract_site_definition()
142
143
144
    def extract_regex_from_site(self, site_name):
145
        """
146
        Extracts a regex from a site as defined in the YAML file.
147
        Returns the regex if it exists or None otherwise.
148
        """
149
150
        return self.extract_variable_from_site(site_name, 'regex', None)
151
152
    # End of extract_regex_from_site() function
153
154
155
    def extract_multiproject_from_site(self, site_name):
156
        """
157
        Extracts from a site its separator list for its multiple
158
        projects in one title. It returns None if multiproject
159
        is not defined and the list of separators instead
160
        """
161
162
        return self.extract_variable_from_site(site_name, 'multiproject', None)
163
164
    # End of extract…multiproject_from_site() function
165
166
167
    def extract_variable_from_site(self, site_name, variable, default_return):
168
        """
169
        Extracts variable from site site_name if it exists and return
170
        default_return otherwise
171
        """
172
173
        site_definition = self.extract_site_definition(site_name)
174
175
        if variable in site_definition:
176
            value = site_definition[variable]
177
            if value is None:
178
                print(u'Warning: no variable "{}" for site "{}".'.format(variable, site_name))
179
                value = default_return
180
        else:
181
            value = default_return
182
183
        return value
184
185
    # End of extract_variable_from_site() function
186
187
188
    def extract_project_list_from_site(self, site_name):
189
        """
190
        Extracts a project list from a site as defined in the YAML file.
191
        """
192
193
        return self.extract_variable_from_site(site_name, 'projects', [])
194
195
    # End of extract_project_list_from_site() function
196
197
198
    def extract_project_url(self, site_name):
199
        """
200
        Extracts the url definition where to check project version.
201
        """
202
203
        return self.extract_variable_from_site(site_name, 'url', '')
204
205
    # End of extract_project_url() function
206
207
208
    def extract_project_entry(self, site_name):
209
        """
210
        Extracts the entry definition (if any) of a site.
211
        """
212
213
        return self.extract_variable_from_site(site_name, 'entry', '')
214
215
    # End of extract_project_entry() function.
216
217
218
    def is_site_of_type(self, site_name, site_type):
219
        """
220
        Returns True if site_name is of type 'site_type'
221
        """
222
223
        site_definition = self.extract_site_definition(site_name)
224
        if 'type' in site_definition:
225
            return (site_definition['type'] == site_type)
226
        else:
227
            return False
228
229
    # End of is_site_of_type() function
230
231
232
    def extract_site_list(self, site_type):
233
        """
234
        Extracts all sites from a specific type (byproject or list)
235
        """
236
237
        all_site_list = list(self.description.keys())
238
        site_list = []
239
        for site_name in all_site_list:
240
            if self.is_site_of_type(site_name, site_type):
241
                site_list.insert(0, site_name)
242
243
        return site_list
244
245
    # End of extract_site_list() function
246
247
248
    def make_site_cache_list_name(self):
249
        """
250
        Formats list of cache filenames for all sites.
251
        """
252
253
        all_site_list = list(self.description.keys())
254
        cache_list = []
255
        for site_name in all_site_list:
256
            site_cache = u'{}.cache'.format(site_name)
257
            cache_list.insert(0, site_cache)
258
259
        return cache_list
260
261
    # End of make_site_cache_list_name() function
262
263
264
    def get_infos_for_site(self, site_name):
265
        """
266
        Returns informations about a site as a tuple
267
        (list of projects, url to check, filename of the cache, entry checking type)
268
        """
269
270
        project_list = self.extract_project_list_from_site(site_name)
271
        project_url = self.extract_project_url(site_name)
272
        project_entry = self.extract_project_entry(site_name)
273
        cache_filename = u'{}.cache'.format(site_name)
274
275
        return (project_list, project_url, cache_filename, project_entry)
276
277
    # End of get_infos_for_site() function
278
# End of Conf class
279