#!/usr/bin/env python
#
# $Id: utils.py,v 1.4 2006/12/05 13:10:45 doughellmann Exp $
#
# Copyright 2002 Doug Hellmann.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""Utility functions which do not seem to fit elsewhere.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: utils.py,v $',
'rcs_id' : '$Id: utils.py,v 1.4 2006/12/05 13:10:45 doughellmann Exp $',
'creator' : 'Doug Hellmann',
'project' : 'HappyDoc',
'created' : 'Sun, 15-Dec-2002 10:12:56 EST',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.4 $',
'date' : '$Date: 2006/12/05 13:10:45 $',
}
try:
__version__ = __rcs_info__['version'].split(' ')[1]
except:
__version__ = '0.0'
#
# Import system modules
#
import mimetypes
import os
import re
#
# Import Local modules
#
#
# Module
#
mimetypes.types_map['.stx'] = 'text/x-structured'
def isSomethingThatLooksLikeDirectory(path, includeLinks=1):
"""Returns boolean indicating whether or not path looks like a directory.
"""
return (os.path.isdir(path)
or
(includeLinks and os.path.islink(path))
or
os.path.ismount(path)
)
def getMimeType(path):
"""Returns the mimetype and encoding for path.
"""
if isSomethingThatLooksLikeDirectory(path, includeLinks=0):
mimetype = 'application/x-directory'
encoding = None
else:
mimetype, encoding = mimetypes.guess_type(path)
if (mimetype, encoding) == (None, None) and os.path.islink(path):
mimetype = 'application/x-directory'
encoding = None
return (mimetype, encoding)
def extractSummary(text,
_summary_pattern = re.compile(r'^\s*([^\n]+)\n'),
):
"""Given a block of text, extract a summary from it.
"""
text = text.strip()
#
# Remove surrounding quotes, if present.
#
while text and (text[0] in ('"', "'")):
text = text[1:]
while text and (text[-1] in ('"', "'")):
text = text[:-1]
#
# Pull out the first line, and return it if
# we can find it. Otherwise, return the whole
# string since that means that the whole thing
# is just one line.
#
matchObj = _summary_pattern.search(text)
if matchObj:
return matchObj.group(0).strip()
else:
return text
|