Skip to content

Commit 7b3607d

Browse files
committed
update config method. ref #20
1 parent 66c97e9 commit 7b3607d

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

wfdb/_rdsamp.py

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import re
33
import os
44
import sys
5-
import configparser
65
import requests
7-
6+
from ConfigParser import ConfigParser
7+
# from distutils.sysconfig import get_python_lib
88

99
def checkrecordfiles(recordname, filedirectory):
1010
"""Check a local directory along with the database cache directory specified in
@@ -38,14 +38,9 @@ def checkrecordfiles(recordname, filedirectory):
3838
- downloadedfiles: The list of files downloaded from PhysioBank.
3939
"""
4040

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')
4944
basedir, baserecname = os.path.split(recordname)
5045

5146
# 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):
845840
"initvalue",
846841
"signame"]
847842

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
848855

849856
def rdsamp(
850857
recordname,
@@ -883,23 +890,12 @@ def rdsamp(
883890
"""
884891

885892
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
903899

904900
fields = readheader(recordname) # Get the info from the header file
905901

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Options for downloading physiobank data when file is not already on local machine.
2-
[PBDOWNLOAD]
32

3+
[pbdownload]
44
# Flag that specifies whether to search and download missing files from physiobank
5-
getPBfiles=1
5+
getpbfiles: 1
66

77
# Flag that specifies whether to keep automatically downloaded physiobank files
8-
keepdledfiles=1
9-
10-
# Base cache directory in which data files are searched for (after searching the current working directory), and missing files are downloaded. The full download directory is cachedir/pbsubdir where pbsubdir is the . Default value: /usr/local/database/.
11-
dbcachedir=/usr/local/database/
8+
keepdledfiles: 1
129

10+
# Base cache directory in which data files are searched for (after searching the current working directory)
11+
# and missing files are downloaded. The full download directory is cachedir/pbsubdir where pbsubdir is the .
12+
# Default value: /usr/local/database/.
13+
dbcachedir: '/usr/local/database/'

0 commit comments

Comments
 (0)