Skip to content

Commit

Permalink
Merge pull request #32 from materials-data-facility/forge-dev
Browse files Browse the repository at this point in the history
Alpha sync
  • Loading branch information
jgaff authored Oct 29, 2018
2 parents 9dd0bb7 + 3553f54 commit 0c3be38
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ deploy:
tags: true
distributions: sdist bdist_wheel
password:
secure: ap4GShL/gmptUyRbJS6ZC7AY6WfdB08zt2KpkN5Qw7LAj7AxZLcPCc7Hr+bFpb3P3f5iOHcZHRXnKJhkr/nYw9QtBuVwcD8k6ZZzCCfXBTHrLOV6z1c6texRcuzYCG2lVCEMRmfBhryVWPrdswf6jnFt9+sWsNgTd0kVaj/BMumWWFH7S5MPckiQ0RSZfBrhNgQ1WZov5zMxotP5jJylOVPJeeNlKBpjvP1t2u8J+fYuogenNqdG2dGA2B3jIbrZUfhvVqGu7h7GOevPFoE0y4kC4X3WWnSS22KCxn038OdhqFmfW4MZRGJ9Xh9oWJDP3pKgJ3jSBrLYUKm240wVCoWvOsez8Ixxzit8jJYkzSE6foVSe4Xf12moHChjt4JqAY+Kj8uLe5IRAEwj/OrJcL2bjYhj+TkI+9yu8z8VaaxNRJim4B6GPeOOFXX/AkyOdXYgw0yZo0gNE3KmPJCvU6hAVIJAA1d0fZVWiyHCHH3AGOFNJChPXWbsnfGpganhAp660y9JFOQOeD9MCVF5CppY5/5QFhGIGilUuEZijWRBldcG70hDAeU9P6JNunnV2jvrpdfk5Z+0QkM4unJYGQcEnVU/gJWxN3335j5xWSgJR3qPsZ2G2vr8IL/LNLS3dj8S+xShpQChGMvD2ZtSYR4q97rC4guEjQydkfZjYSA=
secure: D94fhT0J3BQ29LdGJA0mvQiDZbuxapfMDkY99RaRf+VY/z9YlsvdSdyvlB/I01seyFQBJKma1sLJbpc+GRdcfd6NfILO7i27NgY0W+wJOafefUOOWhucmDX2PnnKSY+3PDnhlBI7Tn4lWcL7kzxp2TgUCc36VNhutg8PUQmA/+9H0HOh4q1oBWCaH+gNlQCA2XEYJ9KiBvrMJdZOPieo0JOWWhUlko8DVxttWXPW9ocww4jpHv4C7nHPFv8f7eFm6IDYmOT7gfBy8xK/B1iqQ78geGr/52zqNaU6f8KNTFJCu8XKNtv6nYXutPNlqkZoCwNjhW3rztO8ZbJRh6+oDSZlFrCsoRqGyGm4YDdvwyZQAFqsuwq8N47jMK1x0Xfe21d0QV5BElXvLkzRmMy++DdnRPrdvyyy2N5m3D4ZiMuBsClVzShT0wrBfs/w6+rDQTNjcEP6xqk0MHzScp2yBCk2mLERSd3oxtQ63OJYAagWgpPLC+yZVcLaH8CC9QfRoX+b+rHYQ46v0Z9k46q0LBoPt56NdTGGCXZLn8Zqr8hnliISnx2dOWneJ8gH0AlbNQDacU67sXmbgZMSdPrkcsZLzQOT8KqzUu8HhV1flodZKoeOKsqzzWk0Zo4LLvRLOqDox/p/X/Uj6rau3YOn6cn5sk0050BkeXnQhwwCMGk=
71 changes: 36 additions & 35 deletions mdf_forge/forge.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class Forge:
__transfer_interval = 60 # 1 minute, in seconds
__inactivity_time = 1 * 60 * 60 # 1 hour, in seconds

