From dd7a17894c223577d58f5471c01ccdf53115b1a8 Mon Sep 17 00:00:00 2001 From: nico Date: Mon, 26 Oct 2020 21:09:25 +0100 Subject: code quality thingemagic stuff + add pre-commit framework + add black config file * black reformat --- .pre-commit-config.yaml | 31 +++++++++++++++++++++++++++++++ config.py | 4 +--- main.py | 23 +++++++++++------------ pyproject.toml | 20 ++++++++++++++++++++ 4 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 pyproject.toml diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..0f05fc8 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,31 @@ +# See https://pre-commit.com for more information +# See https://pre-commit.com/hooks.html for more hooks +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v3.3.0 + hooks: + - id: check-docstring-first + - id: check-executables-have-shebangs + - id: check-yaml + files: config.yml + - id: debug-statements + - id: end-of-file-fixer + - id: fix-encoding-pragma + - id: mixed-line-ending + args: + - "--fix=lf" + - id: trailing-whitespace + +- repo: https://github.com/psf/black + rev: 20.8b1 + hooks: + - id: black + +- repo: https://gitlab.com/pycqa/flake8 + rev: 3.8.4 + hooks: + - id: flake8 + args: + - "--max-complexity=10" + - "--max-line-length=127" + - "--ignore=E501,E203" diff --git a/config.py b/config.py index 9287aca..323d557 100644 --- a/config.py +++ b/config.py @@ -51,9 +51,7 @@ class Config: print(err, file=sys.stderr) sys.exit(err.errno) - def get( - self, key: str = None, default: (str, int) = None - ) -> (dict, str, int, None): + def get(self, key: str = None, default: (str, int) = None) -> (dict, str, int, None): """method to retrieve the whole config data, a single value or the optional default value""" # if a special key is request, return only that value if key is not None: diff --git a/main.py b/main.py index c7eb398..e40a82f 100644 --- a/main.py +++ b/main.py @@ -15,7 +15,7 @@ class RetiredMucBot(ClientXMPP): # passthrough the config obj self.config = config - + self.rooms = None self.nick = nick self.messages = self.config.get("messages") @@ -61,16 +61,16 @@ class RetiredMucBot(ClientXMPP): """ # do not process our own messages - ourself = self.plugin['xep_0045'].get_our_jid_in_room(msg.get_mucroom()) - if msg['from'] == ourself: + ourself = self.plugin["xep_0045"].get_our_jid_in_room(msg.get_mucroom()) + if msg["from"] == ourself: return # ever other messages will be answered statically - if msg['type'] in ('normal', 'chat'): + if msg["type"] in ("normal", "chat"): self.send_message( - mto=msg['from'], - mbody=self.messages['direct_msg'].format(nick=self.nick), - mtype=msg['type'], + mto=msg["from"], + mbody=self.messages["direct_msg"].format(nick=self.nick), + mtype=msg["type"], ) def notify_user(self, presence): @@ -89,11 +89,11 @@ class RetiredMucBot(ClientXMPP): new_room = self.rooms[presence.get_from().bare] self.send_message( mto=presence.get_from().bare, - mbody=self.messages['grp_msg'].format(user_nick=user_nick, new_room=new_room), + mbody=self.messages["grp_msg"].format(user_nick=user_nick, new_room=new_room), mtype="groupchat", ) - if self.functions['direct_invite']: + if self.functions["direct_invite"]: jid = presence["muc"].get_jid().bare self.invite_user(jid, new_room) @@ -106,7 +106,7 @@ class RetiredMucBot(ClientXMPP): self.plugin["xep_0045"].invite( room=jid, jid=room, - reason='redirection due to retirement', + reason="redirection due to retirement", ) @@ -121,11 +121,10 @@ if __name__ == "__main__": # init the bot and register used slixmpp plugins xmpp = RetiredMucBot(login["jid"], login["password"], login["nick"], config) - #xmpp.register_plugin("xep_0004") # Data Forms xmpp.register_plugin("xep_0030") # Service Discovery xmpp.register_plugin("xep_0045") # Multi-User Chat xmpp.register_plugin("xep_0085") # Chat State Notifications - xmpp.register_plugin('xep_0092') # Software Version + xmpp.register_plugin("xep_0092") # Software Version xmpp.register_plugin("xep_0199") # XMPP Ping xmpp.register_plugin("xep_0249") # Direct MUC Invite diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..e834e5c --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,20 @@ +[tool.black] +line-length = 120 +target-version = ['py37', 'py38'] +include = '\.pyi?$' +exclude = ''' +( + /( + \.eggs # exclude a few common directories in the + | \.git # root of the project + | \.hg + | \.mypy_cache + | \.tox + | \.venv + | _build + | buck-out + | build + | dist + )/ # the root of the project +) +''' -- cgit v1.2.3-54-g00ecf