|
| 1 | +# Import this module to automatically setup path to local airsim module |
| 2 | +# This module first tries to see if airsim module is installed via pip |
| 3 | +# If it does then we don't do anything else |
| 4 | +# Else we look up grand-parent folder to see if it has airsim folder |
| 5 | +# and if it does then we add that in sys.path |
| 6 | + |
| 7 | +import os,sys,inspect,logging |
| 8 | + |
| 9 | +#this class simply tries to see if airsim |
| 10 | +class SetupPath: |
| 11 | + @staticmethod |
| 12 | + def getDirLevels(path): |
| 13 | + path_norm = os.path.normpath(path) |
| 14 | + return len(path_norm.split(os.sep)) |
| 15 | + |
| 16 | + @staticmethod |
| 17 | + def getCurrentPath(): |
| 18 | + cur_filepath = os.path.abspath(inspect.getfile(inspect.currentframe())) |
| 19 | + return os.path.dirname(cur_filepath) |
| 20 | + |
| 21 | + @staticmethod |
| 22 | + def getGrandParentDir(): |
| 23 | + cur_path = SetupPath.getCurrentPath() |
| 24 | + if SetupPath.getDirLevels(cur_path) >= 2: |
| 25 | + return os.path.dirname(os.path.dirname(cur_path)) |
| 26 | + return '' |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def getParentDir(): |
| 30 | + cur_path = SetupPath.getCurrentPath() |
| 31 | + if SetupPath.getDirLevels(cur_path) >= 1: |
| 32 | + return os.path.dirname(cur_path) |
| 33 | + return '' |
| 34 | + |
| 35 | + @staticmethod |
| 36 | + def addAirSimModulePath(): |
| 37 | + # if airsim module is installed then don't do anything else |
| 38 | + #import pkgutil |
| 39 | + #airsim_loader = pkgutil.find_loader('airsim') |
| 40 | + #if airsim_loader is not None: |
| 41 | + # return |
| 42 | + |
| 43 | + parent = SetupPath.getParentDir() |
| 44 | + if parent != '': |
| 45 | + airsim_path = os.path.join(parent, 'airsim') |
| 46 | + client_path = os.path.join(airsim_path, 'client.py') |
| 47 | + if os.path.exists(client_path): |
| 48 | + sys.path.insert(0, parent) |
| 49 | + else: |
| 50 | + logging.warning("airsim module not found in parent folder. Using installed package (pip install airsim).") |
| 51 | + |
| 52 | +SetupPath.addAirSimModulePath() |
0 commit comments