Skip to content

Commit 33a0bd2

Browse files
initial commit: added user_config and load_user_info, tests not written yet
1 parent 5c3eed7 commit 33a0bd2

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/diffpy/labpdfproc/labpdfprocapp.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from diffpy.labpdfproc.functions import apply_corr, compute_cve
55
from diffpy.labpdfproc.tools import (
66
known_sources,
7+
load_user_info,
78
load_user_metadata,
89
set_input_lists,
910
set_output_directory,
@@ -96,6 +97,7 @@ def get_args(override_cli_inputs=None):
9697

9798
def main():
9899
args = get_args()
100+
args = load_user_info(args)
99101
args = set_input_lists(args)
100102
args.output_directory = set_output_directory(args)
101103
args.wavelength = set_wavelength(args)

src/diffpy/labpdfproc/tools.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pathlib import Path
22

3+
from diffpy.labpdfproc.user_config import find_conf_file, read_conf_file, user_info_conf
4+
35
WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
46
known_sources = [key for key in WAVELENGTHS.keys()]
57

@@ -171,3 +173,17 @@ def load_user_metadata(args):
171173
setattr(args, key, value)
172174
delattr(args, "user_metadata")
173175
return args
176+
177+
178+
def load_user_info(args):
179+
conf_file_path = find_conf_file()
180+
if conf_file_path is not None:
181+
conf_file = read_conf_file(conf_file_path)
182+
if "username" in conf_file and "useremail" in conf_file:
183+
setattr(args, "username", conf_file["username"])
184+
setattr(args, "useremail", conf_file["useremail"])
185+
return args
186+
else:
187+
return user_info_conf(args)
188+
else:
189+
return user_info_conf(args)

src/diffpy/labpdfproc/user_config.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import json
2+
import os
3+
from pathlib import Path
4+
5+
CONFIG_FILE = "labpdfprocconfig.json"
6+
7+
8+
def find_conf_file():
9+
# Return config file path if such a file exists
10+
if os.path.exists(CONFIG_FILE):
11+
return Path(CONFIG_FILE).resolve()
12+
return None
13+
14+
15+
def read_conf_file(file_path):
16+
with open(file_path, "r") as f:
17+
return json.load(f)
18+
19+
20+
def write_conf_file(file_path, username, useremail):
21+
config = {"username": username, "useremail": useremail}
22+
with open(file_path, "w") as f:
23+
json.dump(config, f)
24+
25+
26+
def prompt_user_info():
27+
username = input("Please enter your username (or press Enter to skip): ")
28+
useremail = input("Please enter your email (or press Enter to skip): ")
29+
return username, useremail
30+
31+
32+
def user_info_conf(args):
33+
username, useremail = prompt_user_info()
34+
write_conf_file(CONFIG_FILE, username, useremail)
35+
setattr(args, "username", username)
36+
setattr(args, "useremail", useremail)
37+
return args

0 commit comments

Comments
 (0)