Skip to content

Commit efe0b36

Browse files
Change default cache directory + generate sample config (#213)
* Set $TEMPDIR/pyard/ as the default path for storing db files * Set $TEMPDIR/pyard/ as the default * Option to generate sample config/file - `--generate-sample` generates config/file ``` /tmp> pyard-reduce-csv --generate-sample Created sample_reduce_conf.json Created sample.csv ``` * Add username to the default path to make it distinguishable in multi-user environments.
1 parent c7b20af commit efe0b36

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

pyard/db.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,24 @@
2020
# > http://www.fsf.org/licensing/licenses/lgpl.html
2121
# > http://www.opensource.org/licenses/lgpl-license.php
2222
#
23+
import os
2324
import pathlib
2425
import sqlite3
26+
import tempfile
2527
from typing import Tuple, Dict, Set, List
2628

2729
from pyard.misc import get_imgt_db_versions
2830

2931

30-
def get_pyard_db_install_directory():
31-
return pathlib.Path.home() / ".pyard"
32+
def get_pyard_db_default_directory():
33+
"""
34+
The default directory is $TMPDIR/$USER-pyard
35+
Check for `USERNAME` on Windows
36+
@return: directory path to the default db directory
37+
"""
38+
return pathlib.Path(tempfile.gettempdir()) / (
39+
os.environ.get("USER", os.environ.get("USERNAME")) + "-pyard"
40+
)
3241

3342

3443
def create_db_connection(data_dir, imgt_version, ro=False):
@@ -43,7 +52,7 @@ def create_db_connection(data_dir, imgt_version, ro=False):
4352
"""
4453
# Set data directory where all the downloaded files will go
4554
if data_dir is None:
46-
data_dir = get_pyard_db_install_directory()
55+
data_dir = get_pyard_db_default_directory()
4756

4857
db_filename = f"{data_dir}/pyard-{imgt_version}.sqlite3"
4958

@@ -70,6 +79,9 @@ def create_db_connection(data_dir, imgt_version, ro=False):
7079
if not pathlib.Path(data_dir).exists():
7180
pathlib.Path(data_dir).mkdir(parents=True, exist_ok=True)
7281

82+
if not pathlib.Path(db_filename).exists():
83+
print(f"Creating {db_filename} as cache.")
84+
7385
# Open the database for read/write
7486
file_uri = f"file:{db_filename}"
7587
return sqlite3.connect(file_uri, uri=True), db_filename

pyard/misc.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,27 @@ def get_imgt_db_versions() -> List[str]:
8585
return versions
8686

8787

88+
def download_to_file(url: str, local_filename: str):
89+
import urllib.request
90+
91+
req = urllib.request.Request(url)
92+
res = urllib.request.urlopen(req, timeout=5)
93+
if res.status == 200:
94+
file_content = res.read().decode("utf-8")
95+
with open(local_filename, "wt") as f:
96+
f.write(file_content)
97+
else:
98+
print(f"Error downloading {url}")
99+
100+
88101
def get_data_dir(data_dir):
89102
if data_dir:
90103
path = pathlib.Path(data_dir)
91104
if not path.exists() or not path.is_dir():
92105
raise RuntimeError(f"{data_dir} is not a valid directory")
93106
data_dir = path
94107
else:
95-
data_dir = db.get_pyard_db_install_directory()
108+
data_dir = db.get_pyard_db_default_directory()
96109
return data_dir
97110

98111

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
toml==0.10.2
2-
pandas>=1.1.4
2+
pandas==1.5.3

scripts/pyard-reduce-csv

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import pandas as pd
3838
import pyard
3939
import pyard.drbx as drbx
4040
from pyard.exceptions import PyArdError
41-
from pyard.misc import get_data_dir, get_imgt_version
41+
from pyard.misc import get_data_dir, get_imgt_version, download_to_file
4242

4343

4444
def is_serology(allele: str) -> bool:
@@ -173,7 +173,7 @@ def create_drbx(row, locus_in_allele_name):
173173
if __name__ == "__main__":
174174
# config is specified with a -c parameter
175175
parser = argparse.ArgumentParser()
176-
parser.add_argument("-c", "--config", help="JSON Configuration file", required=True)
176+
parser.add_argument("-c", "--config", help="JSON Configuration file")
177177
parser.add_argument(
178178
"-d",
179179
"--data-dir",
@@ -194,8 +194,33 @@ if __name__ == "__main__":
194194
default=False,
195195
help="Don't print verbose log",
196196
)
197+
parser.add_argument(
198+
"-g",
199+
"--generate-sample",
200+
dest="generate",
201+
action="store_true",
202+
default=False,
203+
help="Generate sample config file and csv file",
204+
)
205+
197206
args = parser.parse_args()
207+
208+
if args.generate:
209+
config_url = "https://raw.githubusercontent.com/nmdp-bioinformatics/py-ard/master/extras/reduce_conf.json"
210+
sample_config = "sample_reduce_conf.json"
211+
download_to_file(config_url, sample_config)
212+
print(f"Created {sample_config}")
213+
214+
sample_url = "https://raw.githubusercontent.com/nmdp-bioinformatics/py-ard/master/extras/sample.csv"
215+
sample_csv = "sample.csv"
216+
download_to_file(sample_url, sample_csv)
217+
print(f"Created {sample_csv}")
218+
sys.exit(0)
219+
198220
config_filename = args.config
221+
if not config_filename:
222+
print("Config file required. Specify with -c/--config")
223+
sys.exit(1)
199224

200225
print("Using config file:", config_filename)
201226
with open(config_filename) as conf_file:

0 commit comments

Comments
 (0)