From 1d82fb3f44e50a1def23f6e03dab423edbbfbe8b Mon Sep 17 00:00:00 2001 From: Python3pkg Date: Sun, 21 May 2017 05:30:07 -0700 Subject: [PATCH] Convert to Python3 --- cylon/__main__.py | 18 +++++++++--------- cylon/builtins.py | 2 +- cylon/command.py | 6 +++--- cylon/conf.py | 10 +++++----- cylon/hook.py | 5 ++--- cylon/plugin.py | 11 +++-------- 6 files changed, 23 insertions(+), 29 deletions(-) diff --git a/cylon/__main__.py b/cylon/__main__.py index 5b5b573..123ed74 100644 --- a/cylon/__main__.py +++ b/cylon/__main__.py @@ -124,14 +124,14 @@ def message_handler(self, conn, mess): logging.info(cmd) cmd_parameters = cmd.split() plugin_name = cmd_parameters[0] - if modules.has_key(plugin_name) or aliases.has_key(plugin_name): - if aliases.has_key(plugin_name): - func = aliases[plugin_name].keys()[0] + if plugin_name in modules or plugin_name in aliases: + if plugin_name in aliases: + func = list(aliases[plugin_name].keys())[0] inst = aliases[plugin_name][func] cmd_parameters.pop(0) try: msg = self.__call_plugin(mess, inst, func, cmd_parameters) - except AttributeError, e: + except AttributeError as e: msg = "Function %s not implemented." % func logging.error("%s plugin exec: %s" % (class_, str(e))) else: @@ -145,7 +145,7 @@ def message_handler(self, conn, mess): # Way to test if class exists.If exception, error msg. method = getattr(inst, func) msg = self.__call_plugin(mess, inst, func, cmd_parameters) - except AttributeError, e: + except AttributeError as e: msg = "Function %s not implemented." % func logging.error("%s plugin exec: %s" % (class_, str(e))) else: @@ -161,7 +161,7 @@ def __call_plugin(self, xmpp_mess, class_, func, param): msg = class_.wrapper(func, xmpp_mess.getBody(), xmpp_mess.getFrom(), xmpp_mess.getType(), param) - except Exception, e: + except Exception as e: msg = "Error during %s function execution." % func logging.error("%s plugin exec: %s" % (class_, str(e))) @@ -236,7 +236,7 @@ def __connect(self): logging.error("Unable to connect to server %s." % self._jid.getDomain()) exit() - if res<>'tls': + if res!='tls': logging.warning("Unable to establish TLS connection.") res = conn.auth(self._jid.getNode(), @@ -245,7 +245,7 @@ def __connect(self): if not res: logging.error("Unable to authenticate this connection.") exit() - if res<>'sasl': + if res!='sasl': logging.warning("Unable to get SASL creditential for: %s." % self.jid.getDomain()) conn.RegisterHandler('message', self.message_handler) @@ -261,7 +261,7 @@ def __connect(self): def __join_muc(self): for room_config in self._settings.groupchat: if isinstance(room_config, dict): - for k, v in room_config.iteritems(): + for k, v in room_config.items(): presence = xmpp.Presence(to="%s/%s" % (k, self._settings.chat_name)) presence.setTag('x', namespace='http://jabber.org/protocol/muc').setTagData('password',v) else: diff --git a/cylon/builtins.py b/cylon/builtins.py index 2bff8ea..170fabf 100644 --- a/cylon/builtins.py +++ b/cylon/builtins.py @@ -26,7 +26,7 @@ def help(self, body, from_user, chat_type, args): def list(self, body, from_user, chat_type, args): msg = "\n" - for type_ in list(self.modules.viewkeys()): + for type_ in list(self.modules.keys()): msg = msg + type_ + ":\n" for plugin_name in self.modules[type_]: msg = msg + " - %s\n" % plugin_name diff --git a/cylon/command.py b/cylon/command.py index db47700..9bf5161 100644 --- a/cylon/command.py +++ b/cylon/command.py @@ -69,7 +69,7 @@ def cmd_to_class(command): def compute_aliases(alias_info, class_inst, plugin_name): logging.debug("Loading aliases...") alias_hash = {} - for method_name, alias_name in alias_info.iteritems(): + for method_name, alias_name in alias_info.items(): alias_hash.update({alias_name : { method_name : class_inst }}) logging.debug("Final alias list:") logging.debug(alias_hash) @@ -98,11 +98,11 @@ def get_needed_class(filenames, plugin_list, alias_list): class_inst = class_() if class_inst.is_public(): plugins['publics'].update({name : class_inst}) - if name in alias_list.keys(): + if name in list(alias_list.keys()): aliases['publics'].update(compute_aliases(alias_list[name], class_inst, name)) else: plugins['privates'].update({name : class_inst}) - if alias_list.has_key(name): + if name in alias_list: aliases['privates'].update(compute_aliases(alias_list[name], class_inst, name)) except TypeError: logging.error("You need to define help method for %s plugin class" % wanted_class) diff --git a/cylon/conf.py b/cylon/conf.py index 0803499..b745808 100644 --- a/cylon/conf.py +++ b/cylon/conf.py @@ -25,7 +25,7 @@ def __init__(self, conf_file): logging.debug("Starting configuration parsing") try: stream = file(conf_file, 'r') - except Exception, e: + except Exception as e: logging.error("%s read: %s" % (conf_file, str(e))) exit() logging.info("Loading %s" % conf_file) @@ -39,7 +39,7 @@ def __build_alias_settings(self, alias_list): logging.debug("Get alias list settings:") alias_hash = {} for alias in alias_list: - str_ = alias.keys()[0] + str_ = list(alias.keys())[0] data = str_.split('.') if str_ == data[0]: logging.info("Alias %s not loaded." % data[0]) @@ -51,19 +51,19 @@ def __build_alias_settings(self, alias_list): logging.info("Alias %s not loaded." % data[0]) continue plugin_method = data[1] - if alias_hash.has_key(plugin_name): + if plugin_name in alias_hash: alias_hash[plugin_name].update({ plugin_method : alias_name}) else: alias_hash.update({ plugin_name : { plugin_method : alias_name }}) del alias - print alias_hash + print(alias_hash) return alias_hash def __check(self): logging.debug("Configuration check") for attr_type in self.ATTRS: for attr in attr_type: - if self._conf_file_values.has_key(attr): + if attr in self._conf_file_values: if not isinstance(self._conf_file_values[attr], attr_type[attr]): if not attr.startswith("loaded_"): logging.error("Type error in configuration file: %s has to be a %s." % diff --git a/cylon/hook.py b/cylon/hook.py index 093f43f..817e16a 100644 --- a/cylon/hook.py +++ b/cylon/hook.py @@ -2,10 +2,9 @@ import logging from abc import ABCMeta, abstractmethod -class Hook: +class Hook(metaclass=ABCMeta): - __metaclass__ = ABCMeta hooks = {} settings = {} connection = None @@ -19,5 +18,5 @@ def __init__(self): def build_regex(self): - for r in self.ACTIONS.keys(): + for r in list(self.ACTIONS.keys()): self.regex.append((re.compile(r), self.ACTIONS[r])) diff --git a/cylon/plugin.py b/cylon/plugin.py index 82878a7..d0d13dc 100644 --- a/cylon/plugin.py +++ b/cylon/plugin.py @@ -1,9 +1,8 @@ import logging from abc import ABCMeta, abstractmethod -class Plugin: +class Plugin(metaclass=ABCMeta): - __metaclass__ = ABCMeta plugins = {} settings = {} connection = None @@ -24,9 +23,7 @@ def default(self, *args): def help(self): pass -class Public(Plugin): - - __metaclass__ = ABCMeta +class Public(Plugin, metaclass=ABCMeta): def is_public(self): return True @@ -38,9 +35,7 @@ def is_private(self): def help(self): pass -class Private(Plugin): - - __metaclass__ = ABCMeta +class Private(Plugin, metaclass=ABCMeta): def is_public(self): return False