Browse Source

corrected some more errors in the main scripts and changed the exceptions for python 3 compatibility

Helmut Pozimski 10 years ago
parent
commit
4bb61a2899
2 changed files with 72 additions and 69 deletions
  1. 1 1
      lib_stov/stov_exceptions.py
  2. 71 68
      stov

+ 1 - 1
lib_stov/stov_exceptions.py

@@ -146,7 +146,7 @@ class SubscriptionDisabledException(Exception):
     """
     def __init__(self, subscription):
         self.__message = _("The subscription %s is disabled, videos will"
-                           "not be downloaded.") % subscription
+                           " not be downloaded.") % subscription
 
     def __str__(self):
         return self.__message

+ 71 - 68
stov

@@ -263,12 +263,13 @@ if os.access(os.environ['HOME'] + "/.stov", os.F_OK & os.W_OK) is not True:
     if sys.version_info >= (3, 0):
         temp_input = input()
     else:
+        # noinspection PyCompatibility
         temp_input = raw_input()
     if temp_input == "yes":
         conf.assist()
         try:
             conf.Initialize()
-        except stov_exceptions.ConfigFileWriteErrorException, e:
+        except stov_exceptions.ConfigFileWriteErrorException as e:
             logger.error(e)
         else:
             logger.info(_("Writing initial configuration according to your "
@@ -280,18 +281,18 @@ if os.access(os.environ['HOME'] + "/.stov", os.F_OK & os.W_OK) is not True:
                        "and database."))
         try:
             conf.Initialize()
-        except stov_exceptions.DirectoryCreationFailedException, e:
+        except stov_exceptions.DirectoryCreationFailedException as e:
             logger.error(e)
-        except stov_exceptions.ConfigFileWriteErrorException, e:
+        except stov_exceptions.ConfigFileWriteErrorException as e:
             logger.error(e)
 else:
     conf = configuration.conf()
     try:
         logger.debug(_("Comparing current and running configuration version."))
         check_result = conf.CheckConfig()
-    except stov_exceptions.ConfigFileReadErrorException, e:
+    except stov_exceptions.ConfigFileReadErrorException as e:
         logger.error(e)
-    except stov_exceptions.InvalidConfigurationVersionException, e:
+    except stov_exceptions.InvalidConfigurationVersionException as e:
         logger.error(e)
     else:
         if check_result is not True:
@@ -299,9 +300,9 @@ else:
                           " update now."))
             try:
                 conf.UpdateConfig()
-            except stov_exceptions.ConfigFileReadErrorException, e:
+            except stov_exceptions.ConfigFileReadErrorException as e:
                 logger.error(e)
-            except stov_exceptions.ConfigFileWriteErrorException, e:
+            except stov_exceptions.ConfigFileWriteErrorException as e:
                 logger.error(e)
 
 
@@ -310,19 +311,19 @@ else:
 if os.access(conf.dbpath, os.F_OK):
     try:
         db = database.db(path=conf.dbpath, version=conf.values["db_version"])
-    except stov_exceptions.DBConnectionFailedException, e:
+    except stov_exceptions.DBConnectionFailedException as e:
         logger.error(e)
         sys.exit(1)
 else:
     try:
         db = database.db(path=conf.dbpath, version=conf.values["db_version"])
-    except stov_exceptions.DBConnectionFailedException, e:
+    except stov_exceptions.DBConnectionFailedException as e:
         logger.error(e)
         sys.exit(1)
     else:
         try:
             db.Populate()
-        except stov_exceptions.DBWriteAccessFailedException, e:
+        except stov_exceptions.DBWriteAccessFailedException as e:
             logger.error(e)
             sys.exit(1)
 
@@ -336,7 +337,7 @@ try:
                 logger.debug("Opening configuration file.")
                 try:
                     conf.WriteConfig()
-                except stov_exceptions.ConfigFileWriteErrorException, e:
+                except stov_exceptions.ConfigFileWriteErrorException as e:
                     logger.error(e)
 except stov_exceptions.DBWriteAccessFailedException as e:
     logger.error(e)
@@ -382,7 +383,6 @@ run the corresponding code
 
 """
 if options.add is True:
-    AddSub = True
     if options.channel is not None and options.searchparameter is None:
         NewSubscription = subscription.sub(type="channel",
                                            name=options.channel, conf=conf)
