|
1 | 1 | from functools import partial |
| 2 | +import os |
2 | 3 | import sys |
3 | 4 |
|
| 5 | +try: |
| 6 | + from configparser import ConfigParser # Python 3 |
| 7 | +except: |
| 8 | + from ConfigParser import ConfigParser # Python 2 |
| 9 | + |
| 10 | +from .config import USER_CONFIG |
4 | 11 | from .error import GeosupportError |
5 | 12 | from .function_info import FUNCTIONS, function_help, list_functions, input_help |
6 | 13 | from .io import format_input, parse_output, set_mode |
7 | 14 |
|
| 15 | +GEOLIB = None |
| 16 | + |
8 | 17 | class Geosupport(object): |
9 | | - def __init__(self): |
| 18 | + def __init__(self, geosupport_path=None, geosupport_version=None): |
| 19 | + global GEOLIB |
10 | 20 | self.py_version = sys.version_info[0] |
11 | 21 | self.platform = sys.platform |
12 | 22 | self.py_bit = '64' if (sys.maxsize > 2 ** 32) else '32' |
13 | 23 |
|
| 24 | + if geosupport_version is not None: |
| 25 | + config = ConfigParser() |
| 26 | + config.read(os.path.expanduser(USER_CONFIG)) |
| 27 | + versions = dict(config['versions'].items()) |
| 28 | + geosupport_path = versions[geosupport_version.lower()] |
| 29 | + |
| 30 | + if geosupport_path is not None: |
| 31 | + os.environ['GEOFILES'] = os.path.join(geosupport_path, 'Fls\\') |
| 32 | + os.environ['PATH'] = ';'.join([ |
| 33 | + i for i in os.environ['PATH'].split(';') if |
| 34 | + 'GEOSUPPORT' not in i.upper() |
| 35 | + ]) |
| 36 | + os.environ['PATH'] += ';' + os.path.join(geosupport_path, 'bin') |
| 37 | + |
14 | 38 | try: |
15 | 39 | if self.platform == 'win32': |
| 40 | + from ctypes import windll, cdll, WinDLL, wintypes |
| 41 | + |
| 42 | + if GEOLIB is not None: |
| 43 | + kernel32 = WinDLL('kernel32') |
| 44 | + kernel32.FreeLibrary.argtypes = [wintypes.HMODULE] |
| 45 | + kernel32.FreeLibrary(GEOLIB._handle) |
| 46 | + |
16 | 47 | if self.py_bit == '64': |
17 | | - from ctypes import cdll |
18 | 48 | self.geolib = cdll.LoadLibrary("NYCGEO.dll") |
19 | 49 | else: |
20 | | - # must use windll for 32-bit Windows binaries |
21 | | - from ctypes import windll |
22 | 50 | self.geolib = windll.LoadLibrary("NYCGEO.dll") |
23 | 51 | elif self.platform.startswith('linux'): |
24 | | - import os |
25 | 52 | from ctypes import cdll |
| 53 | + |
| 54 | + if GEOLIB is not None: |
| 55 | + cdll.LoadLibrary('libdl.so').dlclose(GEOLIB._handle) |
| 56 | + |
26 | 57 | self.geolib = cdll.LoadLibrary("libgeo.so") |
27 | 58 | else: |
28 | | - raise Exception('This Operating System is currently not supported.') |
| 59 | + raise GeosupportError( |
| 60 | + 'This Operating System is currently not supported.' |
| 61 | + ) |
| 62 | + |
| 63 | + GEOLIB = self.geolib |
29 | 64 | except OSError as e: |
30 | | - sys.exit( |
| 65 | + raise GeosupportError( |
31 | 66 | '%s\n' |
32 | 67 | 'You are currently using a %s-bit Python interpreter. ' |
33 | 68 | 'Is the installed version of Geosupport %s-bit?' % ( |
|
0 commit comments