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.
Passed
Push — master ( 2e6b2b...d81657 )
by dup
02:17
created

configuration.get_latest_release_by_title()   B

Complexity

Conditions 8

Size

Total Lines 41
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 8

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 41
ccs 20
cts 20
cp 1
rs 7.3333
c 0
b 0
f 0
cc 8
nop 6
crap 8
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 1
import codecs
26 1
import argparse
27 1
import os
28 1
import errno
29 1
import yaml
30
31 1
__author__ = "Olivier Delhomme <[email protected]>"
32 1
__date__ = "06.11.2018"
33 1
__version__ = "1.5.2"
34
35
36 1
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 1
    try:
43 1
        os.makedirs(path)
44
45 1
    except OSError as exc:
46
47 1
        if exc.errno != errno.EEXIST or os.path.isdir(path) is not True:
48
            raise
49
50
# End of make_directories() function
51
52 1
class Conf:
53
    """
54
    Class to store configuration of the program and check version.
55
    """
56
57 1
    config_dir = ''
58 1
    local_dir = ''
59 1
    config_filename = ''
60 1
    description = {}
61 1
    options = None
62
63 1
    def __init__(self):
64
        """
65
        Inits the class
66
        """
67 1
        self.config_dir = os.path.expanduser("~/.config/versions")
68 1
        self.local_dir = os.path.expanduser("~/.local/versions")
69 1
        self.config_filename = ''  # At this stage we do not know if a filename has been set on the command line
70 1
        self.description = {}
71 1
        self.options = None
72
73
        # Make sure that the directories exists
74 1
        make_directories(self.config_dir)
75 1
        make_directories(self.local_dir)
76
77 1
        self._get_command_line_arguments()
78
79
    # End of init() function
80
81
82 1
    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 1
        config_file = codecs.open(filename, 'r', encoding='utf-8')
91
92 1
        try:
93 1
            self.description = yaml.safe_load(config_file)
94 1
        except yaml.YAMLError as err:
95 1
            if hasattr(err, 'problem_mark'):
96 1
                mark = err.problem_mark
97 1
                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 1
        config_file.close()
102
103
    # End of load_yaml_from_config_file() function
104
105
106 1
    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 1
        str_version = 'versions.py - %s' % __version__
113
114 1
        parser = argparse.ArgumentParser(description='This program checks releases and versions of programs through RSS or Atom feeds')
115
116 1
        parser.add_argument('-v', '--version', action='version', version=str_version)
117 1
        parser.add_argument('-f', '--file', action='store', dest='filename', help='Configuration file with projects to check', default='')
118 1
        parser.add_argument('-l', '--list-cache', action='store_true', dest='list_cache', help='Lists all projects and their version in cache', default=False)
119 1
        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 1
        self.options = parser.parse_args()
122
123 1
        if self.options.filename != '':
124 1
            self.config_filename = self.options.filename
125
        else:
126 1
            self.config_filename = os.path.join(self.config_dir, 'versions.yaml')
127
128
    # End of get_command_line_arguments() function
129
130
131 1
    def extract_site_definition(self, site_name):
132
        """
133
        extracts whole site definition
134
        """
135
136 1
        if site_name in self.description:
137 1
            return self.description[site_name]
138
        else:
139
            return dict()
140
141
    # End of extract_site_definition()
142
143
144 1
    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 1
        return self.extract_variable_from_site(site_name, 'regex', None)
151
152
    # End of extract_regex_from_site() function
153
154
155 1
    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 1
        return self.extract_variable_from_site(site_name, 'multiproject', None)
163
164
    # End of extract…multiproject_from_site() function
165
166
167 1
    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 1
        site_definition = self.extract_site_definition(site_name)
174
175 1
        if variable in site_definition:
176 1
            value = site_definition[variable]
177 1
            if value is None:
178 1
                print(u'Warning: no variable "{}" for site "{}".'.format(variable, site_name))
179 1
                value = default_return
180
        else:
181 1
            value = default_return
182
183 1
        return value
184
185
    # End of extract_variable_from_site() function
186
187
188 1
    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 1
        return self.extract_variable_from_site(site_name, 'projects', [])
194
195
    # End of extract_project_list_from_site() function
196
197
198 1
    def extract_project_url(self, site_name):
199
        """
200
        Extracts the url definition where to check project version.
201
        """
202
203 1
        return self.extract_variable_from_site(site_name, 'url', '')
204
205
    # End of extract_project_url() function
206
207
208 1
    def extract_project_entry(self, site_name):
209
        """
210
        Extracts the entry definition (if any) of a site.
211
        """
212
213 1
        return self.extract_variable_from_site(site_name, 'entry', '')
214
215
    # End of extract_project_entry() function.
216
217
218 1
    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 1
        site_definition = self.extract_site_definition(site_name)
224 1
        if 'type' in site_definition:
225 1
            return (site_definition['type'] == site_type)
226
        else:
227
            return False
228
229
    # End of is_site_of_type() function
230
231
232 1
    def extract_site_list(self, site_type):
233
        """
234
        Extracts all sites from a specific type (byproject or list)
235
        """
236
237 1
        all_site_list = list(self.description.keys())
238 1
        site_list = []
239 1
        for site_name in all_site_list:
240 1
            if self.is_site_of_type(site_name, site_type):
241 1
                site_list.insert(0, site_name)
242
243 1
        return site_list
244
245
    # End of extract_site_list() function
246
247
248 1
    def make_site_cache_list_name(self):
249
        """
250
        Formats list of cache filenames for all sites.
251
        """
252
253 1
        all_site_list = list(self.description.keys())
254 1
        cache_list = []
255 1
        for site_name in all_site_list:
256 1
            site_cache = u'{}.cache'.format(site_name)
257 1
            cache_list.insert(0, site_cache)
258
259 1
        return cache_list
260
261
    # End of make_site_cache_list_name() function
262
263
264 1
    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)
268
        """
269
270 1
        project_list = self.extract_project_list_from_site(site_name)
271 1
        project_url = self.extract_project_url(site_name)
272 1
        project_entry = self.extract_project_entry(site_name)
273 1
        cache_filename = u'{}.cache'.format(site_name)
274
275 1
        return (project_list, project_url, cache_filename, project_entry)
276
277
    # End of get_infos_for_site() function
278
# End of Conf class
279