[wip] Automatic conversion 2to3

This commit is contained in:
ljf 2018-10-04 17:19:49 +02:00
parent f7802d724b
commit 74ae352221
11 changed files with 68 additions and 67 deletions

View file

@ -5,7 +5,7 @@ import re
import errno
import logging
import yaml
import cPickle as pickle
import pickle as pickle
from time import time
from collections import OrderedDict
@ -154,7 +154,7 @@ class PatternParameter(_ExtraParameter):
# Use temporarly utf-8 encoded value
try:
v = unicode(arg_value, 'utf-8')
v = str(arg_value, 'utf-8')
except:
v = arg_value
@ -240,7 +240,7 @@ class ExtraArgumentParser(object):
if iface in klass.skipped_iface:
continue
self.extra[klass.name] = klass
logger.debug('extra parameter classes loaded: %s', self.extra.keys())
logger.debug('extra parameter classes loaded: %s', list(self.extra.keys()))
def validate(self, arg_name, parameters):
"""
@ -252,7 +252,7 @@ class ExtraArgumentParser(object):
"""
# Iterate over parameters to validate
for p, v in parameters.items():
for p, v in list(parameters.items()):
klass = self.extra.get(p, None)
if not klass:
# Remove unknown parameters
@ -300,9 +300,9 @@ class ExtraArgumentParser(object):
extra_args.update(self._extra_params.get(tid, {}))
# Iterate over action arguments with extra parameters
for arg_name, extra_params in extra_args.items():
for arg_name, extra_params in list(extra_args.items()):
# Iterate over available extra parameters
for p, cls in self.extra.items():
for p, cls in list(self.extra.items()):
try:
extra_value = extra_params[p]
except KeyError:
@ -593,7 +593,7 @@ class ActionsMap(object):
# * namespace define the top "name", for us it will always be
# "yunohost" and there well be only this one
# * actionsmap is the actual actionsmap that we care about
for namespace, actionsmap in actionsmaps.items():
for namespace, actionsmap in list(actionsmaps.items()):
# Retrieve global parameters
_global = actionsmap.pop('_global', {})
@ -605,7 +605,7 @@ class ActionsMap(object):
# category_name is stuff like "user", "domain", "hooks"...
# category_values is the values of this category (like actions)
for category_name, category_values in actionsmap.items():
for category_name, category_values in list(actionsmap.items()):
if "actions" in category_values:
actions = category_values.pop('actions')
@ -623,7 +623,7 @@ class ActionsMap(object):
# action_name is like "list" of "domain list"
# action_options are the values
for action_name, action_options in actions.items():
for action_name, action_options in list(actions.items()):
arguments = action_options.pop('arguments', {})
tid = (namespace, category_name, action_name)
@ -647,7 +647,7 @@ class ActionsMap(object):
# subcategory_name is like "cert" in "domain cert status"
# subcategory_values is the values of this subcategory (like actions)
for subcategory_name, subcategory_values in subcategories.items():
for subcategory_name, subcategory_values in list(subcategories.items()):
actions = subcategory_values.pop('actions')
@ -656,7 +656,7 @@ class ActionsMap(object):
# action_name is like "status" of "domain cert status"
# action_options are the values
for action_name, action_options in actions.items():
for action_name, action_options in list(actions.items()):
arguments = action_options.pop('arguments', {})
tid = (namespace, category_name, subcategory_name, action_name)

