|
2 | 2 | import re |
3 | 3 | import os |
4 | 4 | import sys |
5 | | -import configparser |
6 | 5 | import requests |
7 | | - |
| 6 | +from ConfigParser import ConfigParser |
| 7 | +# from distutils.sysconfig import get_python_lib |
8 | 8 |
|
9 | 9 | def checkrecordfiles(recordname, filedirectory): |
10 | 10 | """Check a local directory along with the database cache directory specified in |
@@ -38,14 +38,9 @@ def checkrecordfiles(recordname, filedirectory): |
38 | 38 | - downloadedfiles: The list of files downloaded from PhysioBank. |
39 | 39 | """ |
40 | 40 |
|
41 | | - config = configparser.ConfigParser() |
42 | | - config.read( |
43 | | - os.path.join( |
44 | | - os.path.abspath( |
45 | | - os.path.dirname(__file__)), |
46 | | - "config.ini")) |
47 | | - # Base directory to store downloaded physiobank files, read from config.ini |
48 | | - dbcachedir = config['PBDOWNLOAD']['dbcachedir'] |
| 41 | + # Base directory to store downloaded physiobank files |
| 42 | + config = loadconfig('wfdb.config') |
| 43 | + dbcachedir = config.get('pbdownload','dbcachedir') |
49 | 44 | basedir, baserecname = os.path.split(recordname) |
50 | 45 |
|
51 | 46 | # At this point we do not know whether basedir is a local directory, a |
@@ -845,6 +840,18 @@ def processwfdbbytes(fp, fmt, siglen, nsig, sampsperframe, floorsamp=0): |
845 | 840 | "initvalue", |
846 | 841 | "signame"] |
847 | 842 |
|
| 843 | +def loadconfig(fn): |
| 844 | + """ |
| 845 | + Search for a configuration file. Load the first version found. |
| 846 | + """ |
| 847 | + config = ConfigParser() |
| 848 | + for loc in [os.curdir,os.path.expanduser("~"),os.path.dirname(__file__)]: |
| 849 | + try: |
| 850 | + with open(os.path.join(loc,fn)) as source: |
| 851 | + config.readfp(source) |
| 852 | + except IOError: |
| 853 | + pass |
| 854 | + return config |
848 | 855 |
|
849 | 856 | def rdsamp( |
850 | 857 | recordname, |
@@ -883,23 +890,12 @@ def rdsamp( |
883 | 890 | """ |
884 | 891 |
|
885 | 892 | filestoremove = [] |
886 | | - if os.path.isfile( |
887 | | - os.path.join( |
888 | | - os.path.abspath( |
889 | | - os.path.dirname(__file__)), |
890 | | - "config.ini")): # Read the configuration file if any. |
891 | | - config = configparser.ConfigParser() |
892 | | - config.read( |
893 | | - os.path.join( |
894 | | - os.path.abspath( |
895 | | - os.path.dirname(__file__)), |
896 | | - "config.ini")) |
897 | | - if int(config['PBDOWNLOAD'][ |
898 | | - 'getPBfiles']): # Flag specifying whether to allow downloading from physiobank |
899 | | - recordname, dledfiles = checkrecordfiles(recordname, os.getcwd()) |
900 | | - if not int(config['PBDOWNLOAD'][ |
901 | | - 'keepdledfiles']): # Flag specifying whether to keep downloaded physiobank files |
902 | | - filestoremove = dledfiles |
| 893 | + config = loadconfig('wfdb.config') |
| 894 | + |
| 895 | + if config.get('pbdownload','getpbfiles'): # Flag specifying whether to allow downloading from physiobank |
| 896 | + recordname, dledfiles = checkrecordfiles(recordname, os.getcwd()) |
| 897 | + if not int(config.get('pbdownload','keepdledfiles')): # Flag specifying whether to keep downloaded physiobank files |
| 898 | + filestoremove = dledfiles |
903 | 899 |
|
904 | 900 | fields = readheader(recordname) # Get the info from the header file |
905 | 901 |
|
|
0 commit comments