[3] | 1 | # $Id: Misc.py,v 1.8 2005/11/02 22:26:08 tavis_rudd Exp $ |
---|
| 2 | """Miscellaneous functions/objects used by Cheetah but also useful standalone. |
---|
| 3 | |
---|
| 4 | Meta-Data |
---|
| 5 | ================================================================================ |
---|
| 6 | Author: Mike Orr <iron@mso.oz.net> |
---|
| 7 | License: This software is released for unlimited distribution under the |
---|
| 8 | terms of the MIT license. See the LICENSE file. |
---|
| 9 | Version: $Revision: 1.8 $ |
---|
| 10 | Start Date: 2001/11/07 |
---|
| 11 | Last Revision Date: $Date: 2005/11/02 22:26:08 $ |
---|
| 12 | """ |
---|
| 13 | __author__ = "Mike Orr <iron@mso.oz.net>" |
---|
| 14 | __revision__ = "$Revision: 1.8 $"[11:-2] |
---|
| 15 | |
---|
| 16 | import os # Used in mkdirsWithPyInitFile. |
---|
| 17 | import types # Used in useOrRaise. |
---|
| 18 | import sys # Used in die. |
---|
| 19 | |
---|
| 20 | ################################################## |
---|
| 21 | ## MISCELLANEOUS FUNCTIONS |
---|
| 22 | |
---|
| 23 | def die(reason): |
---|
| 24 | sys.stderr.write(reason + '\n') |
---|
| 25 | sys.exit(1) |
---|
| 26 | |
---|
| 27 | def useOrRaise(thing, errmsg=''): |
---|
| 28 | """Raise 'thing' if it's a subclass of Exception. Otherwise return it. |
---|
| 29 | |
---|
| 30 | Called by: Cheetah.Servlet.cgiImport() |
---|
| 31 | """ |
---|
| 32 | if type(thing) == types.ClassType and issubclass(thing, Exception): |
---|
| 33 | raise thing(errmsg) |
---|
| 34 | return thing |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | def checkKeywords(dic, legalKeywords, what='argument'): |
---|
| 38 | """Verify no illegal keyword arguments were passed to a function. |
---|
| 39 | |
---|
| 40 | in : dic, dictionary (**kw in the calling routine). |
---|
| 41 | legalKeywords, list of strings, the keywords that are allowed. |
---|
| 42 | what, string, suffix for error message (see function source). |
---|
| 43 | out: None. |
---|
| 44 | exc: TypeError if 'dic' contains a key not in 'legalKeywords'. |
---|
| 45 | called by: Cheetah.Template.__init__() |
---|
| 46 | """ |
---|
| 47 | # XXX legalKeywords could be a set when sets get added to Python. |
---|
| 48 | for k in dic.keys(): # Can be dic.iterkeys() if Python >= 2.2. |
---|
| 49 | if k not in legalKeywords: |
---|
| 50 | raise TypeError("'%s' is not a valid %s" % (k, what)) |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | def removeFromList(list_, *elements): |
---|
| 54 | """Save as list_.remove(each element) but don't raise an error if |
---|
| 55 | element is missing. Modifies 'list_' in place! Returns None. |
---|
| 56 | """ |
---|
| 57 | for elm in elements: |
---|
| 58 | try: |
---|
| 59 | list_.remove(elm) |
---|
| 60 | except ValueError: |
---|
| 61 | pass |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | def mkdirsWithPyInitFiles(path): |
---|
| 65 | """Same as os.makedirs (mkdir 'path' and all missing parent directories) |
---|
| 66 | but also puts a Python '__init__.py' file in every directory it |
---|
| 67 | creates. Does nothing (without creating an '__init__.py' file) if the |
---|
| 68 | directory already exists. |
---|
| 69 | """ |
---|
| 70 | dir, fil = os.path.split(path) |
---|
| 71 | if dir and not os.path.exists(dir): |
---|
| 72 | mkdirsWithPyInitFiles(dir) |
---|
| 73 | if not os.path.exists(path): |
---|
| 74 | os.mkdir(path) |
---|
| 75 | init = os.path.join(path, "__init__.py") |
---|
| 76 | f = open(init, 'w') # Open and close to produce empty file. |
---|
| 77 | f.close() |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | # vim: shiftwidth=4 tabstop=4 expandtab |
---|