first step in python3 support

This commit is contained in:
Romain Bignon 2014-05-17 13:37:47 +02:00
commit 6fcac89dd5
25 changed files with 302 additions and 208 deletions

View file

@ -18,6 +18,8 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import logging
import optparse
from optparse import OptionGroup, OptionParser
@ -27,7 +29,6 @@ import tempfile
import warnings
from weboob.capabilities.base import ConversionWarning, CapBaseObject
from weboob.tools.browser.browser import FormFieldConversionWarning
from weboob.core import Weboob, CallErrors
from weboob.core.backendscfg import BackendsConfig
from weboob.tools.config.iconfig import ConfigError
@ -295,9 +296,9 @@ class BaseApplication(object):
if isinstance(error, MoreResultsAvailable):
return False
print >>sys.stderr, u'Error(%s): %s' % (backend.name, error)
print(u'Error(%s): %s' % (backend.name, error), file=sys.stderr)
if logging.root.level == logging.DEBUG:
print >>sys.stderr, backtrace
print(backtrace, file=sys.stderr)
else:
return True
@ -322,7 +323,7 @@ class BaseApplication(object):
ask_debug_mode = True
if ask_debug_mode:
print >>sys.stderr, debugmsg
print(debugmsg, file=sys.stderr)
def parse_args(self, args):
self.options, args = self._parser.parse_args(args)
@ -333,7 +334,7 @@ class BaseApplication(object):
if not option.help is optparse.SUPPRESS_HELP:
items.update(str(option).split('/'))
items.update(self._get_completions())
print ' '.join(items)
print(' '.join(items))
sys.exit(0)
if self.options.debug or self.options.save_responses:
@ -350,13 +351,18 @@ class BaseApplication(object):
# this only matters to developers
if not self.options.debug and not self.options.save_responses:
warnings.simplefilter('ignore', category=ConversionWarning)
warnings.simplefilter('ignore', category=FormFieldConversionWarning)
try:
from weboob.tools.browser.browser import FormFieldConversionWarning
except ImportError:
pass
else:
warnings.simplefilter('ignore', category=FormFieldConversionWarning)
handlers = []
if self.options.save_responses:
responses_dirname = tempfile.mkdtemp(prefix='weboob_session_')
print >>sys.stderr, 'Debug data will be saved in this directory: %s' % responses_dirname
print('Debug data will be saved in this directory: %s' % responses_dirname, file=sys.stderr)
log_settings['save_responses'] = True
log_settings['responses_dirname'] = responses_dirname
handlers.append(self.create_logging_file_handler(os.path.join(responses_dirname, 'debug.log')))
@ -424,12 +430,12 @@ class BaseApplication(object):
cls.setup_logging(logging.INFO, [cls.create_default_logger()])
if args is None:
args = [(sys.stdin.encoding and arg.decode(sys.stdin.encoding) or to_unicode(arg)) for arg in sys.argv]
args = [(sys.stdin.encoding and isinstance(arg, bytes) and arg.decode(sys.stdin.encoding) or to_unicode(arg)) for arg in sys.argv]
try:
app = cls()
except BackendsConfig.WrongPermissions as e:
print >>sys.stderr, e
print(e, file=sys.stderr)
sys.exit(1)
try:
@ -437,12 +443,12 @@ class BaseApplication(object):
args = app.parse_args(args)
sys.exit(app.main(args))
except KeyboardInterrupt:
print >>sys.stderr, 'Program killed by SIGINT'
print('Program killed by SIGINT', file=sys.stderr)
sys.exit(0)
except EOFError:
sys.exit(0)
except ConfigError as e:
print >>sys.stderr, 'Configuration error: %s' % e
print('Configuration error: %s' % e, file=sys.stderr)
sys.exit(1)
except CallErrors as e:
try:
@ -451,7 +457,7 @@ class BaseApplication(object):
pass
sys.exit(1)
except ResultsConditionError as e:
print >>sys.stderr, '%s' % e
print('%s' % e, file=sys.stderr)
sys.exit(1)
finally:
app.deinit()