Skip to content

Commit 0c3be38

Browse files
authored
Merge pull request #32 from materials-data-facility/forge-dev
Alpha sync
2 parents 9dd0bb7 + 3553f54 commit 0c3be38

4 files changed

Lines changed: 41 additions & 38 deletions

File tree

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ deploy:
3636
tags: true
3737
distributions: sdist bdist_wheel
3838
password:
39-
secure: ap4GShL/gmptUyRbJS6ZC7AY6WfdB08zt2KpkN5Qw7LAj7AxZLcPCc7Hr+bFpb3P3f5iOHcZHRXnKJhkr/nYw9QtBuVwcD8k6ZZzCCfXBTHrLOV6z1c6texRcuzYCG2lVCEMRmfBhryVWPrdswf6jnFt9+sWsNgTd0kVaj/BMumWWFH7S5MPckiQ0RSZfBrhNgQ1WZov5zMxotP5jJylOVPJeeNlKBpjvP1t2u8J+fYuogenNqdG2dGA2B3jIbrZUfhvVqGu7h7GOevPFoE0y4kC4X3WWnSS22KCxn038OdhqFmfW4MZRGJ9Xh9oWJDP3pKgJ3jSBrLYUKm240wVCoWvOsez8Ixxzit8jJYkzSE6foVSe4Xf12moHChjt4JqAY+Kj8uLe5IRAEwj/OrJcL2bjYhj+TkI+9yu8z8VaaxNRJim4B6GPeOOFXX/AkyOdXYgw0yZo0gNE3KmPJCvU6hAVIJAA1d0fZVWiyHCHH3AGOFNJChPXWbsnfGpganhAp660y9JFOQOeD9MCVF5CppY5/5QFhGIGilUuEZijWRBldcG70hDAeU9P6JNunnV2jvrpdfk5Z+0QkM4unJYGQcEnVU/gJWxN3335j5xWSgJR3qPsZ2G2vr8IL/LNLS3dj8S+xShpQChGMvD2ZtSYR4q97rC4guEjQydkfZjYSA=
39+
secure: D94fhT0J3BQ29LdGJA0mvQiDZbuxapfMDkY99RaRf+VY/z9YlsvdSdyvlB/I01seyFQBJKma1sLJbpc+GRdcfd6NfILO7i27NgY0W+wJOafefUOOWhucmDX2PnnKSY+3PDnhlBI7Tn4lWcL7kzxp2TgUCc36VNhutg8PUQmA/+9H0HOh4q1oBWCaH+gNlQCA2XEYJ9KiBvrMJdZOPieo0JOWWhUlko8DVxttWXPW9ocww4jpHv4C7nHPFv8f7eFm6IDYmOT7gfBy8xK/B1iqQ78geGr/52zqNaU6f8KNTFJCu8XKNtv6nYXutPNlqkZoCwNjhW3rztO8ZbJRh6+oDSZlFrCsoRqGyGm4YDdvwyZQAFqsuwq8N47jMK1x0Xfe21d0QV5BElXvLkzRmMy++DdnRPrdvyyy2N5m3D4ZiMuBsClVzShT0wrBfs/w6+rDQTNjcEP6xqk0MHzScp2yBCk2mLERSd3oxtQ63OJYAagWgpPLC+yZVcLaH8CC9QfRoX+b+rHYQ46v0Z9k46q0LBoPt56NdTGGCXZLn8Zqr8hnliISnx2dOWneJ8gH0AlbNQDacU67sXmbgZMSdPrkcsZLzQOT8KqzUu8HhV1flodZKoeOKsqzzWk0Zo4LLvRLOqDox/p/X/Uj6rau3YOn6cn5sk0050BkeXnQhwwCMGk=

mdf_forge/forge.py

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class Forge:
3434
__transfer_interval = 60 # 1 minute, in seconds
3535
__inactivity_time = 1 * 60 * 60 # 1 hour, in seconds
3636

37-
def __init__(self, index=__default_index, local_ep=None, anonymous=False, **kwargs):
37+
def __init__(self, index=__default_index, local_ep=None, anonymous=False,
38+
clear_old_tokens=False, **kwargs):
3839
"""**Initialize the Forge instance.**
3940
4041
Args:
@@ -43,6 +44,8 @@ def __init__(self, index=__default_index, local_ep=None, anonymous=False, **kwar
4344
If not provided, may be autodetected as possible.
4445
anonymous (bool): If **True**, will not authenticate with Globus Auth.
4546
If **False**, will require authentication.
47+
clear_old_tokens (bool): If **True**, will force reauthentication
48+
If **False**, will use existing tokens if possible.
4649
4750
Keyword Args:
4851
**Advanced users only.**
@@ -73,10 +76,12 @@ def __init__(self, index=__default_index, local_ep=None, anonymous=False, **kwar
7376
clients = (mdf_toolbox.anonymous_login(services) if services else {})
7477
else:
7578
services = kwargs.get('services', self.__auth_services)
76-
clients = (mdf_toolbox.login(credentials={
79+
clients = (mdf_toolbox.login(
80+
credentials={
7781
"app_name": self.__app_name,
7882
"services": services,
79-
"index": self.index}) if services else {})
83+
"index": self.index},
84+
clear_old_tokens=clear_old_tokens) if services else {})
8085
user_clients = kwargs.get("clients", {})
8186
self.__search_client = user_clients.get("search", clients.get("search", None))
8287
self.__transfer_client = user_clients.get("transfer", clients.get("transfer", None))
@@ -101,10 +106,9 @@ def transfer_client(self):
101106
def mdf_authorizer(self):
102107
return self.__data_mdf_authorizer
103108

104-
105-
# ***********************************************
106-
# * Core functions
107-
# ***********************************************
109+
# ***********************************************
110+
# * Core functions
111+
# ***********************************************
108112

109113
def match_field(self, field, value, required=True, new_group=False):
110114
"""Add a field:value term to the query.
@@ -125,6 +129,9 @@ def match_field(self, field, value, required=True, new_group=False):
125129
Returns:
126130
self (Forge): For chaining.
127131
"""
132+
# No-op on missing arguments
133+
if not field and not value:
134+
return self
128135
# If not the start of the query string, add an AND or OR
129136
if self.__query.initialized:
130137
if required:
@@ -152,6 +159,9 @@ def exclude_field(self, field, value, new_group=False):
152159
Returns:
153160
self (Forge): For chaining.
154161
"""
162+
# No-op on missing arguments
163+
if not field and not value:
164+
return self
155165
# If not the start of the query string, add an AND
156166
# OR would not make much sense for excluding
157167
if self.__query.initialized:
@@ -269,10 +279,9 @@ def reset_query(self):
269279
del self.__query
270280
self.__query = Query(self.__search_client)
271281

