|
1 | 1 | import os
|
2 | 2 | import sys
|
3 | 3 |
|
| 4 | + |
| 5 | +class CustomImporter(object): |
| 6 | + """ |
| 7 | + Import cs50.SQL lazily so that rest of library can be used without SQLAlchemy installed. |
| 8 | +
|
| 9 | + https://docs.python.org/3/library/imp.html |
| 10 | + http://xion.org.pl/2012/05/06/hacking-python-imports/ |
| 11 | + http://dangerontheranger.blogspot.com/2012/07/how-to-use-sysmetapath-with-python.html |
| 12 | + """ |
| 13 | + |
| 14 | + def find_module(self, fullname, path=None): |
| 15 | + if fullname == "cs50.SQL": |
| 16 | + return self |
| 17 | + return None |
| 18 | + |
| 19 | + def load_module(self, name): |
| 20 | + if name in sys.modules: |
| 21 | + return sys.modules[name] |
| 22 | + from .sql import SQL |
| 23 | + sys.modules[name] = SQL |
| 24 | + return SQL |
| 25 | + |
| 26 | + |
4 | 27 | try:
|
5 | 28 |
|
6 | 29 | # Save student's sys.path
|
|
13 | 36 | from .cs50 import eprint, get_char, get_float, get_int, get_string
|
14 | 37 | try:
|
15 | 38 | from .cs50 import get_long
|
16 |
| - except: |
| 39 | + except Exception: |
17 | 40 | pass
|
18 | 41 |
|
19 |
| - # Replace flask's logger |
| 42 | + # Replace Flask's logger |
20 | 43 | from . import flask
|
21 | 44 |
|
22 |
| - class CustomImporter(object): |
23 |
| - """ |
24 |
| - Import cs50.SQL lazily so that rest of library can be used without SQLAlchemy installed. |
25 |
| -
|
26 |
| - https://docs.python.org/3/library/imp.html |
27 |
| - http://xion.org.pl/2012/05/06/hacking-python-imports/ |
28 |
| - http://dangerontheranger.blogspot.com/2012/07/how-to-use-sysmetapath-with-python.html |
29 |
| - """ |
30 |
| - |
31 |
| - def find_module(self, fullname, path=None): |
32 |
| - if fullname == "cs50.SQL": |
33 |
| - return self |
34 |
| - return None |
35 |
| - |
36 |
| - def load_module(self, name): |
37 |
| - if name in sys.modules: |
38 |
| - return sys.modules[name] |
39 |
| - from .sql import SQL |
40 |
| - sys.modules[name] = SQL |
41 |
| - return SQL |
42 |
| - |
| 45 | + # Lazily load CS50.SQL |
43 | 46 | sys.meta_path.append(CustomImporter())
|
44 | 47 |
|
45 | 48 | finally:
|
|
0 commit comments