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.

Conf._get_command_line_arguments()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2.003

Importance

Changes 0
Metric Value
cc 2
eloc 11
nop 1
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
crap 2.003
rs 9.85
c 0
b 0
f 0
1
#!/usr/bin/env python
2
# -*- coding: utf8 -*-
3
#
4
#  configuration.py : configuration related class and functions 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__ = "23.04.2019"
33 1
__version__ = "1.5.4"
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
53 1
class Conf:
54
    """
55
    Class to store configuration of the program and check version.
56
    """
57
58 1
    config_dir = ''
59 1
    local_dir = ''
60 1
    config_filename = ''
61 1
    description = {}
62 1
    options = None
63
64 1
    def __init__(self):
65
        """
66
        Inits the class
67
        """
68 1
        self.config_dir = os.path.expanduser("~/.config/versions")
69 1
        self.local_dir = os.path.expanduser("~/.local/versions")
70 1
        self.config_filename = ''  # At this stage we do not know if a filename has been set on the command line
71 1
        self.description = {}
72 1
        self.options = None
73
74
        # Make sure that the directories exists
75 1
        make_directories(self.config_dir)
76 1
        make_directories(self.local_dir)
77
78 1
        self._get_command_line_arguments()
79
80
    # End of init() function
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 1
    def _get_command_line_arguments(self):
106
        """
107
        Defines and gets all the arguments for the command line using
108
        argparse module. This function is called in the __init__ function
109
        of this class.
110
        """
111 1
        str_version = 'versions.py - %s' % __version__
112
113 1
        parser = argparse.ArgumentParser(description='This program checks releases and versions of programs through RSS or Atom feeds')
114
115 1
        parser.add_argument('-v', '--version', action='version', version=str_version)
116 1
        parser.add_argument('-f', '--file', action='store', dest='filename', help='Configuration file with projects to check', default='')
117 1
        parser.add_argument('-l', '--list-cache', action='store_true', dest='list_cache', help='Lists all projects and their version in cache', default=False)
118 1
        parser.add_argument('-d', '--debug', action='store_true', dest='debug', help='Starts in debug mode and prints things that may help', default=False)
119
120 1
        self.options = parser.parse_args()
121
122 1
        if self.options.filename != '':
123 1
            self.config_filename = self.options.filename
124
        else:
125
            self.config_filename = os.path.join(self.config_dir, 'versions.yaml')
126
127
    # End of get_command_line_arguments() function
128
129 1
    def extract_site_definition(self, site_name):
130
        """
131
        extracts whole site definition
132
        """
133
134 1
        if site_name in self.description:
135 1
            return self.description[site_name]
136
        else:
137
            return dict()
138
139
    # End of extract_site_definition()
140
141 1
    def extract_regex_from_site(self, site_name):
142
        """
143
        Extracts a regex from a site as defined in the YAML file.
144
        Returns the regex if it exists or None otherwise.
145
        """
146
147 1
        return self.extract_variable_from_site(site_name, 'regex', None)
148
149
    # End of extract_regex_from_site() function
150
151 1
    def extract_multiproject_from_site(self, site_name):
152
        """
153
        Extracts from a site its separator list for its multiple
154
        projects in one title. It returns None if multiproject
155
        is not defined and the list of separators instead
156
        """
157
158 1
        return self.extract_variable_from_site(site_name, 'multiproject', None)
159
160
    # End of extract…multiproject_from_site() function
161
162 1
    def extract_variable_from_site(self, site_name, variable, default_return):
163
        """
164
        Extracts variable from site site_name if it exists and return
165
        default_return otherwise
166
        """
167
168 1
        site_definition = self.extract_site_definition(site_name)
169
170 1
        if variable in site_definition:
171 1
            value = site_definition[variable]
172 1
            if value is None:
173 1
                print(u'Warning: no variable "{}" for site "{}".'.format(variable, site_name))
174 1
                value = default_return
175
        else:
176 1
            value = default_return
177
178 1
        return value
179
180
    # End of extract_variable_from_site() function
181
182 1
    def extract_project_list_from_site(self, site_name):
183
        """
184
        Extracts a project list from a site as defined in the YAML file.
185
        """
186
187 1
        return self.extract_variable_from_site(site_name, 'projects', [])
188
189
    # End of extract_project_list_from_site() function
190
191 1
    def extract_project_url(self, site_name):
192
        """
193
        Extracts the url definition where to check project version.
194
        """
195
196 1
        return self.extract_variable_from_site(site_name, 'url', '')
197
198
    # End of extract_project_url() function
199
200 1
    def extract_project_entry(self, site_name):
201
        """
202
        Extracts the entry definition (if any) of a site.
203
        """
204
205 1
        return self.extract_variable_from_site(site_name, 'entry', '')
206
207
    # End of extract_project_entry() function.
208
209 1
    def is_site_of_type(self, site_name, site_type):
210
        """
211
        Returns True if site_name is of type 'site_type'
212
        """
213
214 1
        site_definition = self.extract_site_definition(site_name)
215 1
        if 'type' in site_definition:
216 1
            return (site_definition['type'] == site_type)
217
        else:
218
            return False
219
220
    # End of is_site_of_type() function
221
222 1
    def extract_site_list(self, site_type):
223
        """
224
        Extracts all sites from a specific type (byproject or list)
225
        """
226
227 1
        all_site_list = list(self.description.keys())
228 1
        site_list = []
229 1
        for site_name in all_site_list:
230 1
            if self.is_site_of_type(site_name, site_type):
231 1
                site_list.insert(0, site_name)
232
233 1
        return site_list
234
235
    # End of extract_site_list() function
236
237 1
    def make_site_cache_list_name(self):
238
        """
239
        Formats list of cache filenames for all sites.
240
        """
241
242 1
        all_site_list = list(self.description.keys())
243 1
        cache_list = []
244 1
        for site_name in all_site_list:
245 1
            site_cache = u'{}.cache'.format(site_name)
246 1
            cache_list.insert(0, site_cache)
247
248 1
        return cache_list
249
250
    # End of make_site_cache_list_name() function
251
252 1
    def get_infos_for_site(self, site_name):
253
        """
254
        Returns information about a site as a tuple
255
        (list of projects, url to check, filename of the cache, entry checking type)
256
        """
257
258 1
        project_list = self.extract_project_list_from_site(site_name)
259 1
        project_url = self.extract_project_url(site_name)
260 1
        project_entry = self.extract_project_entry(site_name)
261 1
        cache_filename = u'{}.cache'.format(site_name)
262
263 1
        return (project_list, project_url, cache_filename, project_entry)
264
265
    # End of get_infos_for_site() function
266
# End of Conf class
267