more docstring fixes

This commit is contained in:
niphlod
2014-01-29 23:01:52 +01:00
parent ce2d958f9a
commit 41a4de081f
8 changed files with 189 additions and 109 deletions
+67 -29
View File
@@ -2,9 +2,12 @@
# -*- coding: utf-8 -*-
"""
This file is part of the web2py Web Framework
Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
| This file is part of the web2py Web Framework
| Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
| License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
File operations
---------------
"""
import storage
@@ -45,7 +48,15 @@ __all__ = [
def parse_semantic(version="Version 1.99.0-rc.1+timestamp.2011.09.19.08.23.26"):
"http://semver.org/"
"""Parses a version string according to http://semver.org/ rules
Args:
version(str): the SemVer string
Returns:
tuple: Major, Minor, Patch, Release, Build Date
"""
re_version = re.compile('(\d+)\.(\d+)\.(\d+)(\-(?P<pre>[^\s+]*))?(\+(?P<build>\S*))')
m = re_version.match(version.strip().split()[-1])
if not m:
@@ -58,6 +69,15 @@ def parse_semantic(version="Version 1.99.0-rc.1+timestamp.2011.09.19.08.23.26"):
return (a, b, c, pre_release, build)
def parse_legacy(version="Version 1.99.0 (2011-09-19 08:23:26)"):
"""Parses "legacy" version string
Args:
version(str): the version string
Returns:
tuple: Major, Minor, Patch, Release, Build Date
"""
re_version = re.compile('[^\d]+ (\d+)\.(\d+)\.(\d+)\s*\((?P<datetime>.+?)\)\s*(?P<type>[a-z]+)?')
m = re_version.match(version)
a, b, c = int(m.group(1)), int(m.group(2)), int(m.group(3)),
@@ -66,13 +86,17 @@ def parse_legacy(version="Version 1.99.0 (2011-09-19 08:23:26)"):
return (a, b, c, pre_release, build)
def parse_version(version):
"""Attempts to parse SemVer, fallbacks on legacy
"""
version_tuple = parse_semantic(version)
if not version_tuple:
version_tuple = parse_legacy(version)
return version_tuple
def read_file(filename, mode='r'):
"returns content from filename, making sure to close the file explicitly on exit."
"""Returns content from filename, making sure to close the file explicitly
on exit.
"""
f = open(filename, mode)
try:
return f.read()
@@ -81,7 +105,9 @@ def read_file(filename, mode='r'):
def write_file(filename, value, mode='w'):
"writes <value> to filename, making sure to close the file explicitly on exit."
"""Writes <value> to filename, making sure to close the file
explicitly on exit.
"""
f = open(filename, mode)
try:
return f.write(value)
@@ -90,7 +116,8 @@ def write_file(filename, value, mode='w'):
def readlines_file(filename, mode='r'):
"applies .split('\n') to the output of read_file()"
"""Applies .split('\n') to the output of `read_file()`
"""
return read_file(filename, mode).split('\n')
@@ -112,8 +139,8 @@ def listdir(
maxnum = None,
):
"""
like os.listdir() but you can specify a regex pattern to filter files.
if add_dirs is True, the returned items will have the full path.
Like `os.listdir()` but you can specify a regex pattern to filter files.
If `add_dirs` is True, the returned items will have the full path.
"""
if path[-1:] != os.path.sep:
path = path + os.path.sep
@@ -122,7 +149,7 @@ def listdir(
else:
n = 0
regex = re.compile(expression)
items = []
items = []
for (root, dirs, files) in os.walk(path, topdown=True):
for dir in dirs[:]:
if dir.startswith('.'):
@@ -141,6 +168,8 @@ def listdir(
def recursive_unlink(f):
"""Deletes `f`. If it's a folder, also its contents will be deleted
"""
if os.path.isdir(f):
for s in os.listdir(f):
recursive_unlink(os.path.join(f, s))
@@ -150,8 +179,7 @@ def recursive_unlink(f):
def cleanpath(path):
"""
turns any expression/path into a valid filename. replaces / with _ and
"""Turns any expression/path into a valid filename. replaces / with _ and
removes special characters.
"""
@@ -165,6 +193,7 @@ def cleanpath(path):
def _extractall(filename, path='.', members=None):
# FIXME: this should be dropped because python 2.4 support was dropped
if not hasattr(tarfile.TarFile, 'extractall'):
from tarfile import ExtractError
@@ -172,10 +201,10 @@ def _extractall(filename, path='.', members=None):
def extractall(self, path='.', members=None):
"""Extract all members from the archive to the current working
directory and set owner, modification time and permissions on
directories afterwards. `path' specifies a different directory
to extract to. `members' is optional and must be a subset of the
list returned by getmembers().
directory and set owner, modification time and permissions on
directories afterwards. `path' specifies a different directory
to extract to. `members' is optional and must be a subset of the
list returned by getmembers().
"""
directories = []
@@ -226,8 +255,7 @@ def _extractall(filename, path='.', members=None):
def tar(file, dir, expression='^.+$', filenames=None):
"""
tars dir into file, only tars file that match expression
"""Tars dir into file, only tars file that match expression
"""
tar = tarfile.TarFile(file, 'w')
@@ -240,14 +268,21 @@ def tar(file, dir, expression='^.+$', filenames=None):
tar.close()
def untar(file, dir):
"""
untar file into dir
"""Untar file into dir
"""
_extractall(file, dir)
def w2p_pack(filename, path, compiled=False, filenames=None):
"""Packs a web2py application.
Args:
filename(str): path to the resulting archive
path(str): path to the application
compiled(bool): if `True` packs the compiled version
filenames(list): adds filenames to the archive
"""
filename = abspath(filename)
path = abspath(path)
tarname = filename + '.tar'
@@ -296,10 +331,12 @@ def w2p_unpack(filename, path, delete_tar=True):
def w2p_pack_plugin(filename, path, plugin_name):
"""Pack the given plugin into a w2p file.
Will match files at:
"""Packs the given plugin into a w2p file.
Will match files at::
<path>/*/plugin_[name].*
<path>/*/plugin_[name]/*
"""
filename = abspath(filename)
path = abspath(path)
@@ -329,9 +366,8 @@ def w2p_unpack_plugin(filename, path, delete_tar=True):
def tar_compiled(file, dir, expression='^.+$'):
"""
used to tar a compiled application.
the content of models, views, controllers is not stored in the tar file.
"""Used to tar a compiled application.
The content of models, views, controllers is not stored in the tar file.
"""
tar = tarfile.TarFile(file, 'w')
@@ -357,7 +393,7 @@ def up(path):
def get_session(request, other_application='admin'):
""" checks that user is authorized to access other_application"""
"""Checks that user is authorized to access other_application"""
if request.application == other_application:
raise KeyError
try:
@@ -370,7 +406,7 @@ def get_session(request, other_application='admin'):
return osession
def set_session(request, session, other_application='admin'):
""" checks that user is authorized to access other_application"""
"""Checks that user is authorized to access other_application"""
if request.application == other_application:
raise KeyError
session_id = request.cookies['session_id_' + other_application].value
@@ -380,7 +416,7 @@ def set_session(request, session, other_application='admin'):
def check_credentials(request, other_application='admin',
expiration=60 * 60, gae_login=True):
""" checks that user is authorized to access other_application"""
"""Checks that user is authorized to access other_application"""
if request.env.web2py_runtime_gae:
from google.appengine.api import users
if users.is_current_user_admin():
@@ -455,7 +491,9 @@ from settings import global_settings # we need to import settings here because
def abspath(*relpath, **base):
"convert relative path to absolute path based (by default) on applications_parent"
"""Converts relative path to absolute path based (by default) on
applications_parent
"""
path = os.path.join(*relpath)
gluon = base.get('gluon', False)
if os.path.isabs(path):