Skip to content

Commit 9fd5a6d

Browse files
ezd1000ezd1000
authored andcommitted
imp updated to importlib for load_source past Python 3.4
1 parent 8c581b1 commit 9fd5a6d

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

dbprocessing/dbprocessing.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,12 @@ def figureProduct(self, filename=None):
262262
import imp # imp is deprecated at python 3.4
263263
inspect = imp.load_source('inspect', code)
264264
else:
265-
import importlib.util # used in python 3.4 and above
266-
specification = importlib.util.spec_from_file_location("inspect", code) # Makes specification from the file path
267-
inspect = importlib.util.module_from_spec(specification) # Creates module
268-
specification.loader.exec_module(inspect) # Loads module
265+
import importlib.util # used in python 3.4 and above
266+
import importlib.machinery
267+
loader = importlib.machinery.SourceFileLoader('inspect', code)
268+
spec = importlib.util.spec_from_file_location('inspect', code, loader=loader)
269+
inspect = importlib.util.module_from_spec(spec)
270+
loader.exec_module(inspect)
269271
except IOError as msg:
270272
DBlogging.dblogger.error('Inspector: "{0}" not found: {1}'.format(code, msg))
271273
if os.path.isfile(code + ' '):

unit_tests/test_Inspector.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import datetime
55
import unittest
66
import tempfile
7-
import imp
7+
import sys
88
import warnings
99
import os
1010

@@ -59,9 +59,17 @@ def setUp(self):
5959
self.makeTestDB()
6060
self.loadData(os.path.join(dbp_testing.testsdir, 'data', 'db_dumps',
6161
'testDB_dump.json'))
62-
self.inspect = imp.load_source('inspect', os.path.join(
63-
dbp_testing.testsdir, 'inspector', 'rot13_L1.py'))
64-
62+
if sys.version_info < (3, 4):
63+
import imp
64+
self.inspect = imp.load_source('inspect', os.path.join(dbp_testing.testsdir, 'inspector', 'rot13_L1.py'))
65+
else:
66+
import importlib.util
67+
import importlib.machinery
68+
loader = importlib.machinery.SourceFileLoader('inspect', os.path.join(dbp_testing.testsdir, 'inspector', 'rot13_L1.py'))
69+
spec = importlib.util.spec_from_file_location('inspect', os.path.join(dbp_testing.testsdir, 'inspector', 'rot13_L1.py'), loader=loader)
70+
self.inspect = importlib.util.module_from_spec(spec)
71+
loader.exec_module(self.inspect)
72+
6573
def tearDown(self):
6674
super(InspectorClass, self).tearDown()
6775
self.removeTestDB()
@@ -80,10 +88,18 @@ def test_inspector(self):
8088
self.assertEqual(repr(Diskfile.Diskfile(goodfile, self.dbu)), repr(self.inspect.Inspector(goodfile, self.dbu, 1,)()))
8189
#self.assertEqual(None, self.inspect.Inspector(goodfile, self.dbu, 1,).extract_YYYYMMDD())
8290
# This inspector sets the data_level - not allowed
83-
inspect = imp.load_source('inspect', os.path.join(
84-
dbp_testing.testsdir, 'inspector', 'rot13_L1_dlevel.py'))
85-
with warnings.catch_warnings(record=True) as w:
86-
self.assertEqual(repr(Diskfile.Diskfile(goodfile, self.dbu)), repr(self.inspect.Inspector(goodfile, self.dbu, 1,)()))
91+
if sys.version_info < (3, 4):
92+
import imp # Depracated in Python 3.4
93+
inspect = imp.load_source('inspect', os.path.join(dbp_testing.testsdir, 'inspector', 'rot13_L1_dlevel.py'))
94+
else:
95+
import importlib.util
96+
import importlib.machinery
97+
loader = importlib.machinery.SourceFileLoader('inspect', os.path.join(dbp_testing.testsdir, 'inspector', 'rot13_L1_dlevel.py'))
98+
spec = importlib.util.spec_from_file_location('inspect', os.path.join(dbp_testing.testsdir, 'inspector', 'rot13_L1_dlevel.py'), loader=loader)
99+
inspect = importlib.util.module_from_spec(spec)
100+
loader.exec_module(inspect)
101+
with warnings.catch_warnings(record=True) as w:
102+
self.assertEqual(repr(Diskfile.Diskfile(goodfile, self.dbu)), repr(inspect.Inspector(goodfile, self.dbu, 1,)()))
87103
self.assertEqual(len(w), 1)
88104
self.assertTrue(isinstance(w[0].message, UserWarning))
89105
self.assertEqual('Inspector rot13_L1_dlevel.py: set level to 2.0, '
@@ -93,8 +109,17 @@ def test_inspector(self):
93109
# The file doesn't match the inspector pattern...
94110
badfile = os.path.join(
95111
dbp_testing.testsdir, 'inspector', 'testDB_01_first.raw')
96-
inspect = imp.load_source('inspect', os.path.join(
97-
dbp_testing.testsdir, 'inspector', 'rot13_L1.py'))
112+
if sys.version_info < (3, 4):
113+
import imp # Depracated in Python 3.4
114+
inspect = imp.load_source('inspect', os.path.join(dbp_testing.testsdir, 'inspector', 'rot13_L1.py'))
115+
else:
116+
import importlib.util
117+
import importlib.machinery
118+
loader = importlib.machinery.SourceFileLoader('inspect', os.path.join(dbp_testing.testsdir, 'inspector', 'rot13_L1.py'))
119+
spec = importlib.util.spec_from_file_location('inspect', os.path.join(dbp_testing.testsdir, 'inspector', 'rot13_L1.py'), loader=loader)
120+
inspect = importlib.util.module_from_spec(spec)
121+
loader.exec_module(inspect)
122+
98123
self.assertEqual(None, inspect.Inspector(badfile, self.dbu, 1,)())
99124

100125
def test_inspector_regex(self):

0 commit comments

Comments
 (0)