272-
273-
# ***********************************************
274-
# * Expanded functions
275-
# ***********************************************
282+
# ***********************************************
283+
# * Expanded functions
284+
# ***********************************************
276285

277286
def match_range(self, field, start="*", stop="*", inclusive=True,
278287
required=True, new_group=False):
@@ -354,10 +363,9 @@ def exclude_range(self, field, start="*", stop="*", inclusive=True,
354363
self.exclude_field(field, value, new_group=new_group)
355364
return self
356365

357-
358-
# ***********************************************
359-
# * Helper functions
360-
# ***********************************************
366+
# ***********************************************
367+
# * Helper functions
368+
# ***********************************************
361369

362370
def exclusive_match(self, field, value):
363371
"""Match exactly the given value, with no other data in the field.
@@ -564,10 +572,9 @@ def match_resource_types(self, types):
564572
self.match_field(field="mdf.resource_type", value=rt, required=False, new_group=False)
565573
return self
566574

567-
568-
# ***********************************************
569-
# * Premade searches
570-
# ***********************************************
575+
# ***********************************************
576+
# * Premade searches
577+
# ***********************************************
571578

572579
def search_by_elements(self, elements, source_names=[], index=None, limit=None,
573580
match_all=True, info=False):
@@ -707,10 +714,9 @@ def get_dataset_version(self, source_name):
707714
else:
708715
return hits[0]['mdf']['version']
709716

710-
711-
# ***********************************************
712-
# * Data retrieval functions
713-
# ***********************************************
717+
# ***********************************************
718+
# * Data retrieval functions
719+
# ***********************************************
714720

715721
def http_download(self, results, dest=".", preserve_dir=False, verbose=True):
716722
"""Download data files from the provided results using HTTPS.
@@ -788,23 +794,18 @@ def http_download(self, results, dest=".", preserve_dir=False, verbose=True):
788794
# Check if file already exists, change filename if necessary
789795
collisions = 0
790796
while os.path.exists(local_path):
791-
# Find period marking extension, if exists
792-
# Will be after last slash
793-
last_slash = local_path.rfind("/")
794-
index = local_path.rfind(".", (last_slash if last_slash != -1 else 0))
795-
if index < 0:
796-
ext = ""
797-
else:
798-
ext = local_path[index:]
799-
local_path = local_path[:index]
797+
# Save and remove extension
798+
local_path, ext = os.path.splitext(local_path)
800799
# Check if already added number to end
801800
old_add = "("+str(collisions)+")"
802801
collisions += 1
803802
new_add = "("+str(collisions)+")"
803+
# Remove old number if exists
804804
if local_path.endswith(old_add):
805-
local_path = local_path[:-len(old_add)] + new_add + ext
806-
else:
807-
local_path = local_path + new_add + ext
805+
local_path = local_path[:-len(old_add)]
806+
# Add new number
807+
local_path = local_path + new_add + ext
808+
808809
headers = {}
809810
# Check for Petrel vs. NCSA url for authorizer
810811
# Petrel

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name='mdf_forge',
5-
version='0.6.3',
5+
version='0.6.4',
66
packages=['mdf_forge'],
77
description='Materials Data Facility python package',
88
long_description=("Forge is the Materials Data Facility Python package"

tests/test_forge.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,9 @@ def test_forge_match_field():
405405
# Check that query clears
406406
assert f.search() == []
407407

408-
# Also checking check_field
408+
# Also checking check_field and no-op
409409
f.match_field("material.elements", "Al")
410+
f.match_field("", "")
410411
res2 = f.search()
411412
assert check_field(res2, "material.elements", "Al") == 1
412413

@@ -415,6 +416,7 @@ def test_forge_exclude_field():
415416
f = forge.Forge(index="mdf")
416417
# Basic usage
417418
f.exclude_field("material.elements", "Al")
419+
f.exclude_field("", "")
418420
f.match_field("mdf.source_name", "ab_initio_solute_database*")
419421
res1 = f.search()
420422
assert check_field(res1, "material.elements", "Al") == -1

0 commit comments

Comments
 (0)