From d3cce38832733b56d5d1faa689ca422e638c98fd Mon Sep 17 00:00:00 2001 From: Nick Steel Date: Mon, 6 Jan 2020 00:26:55 +0000 Subject: [PATCH] Use pathlib --- mopidy_iris/__init__.py | 8 ++++---- mopidy_iris/core.py | 20 ++++++++------------ mopidy_iris/system.py | 12 ++++++------ 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/mopidy_iris/__init__.py b/mopidy_iris/__init__.py index 50dbee4d..3cbcb5ad 100755 --- a/mopidy_iris/__init__.py +++ b/mopidy_iris/__init__.py @@ -1,4 +1,4 @@ -import logging, os, json, pathlib +import logging, json, pathlib import tornado.web import tornado.websocket @@ -21,8 +21,7 @@ class Extension( ext.Extension ): ext_name = 'iris' def get_default_config(self): - conf_file = os.path.join(os.path.dirname(__file__), 'ext.conf') - return config.read(conf_file) + return config.read(pathlib.Path(__file__).parent / "ext.conf") def get_config_schema(self): schema = config.ConfigSchema(self.ext_name) @@ -55,7 +54,8 @@ class ReactRouterHandler(tornado.web.StaticFileHandler): def initialize(self, path): self.path = path self.absolute_path = path - self.dirname, self.filename = os.path.split(path) + self.dirname = path.parent + self.filename = path.name super().initialize(self.dirname) def get(self, path=None, include_body=True): diff --git a/mopidy_iris/core.py b/mopidy_iris/core.py index 3169f80c..5c7b5d1b 100755 --- a/mopidy_iris/core.py +++ b/mopidy_iris/core.py @@ -1,4 +1,4 @@ -import random, string, logging, json, pykka, urllib, os, sys, mopidy_iris, subprocess +import random, string, logging, json, pathlib, pykka, urllib, os, sys, mopidy_iris, subprocess import tornado.web import tornado.ioloop import tornado.httpclient @@ -11,6 +11,7 @@ from pkg_resources import parse_version from tornado.escape import json_encode, json_decode from tornado.httpclient import AsyncHTTPClient +from . import Extension from .system import IrisSystemThread if sys.platform == 'win32': @@ -62,15 +63,11 @@ class IrisCore(pykka.ThreadingActor): # @return void ## def save_to_file(self, dict, name): - path = self.config['iris'].get('data_dir') - - # Create the folder if it doesn't yet exist - if not os.path.exists(path): - os.makedirs(path) + file_path = Extension.get_data_dir(self.config) / ('%s.pkl' % name) # And now open the file, and drop in our dict try: - with open(path + '/' + name + '.pkl', 'wb') as f: + with file_path.open('wb') as f: pickle.dump(dict, f, pickle.HIGHEST_PROTOCOL) except Exception: return False @@ -82,10 +79,10 @@ class IrisCore(pykka.ThreadingActor): # @return Dict ## def load_from_file(self, name): - path = self.config['iris'].get('data_dir') + file_path = Extension.get_data_dir(self.config) / ('%s.pkl' % name) try: - with open(path + '/' + name + '.pkl', 'rb') as f: + with file_path.open('wb') as f: return pickle.load(f) except Exception: return {} @@ -96,10 +93,9 @@ class IrisCore(pykka.ThreadingActor): # @return String ## def load_version(self): - filepath = os.path.join(os.path.dirname(__file__), '..', 'IRIS_VERSION') + file_path = pathlib.Path(__file__).parent.parent / 'IRIS_VERSION' try: - with open(filepath, 'r') as f: - return f.read() + return file_path.read_text() except Exception: return "Unknown" diff --git a/mopidy_iris/system.py b/mopidy_iris/system.py index ab1210a1..64ee4c74 100755 --- a/mopidy_iris/system.py +++ b/mopidy_iris/system.py @@ -1,5 +1,5 @@ from threading import Thread -import os, logging, subprocess, json +import logging, pathlib, subprocess, json # import logger logger = logging.getLogger(__name__) @@ -9,7 +9,7 @@ class IrisSystemThread(Thread): Thread.__init__(self) self.action = action self.callback = callback - self.path = os.path.dirname(__file__) + self.script_path = pathlib.Path(__file__).parent / "system.sh" ## # Run the defined action @@ -31,9 +31,9 @@ class IrisSystemThread(Thread): 'error': error } - logger.debug("sudo "+ self.path +"/system.sh "+ self.action) + logger.debug("sudo %s %s", self.script_path, self.action) - proc = subprocess.Popen(["sudo", self.path+"/system.sh", self.action], + proc = subprocess.Popen(["sudo", str(self.script_path), self.action], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) @@ -56,12 +56,12 @@ class IrisSystemThread(Thread): def can_run(self, *args, **kwargs): # Attempt an empty call to our system file - process = subprocess.Popen("sudo -n "+self.path+"/system.sh check", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) + process = subprocess.Popen("sudo -n %s check" % self.script_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) result, error = process.communicate() exitCode = process.wait() # Some kind of failure, so we can't run any commands this way if exitCode > 0: - raise Exception("Password-less access to "+self.path+"/system.sh was refused. Check your /etc/sudoers file.") + raise Exception("Password-less access to %s was refused. Check your /etc/sudoers file." % self.script_path) else: return True