om@2: #!/bin/env python om@2: # -*- coding: utf-8 -*- om@2: om@2: # ------------------------------------------------------------ om@2: # environment.py om@2: # om@2: # pick some current environment infos om@2: # om@2: # Autor: Oliver Maurhart, om@2: # om@2: # Copyright (C) 2013 AIT Austrian Institute of Technology om@2: # AIT Austrian Institute of Technology GmbH om@2: # Donau-City-Strasse 1 | 1220 Vienna | Austria om@2: # http://www.ait.ac.at om@2: # om@2: # This program is free software; you can redistribute it and/or om@2: # modify it under the terms of the GNU General Public License om@2: # as published by the Free Software Foundation version 2. om@2: # om@2: # This program is distributed in the hope that it will be useful, om@2: # but WITHOUT ANY WARRANTY; without even the implied warranty of om@2: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the om@2: # GNU General Public License for more details. om@2: # om@2: # You should have received a copy of the GNU General Public License om@2: # along with this program; if not, write to the Free Software om@2: # Foundation, Inc., 51 Franklin Street, Fifth Floor, om@2: # Boston, MA 02110-1301, USA. om@2: # ------------------------------------------------------------ om@2: om@2: om@2: # ------------------------------------------------------------ om@2: # imports om@2: om@2: import os om@2: import os.path om@2: import sys om@2: om@2: om@2: # ------------------------------------------------------------ om@2: # code om@2: om@2: om@2: class Environment(object): om@2: om@2: """Hold some nifty environment stuff in a dedicated class.""" om@2: om@2: def __init__(self, application = None): om@2: om@2: # if we ain't got a path to start from, all is valid/lost om@2: if len(sys.path[0]) == 0: om@2: self.prefix_path = '' om@2: self.data_path = '' om@2: self.image_path = '' om@2: return om@2: om@2: # the prefix path om@2: # om@2: # - on Linux: this is ../../ to the current executable om@2: # e.g. "/usr/bin/myprogram" --> "/usr" om@2: # om@2: # - on Windows: this is the installation folder om@2: # e.g. "C:/Program Files/MyProgram/bin/myprogam" --> "C:/Program Files/MyProgram" om@2: # om@2: if sys.platform == 'linux2': om@2: self.prefix_path = os.path.split(sys.path[0])[0] om@2: elif sys.platform == 'win32': om@2: self.prefix_path = os.path.split(sys.path[0])[0] om@2: om@2: # the data path where all data files are stored om@2: if sys.platform == 'linux2': om@2: if not application is None: om@2: self.data_path = os.path.join(self.prefix_path, os.path.join('share', application)) om@2: else: om@2: self.data_path = os.path.join(self.prefix_path, 'share') om@2: elif sys.platform == 'win32': om@2: self.data_path = self.prefix_path om@2: om@2: # the image path om@2: if sys.platform == 'linux2': om@2: self.image_path = os.path.join(self.data_path, 'gfx') om@2: elif sys.platform == 'win32': om@2: self.image_path = os.path.join(self.data_path, 'gfx') om@2: om@2: om@2: # test the module om@2: def test(): om@2: """Module test call.""" om@2: om@2: e = Environment("opensecurity") om@2: print("prefix_path: {0}".format(e.prefix_path)) om@2: print(" data_path: {0}".format(e.data_path)) om@2: print(" image_path: {0}".format(e.image_path)) om@2: om@2: # standalone calls are module tests om@2: if __name__ == '__main__': om@2: test()