Skip to content

Commit efa3ace

Browse files
committed
Workaround for duplicate models for pit_crew
Signed-off-by: methylDragon <[email protected]>
1 parent a83d0e8 commit efa3ace

File tree

1 file changed

+34
-27
lines changed

1 file changed

+34
-27
lines changed

rmf_building_map_tools/pit_crew/pit_crew.py

+34-27
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"build_and_update_cache",
6464
"init_logging",
6565
"PitCrewFormatter",
66-
"ModelNames",
66+
"ModelTuple",
6767
"download_model_fuel_tools",
6868
"sync_sdf"
6969
]
@@ -72,7 +72,8 @@
7272
logger = logging.getLogger(__name__)
7373
logger.addHandler(logging.NullHandler())
7474

75-
ModelNames = namedtuple("ModelNames", ["model_name", "author_name"])
75+
ModelTuple = namedtuple(
76+
"ModelTuple", ["model_name", "author_name", "upload_date"])
7677

7778

7879
def remove_spaces(original):
@@ -113,8 +114,8 @@ def get_missing_models(model_names, model_path=None,
113114
114115
Args:
115116
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)!
118119
model_path (str, optional): Overall path to model directory.
119120
Defaults to None. If None, function will use "~/.gazebo/models" or
120121
"~/.ignition/fuel" depending on the value of ign.
@@ -144,9 +145,11 @@ def get_missing_models(model_names, model_path=None,
144145
- Missing models are models that are not in your local directory
145146
and also missing from Fuel.
146147
"""
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, \
150153
"Invalid model name tuple given: %s!" % model_name
151154

152155
if update_cache:
@@ -192,7 +195,7 @@ def get_missing_models(model_names, model_path=None,
192195
elif isinstance(model, tuple):
193196
model_name = model[0]
194197
author_name = model[1]
195-
elif isinstance(model, ModelNames):
198+
elif isinstance(model, ModelTuple):
196199
model_name = model.model_name
197200
author_name = model.author_name
198201

@@ -243,7 +246,7 @@ def get_local_model_name_tuples(path=None, config_file="model.config",
243246
default_author_name="", lower=True,
244247
use_dir_as_name=False, ign=False):
245248
"""
246-
Gets all ModelNames tuples from a given overall local model path.
249+
Gets all ModelTuple tuples from a given overall local model path.
247250
248251
Args:
249252
path (str, optional): Overall path to model directory.
@@ -261,7 +264,7 @@ def get_local_model_name_tuples(path=None, config_file="model.config",
261264
following Ignition's directory structure. Defaults to False.
262265
263266
Returns:
264-
dictionary (ModelNames: str): Dictionary where ModelNames tuples are
267+
dictionary (ModelTuple: str): Dictionary where ModelTuple tuples are
265268
keys, and the model path is the value. Each name will be lower-case
266269
only unless lower is False.
267270
"""
@@ -313,7 +316,7 @@ def get_local_model_name_tuples(path=None, config_file="model.config",
313316
if model_path.endswith("/"):
314317
model_path = model_path[:-1]
315318

316-
name_tuple = ModelNames(os.path.basename(model_path),
319+
name_tuple = ModelTuple(os.path.basename(model_path),
317320
name_tuple.author_name)
318321

319322
output[name_tuple] = latest_ver_path
@@ -339,7 +342,7 @@ def get_model_name_tuple(config_file_path, config_file="model.config",
339342
Defaults to True.
340343
341344
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
343346
will be lower-case only unless lower is False.
344347
345348
Warnings:
@@ -372,17 +375,17 @@ def get_model_name_tuple(config_file_path, config_file="model.config",
372375
author_name = default_author_name
373376

374377
if lower:
375-
return ModelNames(model_name.lower(), author_name.lower())
378+
return ModelTuple(model_name.lower(), author_name.lower())
376379
else:
377-
return ModelNames(model_name, author_name)
380+
return ModelTuple(model_name, author_name)
378381

379382

380383
def get_author_to_model_dict(model_name_tuples, lower=True):
381384
"""
382385
Get a dictionary of author names mapped to model names.
383386
384387
Args:
385-
model_name_tuples (iterable): An iterable of ModelNames or unnamed
388+
model_name_tuples (iterable): An iterable of ModelTuple or unnamed
386389
tuples of (model_name, author_name).
387390
lower (bool, optional): Make all output names lower-case.
388391
Defaults to True.
@@ -393,7 +396,7 @@ def get_author_to_model_dict(model_name_tuples, lower=True):
393396
"""
394397
output = {}
395398

396-
for (model_name, author_name) in model_name_tuples:
399+
for (model_name, author_name, _) in model_name_tuples:
397400
if lower:
398401
model_name = model_name.lower()
399402
author_name = author_name.lower()
@@ -411,7 +414,7 @@ def get_model_to_author_dict(model_name_tuples, lower=True):
411414
Get a dictionary of model names mapped to author names.
412415
413416
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
415418
(model_name, author_name). Each name will be lower-case only.
416419
lower (bool, optional): Make all output names lower-case.
417420
Defaults to True.
@@ -422,7 +425,7 @@ def get_model_to_author_dict(model_name_tuples, lower=True):
422425
"""
423426
output = {}
424427

425-
for (model_name, author_name) in model_name_tuples:
428+
for (model_name, author_name, _) in model_name_tuples:
426429
if lower:
427430
model_name = model_name.lower()
428431
author_name = author_name.lower()
@@ -447,8 +450,8 @@ def get_fuel_authors(model_name, cache_file_path=None, update_cache=True):
447450
else:
448451
cache = load_cache(cache_file_path)
449452

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!" \
452455
% model_name
453456
model_name = model_name[0]
454457

@@ -713,6 +716,8 @@ def download_model_fuel_tools(model_name, author_name,
713716
(model_name, export_path))
714717

715718
return True
719+
except KeyError as e:
720+
return False
716721
except Exception as e:
717722
logger.error("Could not download model '%s'! %s" % (model_name, e))
718723
return False
@@ -745,7 +750,7 @@ def load_cache(cache_file_path=None, lower=True):
745750
746751
Returns:
747752
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
749754
(model_name, author_name).
750755
Whereas fuel_cache will contain JSON responses from Fuel.
751756
@@ -765,12 +770,12 @@ def load_cache(cache_file_path=None, lower=True):
765770

766771
if lower:
767772
model_cache = set(
768-
ModelNames(*[name.lower() for name in x])
773+
ModelTuple(*[name.lower() for name in x])
769774
for x in loaded_cache.get("model_cache")
770775
)
771776
else:
772777
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")
774779
)
775780

776781
logger.info("Load success!\n")
@@ -844,25 +849,27 @@ def build_and_update_cache(cache_file_path=None, write_to_cache=True,
844849
while status == 200 and not break_flag:
845850
logger.info("Fetching page: %d" % page)
846851

847-
resp = requests.get("%s?page=%d" % (url_base, page))
852+
resp = requests.get("%s?page=%d&per_page=100" % (url_base, page))
848853
status = resp.status_code
849854
page += 1
850855

851856
if status == 200:
852857
for model in json.loads(resp.text):
853858
model_name = model.get("name", "")
854859
author_name = model.get("owner", "")
860+
upload_date = model.get("upload_date", "")
855861

856862
# 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),
859865
"Halting Fuel traversal...")
860866
break_flag = True
861867
break
862868
# Otherwise, add it to the cache
863869
else:
864870
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))
866873
old_cache['fuel_cache'].append(model)
867874
else:
868875
break

0 commit comments

Comments
 (0)