"Fossies" - the Fresh Open Source Software Archive 
Member "versions-1.5.5/versions/configuration.py" (27 Oct 2021, 8626 Bytes) of package /linux/misc/versions-1.5.5.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
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 : olivier.delhomme@free.fr
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 <olivier.delhomme@free.fr>"
32 __date__ = "23.04.2019"
33 __version__ = "1.5.4"
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
53 class Conf:
54 """
55 Class to store configuration of the program and check version.
56 """
57
58 config_dir = ''
59 local_dir = ''
60 config_filename = ''
61 description = {}
62 options = None
63
64 def __init__(self):
65 """
66 Inits the class
67 """
68 self.config_dir = os.path.expanduser("~/.config/versions")
69 self.local_dir = os.path.expanduser("~/.local/versions")
70 self.config_filename = '' # At this stage we do not know if a filename has been set on the command line
71 self.description = {}
72 self.options = None
73
74 # Make sure that the directories exists
75 make_directories(self.config_dir)
76 make_directories(self.local_dir)
77
78 self._get_command_line_arguments()
79
80 # End of init() function
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 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 str_version = 'versions.py - %s' % __version__
112
113 parser = argparse.ArgumentParser(description='This program checks releases and versions of programs through RSS or Atom feeds')
114
115 parser.add_argument('-v', '--version', action='version', version=str_version)
116 parser.add_argument('-f', '--file', action='store', dest='filename', help='Configuration file with projects to check', default='')
117 parser.add_argument('-l', '--list-cache', action='store_true', dest='list_cache', help='Lists all projects and their version in cache', default=False)
118 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 self.options = parser.parse_args()
121
122 if self.options.filename != '':
123 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 def extract_site_definition(self, site_name):
130 """
131 extracts whole site definition
132 """
133
134 if site_name in self.description:
135 return self.description[site_name]
136 else:
137 return dict()
138
139 # End of extract_site_definition()
140
141 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 return self.extract_variable_from_site(site_name, 'regex', None)
148
149 # End of extract_regex_from_site() function
150
151 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 return self.extract_variable_from_site(site_name, 'multiproject', None)
159
160 # End of extract…multiproject_from_site() function
161
162 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 site_definition = self.extract_site_definition(site_name)
169
170 if variable in site_definition:
171 value = site_definition[variable]
172 if value is None:
173 print(u'Warning: no variable "{}" for site "{}".'.format(variable, site_name))
174 value = default_return
175 else:
176 value = default_return
177
178 return value
179
180 # End of extract_variable_from_site() function
181
182 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 return self.extract_variable_from_site(site_name, 'projects', [])
188
189 # End of extract_project_list_from_site() function
190
191 def extract_project_url(self, site_name):
192 """
193 Extracts the url definition where to check project version.
194 """
195
196 return self.extract_variable_from_site(site_name, 'url', '')
197
198 # End of extract_project_url() function
199
200 def extract_project_entry(self, site_name):
201 """
202 Extracts the entry definition (if any) of a site.
203 """
204
205 return self.extract_variable_from_site(site_name, 'entry', '')
206
207 # End of extract_project_entry() function.
208
209 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 site_definition = self.extract_site_definition(site_name)
215 if 'type' in site_definition:
216 return (site_definition['type'] == site_type)
217 else:
218 return False
219
220 # End of is_site_of_type() function
221
222 def extract_site_list(self, site_type):
223 """
224 Extracts all sites from a specific type (byproject or list)
225 """
226
227 all_site_list = list(self.description.keys())
228 site_list = []
229 for site_name in all_site_list:
230 if self.is_site_of_type(site_name, site_type):
231 site_list.insert(0, site_name)
232
233 return site_list
234
235 # End of extract_site_list() function
236
237 def make_site_cache_list_name(self):
238 """
239 Formats list of cache filenames for all sites.
240 """
241
242 all_site_list = list(self.description.keys())
243 cache_list = []
244 for site_name in all_site_list:
245 site_cache = u'{}.cache'.format(site_name)
246 cache_list.insert(0, site_cache)
247
248 return cache_list
249
250 # End of make_site_cache_list_name() function
251
252 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 project_list = self.extract_project_list_from_site(site_name)
259 project_url = self.extract_project_url(site_name)
260 project_entry = self.extract_project_entry(site_name)
261 cache_filename = u'{}.cache'.format(site_name)
262
263 return (project_list, project_url, cache_filename, project_entry)
264
265 # End of get_infos_for_site() function
266 # End of Conf class