View file

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# TODO: Use Python3 to remove this fix!
from __future__ import absolute_import
import errno
import logging
import random
@ -232,7 +232,7 @@ class Authenticator(BaseAuthenticator):
Boolean | MoulinetteError
"""
for attr, value in value_dict.items():
for attr, value in list(value_dict.items()):
if not self.search(filter=attr + '=' + value):
continue
else:

View file

@ -185,7 +185,7 @@ class Moulinette18n(object):
self.locale = locale
self._global.set_locale(locale)
for n in self._namespaces.values():
for n in list(self._namespaces.values()):
n.set_locale(locale)
def g(self, key, *args, **kwargs):
@ -234,7 +234,7 @@ class MoulinetteSignals(object):
self.clear_handler(s)
# Iterate over signals to connect
for s, h in kwargs.items():
for s, h in list(kwargs.items()):
self.set_handler(s, h)
def set_handler(self, signal, handler):
@ -366,7 +366,7 @@ def init_interface(name, kwargs={}, actionsmap={}):
return interface(amap, **kwargs)
def init_authenticator((vendor, name), kwargs={}):
def init_authenticator(xxx_todo_changeme, kwargs={}):
"""Return a new authenticator instance
Retrieve the given authenticator vendor and return a new instance of
@ -378,6 +378,7 @@ def init_authenticator((vendor, name), kwargs={}):
- kwargs -- A dict of arguments for the authenticator profile
"""
(vendor, name) = xxx_todo_changeme
try:
mod = import_module('moulinette.authenticators.%s' % vendor)
except ImportError:

View file

@ -286,7 +286,7 @@ class BaseActionsMapParser(object):
"for 'authenticator'")
else:
auths = {}
for auth_name, auth_conf in auth.items():
for auth_name, auth_conf in list(auth.items()):
# Add authenticator profile as a 3-tuple
# (identifier, configuration, parameters) with
# - identifier: the authenticator vendor and its
@ -518,7 +518,7 @@ class ExtendedArgumentParser(argparse.ArgumentParser):
def dequeue_callbacks(self, namespace):
queue = self._get_callbacks_queue(namespace, False)
for _i in xrange(len(queue)):
for _i in range(len(queue)):
c, v = queue.popleft()
# FIXME: break dequeue if callback returns
c.execute(namespace, v)
@ -539,7 +539,7 @@ class ExtendedArgumentParser(argparse.ArgumentParser):
return queue
def add_arguments(self, arguments, extraparser, format_arg_names=None, validate_extra=True):
for argument_name, argument_options in arguments.items():
for argument_name, argument_options in list(arguments.items()):
# will adapt arguments name for cli or api context
names = format_arg_names(str(argument_name),
argument_options.pop('full', None))
@ -600,11 +600,11 @@ class ExtendedArgumentParser(argparse.ArgumentParser):
subcategories_subparser = copy.copy(action_group._group_actions[0])
# Filter "action"-type and "subcategory"-type commands
actions_subparser.choices = OrderedDict([(k, v) for k, v in actions_subparser.choices.items() if v.type == "action"])
subcategories_subparser.choices = OrderedDict([(k, v) for k, v in subcategories_subparser.choices.items() if v.type == "subcategory"])
actions_subparser.choices = OrderedDict([(k, v) for k, v in list(actions_subparser.choices.items()) if v.type == "action"])
subcategories_subparser.choices = OrderedDict([(k, v) for k, v in list(subcategories_subparser.choices.items()) if v.type == "subcategory"])
actions_choices = actions_subparser.choices.keys()
subcategories_choices = subcategories_subparser.choices.keys()
actions_choices = list(actions_subparser.choices.keys())
subcategories_choices = list(subcategories_subparser.choices.keys())
actions_subparser._choices_actions = [c for c in choice_actions if c.dest in actions_choices]
subcategories_subparser._choices_actions = [c for c in choice_actions if c.dest in subcategories_choices]

View file

@ -82,7 +82,7 @@ class _HTTPArgumentParser(object):
return self._parser.get_default(dest)
def add_arguments(self, arguments, extraparser, format_arg_names=None, validate_extra=True):
for argument_name, argument_options in arguments.items():
for argument_name, argument_options in list(arguments.items()):
# will adapt arguments name for cli or api context
names = format_arg_names(str(argument_name),
argument_options.pop('full', None))
@ -149,7 +149,7 @@ class _HTTPArgumentParser(object):
arg_strings = append(arg_strings, args[dest])
# Iterate over optional arguments
for dest, opt in self._optional.items():
for dest, opt in list(self._optional.items()):
if dest in args:
arg_strings = append(arg_strings, args[dest], opt[0])
@ -262,7 +262,7 @@ class _ActionsMapPlugin(object):
for a in args:
params[a] = True
# Append other request params
for k, v in request.params.dict.items():
for k, v in list(request.params.dict.items()):
v = _format(v)
try:
curr_v = params[k]
@ -558,7 +558,7 @@ class ActionsMapParser(BaseActionsMapParser):
@property
def routes(self):
"""Get current routes"""
return self._parsers.keys()
return list(self._parsers.keys())
# Implement virtual properties
@ -733,7 +733,7 @@ class Interface(BaseInterface):
# Append additional routes
# TODO: Add optional authentication to those routes?
for (m, p), c in routes.items():
for (m, p), c in list(routes.items()):
app.route(p, method=m, callback=c, skip=['actionsmap'])
self._app = app

View file

@ -85,11 +85,11 @@ def plain_print_dict(d, depth=0):
for v in d:
plain_print_dict(v, depth + 1)
elif isinstance(d, dict):
for k, v in d.items():
print("{}{}".format("#" * (depth + 1), k))
for k, v in list(d.items()):
print(("{}{}".format("#" * (depth + 1), k)))
plain_print_dict(v, depth + 1)
else:
if isinstance(d, unicode):
if isinstance(d, str):
d = d.encode('utf-8')
print(d)
@ -104,7 +104,7 @@ def pretty_print_dict(d, depth=0):
- depth -- The recursive depth of the dictionary
"""
keys = d.keys()
keys = list(d.keys())
if not isinstance(d, OrderedDict):
keys = sorted(keys)
for k in keys:
@ -115,23 +115,23 @@ def pretty_print_dict(d, depth=0):
if isinstance(v, list) and len(v) == 1:
v = v[0]
if isinstance(v, dict):
print("{:s}{}: ".format(" " * depth, k))
print(("{:s}{}: ".format(" " * depth, k)))
pretty_print_dict(v, depth + 1)
elif isinstance(v, list):
print("{:s}{}: ".format(" " * depth, k))
print(("{:s}{}: ".format(" " * depth, k)))
for key, value in enumerate(v):
if isinstance(value, tuple):
pretty_print_dict({value[0]: value[1]}, depth + 1)
elif isinstance(value, dict):
pretty_print_dict({key: value}, depth + 1)
else:
if isinstance(value, unicode):
if isinstance(value, str):
value = value.encode('utf-8')
print("{:s}- {}".format(" " * (depth + 1), value))
print(("{:s}- {}".format(" " * (depth + 1), value)))
else:
if isinstance(v, unicode):
if isinstance(v, str):
v = v.encode('utf-8')
print("{:s}{}: {}".format(" " * depth, k, v))
print(("{:s}{}: {}".format(" " * depth, k, v)))
def get_locale():
@ -315,7 +315,7 @@ class ActionsMapParser(BaseActionsMapParser):
deprecated_alias=deprecated_alias)
def add_global_arguments(self, arguments):
for argument_name, argument_options in arguments.items():
for argument_name, argument_options in list(arguments.items()):
# will adapt arguments name for cli or api context
names = self.format_arg_names(str(argument_name),
argument_options.pop('full', None))
@ -399,7 +399,7 @@ class Interface(BaseInterface):
if output_as == 'json':
import json
from moulinette.utils.serialize import JSONExtendedEncoder
print(json.dumps(ret, cls=JSONExtendedEncoder))
print((json.dumps(ret, cls=JSONExtendedEncoder)))
else:
plain_print_dict(ret)
elif isinstance(ret, dict):
@ -433,7 +433,7 @@ class Interface(BaseInterface):
prompt = lambda m: getpass.getpass(colorize(m18n.g('colon', m),
color))
else:
prompt = lambda m: raw_input(colorize(m18n.g('colon', m), color))
prompt = lambda m: input(colorize(m18n.g('colon', m), color))
value = prompt(message)
if confirm:
@ -449,13 +449,13 @@ class Interface(BaseInterface):
Handle the core.MoulinetteSignals.display signal.
"""
if isinstance(message, unicode):
if isinstance(message, str):
message = message.encode('utf-8')
if style == 'success':
print('{} {}'.format(colorize(m18n.g('success'), 'green'), message))
print(('{} {}'.format(colorize(m18n.g('success'), 'green'), message)))
elif style == 'warning':
print('{} {}'.format(colorize(m18n.g('warning'), 'yellow'), message))
print(('{} {}'.format(colorize(m18n.g('warning'), 'yellow'), message)))
elif style == 'error':
print('{} {}'.format(colorize(m18n.g('error'), 'red'), message))
print(('{} {}'.format(colorize(m18n.g('error'), 'red'), message)))
else:
print(message)

View file

@ -20,7 +20,7 @@ def read_file(file_path):
Keyword argument:
file_path -- Path to the text file
"""
assert isinstance(file_path, basestring)
assert isinstance(file_path, str)
# Check file exists
if not os.path.isfile(file_path):
@ -98,14 +98,14 @@ def write_to_file(file_path, data, file_mode="w"):
file_mode -- Mode used when writing the file. Option meant to be used
by append_to_file to avoid duplicating the code of this function.
"""
assert isinstance(data, basestring) or isinstance(data, list)
assert isinstance(data, str) or isinstance(data, list)
assert not os.path.isdir(file_path)
assert os.path.isdir(os.path.dirname(file_path))
# If data is a list, check elements are strings and build a single string
if not isinstance(data, basestring):
if not isinstance(data, str):
for element in data:
assert isinstance(element, basestring)
assert isinstance(element, str)
data = '\n'.join(data)
try:
@ -143,7 +143,7 @@ def write_to_json(file_path, data):
"""
# Assumptions
assert isinstance(file_path, basestring)
assert isinstance(file_path, str)
assert isinstance(data, dict) or isinstance(data, list)
assert not os.path.isdir(file_path)
assert os.path.isdir(os.path.dirname(file_path))
@ -162,7 +162,7 @@ def write_to_json(file_path, data):
file=file_path, error=str(e)))
def mkdir(path, mode=0777, parents=False, uid=None, gid=None, force=False):
def mkdir(path, mode=0o777, parents=False, uid=None, gid=None, force=False):
"""Create a directory with optional features
Create a directory and optionaly set its permissions to mode and its
@ -214,7 +214,7 @@ def chown(path, uid=None, gid=None, recursive=False):
raise ValueError("either uid or gid argument is required")
# Retrieve uid/gid
if isinstance(uid, basestring):
if isinstance(uid, str):
try:
uid = getpwnam(uid).pw_uid
except KeyError:
@ -222,7 +222,7 @@ def chown(path, uid=None, gid=None, recursive=False):
m18n.g('unknown_user', user=uid))
elif uid is None:
uid = -1
if isinstance(gid, basestring):
if isinstance(gid, str):
try:
gid = grp.getgrnam(gid).gr_gid
except KeyError:

View file

@ -63,7 +63,7 @@ def call_async_output(args, callback, **kwargs):
if "stdinfo" in kwargs and kwargs["stdinfo"] != None:
assert len(callback) == 3
stdinfo = kwargs.pop("stdinfo")
os.mkfifo(stdinfo, 0600)
os.mkfifo(stdinfo, 0o600)
# Open stdinfo for reading (in a nonblocking way, i.e. even
# if command does not write in the stdinfo pipe...)
stdinfo_f = os.open(stdinfo, os.O_RDONLY|os.O_NONBLOCK)

View file

@ -29,7 +29,7 @@ old_translate = moulinette.core.Translator.translate
def new_translate(self, key, *args, **kwargs):
if key not in self._translations[self.default_locale].keys():
if key not in list(self._translations[self.default_locale].keys()):
raise KeyError("Unable to retrieve key %s for default locale !" % key)
return old_translate(self, key, *args, **kwargs)

View file

@ -72,7 +72,7 @@ def test_read_file_badpermissions():
def test_read_json():
content = read_json(TMP_TEST_JSON)
assert "foo" in content.keys()
assert "foo" in list(content.keys())
assert content["foo"] == "bar"
@ -162,8 +162,8 @@ def text_write_dict_to_json():
dummy_dict = {"foo": 42, "bar": ["a", "b", "c"]}
write_to_json(TMP_TEST_FILE, dummy_dict)
j = read_json(TMP_TEST_FILE)
assert "foo" in j.keys()
assert "bar" in j.keys()
assert "foo" in list(j.keys())
assert "bar" in list(j.keys())
assert j["foo"] == 42
assert j["bar"] == ["a", "b", "c"]
assert read_file(TMP_TEST_FILE) == "foo\nbar\nyolo\nswag"
@ -239,13 +239,13 @@ def test_setpermissions_file():
assert get_permissions(TMP_TEST_FILE) == ("root", "root", "700")
# Change the permissions
set_permissions(TMP_TEST_FILE, NON_ROOT_USER, NON_ROOT_GROUP, 0111)
set_permissions(TMP_TEST_FILE, NON_ROOT_USER, NON_ROOT_GROUP, 0o111)
# Check the permissions got changed
assert get_permissions(TMP_TEST_FILE) == (NON_ROOT_USER, NON_ROOT_GROUP, "111")
# Change the permissions again
set_permissions(TMP_TEST_FILE, "root", "root", 0777)
set_permissions(TMP_TEST_FILE, "root", "root", 0o777)
# Check the permissions got changed
assert get_permissions(TMP_TEST_FILE) == ("root", "root", "777")
@ -257,13 +257,13 @@ def test_setpermissions_directory():
assert get_permissions(TMP_TEST_DIR) == ("root", "root", "755")
# Change the permissions
set_permissions(TMP_TEST_DIR, NON_ROOT_USER, NON_ROOT_GROUP, 0111)
set_permissions(TMP_TEST_DIR, NON_ROOT_USER, NON_ROOT_GROUP, 0o111)
# Check the permissions got changed
assert get_permissions(TMP_TEST_DIR) == (NON_ROOT_USER, NON_ROOT_GROUP, "111")
# Change the permissions again
set_permissions(TMP_TEST_DIR, "root", "root", 0777)
set_permissions(TMP_TEST_DIR, "root", "root", 0o777)
# Check the permissions got changed
assert get_permissions(TMP_TEST_DIR) == ("root", "root", "777")
@ -274,22 +274,22 @@ def test_setpermissions_permissiondenied():
switch_to_non_root_user()
with pytest.raises(MoulinetteError):
set_permissions(TMP_TEST_FILE, NON_ROOT_USER, NON_ROOT_GROUP, 0111)
set_permissions(TMP_TEST_FILE, NON_ROOT_USER, NON_ROOT_GROUP, 0o111)
def test_setpermissions_badfile():
with pytest.raises(MoulinetteError):
set_permissions("/foo/bar/yolo", NON_ROOT_USER, NON_ROOT_GROUP, 0111)
set_permissions("/foo/bar/yolo", NON_ROOT_USER, NON_ROOT_GROUP, 0o111)
def test_setpermissions_baduser():
with pytest.raises(MoulinetteError):
set_permissions(TMP_TEST_FILE, "foo", NON_ROOT_GROUP, 0111)
set_permissions(TMP_TEST_FILE, "foo", NON_ROOT_GROUP, 0o111)
def test_setpermissions_badgroup():
with pytest.raises(MoulinetteError):
set_permissions(TMP_TEST_FILE, NON_ROOT_USER, "foo", 0111)
set_permissions(TMP_TEST_FILE, NON_ROOT_USER, "foo", 0o111)

View file

@ -77,7 +77,7 @@ def test_download_json():
fetched_json = download_json(TEST_URL)
assert "foo" in fetched_json.keys()
assert "foo" in list(fetched_json.keys())
assert fetched_json["foo"] == "bar"