"Fossies" - the Fresh Open Source Software Archive

Member "versions-1.5.5/versions/common.py" (27 Oct 2021, 4535 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 #  common.py : common tools used by versions.py modules
    5 #
    6 #  (C) Copyright 2016 - 2018 Olivier Delhomme
    7 #  e-mail : olivier.delhomme@free.fr
    8 #
    9 #  This program is free software; you can redistribute it and/or modify
   10 #  it under the terms of the GNU General Public License as published by
   11 #  the Free Software Foundation; either version 3, or (at your option)
   12 #  any later version.
   13 #
   14 #  This program is distributed in the hope that it will be useful,
   15 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
   16 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17 #  GNU General Public License for more details.
   18 #
   19 #  You should have received a copy of the GNU General Public License
   20 #  along with this program; if not, write to the Free Software Foundation,
   21 #  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   22 #
   23 import feedparser
   24 import time
   25 
   26 
   27 def get_entry_published_date(entry):
   28     """
   29     Returns the published date of an entry.
   30     Selects the right field to do so
   31     """
   32     published_date = None
   33     field_name = ''
   34 
   35     if 'published_parsed' in entry:
   36         published_date = entry.published_parsed
   37         field_name = 'published_parsed'
   38     elif 'updated_parsed' in entry:
   39         published_date = entry.updated_parsed
   40         field_name = 'updated_parsed'
   41     elif 'pubDate' in entry:    # rss-0.91.dtd (netscape)
   42         published_date = entry.pubDate
   43         field_name = 'pubDate'
   44 
   45     return (published_date, field_name)
   46 
   47 # End of get_entry_published_date() function
   48 
   49 
   50 def make_list_of_newer_feeds(feed, feed_info, debug):
   51     """
   52     Compares feed entries and keep those that are newer than the latest
   53     check we've done and inserting the newer ones in reverse order in
   54     a list to be returned
   55     """
   56 
   57     feed_list = []
   58 
   59     # inserting into a list in reverse order to keep the most recent
   60     # version in case of multiple release of the same project in the
   61     # feeds
   62     for a_feed in feed.entries:
   63 
   64         if a_feed:
   65             (published_date, field_name) = get_entry_published_date(a_feed)
   66 
   67             print_debug(debug, u'\tFeed entry ({0}): Feed title: "{1:16}"'.format(time.strftime('%x %X', published_date), a_feed.title))
   68 
   69             if feed_info.is_newer(published_date):
   70                 feed_list.insert(0, a_feed)
   71         else:
   72             print(u'Warning: empty feed in {}'.format(feed))
   73 
   74     return feed_list
   75 
   76 # End of make_list_of_newer_feeds() function
   77 
   78 
   79 def manage_http_status(feed, url):
   80     """
   81     Manages http status code present in feed and prints
   82     an error in case of a 3xx, 4xx or 5xx and stops
   83     doing anything for the feed by returning None.
   84     """
   85 
   86     err = feed.status / 100
   87 
   88     if err > 2:
   89         print(u'Error {} while fetching "{}".'.format(feed.status, url))
   90         feed = None
   91 
   92     return feed
   93 
   94 # End of manage_http_status() function
   95 
   96 
   97 def manage_non_http_errors(feed, url):
   98     """
   99     Tries to manage non http errors and gives
  100     a message to the user.
  101     """
  102 
  103     if feed.bozo:
  104         if feed.bozo_exception:
  105             exc = feed.bozo_exception
  106             if hasattr(exc, 'reason'):
  107                 message = exc.reason
  108             else:
  109                 message = 'unaddressed'
  110 
  111             print(u'Error {} while fetching "{}".'.format(message, url))
  112 
  113         else:
  114             print(u'Error while fetching url "{}".'.format(url))
  115 
  116 # End of manage_non_http_errors() function
  117 
  118 
  119 def get_feed_entries_from_url(url):
  120     """
  121     Gets feed entries from an url that should be an
  122     RSS or Atom feed.
  123     >>> get_feed_entries_from_url("http://delhomme.org/notfound.html")
  124     Error 404 while fetching "http://delhomme.org/notfound.html".
  125     >>> feed = get_feed_entries_from_url("https://github.com/dupgit/versions/tags.atom")
  126     >>> feed.status
  127     200
  128     """
  129 
  130     feed = feedparser.parse(url)
  131 
  132     if 'status' in feed:
  133         feed = manage_http_status(feed, url)
  134     else:
  135         # An error happened such that the feed does not contain an HTTP answer.
  136         manage_non_http_errors(feed, url)
  137         feed = None
  138 
  139     return feed
  140 
  141 # End of get_feed_entries_from_url() function
  142 
  143 
  144 def print_project_version(project, version):
  145     """
  146     Prints to the standard output project name and it's version.
  147     """
  148 
  149     print(u'{} {}'.format(project, version))
  150 
  151 # End of print_project_version() function
  152 
  153 
  154 def print_debug(debug, message):
  155     """
  156     Prints 'message' if debug mode is True
  157     """
  158 
  159     if debug:
  160         print(u'{}'.format(message))
  161 
  162 # End of print_debug() function