From e7ce00d54f9606499c32cb5d3c05be404ee79aef Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Thu, 31 Dec 2015 12:49:41 +0100 Subject: [PATCH] Style --- radicale/__main__.py | 9 ++++---- radicale/config.py | 2 +- radicale/pathutils.py | 35 ++++++++++++++++++++--------- radicale/rights/__init__.py | 7 ++++-- radicale/storage/filesystem.py | 12 +++++----- radicale/storage/multifilesystem.py | 24 ++++++++++---------- 6 files changed, 53 insertions(+), 36 deletions(-) diff --git a/radicale/__main__.py b/radicale/__main__.py index 547abe1..040c2af 100644 --- a/radicale/__main__.py +++ b/radicale/__main__.py @@ -102,11 +102,12 @@ def run(): # Check and create PID file in a race-free manner if config.get("server", "pid"): try: - pid_fd = os.open(config.get("server", "pid"), - os.O_CREAT | os.O_EXCL | os.O_WRONLY) + pid_fd = os.open( + config.get("server", "pid"), + os.O_CREAT | os.O_EXCL | os.O_WRONLY) except: - raise OSError("PID file exists: %s" % - config.get("server", "pid")) + raise OSError( + "PID file exists: %s" % config.get("server", "pid")) pid = os.fork() if pid: sys.exit() diff --git a/radicale/config.py b/radicale/config.py index 5d2b799..656266e 100644 --- a/radicale/config.py +++ b/radicale/config.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # # This file is part of Radicale Server - Calendar Server -# Copyright © 2008-2013 Guillaume Ayoub +# Copyright © 2008-2015 Guillaume Ayoub # Copyright © 2008 Nicolas Kandel # Copyright © 2008 Pascal Halter # diff --git a/radicale/pathutils.py b/radicale/pathutils.py index 2aa13af..6d1a96f 100644 --- a/radicale/pathutils.py +++ b/radicale/pathutils.py @@ -16,7 +16,7 @@ # along with Radicale. If not, see . """ -Helper functions for working with paths +Helper functions for working with paths. """ @@ -27,8 +27,11 @@ from . import log def sanitize_path(path): - """Make absolute (with leading slash) to prevent access to other data. - Preserves an potential trailing slash.""" + """Make path absolute with leading slash to prevent access to other data. + + Preserve a potential trailing slash. + + """ trailing_slash = "/" if path.endswith("/") else "" path = posixpath.normpath(path) new_path = "/" @@ -41,7 +44,11 @@ def sanitize_path(path): def is_safe_path_component(path): - """Checks if path is a single component of a path and is safe to join""" + """Check if path is a single component of a POSIX path. + + Check that the path is safe to join too. + + """ if not path: return False head, _ = posixpath.split(path) @@ -53,8 +60,11 @@ def is_safe_path_component(path): def is_safe_filesystem_path_component(path): - """Checks if path is a single component of a local filesystem path - and is safe to join""" + """Check if path is a single component of a filesystem path. + + Check that the path is safe to join too. + + """ if not path: return False drive, _ = os.path.splitdrive(path) @@ -69,16 +79,19 @@ def is_safe_filesystem_path_component(path): def path_to_filesystem(path, base_folder): - """Converts path to a local filesystem path relative to base_folder - in a secure manner or raises ValueError.""" + """Convert path to a local filesystem path relative to base_folder. + + Conversion is done in a secure manner, or raises ValueError. + + """ sane_path = sanitize_path(path).strip("/") safe_path = base_folder if not sane_path: return safe_path for part in sane_path.split("/"): if not is_safe_filesystem_path_component(part): - log.LOGGER.debug("Can't translate path safely to filesystem: %s", - path) + log.LOGGER.debug( + "Can't translate path safely to filesystem: %s", path) raise ValueError("Unsafe path") safe_path = os.path.join(safe_path, part) - return safe_path \ No newline at end of file + return safe_path diff --git a/radicale/rights/__init__.py b/radicale/rights/__init__.py index 2fa6cd6..be8ddff 100644 --- a/radicale/rights/__init__.py +++ b/radicale/rights/__init__.py @@ -23,6 +23,7 @@ This module loads the rights backend, according to the rights configuration. """ + import sys from .. import config @@ -43,7 +44,9 @@ def load(): def authorized(user, collection, right): - """ Check when user has rights on collection - This method is overriden when appropriate rights backend loaded. + """Check that an user has rights on a collection. + + This method is overriden when the appropriate rights backend is loaded. + """ raise NotImplementedError() diff --git a/radicale/storage/filesystem.py b/radicale/storage/filesystem.py index 7b5e1b6..deeba20 100644 --- a/radicale/storage/filesystem.py +++ b/radicale/storage/filesystem.py @@ -103,8 +103,7 @@ class Collection(ical.Collection): # make sure that the local filename can be translated # into an internal path if not pathutils.is_safe_path_component(filename): - log.LOGGER.debug("Skipping unsupported filename: %s", - filename) + log.LOGGER.debug("Skipping unsupported filename: %s", filename) continue rel_filename = posixpath.join(path, filename) if cls.is_node(rel_filename) or cls.is_leaf(rel_filename): @@ -118,13 +117,14 @@ class Collection(ical.Collection): @classmethod def is_leaf(cls, path): filesystem_path = pathutils.path_to_filesystem(path, FOLDER) - return (os.path.isfile(filesystem_path) and not - filesystem_path.endswith(".props")) + return ( + os.path.isfile(filesystem_path) and not + filesystem_path.endswith(".props")) @property def last_modified(self): - modification_time = \ - time.gmtime(os.path.getmtime(self._filesystem_path)) + modification_time = time.gmtime( + os.path.getmtime(self._filesystem_path)) return time.strftime("%a, %d %b %Y %H:%M:%S +0000", modification_time) @property diff --git a/radicale/storage/multifilesystem.py b/radicale/storage/multifilesystem.py index 93cec87..d4243d9 100644 --- a/radicale/storage/multifilesystem.py +++ b/radicale/storage/multifilesystem.py @@ -84,8 +84,9 @@ class Collection(filesystem.Collection): try: filenames = os.listdir(self._filesystem_path) except (OSError, IOError) as e: - log.LOGGER.info('Error while reading collection %r: %r' - % (self._filesystem_path, e)) + log.LOGGER.info( + 'Error while reading collection %r: %r' % ( + self._filesystem_path, e)) return "" for filename in filenames: @@ -94,25 +95,24 @@ class Collection(filesystem.Collection): with filesystem.open(path) as fd: items.update(self._parse(fd.read(), components)) except (OSError, IOError) as e: - log.LOGGER.warning('Error while reading item %r: %r' - % (path, e)) + log.LOGGER.warning( + 'Error while reading item %r: %r' % (path, e)) return ical.serialize( self.tag, self.headers, sorted(items, key=lambda x: x.name)) @classmethod def is_node(cls, path): - filesystem_path = pathutils.path_to_filesystem(path, - filesystem.FOLDER) - return (os.path.isdir(filesystem_path) and - not os.path.exists(filesystem_path + ".props")) + filesystem_path = pathutils.path_to_filesystem(path, filesystem.FOLDER) + return ( + os.path.isdir(filesystem_path) and + not os.path.exists(filesystem_path + ".props")) @classmethod def is_leaf(cls, path): - filesystem_path = pathutils.path_to_filesystem(path, - filesystem.FOLDER) - return (os.path.isdir(filesystem_path) and - os.path.exists(path + ".props")) + filesystem_path = pathutils.path_to_filesystem(path, filesystem.FOLDER) + return ( + os.path.isdir(filesystem_path) and os.path.exists(path + ".props")) @property def last_modified(self):