OpenSecurity/bin/environment.py
author mb
Thu, 09 Jan 2014 10:44:42 +0100
changeset 46 f659d8fb57a8
parent 16 e16d64b5e008
child 71 0ca25608ed0f
permissions -rwxr-xr-x
latest changes from december 2013
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # environment.py
     6 # 
     7 # pick some current environment infos
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    10 #
    11 # Copyright (C) 2013 AIT Austrian Institute of Technology
    12 # AIT Austrian Institute of Technology GmbH
    13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
    14 # http://www.ait.ac.at
    15 #
    16 # This program is free software; you can redistribute it and/or
    17 # modify it under the terms of the GNU General Public License
    18 # as published by the Free Software Foundation version 2.
    19 # 
    20 # This program is distributed in the hope that it will be useful,
    21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    23 # GNU General Public License for more details.
    24 # 
    25 # You should have received a copy of the GNU General Public License
    26 # along with this program; if not, write to the Free Software
    27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    28 # Boston, MA  02110-1301, USA.
    29 # ------------------------------------------------------------
    30 
    31 
    32 # ------------------------------------------------------------
    33 # imports
    34 
    35 import os
    36 import os.path
    37 import sys
    38 
    39 
    40 # ------------------------------------------------------------
    41 # code
    42 
    43 
    44 class Environment(object):
    45     
    46     """Hold some nifty environment stuff in a dedicated class."""
    47     
    48     def __init__(self, application = None):
    49         
    50         # if we ain't got a path to start from, all is valid/lost
    51         if len(sys.path[0]) == 0:
    52             self._prefix_path = ''
    53             self._data_path = ''
    54             return
    55         
    56         # the prefix path
    57         #
    58         # - on Linux: this is ../../ to the current executable
    59         #   e.g. "/usr/bin/myprogram" --> "/usr"
    60         #
    61         # - on Windows (inkl. Cygwin): this is the installation folder
    62         #   e.g. "C:/Program Files/MyProgram/myprogam" --> "C:/Program Files/MyProgram"
    63         #
    64         if sys.platform == 'linux2':
    65             self._prefix_path = os.path.split(sys.path[0])[0]
    66         elif sys.platform == 'win32' or sys.platform == 'cygwin':
    67             self._prefix_path = sys.path[0]
    68             
    69         # the data path where all data files are stored
    70         if sys.platform == 'linux2':
    71             if not application is None:
    72                 self._data_path = os.path.join(self._prefix_path, os.path.join('share', application))
    73             else:
    74                 self._data_path = os.path.join(self._prefix_path, 'share')
    75         elif sys.platform == 'win32' or sys.platform == 'cygwin':
    76             self._data_path = self._prefix_path
    77 
    78             
    79     def data_path_get(self):
    80         """dat_path get"""
    81         return self._data_path
    82         
    83     data_path = property(data_path_get)
    84             
    85             
    86     def prefix_path_get(self):
    87         """prefix_path get"""
    88         return self._prefix_path
    89         
    90     prefix_path = property(prefix_path_get)
    91             
    92 # test method			
    93 def test():
    94 
    95     """Test: class Environment"""
    96     e = Environment('My Application')
    97     print('prefix_path: "{0}"'.format(e.prefix_path))
    98     print('  data_path: "{0}"'.format(e.data_path))
    99 
   100 
   101 # test the module			
   102 if __name__ == '__main__':
   103     test()