[Happydoc-checkins] CVS: HappyDoc3/happydoclib utils.py,1.2,1.3
Brought to you by:
doughellmann,
krlosaqp
From: Doug H. <dou...@us...> - 2003-01-18 19:57:38
|
Update of /cvsroot/happydoc/HappyDoc3/happydoclib In directory sc8-pr-cvs1:/tmp/cvs-serv12527/happydoclib Modified Files: utils.py Log Message: Add getMimeType() function which wraps the mimetypes module guess_type() with logic to deal with directories. Add extractSummary() function for pulling one line descriptions out of text blobs. Index: utils.py =================================================================== RCS file: /cvsroot/happydoc/HappyDoc3/happydoclib/utils.py,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** utils.py 28 Dec 2002 15:57:53 -0000 1.2 --- utils.py 18 Jan 2003 19:57:33 -0000 1.3 *************** *** 55,59 **** --- 55,61 ---- # Import system modules # + import mimetypes import os + import re # *************** *** 65,76 **** # Module # ! def isSomethingThatLooksLikeDirectory(path): """Returns boolean indicating whether or not path looks like a directory. """ return (os.path.isdir(path) or ! os.path.islink(path) or os.path.ismount(path) ) --- 67,117 ---- # 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 |