2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # pick some current environment infos
9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
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
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.
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.
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 # ------------------------------------------------------------
32 # ------------------------------------------------------------
40 # ------------------------------------------------------------
44 class Environment(object):
46 """Hold some nifty environment stuff in a dedicated class."""
48 def __init__(self, application = None):
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 = ''
58 # - on Linux: this is ../../ to the current executable
59 # e.g. "/usr/bin/myprogram" --> "/usr"
61 # - on Windows (inkl. Cygwin): this is the installation folder
62 # e.g. "C:/Program Files/MyProgram/myprogam" --> "C:/Program Files/MyProgram"
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]
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))
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
79 def data_path_get(self):
81 return self._data_path
83 data_path = property(data_path_get)
86 def prefix_path_get(self):
88 return self._prefix_path
90 prefix_path = property(prefix_path_get)
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))
102 if __name__ == '__main__':