Bläddra i källkod

started development cycle for 0.9, implemented switching of the configuration to json

Helmut Pozimski 10 år sedan
förälder
incheckning
a14b087585
6 ändrade filer med 55 tillägg och 15 borttagningar
  1. 1 2
      TODO
  2. 2 2
      doc/conf.py
  3. 36 9
      lib_stov/configuration.py
  4. 11 0
      lib_stov/stov_exceptions.py
  5. 1 1
      setup.py
  6. 4 1
      stov

+ 1 - 2
TODO

@@ -2,6 +2,5 @@ TODOs for 1.0:
 
 * Reintroduce some of the removed debug output
 * Test the whole code again
-* Switch to json for configuration
 * Remove python 2 compatibility code, support python 3 only
-* Implement youtube API version 3
+* Implement youtube-dl as alternative mode to API version 2

+ 2 - 2
doc/conf.py

@@ -52,9 +52,9 @@ copyright = (u"2013, Helmut Pozimski License GPLv2, GNU GPL version 2 "
 # built documents.
 #
 # The short X.Y version.
-version = '0.8'
+version = '0.9'
 # The full version, including alpha/beta/rc tags.
-release = '0.8'
+release = '0.9-wip'
 
 # The language for content autogenerated by Sphinx. Refer to documentation
 # for a list of supported languages.

+ 36 - 9
lib_stov/configuration.py

@@ -21,6 +21,7 @@ from __future__ import print_function
 import os
 import subprocess
 import sys
+import json
 
 from lib_stov import stov_exceptions
 
@@ -51,7 +52,7 @@ class conf(object):
             "db_version": "3",
             "videocodec": "h264",
             "maxresolution": "1080p",
-            "maxfails": "50",
+            "maxfails": 50,
             "check_title": "no"
         }
 
@@ -92,13 +93,11 @@ class conf(object):
         """
         try:
             configfile = open(str(os.environ['HOME']) +
-                              "/.stov/stov.config", "w")
+                              "/.stov/stov.json", "w")
         except IOError:
             raise stov_exceptions.ConfigFileWriteErrorException()
         else:
-            for key, value in self.values.items():
-                configfile.write(key.upper() + "=" + self.values[key] +
-                                 "\n")
+            json.dump(self.values, configfile, indent=0)
             configfile.close()
 
     def Initialize(self):
@@ -117,9 +116,10 @@ class conf(object):
             self.values["youtube-dl"] = process.communicate()[0].strip()
             self.WriteConfig()
 
-    def ReadConfig(self):
-        """Reads the existing configuration file and places the values in the
-        dictionary. Existing values (such as default values) are overwritten.
+    def ReadOldConfig(self):
+        """Reads the existing plain text configuration file and places the
+        values in the dictionary. Existing values (such as default values)
+        are overwritten.
 
         """
         try:
@@ -135,6 +135,19 @@ class conf(object):
         self.dbpath = str(os.environ['HOME']) + "/.stov/" + \
             self.values["database"]
 
+    def ReadConfig(self):
+        """Reads the existing json configuration files and loads the values in
+        the dictionary.
+        """
+        try:
+            configfile = open(str(os.environ['HOME']) + "/.stov/stov.json",
+                              "r")
+        except IOError:
+            raise stov_exceptions.ConfigFileReadErrorException()
+        else:
+            self.values = json.load(configfile)
+            configfile.close()
+
     def CheckConfig(self):
         """Checks if the configuration is up-to-date with the running
         stov version.
@@ -151,7 +164,7 @@ class conf(object):
             self.values["config_version"] = str(currentversion)
             return False
         else:
-            self.values["config_version"] = str(currentversion)
+            self.values["config_version"] = currentversion
             return True
 
     def UpdateConfig(self):
@@ -229,3 +242,17 @@ class conf(object):
                 self.values[value] = user_input
         self.dbpath = str(os.environ['HOME']) + "/.stov/" + \
             self.values["database"]
+
+    def migrate_config(self):
+        """Migrates the configuration from the old plain text config to
+        the new and shiny json configuration file.
+        """
+        try:
+            self.ReadOldConfig()
+            self.WriteConfig()
+        except stov_exceptions.ConfigFileReadErrorException:
+            raise stov_exceptions.ConfigurationMigrationFailed()
+        except stov_exceptions.ConfigFileWriteErrorException:
+            raise  stov_exceptions.ConfigurationMigrationFailed
+        else:
+            os.remove(str(os.environ['HOME']) + "/.stov/stov.config")

+ 11 - 0
lib_stov/stov_exceptions.py

@@ -164,3 +164,14 @@ class DownloadDirectoryCreationFailedException(Exception):
 
     def __str__(self):
         return self.__message
+
+class ConfigurationMigrationFailed(Exception):
+    """This exception will be raised when the migration of the configuration
+    to the json format fails.
+    """
+    def __init__(self):
+        self._message = _("The migration of the configuration to the json "
+                          "format failed.")
+
+    def __str__(self):
+        return  self._message

+ 1 - 1
setup.py

@@ -22,7 +22,7 @@ from distutils.core import setup
 
 setup(
     name="stov",
-    version="0.8",
+    version="0.9-wip",
     author="Helmut Pozimski",
     author_email="helmut@pozimski.de",
     description=("a program to subscribe to channels and users from youtube and"

+ 4 - 1
stov

@@ -287,6 +287,9 @@ if os.access(os.environ['HOME'] + "/.stov", os.F_OK & os.W_OK) is not True:
             logger.error(e)
 else:
     conf = configuration.conf()
+    if os.access(str(os.environ['HOME']) + "/.stov/stov.config", os.F_OK):
+        logger.debug(_("Migrating configuration from plain text to json."))
+        conf.migrate_config()
     try:
         logger.debug(_("Comparing current and running configuration version."))
         check_result = conf.CheckConfig()
@@ -732,7 +735,7 @@ elif options.license is True:
     You should have received a copy of the GNU General Public License
     along with stov.  If not, see <http://www.gnu.org/licenses/>.""")
 elif options.version is True:
-    logger.info("0.8")
+    logger.info("0.9-wip")
 else:
     parser.print_help()