/usr/share/pyshared/traits/util/clean_strings.py is in python-traits 4.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #-----------------------------------------------------------------------------
#
# Copyright (c) 2006 by Enthought, Inc.
# All rights reserved.
#
#-----------------------------------------------------------------------------
""" Provides functions that munge strings to avoid characters that would be
problematic in certain situations.
"""
# Standard library imports.
import copy
import datetime
import keyword
import re
def clean_filename(name):
""" Munge a string to avoid characters that might be problematic as
a filename in some filesystems.
"""
# The only acceptable characters are alphanumeric (in the current locale)
# plus a period and dash.
wordparts = re.split('[^\w\.\-]+', name)
# Filter out empty strings at the beginning or end of the list.
wordparts = filter(None, wordparts)
# Make sure this is an ASCII-encoded string, not a Unicode string.
filename = '_'.join(wordparts).encode('ascii')
return filename
def clean_timestamp(dt=None, microseconds=False):
""" Return a timestamp that has been cleansed of characters that might
cause problems in filenames, namely colons. If no datetime object
is provided, then uses the current time.
Description
-----------
The timestamp is in ISO-8601 format with the following exceptions:
* Colons ':' are replaced by underscores '_'.
* Microseconds are not displayed if the 'microseconds' parameter is
False.
Parameters
----------
dt : None or datetime.datetime object
If None, then the current time is used.
microseconds : bool
Display microseconds or not.
Returns
-------
A string timestamp.
"""
if dt is None:
dt = datetime.datetime.now()
else:
# Operate on a copy.
dt = copy.copy(dt)
if not microseconds:
# The microseconds are largely uninformative but annoying.
dt = dt.replace(microsecond=0)
stamp = dt.isoformat().replace(':', '_')
return stamp
def python_name(name):
""" Attempt to make a valid Python identifier out of a name.
"""
if len(name) > 0:
# Replace spaces with underscores.
name = name.replace(' ', '_').lower()
# If the name is a Python keyword then prefix it with an
# underscore.
if keyword.iskeyword(name):
name = '_' + name
# If the name starts with a digit then prefix it with an
# underscore.
if name[0].isdigit():
name = '_' + name
return name
### EOF ######################################################################
|