From 80ba9bebb318dfda4e0d8cb4b88c43dcbbe7f591 Mon Sep 17 00:00:00 2001 From: Dan Elkouby Date: Sun, 10 Jun 2018 11:06:56 +0300 Subject: [PATCH 1/3] Use pathlib instead of os.path --- uniborg/storage.py | 14 +++++++------- uniborg/uniborg.py | 3 +-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/uniborg/storage.py b/uniborg/storage.py index 453429d..70fda9b 100644 --- a/uniborg/storage.py +++ b/uniborg/storage.py @@ -1,8 +1,8 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -import os import json +from pathlib import Path FILE_NAME = 'data.json' @@ -21,11 +21,11 @@ class Storage: self._storage._save() def __init__(self, root): - self._root = root + self._root = Path(root) self._autosave = True self._guard = self._Guard(self) - if os.path.isfile(os.path.join(self._root, FILE_NAME)): - with open(os.path.join(self._root, FILE_NAME)) as fp: + if (self._root / FILE_NAME).is_file(): + with open(self._root / FILE_NAME) as fp: self._data = json.load(fp) else: self._data = {} @@ -47,7 +47,7 @@ class Storage: self._save() def _save(self): - if not os.path.isdir(self._root): - os.makedirs(self._root, exist_ok=True) - with open(os.path.join(self._root, FILE_NAME), 'w') as fp: + if not self._root.is_dir(): + self._root(parents=True, exist_ok=True) + with open(self._root / FILE_NAME, 'w') as fp: json.dump(self._data, fp) diff --git a/uniborg/uniborg.py b/uniborg/uniborg.py index 3de13f7..35613f3 100644 --- a/uniborg/uniborg.py +++ b/uniborg/uniborg.py @@ -1,7 +1,6 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -import os import asyncio import importlib.util import logging @@ -24,7 +23,7 @@ class Uniborg(TelegramClient): # storage should be a callable accepting plugin name -> Storage object. # This means that using the Storage type as a storage would work too. self._name = session - self.storage = storage or (lambda n: Storage(os.path.join('data', n))) + self.storage = storage or (lambda n: Storage(Path("data") / n)) self._logger = logging.getLogger(session) self._plugins = {} self._plugin_path = plugin_path From d54015e1b770a71495b117318185e8a90cbb9da9 Mon Sep 17 00:00:00 2001 From: Dan Elkouby Date: Sun, 10 Jun 2018 11:13:23 +0300 Subject: [PATCH 2/3] Allow overriding API ID and key --- uniborg/uniborg.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/uniborg/uniborg.py b/uniborg/uniborg.py index 35613f3..fc9e686 100644 --- a/uniborg/uniborg.py +++ b/uniborg/uniborg.py @@ -28,10 +28,10 @@ class Uniborg(TelegramClient): self._plugins = {} self._plugin_path = plugin_path - super().__init__( - session, - 6, "eb06d4abfb49dc3eeb1aeb98ae0f581e", # yarr - **kwargs) + kwargs = { + "api_id": 6, "api_hash": "eb06d4abfb49dc3eeb1aeb98ae0f581e", + **kwargs} + super().__init__(session, **kwargs) # This is a hack, please avert your eyes # We want this in order for the most recently added handler to take From 2e82c440d0a9bc4bf43e6a616d808459638aa61b Mon Sep 17 00:00:00 2001 From: Tanuj Date: Sun, 10 Jun 2018 17:12:53 +0000 Subject: [PATCH 3/3] Style enhancements (#7) --- stdplugins/headpat.py | 8 +++----- stdplugins/markdown.py | 2 +- stdplugins/snip.py | 5 +---- uniborg/_core.py | 2 +- uniborg/uniborg.py | 6 +++--- uniborg/util.py | 2 +- 6 files changed, 10 insertions(+), 15 deletions(-) diff --git a/stdplugins/headpat.py b/stdplugins/headpat.py index 24f5374..e7cf3a4 100644 --- a/stdplugins/headpat.py +++ b/stdplugins/headpat.py @@ -1,16 +1,14 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -from telethon import events, utils -from telethon.tl import types, functions - import re -import asyncio import random import json import urllib.request import urllib.parse +from telethon import events + pats = [] oops = "OOPSIE WOOPSIE!! Uwu We madea fucky wucky!! A wittle fucko boingo! " \ "The code monkeys at our headquarters are working VEWY HAWD to fix " \ @@ -33,7 +31,7 @@ async def on_pat(event): pats = json.loads(urllib.request.urlopen(urllib.request.Request( "http://headp.at/js/pats.json", headers={"User-Agent": "Mozilla/5.0 (X11; U; Linux i686) " - "Gecko/20071127 Firefox/2.0.0.11"} + "Gecko/20071127 Firefox/2.0.0.11"} )).read().decode("utf-8")) except Exception as e: print(e) diff --git a/stdplugins/markdown.py b/stdplugins/markdown.py index fb79638..ce08713 100644 --- a/stdplugins/markdown.py +++ b/stdplugins/markdown.py @@ -109,7 +109,7 @@ async def reparse(event): message, msg_entities = await borg._parse_message_text(event.text, parse) # filter out entities that we don't generate old_entities = [] - for entity in (event.message.entities or []): + for entity in event.message.entities or []: if isinstance(entity, PARSED_ENTITIES): old_entities.append(entity) diff --git a/stdplugins/snip.py b/stdplugins/snip.py index dd9d905..9668b15 100644 --- a/stdplugins/snip.py +++ b/stdplugins/snip.py @@ -1,11 +1,8 @@ # This Source Code Form is subject to the terms of the Mozilla Public # License, v. 2.0. If a copy of the MPL was not distributed with this # file, You can obtain one at http://mozilla.org/MPL/2.0/. -import os -import sys -import uuid from telethon import events, utils -from telethon.tl import types, functions +from telethon.tl import types TYPE_TEXT = 0 diff --git a/uniborg/_core.py b/uniborg/_core.py index b62c386..933a5ee 100644 --- a/uniborg/_core.py +++ b/uniborg/_core.py @@ -21,7 +21,7 @@ async def load_reload(event): borg.load_plugin(shortname) msg = await event.respond( - f"Successfully (re)loaded plugin {shortname}") + f"Successfully (re)loaded plugin {shortname}") await asyncio.sleep(DELETE_TIMEOUT) await borg.delete_messages(msg.to_id, msg) diff --git a/uniborg/uniborg.py b/uniborg/uniborg.py index fc9e686..4f40251 100644 --- a/uniborg/uniborg.py +++ b/uniborg/uniborg.py @@ -29,8 +29,8 @@ class Uniborg(TelegramClient): self._plugin_path = plugin_path kwargs = { - "api_id": 6, "api_hash": "eb06d4abfb49dc3eeb1aeb98ae0f581e", - **kwargs} + "api_id": 6, "api_hash": "eb06d4abfb49dc3eeb1aeb98ae0f581e", + **kwargs} super().__init__(session, **kwargs) # This is a hack, please avert your eyes @@ -98,6 +98,6 @@ class Uniborg(TelegramClient): raise fut.add_done_callback( - lambda _: self.remove_event_handler(cb, event_matcher)) + lambda _: self.remove_event_handler(cb, event_matcher)) return fut diff --git a/uniborg/util.py b/uniborg/util.py index e9be581..39cfc8c 100644 --- a/uniborg/util.py +++ b/uniborg/util.py @@ -20,7 +20,7 @@ async def is_read(borg, entity, message, is_out=None): is_out = getattr(message, "out", is_out) if not isinstance(is_out, bool): raise ValueError( - "Message was id but is_out not provided or not a bool") + "Message was id but is_out not provided or not a bool") message_id = getattr(message, "id", message) if not isinstance(message_id, int): raise ValueError("Failed to extract id from message")