Skip to content

Commit e18d55b

Browse files
Merge pull request #126 from duckietown/DTSW-7564-Add-Duckietown-Shell-billboards-to-the-Duckiematrix
DTSW-7564-Add-Duckietown-Shell-billboards-to-the-Duckiematrix
2 parents 731517c + 7f1dc08 commit e18d55b

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

lib/dt_shell/shell.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,11 @@ def __init__(self,
281281
# get billboard to show (if any)
282282
bboard: Optional[str] = None
283283
if billboard and self.settings.show_billboards:
284-
bboard = self.get_billboard()
285-
284+
# get billboards from the local database
285+
bboard_db = DTShellDatabase.open(DB_BILLBOARDS)
286+
bboard_names = self.get_billboard_names(bboard_db)
287+
if bboard_names:
288+
bboard = self.get_billboard(bboard_db, bboard_names)
286289
# print banner
287290
if banner:
288291
self._show_banner(profile=self._profile, billboard=bboard)
@@ -816,9 +819,19 @@ def sprint(self, msg: str, color: Optional[str] = None, attrs: Sequence[str] = N
816819
return dts_print(msg=msg, color=color, attrs=attrs)
817820

818821
@staticmethod
819-
def get_billboard() -> Optional[str]:
820-
# get billboards from the local database
821-
db: DTShellDatabase = DTShellDatabase.open(DB_BILLBOARDS)
822+
def get_billboard(db, bboard_names) -> Optional[str]:
823+
# pick one source at random
824+
billboard_name = random.choice(bboard_names)
825+
billboard_dictionary = db.get(billboard_name, None) if billboard_name is not None else None
826+
if billboard_dictionary is not None:
827+
content = billboard_dictionary.get("content", None)
828+
if content is None:
829+
logger.warning(f"Billboard '{billboard_name}' exists but has no content.")
830+
return content
831+
return None
832+
833+
@staticmethod
834+
def get_billboard_names(db) -> List[str]:
822835
# collect billboard names based on priority
823836
# first, find the maximum priority
824837
max_priority = -1
@@ -828,22 +841,19 @@ def get_billboard() -> Optional[str]:
828841
max_priority = priority
829842
# no billboards?
830843
if max_priority < 0:
831-
return None
844+
return []
832845
# collect names with weighted selection
833846
# only include priority 0 billboards if no higher priority exists
834-
names: List[str] = []
847+
bboard_names: List[str] = []
835848
for name, billboard in db.items():
836849
priority = billboard.get("priority", 0)
837850
# skip priority 0 billboards if higher priority ones exist
838851
if priority == 0 and max_priority > 0:
839852
continue
840853
# add billboard (priority + 1) times for weighted selection
841854
billboard_name = str(name)
842-
names.extend([billboard_name] * (priority + 1))
843-
# pick one source at random
844-
billboard_name = random.choice(names)
845-
billboard_dictionary = db.get(billboard_name, None) if billboard_name is not None else None
846-
return billboard_dictionary.get("content", None) if billboard_dictionary is not None else None
855+
bboard_names.extend([billboard_name] * (priority + 1))
856+
return bboard_names
847857

848858
def update_commands(self):
849859
# update all command sets

0 commit comments

Comments
 (0)