Formatted docstrings
This commit is contained in:
parent
5ed86e2666
commit
cccbfa2c34
|
@ -150,11 +150,10 @@ def no_extension_basename(path):
|
||||||
else: return '.'.join(base.split(".")[0:-1])
|
else: return '.'.join(base.split(".")[0:-1])
|
||||||
|
|
||||||
def walk_supported(directory, clean_empties=False):
|
def walk_supported(directory, clean_empties=False):
|
||||||
"""
|
""" A small generator wrapper around os.walk to only give us files
|
||||||
A small generator wrapper around os.walk to only give us files that support
|
that support the extensions we are considering. When clean_empties
|
||||||
the extensions we are considering. When clean_empties is True we
|
is True we recursively delete empty directories left over in
|
||||||
recursively delete empty directories left over in directory after the walk.
|
directory after the walk. """
|
||||||
"""
|
|
||||||
for root, dirs, files in os.walk(directory):
|
for root, dirs, files in os.walk(directory):
|
||||||
full_paths = ( os.path.join(root, name) for name in files
|
full_paths = ( os.path.join(root, name) for name in files
|
||||||
if is_file_supported(name) )
|
if is_file_supported(name) )
|
||||||
|
@ -168,10 +167,8 @@ def file_locked(path):
|
||||||
return bool(f.readlines())
|
return bool(f.readlines())
|
||||||
|
|
||||||
def magic_move(old, new, after_dir_make=lambda : None):
|
def magic_move(old, new, after_dir_make=lambda : None):
|
||||||
"""
|
""" Moves path old to new and constructs the necessary to
|
||||||
Moves path old to new and constructs the necessary to directories for new
|
directories for new along the way """
|
||||||
along the way
|
|
||||||
"""
|
|
||||||
new_dir = os.path.dirname(new)
|
new_dir = os.path.dirname(new)
|
||||||
if not os.path.exists(new_dir): os.makedirs(new_dir)
|
if not os.path.exists(new_dir): os.makedirs(new_dir)
|
||||||
# We need this crusty hack because anytime a directory is created we must
|
# We need this crusty hack because anytime a directory is created we must
|
||||||
|
@ -181,18 +178,15 @@ def magic_move(old, new, after_dir_make=lambda : None):
|
||||||
shutil.move(old,new)
|
shutil.move(old,new)
|
||||||
|
|
||||||
def move_to_dir(dir_path,file_path):
|
def move_to_dir(dir_path,file_path):
|
||||||
"""
|
""" moves a file at file_path into dir_path/basename(filename) """
|
||||||
moves a file at file_path into dir_path/basename(filename)
|
|
||||||
"""
|
|
||||||
bs = os.path.basename(file_path)
|
bs = os.path.basename(file_path)
|
||||||
magic_move(file_path, os.path.join(dir_path, bs))
|
magic_move(file_path, os.path.join(dir_path, bs))
|
||||||
|
|
||||||
def apply_rules_dict(d, rules):
|
def apply_rules_dict(d, rules):
|
||||||
"""
|
""" Consumes a dictionary of rules that maps some keys to lambdas
|
||||||
Consumes a dictionary of rules that maps some keys to lambdas which it
|
which it applies to every matching element in d and returns a new
|
||||||
applies to every matching element in d and returns a new dictionary with
|
dictionary with the rules applied. If a rule returns none then it's
|
||||||
the rules applied. If a rule returns none then it's not applied
|
not applied """
|
||||||
"""
|
|
||||||
new_d = copy.deepcopy(d)
|
new_d = copy.deepcopy(d)
|
||||||
for k, rule in rules.iteritems():
|
for k, rule in rules.iteritems():
|
||||||
if k in d:
|
if k in d:
|
||||||
|
@ -207,17 +201,14 @@ def default_to_f(dictionary, keys, default, condition):
|
||||||
return new_d
|
return new_d
|
||||||
|
|
||||||
def default_to(dictionary, keys, default):
|
def default_to(dictionary, keys, default):
|
||||||
"""
|
""" Checks if the list of keys 'keys' exists in 'dictionary'. If
|
||||||
Checks if the list of keys 'keys' exists in 'dictionary'. If not then it
|
not then it returns a new dictionary with all those missing keys
|
||||||
returns a new dictionary with all those missing keys defaults to 'default'
|
defaults to 'default' """
|
||||||
"""
|
|
||||||
cnd = lambda dictionary, key: key not in dictionary
|
cnd = lambda dictionary, key: key not in dictionary
|
||||||
return default_to_f(dictionary, keys, default, cnd)
|
return default_to_f(dictionary, keys, default, cnd)
|
||||||
|
|
||||||
def remove_whitespace(dictionary):
|
def remove_whitespace(dictionary):
|
||||||
"""
|
""" Remove values that empty whitespace in the dictionary """
|
||||||
Remove values that empty whitespace in the dictionary
|
|
||||||
"""
|
|
||||||
nd = copy.deepcopy(dictionary)
|
nd = copy.deepcopy(dictionary)
|
||||||
bad_keys = []
|
bad_keys = []
|
||||||
for k,v in nd.iteritems():
|
for k,v in nd.iteritems():
|
||||||
|
@ -303,10 +294,9 @@ def organized_path(old_path, root_path, orig_md):
|
||||||
# TODO : Get rid of this function and every one of its uses. We no longer use
|
# TODO : Get rid of this function and every one of its uses. We no longer use
|
||||||
# the md5 signature of a song for anything
|
# the md5 signature of a song for anything
|
||||||
def file_md5(path,max_length=100):
|
def file_md5(path,max_length=100):
|
||||||
"""
|
""" Get md5 of file path (if it exists). Use only max_length
|
||||||
Get md5 of file path (if it exists). Use only max_length characters to save
|
characters to save time and memory. Pass max_length=-1 to read the
|
||||||
time and memory. Pass max_length=-1 to read the whole file (like in mm1)
|
whole file (like in mm1) """
|
||||||
"""
|
|
||||||
if os.path.exists(path):
|
if os.path.exists(path):
|
||||||
with open(path, 'rb') as f:
|
with open(path, 'rb') as f:
|
||||||
m = hashlib.md5()
|
m = hashlib.md5()
|
||||||
|
@ -322,16 +312,12 @@ def encode_to(obj, encoding='utf-8'):
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def convert_dict_value_to_utf8(md):
|
def convert_dict_value_to_utf8(md):
|
||||||
"""
|
""" formats a dictionary to send as a request to api client """
|
||||||
formats a dictionary to send as a request to api client
|
|
||||||
"""
|
|
||||||
return dict([(item[0], encode_to(item[1], "utf-8")) for item in md.items()])
|
return dict([(item[0], encode_to(item[1], "utf-8")) for item in md.items()])
|
||||||
|
|
||||||
def get_system_locale(locale_path='/etc/default/locale'):
|
def get_system_locale(locale_path='/etc/default/locale'):
|
||||||
"""
|
""" Returns the configuration object for the system's default
|
||||||
Returns the configuration object for the system's default locale. Normally
|
locale. Normally requires root access. """
|
||||||
requires root access.
|
|
||||||
"""
|
|
||||||
if os.path.exists(locale_path):
|
if os.path.exists(locale_path):
|
||||||
try:
|
try:
|
||||||
config = ConfigObj(locale_path)
|
config = ConfigObj(locale_path)
|
||||||
|
@ -341,9 +327,7 @@ def get_system_locale(locale_path='/etc/default/locale'):
|
||||||
permissions issue?" % locale_path)
|
permissions issue?" % locale_path)
|
||||||
|
|
||||||
def configure_locale(config):
|
def configure_locale(config):
|
||||||
"""
|
""" sets the locale according to the system's locale. """
|
||||||
sets the locale according to the system's locale.
|
|
||||||
"""
|
|
||||||
current_locale = locale.getlocale()
|
current_locale = locale.getlocale()
|
||||||
if current_locale[1] is None:
|
if current_locale[1] is None:
|
||||||
default_locale = locale.getdefaultlocale()
|
default_locale = locale.getdefaultlocale()
|
||||||
|
@ -360,10 +344,8 @@ def configure_locale(config):
|
||||||
|
|
||||||
def fondle(path,times=None):
|
def fondle(path,times=None):
|
||||||
# TODO : write unit tests for this
|
# TODO : write unit tests for this
|
||||||
"""
|
""" touch a file to change the last modified date. Beware of calling
|
||||||
touch a file to change the last modified date. Beware of calling this
|
this function on the same file from multiple threads. """
|
||||||
function on the same file from multiple threads.
|
|
||||||
"""
|
|
||||||
with file(path, 'a'): os.utime(path, times)
|
with file(path, 'a'): os.utime(path, times)
|
||||||
|
|
||||||
def last_modified(path):
|
def last_modified(path):
|
||||||
|
|
Loading…
Reference in New Issue