@@ -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
0 commit comments