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