[PATCH 15/17] Use command line arguments or explicitly given command line args

Sebastian Spaeth Sebastian at SSpaeth.de
Mon Nov 29 16:02:23 GMT 2010


This will be useful when creating a test suite that excercises OfflineImap

Signed-off-by: Sebastian Spaeth <Sebastian at SSpaeth.de>
---
 offlineimap/init.py |   34 +++++++++++++++++++++++++---------
 1 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/offlineimap/init.py b/offlineimap/init.py
index 70db2a2..f76fb5f 100644
--- a/offlineimap/init.py
+++ b/offlineimap/init.py
@@ -35,14 +35,18 @@ class OfflineImap:
     ui = None
     """class variable, holding the used ui"""
 
-    def __init__(self):
+    def __init__(self, cmdline_opts=None):
+        """:param cmdline_opts: If not None, this is a list of command
+                                line arguments to be used rather than using sys.argv."""
         self._lockfile = None       # lockfile path when locked
         self._pidfile  = None       # pidfile path when locked
-        self.parse_commandline()
+        self.parse_commandline(cmdline_opts)
 
 
     def lock(self):
-        """Create lock file and exit if not possible"""
+        """Create lock file and exit if not possible.
+
+        This is usually called in the startup phase and needs rarely to be directly called by a user."""
         lockfile = os.path.join(self._config.getmetadatadir(), "locked")
         try:
             lockfd = os.open( lockfile, os.O_CREAT|os.O_EXCL)
@@ -55,23 +59,32 @@ class OfflineImap:
                 OfflineImap.ui.terminate(1)
 
     def unlock(self):
-        """Delete lockfile if existing."""
+        """Delete lockfile if existing.
+
+        This is usually called in the cleanup phase and needs rarely to be directly called by a user."""
         if self._lockfile:
             os.unlink(self._lockfile)
 
     def write_pidfile(self, pidfile):
-        """Write the current process pid into a pidfile"""
+        """Write the current process pid into a pidfile.
+
+        This is usually called in the startup phase and needs rarely to be directly called by a user."""
         pidfd = open(pidfile, "aw")
         pidfd.write(str(os.getpid()) + "\n")
         pidfd.close()
         self._pidfile = pidfile
 
     def delete_pidfile(self):
-        """Delete the current pid pidfile if existing."""
+        """Delete the current pid pidfile if existing.
+
+        This is usually called in the cleanup phase and needs rarely to be directly called by a user."""
         os.unlink(self._pidfile)
 
-    def parse_commandline(self):
-        """Parse the commandline and invoke everything"""
+    def parse_commandline(self, cmdline_opts=None):
+        """Parse the commandline and invoke everything
+
+        :param cmdline_opts: If not None, this is a list of command
+                                line arguments to be used rather than using sys.argv."""
 
         parser = OptionParser(version=offlineimap.banner,
                               description="%s.\n\n%s" % 
@@ -167,7 +180,10 @@ class OfflineImap:
               "not usable. Possible interface choices are: %s " %
               ", ".join(DEFAULT_UI_LIST))
 
-        (options, args) = parser.parse_args()
+        #either use sys.argv or explicit cmdline_opts
+        if cmdline_opts is None:
+            cmdline_opts = sys.argv[1:]
+        (options, args) = parser.parse_args(cmdline_opts)
 
         #read in configuration file
         configfilename = os.path.expanduser(options.configfile)
-- 
1.7.1





More information about the OfflineIMAP-project mailing list