]> arthur.barton.de Git - bup.git/blobdiff - lib/bup/options.py
merge_into: accommodate python 3
[bup.git] / lib / bup / options.py
index 7fb5bcf39456c69863a89c91dfb7f7d733e1d56d..37c76aa0f59ad742ade60833382ca4d8d0f131e8 100644 (file)
 #       the documentation and/or other materials provided with the
 #       distribution.
 #
-# THIS SOFTWARE IS PROVIDED BY AVERY PENNARUN ``AS IS'' AND ANY
-# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
-# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
-# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
-# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
-# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
-# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
-# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
-# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
-# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+# THIS SOFTWARE IS PROVIDED BY AVERY PENNARUN AND CONTRIBUTORS ``AS
+# IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
+# <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
+# OF THE POSSIBILITY OF SUCH DAMAGE.
 #
 """Command-line options parser.
 With the help of an options spec string, easily parse command-line options.
@@ -45,10 +46,11 @@ longer one, but the short one can be omitted.
 Long option flags are used as the option's key for the OptDict produced when
 parsing options.
 
-When the flag definition is ended with an equal sign, the option takes one
-string as an argument. Otherwise, the option does not take an argument and
-corresponds to a boolean flag that is true when the option is given on the
-command line.
+When the flag definition is ended with an equal sign, the option takes
+one string as an argument, and that string will be converted to an
+integer when possible. Otherwise, the option does not take an argument
+and corresponds to a boolean flag that is true when the option is
+given on the command line.
 
 The option's description is found at the right of its flags definition, after
 one or more spaces. The description ends at the end of the line. If the
@@ -59,8 +61,20 @@ Options can be put in different groups. Options in the same group must be on
 consecutive lines. Groups are formed by inserting a line that begins with a
 space. The text on that line will be output after an empty line.
 """
+
+from __future__ import absolute_import
 import sys, os, textwrap, getopt, re, struct
 
+try:
+    import fcntl
+except ImportError:
+    fcntl = None
+
+try:
+    import termios
+except ImportError:
+    termios = None
+
 
 def _invert(v, invert):
     if invert:
@@ -122,15 +136,18 @@ def _atoi(v):
         return 0
 
 
-def _tty_width():
-    s = struct.pack("HHHH", 0, 0, 0, 0)
-    try:
-        import fcntl, termios
-        s = fcntl.ioctl(sys.stderr.fileno(), termios.TIOCGWINSZ, s)
-    except (IOError, ImportError):
-        return _atoi(os.environ.get('WIDTH')) or 70
-    (ysize,xsize,ypix,xpix) = struct.unpack('HHHH', s)
-    return xsize or 70
+if not fcntl and termios:
+    def _tty_width():
+        return 70
+else:
+    def _tty_width():
+        s = struct.pack("HHHH", 0, 0, 0, 0)
+        try:
+            s = fcntl.ioctl(sys.stderr.fileno(), termios.TIOCGWINSZ, s)
+        except IOError:
+            return 70
+        ysize, xsize, ypix, xpix = struct.unpack('HHHH', s)
+        return xsize or 70
 
 
 class Options:
@@ -246,12 +263,12 @@ class Options:
         """
         try:
             (flags,extra) = self.optfunc(args, self._shortopts, self._longopts)
-        except getopt.GetoptError, e:
+        except getopt.GetoptError as e:
             self.fatal(e)
 
         opt = OptDict(aliases=self._aliases)
 
-        for k,v in self._defaults.iteritems():
+        for k,v in self._defaults.items():
             opt[k] = v
 
         for (k,v) in flags: