[copr] master: Handle reasonably the case where the config file is missing or incorrect (ccc2493)

pingou at fedoraproject.org pingou at fedoraproject.org
Sun Mar 10 08:39:46 UTC 2013


Repository : http://git.fedorahosted.org/cgit/copr.git

On branch  : master

>---------------------------------------------------------------

commit ccc2493f5a014e10547b30fa5248034845f7e80c
Author: Pierre-Yves Chibon <pingou at pingoured.fr>
Date:   Fri Feb 22 23:40:19 2013 +0100

    Handle reasonably the case where the config file is missing or incorrect
    
    If the configuration file in ~/.config/copr is missing or incorrect,
    raise an error and catch it later to do something smart with it.


>---------------------------------------------------------------

 copr_cli/copr_exceptions.py |   21 +++++++++++++++++++++
 copr_cli/main.py            |   10 +++++++---
 copr_cli/subcommands.py     |   21 +++++++++++++++++----
 3 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/copr_cli/copr_exceptions.py b/copr_cli/copr_exceptions.py
new file mode 100644
index 0000000..555ff27
--- /dev/null
+++ b/copr_cli/copr_exceptions.py
@@ -0,0 +1,21 @@
+#-*- coding: UTF-8 -*-
+
+"""
+Exceptions for copr-cli
+"""
+
+class CoprCliException(Exception):
+    """ Basic exception class for copr-cli. """
+    pass
+
+
+class CoprCliNoConfException(CoprCliException):
+    """ Exception thrown when no config file is found. """
+    pass
+
+
+class CoprCliConfigException(CoprCliException):
+    """ Exception thrown when the config file is incomplete or
+    malformated.
+    """
+    pass
diff --git a/copr_cli/main.py b/copr_cli/main.py
index 0a68128..8dc35e4 100644
--- a/copr_cli/main.py
+++ b/copr_cli/main.py
@@ -4,6 +4,7 @@ import argparse
 import sys
 
 import subcommands
+import copr_exceptions
 
 __version__ = '0.1.0'
 __description__ = "CLI tool to run copr"
@@ -114,9 +115,12 @@ def main(argv=sys.argv[1:]):
     except argparse.ArgumentTypeError, e:
         print "\nError: {0}".format(e)
         sys.exit(2)
-    except Exception, e:
-        print 'Error: {0}'.format(e)
-        sys.exit(100)
+    except copr_exceptions.CoprCliException, e:
+        print "\nError: {0}".format(e)
+        sys.exit(3)
+    #except Exception, e:
+        #print 'Error: {0}'.format(e)
+        #sys.exit(100)
 
 
 if __name__ == '__main__':
diff --git a/copr_cli/subcommands.py b/copr_cli/subcommands.py
index 1d561ee..5da9c76 100644
--- a/copr_cli/subcommands.py
+++ b/copr_cli/subcommands.py
@@ -1,5 +1,10 @@
 #-*- coding: UTF-8 -*-
 
+"""
+Function actually doing the work of calling the API and handling the
+output.
+"""
+
 import ConfigParser
 import json
 import os
@@ -7,14 +12,22 @@ import sys
 
 import requests
 
+import copr_exceptions
+
 
 def get_user():
     """ Retrieve the user information from the config file. """
     config = ConfigParser.ConfigParser()
-    config.read(os.path.join(os.path.expanduser('~'), '.config',
-                'copr'))
-    username = config.get('copr-cli', 'username', None)
-    token = config.get('copr-cli', 'token', None)
+    if not config.read(os.path.join(os.path.expanduser('~'), '.config',
+                'copr')):
+        raise copr_exceptions.CoprCliNoConfException(
+            'No configuration file "~/.config/copr" found.')
+    try:
+        username = config.get('copr-cli', 'username', None)
+        token = config.get('copr-cli', 'token', None)
+    except ConfigParser.Error, err:
+        raise copr_exceptions.CoprCliConfigException(
+            'Bad configuration file: %s' % err)
     return {'username': username, 'token': token}
 
 



More information about the copr-devel mailing list