directory.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 
00003 # Software License Agreement (BSD License)
00004 #
00005 # Copyright (C) 2015, Jack O'Quin
00006 # All rights reserved.
00007 #
00008 # Redistribution and use in source and binary forms, with or without
00009 # modification, are permitted provided that the following conditions
00010 # are met:
00011 #
00012 #  * Redistributions of source code must retain the above copyright
00013 #    notice, this list of conditions and the following disclaimer.
00014 #  * Redistributions in binary form must reproduce the above
00015 #    copyright notice, this list of conditions and the following
00016 #    disclaimer in the documentation and/or other materials provided
00017 #    with the distribution.
00018 #  * Neither the name of the author nor of other contributors may be
00019 #    used to endorse or promote products derived from this software
00020 #    without specific prior written permission.
00021 #
00022 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 # POSSIBILITY OF SUCH DAMAGE.
00034 
00035 """.. module:: directory
00036 
00037 This Python module manages the directory into which logs are saved.
00038 """
00039 # enable some python3 compatibility options:
00040 from __future__ import absolute_import, print_function
00041 
00042 import os
00043 
00044 
00045 class LoggingDirectory(object):
00046     """Logging directory object.
00047 
00048     :param directory: Directory path in which to save logs, or ``None``.
00049     """
00050 
00051     def __init__(self, directory):
00052         """ Constructor.
00053 
00054         :raises: :exc:`OSError` if no usable directory found.
00055         """
00056         try_dirs = []                 # list of directories to try
00057         if directory:
00058             try_dirs.append(directory)
00059         else:
00060             home = os.path.expanduser('~')
00061             if home[0] != '~':          # expansion successful?
00062                 try_dirs.append(os.path.join(home, '.ros',
00063                                              'bwi', 'bwi_logging'))
00064 
00065         # /tmp/bwi/bwi_logging is the last resort if nothing else works
00066         try_dirs.append(os.path.join('/tmp', 'bwi', 'bwi_logging'))
00067 
00068         for d in try_dirs:
00069             if os.access(d, os.W_OK):       # is it writable?
00070                 self.logdir = d
00071                 return
00072             else:                           # create it, if possible
00073                 um = os.umask(0o002)        # override group umask setting
00074                 try:
00075                     os.makedirs(d, mode=0o2775)
00076                 except OSError:
00077                     os.umask(um)            # restore umask
00078                     continue                # try another location
00079                 else:
00080                     os.umask(um)            # restore umask
00081                     self.logdir = d
00082                     return
00083 
00084         # nothing worked
00085         raise OSError('error: no logging directory accessible')
00086 
00087     def chdir(self):
00088         """ Change current directory to the selected logging directory. """
00089         os.chdir(self.logdir)
00090 
00091     def pwd(self):
00092         """ :returns: selected logging directory. """
00093         return self.logdir


bwi_logging
Author(s): Shiqi Zhang
autogenerated on Thu Jun 6 2019 17:57:20