"Fossies" - the Fresh Open Source Software Archive

Member "cheetah3-3.2.6.post2/Cheetah/Utils/Misc.py" (20 Apr 2021, 2191 Bytes) of package /linux/www/cheetah3-3.2.6.post2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "Misc.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 3-3.0.1_vs_3-3.1.0.

    1 """
    2     Miscellaneous functions/objects used by Cheetah but also useful standalone.
    3 """
    4 import os          # Used in mkdirsWithPyInitFile.
    5 import sys         # Used in die.
    6 
    7 ##################################################
    8 # MISCELLANEOUS FUNCTIONS
    9 
   10 
   11 def die(reason):
   12     sys.stderr.write(reason + '\n')
   13     sys.exit(1)
   14 
   15 
   16 def useOrRaise(thing, errmsg=''):
   17     """Raise 'thing' if it's a subclass of Exception.  Otherwise return it.
   18 
   19     Called by: Cheetah.Servlet.cgiImport()
   20     """
   21     if isinstance(thing, type) and issubclass(thing, Exception):
   22         raise thing(errmsg)
   23     return thing
   24 
   25 
   26 def checkKeywords(dic, legalKeywords, what='argument'):
   27     """Verify no illegal keyword arguments were passed to a function.
   28 
   29     in : dic, dictionary (**kw in the calling routine).
   30          legalKeywords, list of strings, the keywords that are allowed.
   31          what, string, suffix for error message (see function source).
   32     out: None.
   33     exc: TypeError if 'dic' contains a key not in 'legalKeywords'.
   34     called by: Cheetah.Template.__init__()
   35     """
   36     # XXX legalKeywords could be a set when sets get added to Python.
   37     for k in dic:
   38         if k not in legalKeywords:
   39             raise TypeError("'%s' is not a valid %s" % (k, what))
   40 
   41 
   42 def removeFromList(list_, *elements):
   43     """Save as list_.remove(each element) but don't raise an error if
   44        element is missing.  Modifies 'list_' in place!  Returns None.
   45     """
   46     for elm in elements:
   47         try:
   48             list_.remove(elm)
   49         except ValueError:
   50             pass
   51 
   52 
   53 def mkdirsWithPyInitFiles(path):
   54     """Same as os.makedirs (mkdir 'path' and all missing parent directories)
   55        but also puts a Python '__init__.py' file in every directory it
   56        creates.  Does nothing (without creating an '__init__.py' file) if the
   57        directory already exists.
   58     """
   59     dir, fil = os.path.split(path)
   60     if dir and not os.path.exists(dir):
   61         mkdirsWithPyInitFiles(dir)
   62     if not os.path.exists(path):
   63         os.mkdir(path)
   64         init = os.path.join(path, "__init__.py")
   65         f = open(init, 'w')  # Open and close to produce empty file.
   66         f.close()
   67 
   68 # vim: shiftwidth=4 tabstop=4 expandtab