63
63
"build_and_update_cache" ,
64
64
"init_logging" ,
65
65
"PitCrewFormatter" ,
66
- "ModelNames " ,
66
+ "ModelTuple " ,
67
67
"download_model_fuel_tools" ,
68
68
"sync_sdf"
69
69
]
72
72
logger = logging .getLogger (__name__ )
73
73
logger .addHandler (logging .NullHandler ())
74
74
75
- ModelNames = namedtuple ("ModelNames" , ["model_name" , "author_name" ])
75
+ ModelTuple = namedtuple (
76
+ "ModelTuple" , ["model_name" , "author_name" , "upload_date" ])
76
77
77
78
78
79
def remove_spaces (original ):
@@ -113,8 +114,8 @@ def get_missing_models(model_names, model_path=None,
113
114
114
115
Args:
115
116
model_names (iterable): Iterable of model names to classify.
116
- Also supports ModelNames tuples, or unnamed tuples of
117
- (model_name, author_name)!
117
+ Also supports ModelTuple tuples, or unnamed tuples of
118
+ (model_name, author_name) or (model_name, author_name, upload_time) !
118
119
model_path (str, optional): Overall path to model directory.
119
120
Defaults to None. If None, function will use "~/.gazebo/models" or
120
121
"~/.ignition/fuel" depending on the value of ign.
@@ -144,9 +145,11 @@ def get_missing_models(model_names, model_path=None,
144
145
- Missing models are models that are not in your local directory
145
146
and also missing from Fuel.
146
147
"""
147
- for key , model_name in enumerate (model_names ):
148
- if isinstance (model_name , ModelNames ) or isinstance (model_name , tuple ):
149
- assert len (model_name ) == 2 , \
148
+ # Enumerate prevents a lone tuple from being interpreted as a list of three
149
+ # singles.
150
+ for _ , model_name in enumerate (model_names ):
151
+ if isinstance (model_name , ModelTuple ) or isinstance (model_name , tuple ):
152
+ assert len (model_name ) == 2 or len (model_name ) == 3 , \
150
153
"Invalid model name tuple given: %s!" % model_name
151
154
152
155
if update_cache :
@@ -192,7 +195,7 @@ def get_missing_models(model_names, model_path=None,
192
195
elif isinstance (model , tuple ):
193
196
model_name = model [0 ]
194
197
author_name = model [1 ]
195
- elif isinstance (model , ModelNames ):
198
+ elif isinstance (model , ModelTuple ):
196
199
model_name = model .model_name
197
200
author_name = model .author_name
198
201
@@ -243,7 +246,7 @@ def get_local_model_name_tuples(path=None, config_file="model.config",
243
246
default_author_name = "" , lower = True ,
244
247
use_dir_as_name = False , ign = False ):
245
248
"""
246
- Gets all ModelNames tuples from a given overall local model path.
249
+ Gets all ModelTuple tuples from a given overall local model path.
247
250
248
251
Args:
249
252
path (str, optional): Overall path to model directory.
@@ -261,7 +264,7 @@ def get_local_model_name_tuples(path=None, config_file="model.config",
261
264
following Ignition's directory structure. Defaults to False.
262
265
263
266
Returns:
264
- dictionary (ModelNames : str): Dictionary where ModelNames tuples are
267
+ dictionary (ModelTuple : str): Dictionary where ModelTuple tuples are
265
268
keys, and the model path is the value. Each name will be lower-case
266
269
only unless lower is False.
267
270
"""
@@ -313,7 +316,7 @@ def get_local_model_name_tuples(path=None, config_file="model.config",
313
316
if model_path .endswith ("/" ):
314
317
model_path = model_path [:- 1 ]
315
318
316
- name_tuple = ModelNames (os .path .basename (model_path ),
319
+ name_tuple = ModelTuple (os .path .basename (model_path ),
317
320
name_tuple .author_name )
318
321
319
322
output [name_tuple ] = latest_ver_path
@@ -339,7 +342,7 @@ def get_model_name_tuple(config_file_path, config_file="model.config",
339
342
Defaults to True.
340
343
341
344
Returns:
342
- (str, str): ModelNames tuple of (model_name, author_name). Each name
345
+ (str, str): ModelTuple tuple of (model_name, author_name). Each name
343
346
will be lower-case only unless lower is False.
344
347
345
348
Warnings:
@@ -372,17 +375,17 @@ def get_model_name_tuple(config_file_path, config_file="model.config",
372
375
author_name = default_author_name
373
376
374
377
if lower :
375
- return ModelNames (model_name .lower (), author_name .lower ())
378
+ return ModelTuple (model_name .lower (), author_name .lower ())
376
379
else :
377
- return ModelNames (model_name , author_name )
380
+ return ModelTuple (model_name , author_name )
378
381
379
382
380
383
def get_author_to_model_dict (model_name_tuples , lower = True ):
381
384
"""
382
385
Get a dictionary of author names mapped to model names.
383
386
384
387
Args:
385
- model_name_tuples (iterable): An iterable of ModelNames or unnamed
388
+ model_name_tuples (iterable): An iterable of ModelTuple or unnamed
386
389
tuples of (model_name, author_name).
387
390
lower (bool, optional): Make all output names lower-case.
388
391
Defaults to True.
@@ -393,7 +396,7 @@ def get_author_to_model_dict(model_name_tuples, lower=True):
393
396
"""
394
397
output = {}
395
398
396
- for (model_name , author_name ) in model_name_tuples :
399
+ for (model_name , author_name , _ ) in model_name_tuples :
397
400
if lower :
398
401
model_name = model_name .lower ()
399
402
author_name = author_name .lower ()
@@ -411,7 +414,7 @@ def get_model_to_author_dict(model_name_tuples, lower=True):
411
414
Get a dictionary of model names mapped to author names.
412
415
413
416
Args:
414
- model_name_tuples (list or set): An iterable of ModelNames tuples of
417
+ model_name_tuples (list or set): An iterable of ModelTuple tuples of
415
418
(model_name, author_name). Each name will be lower-case only.
416
419
lower (bool, optional): Make all output names lower-case.
417
420
Defaults to True.
@@ -422,7 +425,7 @@ def get_model_to_author_dict(model_name_tuples, lower=True):
422
425
"""
423
426
output = {}
424
427
425
- for (model_name , author_name ) in model_name_tuples :
428
+ for (model_name , author_name , _ ) in model_name_tuples :
426
429
if lower :
427
430
model_name = model_name .lower ()
428
431
author_name = author_name .lower ()
@@ -447,8 +450,8 @@ def get_fuel_authors(model_name, cache_file_path=None, update_cache=True):
447
450
else :
448
451
cache = load_cache (cache_file_path )
449
452
450
- if isinstance (model_name , ModelNames ) or isinstance (model_name , tuple ):
451
- assert len (model_name ) == 2 , "Invalid model name tuple given: %s!" \
453
+ if isinstance (model_name , ModelTuple ) or isinstance (model_name , tuple ):
454
+ assert len (model_name ) == 2 or len ( model_name ) == 3 , "Invalid model name tuple given: %s!" \
452
455
% model_name
453
456
model_name = model_name [0 ]
454
457
@@ -713,6 +716,8 @@ def download_model_fuel_tools(model_name, author_name,
713
716
(model_name , export_path ))
714
717
715
718
return True
719
+ except KeyError as e :
720
+ return False
716
721
except Exception as e :
717
722
logger .error ("Could not download model '%s'! %s" % (model_name , e ))
718
723
return False
@@ -745,7 +750,7 @@ def load_cache(cache_file_path=None, lower=True):
745
750
746
751
Returns:
747
752
dict: Cache dict, with keys 'model_cache' and 'fuel_cache'.
748
- model_cache will contain ModelNames tuples of
753
+ model_cache will contain ModelTuple tuples of
749
754
(model_name, author_name).
750
755
Whereas fuel_cache will contain JSON responses from Fuel.
751
756
@@ -765,12 +770,12 @@ def load_cache(cache_file_path=None, lower=True):
765
770
766
771
if lower :
767
772
model_cache = set (
768
- ModelNames (* [name .lower () for name in x ])
773
+ ModelTuple (* [name .lower () for name in x ])
769
774
for x in loaded_cache .get ("model_cache" )
770
775
)
771
776
else :
772
777
model_cache = set (
773
- ModelNames (* x ) for x in loaded_cache .get ("model_cache" )
778
+ ModelTuple (* x ) for x in loaded_cache .get ("model_cache" )
774
779
)
775
780
776
781
logger .info ("Load success!\n " )
@@ -844,25 +849,27 @@ def build_and_update_cache(cache_file_path=None, write_to_cache=True,
844
849
while status == 200 and not break_flag :
845
850
logger .info ("Fetching page: %d" % page )
846
851
847
- resp = requests .get ("%s?page=%d" % (url_base , page ))
852
+ resp = requests .get ("%s?page=%d&per_page=100 " % (url_base , page ))
848
853
status = resp .status_code
849
854
page += 1
850
855
851
856
if status == 200 :
852
857
for model in json .loads (resp .text ):
853
858
model_name = model .get ("name" , "" )
854
859
author_name = model .get ("owner" , "" )
860
+ upload_date = model .get ("upload_date" , "" )
855
861
856
862
# If a cached model was found, halt
857
- if (model_name , author_name ) in old_cache ['model_cache' ]:
858
- logger .info ("Cached model found! "
863
+ if (model_name , author_name , upload_date ) in old_cache ['model_cache' ]:
864
+ logger .info ("Cached model found! " , ( model_name , author_name , upload_date ),
859
865
"Halting Fuel traversal..." )
860
866
break_flag = True
861
867
break
862
868
# Otherwise, add it to the cache
863
869
else :
864
870
new_cache_count += 1
865
- old_cache ['model_cache' ].add ((model_name , author_name ))
871
+ old_cache ['model_cache' ].add (
872
+ (model_name , author_name , upload_date ))
866
873
old_cache ['fuel_cache' ].append (model )
867
874
else :
868
875
break
0 commit comments