From 100994df284e1ce004e8babaeece128b1bd92479 Mon Sep 17 00:00:00 2001 From: mathieui Date: Mon, 6 Aug 2012 15:01:39 +0200 Subject: Add a fallback to optparse if argparse is not found --- doc/en/install.txt | 3 +++ src/args.py | 34 ++++++++++++++++++++++++++++++++++ src/config.py | 11 ++--------- 3 files changed, 39 insertions(+), 9 deletions(-) create mode 100644 src/args.py diff --git a/doc/en/install.txt b/doc/en/install.txt index 49232abd..1d1be48b 100644 --- a/doc/en/install.txt +++ b/doc/en/install.txt @@ -37,6 +37,9 @@ In order for poezio to correctly work, you need the libs SleekXMPP and === Dependencies === +NOTE: If your python3 version is too old because of debian (e.g. < 3.2), you +should install the python3-argparse package if it exists, or use +pip3/virtualenvs to install it. If you want to install SleekXMPP and dnspython by yourself, follow these instructions. Else, go to the next section (recommended). diff --git a/src/args.py b/src/args.py new file mode 100644 index 00000000..b0334fcc --- /dev/null +++ b/src/args.py @@ -0,0 +1,34 @@ +""" +Module related to the argument parsing + +There is a fallback to the deprecated optparse if argparse is not found +""" +from os import path + +def parse_args(CONFIG_PATH=''): + """ + Parse the arguments from the command line + """ + try: + from argparse import ArgumentParser, SUPPRESS + except ImportError: + from optparse import OptionParser + from optparse import SUPPRESS_USAGE as SUPPRESS + parser = OptionParser() + parser.add_option("-f", "--file", dest="filename", default=path.join(CONFIG_PATH, 'poezio.cfg'), + usage="The config file you want to use", metavar="CONFIG_FILE") + parser.add_option("-d", "--debug", dest="debug", + usage="The file where debug will be written", metavar="DEBUG_FILE") + parser.add_option("-v", "--version", dest="version", + usage=SUPPRESS, metavar="VERSION", default="0.8-dev") + (options, args) = parser.parse_args() + else: + parser = ArgumentParser() + parser.add_argument("-f", "--file", dest="filename", default=path.join(CONFIG_PATH, 'poezio.cfg'), + help="The config file you want to use", metavar="CONFIG_FILE") + parser.add_argument("-d", "--debug", dest="debug", + help="The file where debug will be written", metavar="DEBUG_FILE") + parser.add_argument("-v", "--version", dest="version", + help=SUPPRESS, metavar="VERSION", default="0.8-dev") + options = parser.parse_args() + return options diff --git a/src/config.py b/src/config.py index 99a05118..8a0c322c 100644 --- a/src/config.py +++ b/src/config.py @@ -15,7 +15,7 @@ DEFSECTION = "Poezio" from configparser import RawConfigParser, NoOptionError, NoSectionError from os import environ, makedirs, path from shutil import copy2 -from argparse import ArgumentParser, SUPPRESS +from args import parse_args class Config(RawConfigParser): """ @@ -192,14 +192,7 @@ if not path.isfile(path.join(CONFIG_PATH, 'poezio.cfg')): copy2(path.join(path.dirname(__file__), '../data/default_config.cfg'), path.join(CONFIG_PATH, 'poezio.cfg')) firstrun = True -parser = ArgumentParser() -parser.add_argument("-f", "--file", dest="filename", default=path.join(CONFIG_PATH, 'poezio.cfg'), - help="The config file you want to use", metavar="CONFIG_FILE") -parser.add_argument("-d", "--debug", dest="debug", - help="The file where debug will be written", metavar="DEBUG_FILE") -parser.add_argument("-v", "--version", dest="version", - help=SUPPRESS, metavar="VERSION", default="0.8-dev") -options = parser.parse_args() +options = parse_args(CONFIG_PATH) config = Config(options.filename) if firstrun: config.set('firstrun', True) -- cgit v1.2.3