@@ -406,37 +406,37 @@ if options.add is True:
     else:
         logger.error(_("None or invalid subscription type given, please check "
                        "the type option and try again."))
-        AddSub = False
-    if AddSub is True:
-        try:
-            message, id = db.InsertSubscription(NewSubscription.AddSub())
-            NewSubscription.SetID(id)
-        except stov_exceptions.DBWriteAccessFailedException, e:
-            logger.error(e)
-        try:
-            NewSubscription.ParseAPIData()
-        except stov_exceptions.YoutubeAPITimeoutException, e:
-            logger.error(e)
-        except stov_exceptions.NoDataFromYoutubeAPIException, e:
-            logger.error(e)
-        for video in NewSubscription.parsed_response.videos:
-            if not db.VideoInDatabase(video.ytid):
-                if NewSubscription.CheckStringMatch(video):
-                    db.InsertVideo(video, NewSubscription.GetId())
-        logger.info(_("New subscription ") + NewSubscription.GetTitle()
-                    + _(" successfully added"))
+        sys.exit(1)
+    try:
+        message, id = db.InsertSubscription(NewSubscription.AddSub())
+        NewSubscription.SetID(id)
+    except stov_exceptions.DBWriteAccessFailedException as e:
+        logger.error(e)
+    try:
+        NewSubscription.ParseAPIData()
+    except stov_exceptions.YoutubeAPITimeoutException as e:
+        logger.error(e)
+    except stov_exceptions.NoDataFromYoutubeAPIException as e:
+        logger.error(e)
+    for video in NewSubscription.parsed_response.videos:
+        if not db.VideoInDatabase(video.ytid):
+            if NewSubscription.CheckStringMatch(video):
+                db.InsertVideo(video, NewSubscription.GetId())
+    logger.info(_("New subscription ") + NewSubscription.GetTitle()
+                + _(" successfully added"))
 
 elif options.list is True:
     Listofsubscriptions = db.GetSubscriptions()
+    sub_state = None
     if len(Listofsubscriptions) != 0:
         logger.info(_("ID Title"))
-        for subscription in Listofsubscriptions:
-            if int(subscription[6]) == 0:
+        for sub in Listofsubscriptions:
+            if int(sub[6]) == 0:
                 sub_state = _("enabled")
-            elif int(subscription[2]) == 1:
+            elif int(sub[6]) == 1:
                 sub_state = _("disabled")
-            if subscription[0] is not None:
-                logger.info(str(subscription[0]) + " " + subscription[1]
+            if sub[0] is not None:
+                logger.info(str(sub[0]) + " " + sub[1]
                             + " (%s)" % sub_state)
     else:
         logger.info(_("No subscriptions added yet, add one!"))
@@ -450,9 +450,9 @@ elif options.deleteid is not None:
     else:
         try:
             deletemessage = db.DeleteSubscription(DeleteID)
-        except stov_exceptions.SubscriptionNotFoundException, e:
+        except stov_exceptions.SubscriptionNotFoundException as e:
             logger.error(e)
-        except stov_exceptions.DBWriteAccessFailedException, e:
+        except stov_exceptions.DBWriteAccessFailedException as e:
             logger.error(e)
         else:
             logger.info(deletemessage)
@@ -471,9 +471,9 @@ elif options.update is True:
     for element in listofsubscriptions:
         try:
             element.ParseAPIData()
-        except stov_exceptions.YoutubeAPITimeoutException, e:
+        except stov_exceptions.YoutubeAPITimeoutException as e:
             logger.error(e)
-        except stov_exceptions.NoDataFromYoutubeAPIException, e:
+        except stov_exceptions.NoDataFromYoutubeAPIException as e:
             logger.error(e)
         for video in element.parsed_response.videos:
             if not db.VideoInDatabase(video.ytid):
@@ -507,7 +507,7 @@ elif options.download is True:
         element.GatherVideos(videos)
         try:
             element.DownloadVideos(itag_value)
-        except stov_exceptions.SubscriptionDisabledException, e:
+        except stov_exceptions.SubscriptionDisabledException as e:
             logger.debug(e)
         for entry in element.DownloadedVideos:
             db.UpdateVideoDownloadStatus(entry.GetID(), 1)
@@ -519,7 +519,7 @@ elif options.download is True:
                 db.UpdateVideoFailCount(video.failcnt, video.GetID())
                 if video.failcnt >= int(conf.values["maxfails"]):
                     db.DisableFailedVideo(video.GetID())
-            except stov_exceptions.DBWriteAccessFailedException, e:
+            except stov_exceptions.DBWriteAccessFailedException as e:
                 logger.error(e)
     if videosdownloaded > 0 and conf.values["notify"] == "yes":
         MailText = ""
@@ -594,35 +594,38 @@ elif options.download is True:
 elif options.subscriptionid is not None:
     try:
         Data = db.GetSubscription(options.subscriptionid)
-    except stov_exceptions.DBWriteAccessFailedException, e:
+    except stov_exceptions.DBWriteAccessFailedException as e:
         logger.error(e)
-    if Data:
-        Subscription = subscription.sub(id=Data[0][0], title=Data[0][1],
-                                        type=Data[0][2], name=Data[0][3],
-                                        search=Data[0][4],
-                                        directory=Data[0][5],
-                                        disabled=Data[0][6], conf=conf)
-        videos = db.GetVideos(Subscription.GetId())
-        Subscription.GatherVideos(videos)
-        video_list = Subscription.PrintVideos()
-        for video in video_list:
-            logger.info(video)
+        sys.exit(1)
     else:
-        logger.error(_("Invalid subscription, please check the list and try "
-                       "again"))
+        if Data:
+            Subscription = subscription.sub(id=Data[0][0], title=Data[0][1],
+                                            type=Data[0][2], name=Data[0][3],
+                                            search=Data[0][4],
+                                            directory=Data[0][5],
+                                            disabled=Data[0][6], conf=conf)
+            videos = db.GetVideos(Subscription.GetId())
+            Subscription.GatherVideos(videos)
+            video_list = Subscription.PrintVideos()
+            for video in video_list:
+                logger.info(video)
+        else:
+            logger.error(_("Invalid subscription, please check the list and "
+                           "try again"))
 elif options.catchup is not None:
     try:
         sub_data = db.GetSubscriptionTitle(options.catchup)
-    except stov_exceptions.DBWriteAccessFailedException, e:
+    except stov_exceptions.DBWriteAccessFailedException as e:
         logger.error(e)
-    if sub_data != []:
-        try:
-            db.MarkVideosDownloaded(options.catchup)
-        except stov_exceptions.DBWriteAccessFailedException, e:
-            logger.error(e)
     else:
-        logger.error(_("The subscription could not be updated, "
-                       "please check if the ID given is correct."))
+        if sub_data != []:
+            try:
+                db.MarkVideosDownloaded(options.catchup)
+            except stov_exceptions.DBWriteAccessFailedException as e:
+                logger.error(e)
+        else:
+            logger.error(_("The subscription could not be updated, "
+                           "please check if the ID given is correct."))
 
 
 elif options.cleanup is True:
@@ -642,11 +645,11 @@ elif options.cleanup is True:
                            "database") % delete_video.title)
             try:
                 db.DeleteVideo(delete_video.GetID())
-            except stov_exceptions.DBWriteAccessFailedException, e:
+            except stov_exceptions.DBWriteAccessFailedException as e:
                 logger.error(e)
     try:
         db.Vacuum()
-    except stov_exceptions.DBWriteAccessFailedException, e:
+    except stov_exceptions.DBWriteAccessFailedException as e:
         logger.error(e)
 elif options.enableid is not None:
         subscription_state = db.GetSubscription(options.enableid)
@@ -657,7 +660,7 @@ elif options.enableid is not None:
             elif int(subscription_state[0][6]) == 1:
                 try:
                     db.ChangeSubscriptionState(options.enableid, 0)
-                except stov_exceptions.DBWriteAccessFailedException, e:
+                except stov_exceptions.DBWriteAccessFailedException as e:
                     logger.error(e)
                 else:
                     logger.info(_("Enabled subscription ID %s")
@@ -674,7 +677,7 @@ elif options.disableid is not None:
             elif int(subscription_state[0][6]) == 0:
                 try:
                     db.ChangeSubscriptionState(options.disableid, 1)
-                except stov_exceptions.DBWriteAccessFailedException, e:
+                except stov_exceptions.DBWriteAccessFailedException as e:
                     logger.error(e)
                 else:
                     logger.info(_("Disabled subscription ID %s")