def __init__(self, index=__default_index, local_ep=None, anonymous=False, **kwargs):
def __init__(self, index=__default_index, local_ep=None, anonymous=False,
clear_old_tokens=False, **kwargs):
"""**Initialize the Forge instance.**
Args:
Expand All @@ -43,6 +44,8 @@ def __init__(self, index=__default_index, local_ep=None, anonymous=False, **kwar
If not provided, may be autodetected as possible.
anonymous (bool): If **True**, will not authenticate with Globus Auth.
If **False**, will require authentication.
clear_old_tokens (bool): If **True**, will force reauthentication
If **False**, will use existing tokens if possible.
Keyword Args:
**Advanced users only.**
Expand Down Expand Up @@ -73,10 +76,12 @@ def __init__(self, index=__default_index, local_ep=None, anonymous=False, **kwar
clients = (mdf_toolbox.anonymous_login(services) if services else {})
else:
services = kwargs.get('services', self.__auth_services)
clients = (mdf_toolbox.login(credentials={
clients = (mdf_toolbox.login(
credentials={
"app_name": self.__app_name,
"services": services,
"index": self.index}) if services else {})
"index": self.index},
clear_old_tokens=clear_old_tokens) if services else {})
user_clients = kwargs.get("clients", {})
self.__search_client = user_clients.get("search", clients.get("search", None))
self.__transfer_client = user_clients.get("transfer", clients.get("transfer", None))
Expand All @@ -101,10 +106,9 @@ def transfer_client(self):
def mdf_authorizer(self):
return self.__data_mdf_authorizer


# ***********************************************
# * Core functions
# ***********************************************
# ***********************************************
# * Core functions
# ***********************************************

def match_field(self, field, value, required=True, new_group=False):
"""Add a field:value term to the query.
Expand All @@ -125,6 +129,9 @@ def match_field(self, field, value, required=True, new_group=False):
Returns:
self (Forge): For chaining.
"""
# No-op on missing arguments
if not field and not value:
return self
# If not the start of the query string, add an AND or OR
if self.__query.initialized:
if required:
Expand Down Expand Up @@ -152,6 +159,9 @@ def exclude_field(self, field, value, new_group=False):
Returns:
self (Forge): For chaining.
"""
# No-op on missing arguments
if not field and not value:
return self
# If not the start of the query string, add an AND
# OR would not make much sense for excluding
if self.__query.initialized:
Expand Down Expand Up @@ -269,10 +279,9 @@ def reset_query(self):
del self.__query
self.__query = Query(self.__search_client)


# ***********************************************
# * Expanded functions
# ***********************************************
# ***********************************************
# * Expanded functions
# ***********************************************

def match_range(self, field, start="*", stop="*", inclusive=True,
required=True, new_group=False):
Expand Down Expand Up @@ -354,10 +363,9 @@ def exclude_range(self, field, start="*", stop="*", inclusive=True,
self.exclude_field(field, value, new_group=new_group)
return self


# ***********************************************
# * Helper functions
# ***********************************************
# ***********************************************
# * Helper functions
# ***********************************************

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


# ***********************************************
# * Premade searches
# ***********************************************
# ***********************************************
# * Premade searches
# ***********************************************

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


# ***********************************************
# * Data retrieval functions
# ***********************************************
# ***********************************************
# * Data retrieval functions
# ***********************************************

def http_download(self, results, dest=".", preserve_dir=False, verbose=True):
"""Download data files from the provided results using HTTPS.
Expand Down Expand Up @@ -788,23 +794,18 @@ def http_download(self, results, dest=".", preserve_dir=False, verbose=True):
# Check if file already exists, change filename if necessary
collisions = 0
while os.path.exists(local_path):
# Find period marking extension, if exists
# Will be after last slash
last_slash = local_path.rfind("/")
index = local_path.rfind(".", (last_slash if last_slash != -1 else 0))
if index < 0:
ext = ""
else:
ext = local_path[index:]
local_path = local_path[:index]
# Save and remove extension
local_path, ext = os.path.splitext(local_path)
# Check if already added number to end
old_add = "("+str(collisions)+")"
collisions += 1
new_add = "("+str(collisions)+")"
# Remove old number if exists
if local_path.endswith(old_add):
local_path = local_path[:-len(old_add)] + new_add + ext
else:
local_path = local_path + new_add + ext
local_path = local_path[:-len(old_add)]
# Add new number
local_path = local_path + new_add + ext

headers = {}
# Check for Petrel vs. NCSA url for authorizer
# Petrel
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='mdf_forge',
version='0.6.3',
version='0.6.4',
packages=['mdf_forge'],
description='Materials Data Facility python package',
long_description=("Forge is the Materials Data Facility Python package"
Expand Down
4 changes: 3 additions & 1 deletion tests/test_forge.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,9 @@ def test_forge_match_field():
# Check that query clears
assert f.search() == []

# Also checking check_field
# Also checking check_field and no-op
f.match_field("material.elements", "Al")
f.match_field("", "")
res2 = f.search()
assert check_field(res2, "material.elements", "Al") == 1

Expand All @@ -415,6 +416,7 @@ def test_forge_exclude_field():
f = forge.Forge(index="mdf")
# Basic usage
f.exclude_field("material.elements", "Al")
f.exclude_field("", "")
f.match_field("mdf.source_name", "ab_initio_solute_database*")
res1 = f.search()
assert check_field(res1, "material.elements", "Al") == -1
Expand Down

0 comments on commit 0c3be38

Please sign in to comment.