From 80ba9bebb318dfda4e0d8cb4b88c43dcbbe7f591 Mon Sep 17 00:00:00 2001 From: Dan Elkouby Date: Sun, 10 Jun 2018 11:06:56 +0300 Subject: [PATCH] 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