Changeset - 5b0182470cfc
[Not reviewed]
0 1 0
Lance Edgar (lance) - 11 years ago 2013-05-22 23:40:43
lance@edbob.org
Added `temp_path()` function in `files` module.
1 file changed with 15 insertions and 0 deletions:
0 comments (0 inline, 0 general)
rattail/files.py
Show inline comments
 
@@ -19,27 +19,29 @@
 
#
 
#  You should have received a copy of the GNU Affero General Public License
 
#  along with Rattail.  If not, see <http://www.gnu.org/licenses/>.
 
#
 
################################################################################
 

	
 
"""
 
``rattail.files`` -- File Utilities
 

	
 
This module contains various utility functions for use with the filesystem.
 
"""
 

	
 
import os
 
import os.path
 
import shutil
 
import lockfile
 
import tempfile
 
from datetime import datetime
 

	
 
import pkg_resources
 

	
 

	
 
def count_lines(path):
 
    """
 
    Counts the number of lines in a text file.  Some attempt is made to ensure
 
    cross-platform compatibility.
 

	
 
    :param path: Path to the file.
 
    :type path: string
 
@@ -107,12 +109,25 @@ def resource_path(path):
 

	
 
    :returns: Absolute file path to the resource.
 
    :rtype: string
 

	
 
    If ``path`` is a package resource specifier, and the package containing it
 
    is a zipped egg, then the resource will be extracted and the resultant
 
    filename will be returned.
 
    """
 

	
 
    if not os.path.isabs(path) and ':' in path:
 
        return pkg_resources.resource_filename(*path.split(':'))
 
    return path
 

	
 

	
 
def temp_path(suffix='.tmp', prefix='rattail.', **kwargs):
 
    """
 
    Returns a path for a temporary file.
 

	
 
    This is a convenience function which wraps ``tempfile.mkstemp()``.  The
 
    meaning of the arguments is the same.
 
    """
 

	
 
    fd, path = tempfile.mkstemp(suffix, prefix, **kwargs)
 
    os.close(fd)
 
    return path
0 comments (0 inline, 0 general)