Skip to content

Commit

Permalink
Merge pull request #5 from botcity-dev/fix-resource-discovery
Browse files Browse the repository at this point in the history
FIX: Iterate over all frames on the stack to add to search path.
  • Loading branch information
hhslepicka authored Mar 2, 2023
2 parents 79b8f57 + de5d777 commit 4eef921
Showing 1 changed file with 47 additions and 10 deletions.
57 changes: 47 additions & 10 deletions botcity/base/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,32 @@


class BaseBot:

def __init__(self, *args, **kwargs):
self._resource_folder_name = "resources"

@property
def resource_folder_name(self) -> str:
"""The name used for the resources folder
when looking for files.
Returns:
str: The folder name for resources. Defaults to `resources`
"""
return self._resource_folder_name or "resources"

@resource_folder_name.setter
def resource_folder_name(self, name: str):
"""The name used for the resources folder
whe looking for files.
Args:
name (str): The folder name for resources. Defaults to `resources`
"""
if not name:
raise ValueError("Resources folder name must not be None nor empty.")
self._resource_folder_name = name

def action(self, execution=None):
"""
Execute an automation action.
Expand All @@ -18,7 +44,7 @@ def action(self, execution=None):
"""
raise NotImplementedError("You must implement this method.")

def get_resource_abspath(self, filename, resource_folder="resources"):
def get_resource_abspath(self, filename, resource_folder=None):
"""
Compose the resource absolute path taking into account the package path.
Expand All @@ -29,18 +55,26 @@ def get_resource_abspath(self, filename, resource_folder="resources"):
Returns:
abs_path (str): The absolute path to the file.
"""
resource_folder = resource_folder or self.resource_folder_name
return path.join(self._resources_path(resource_folder), filename)

def _resources_path(self, resource_folder="resources"):
def _resources_path(self, resource_folder=None):
# This checks if this is a pyinstaller binary
# More info here: https://pyinstaller.org/en/stable/runtime-information.html#run-time-information
resource_folder = resource_folder or self.resource_folder_name

if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
klass_name = self.__class__.__name__
res_path = os.path.join(sys._MEIPASS, klass_name, "resources")
res_path = os.path.join(sys._MEIPASS, klass_name, resource_folder)
else:
res_path = sys.modules[self.__module__].__file__
return path.join(path.dirname(path.realpath(res_path)), resource_folder)

def _get_frame_path(self, frame):
frame_filename = inspect.getframeinfo(frame).filename
frame_dir = os.path.dirname(frame_filename)
return frame_dir

def _search_image_file(self, label):
"""
When finding images, this is the priority in which we will look into:
Expand All @@ -55,6 +89,7 @@ def _search_image_file(self, label):
- sys._MEIPASS
- current working dir
"""
resource_folder = self.resource_folder_name

# map_images
img_path = self.state.map_images.get(label)
Expand All @@ -71,7 +106,7 @@ def _search_image_file(self, label):
# "resources" folder at sys._MEIPASS/<package>/
locations.append(self.get_resource_abspath(""))
# "resources" folder parallel to the sys._MEIPASS folder
locations.append(os.path.join(sys._MEIPASS, "resources"))
locations.append(os.path.join(sys._MEIPASS, resource_folder))
# sys._MEIPASS
locations.append(sys._MEIPASS)
else:
Expand All @@ -82,15 +117,17 @@ def _search_image_file(self, label):

# "resources" folder parallel to the `find` caller file.
try:
caller = inspect.currentframe().f_back.f_back
caller_filename = inspect.getframeinfo(caller).filename
caller_dir = os.path.dirname(caller_filename)
locations.append(os.path.join(caller_dir, "resources"))
frame = inspect.currentframe()
while frame is not None:
caller_dir = self._get_frame_path(frame)
caller_path = os.path.join(caller_dir, resource_folder)
locations.append(caller_path)
frame = frame.f_back
except: # noqa: E722
pass

# "resources" folder parallel to the current working dir
locations.append(os.path.join(os.getcwd(), "resources"))
locations.append(os.path.join(os.getcwd(), resource_folder))

# current working dir
locations.append(os.getcwd())
Expand Down Expand Up @@ -125,7 +162,7 @@ def main(cls):
execution = None
# TODO: Refactor this later for proper parameters to be passed
# in a cleaner way
if len(sys.argv) == 4:
if len(sys.argv) >= 4:
if maestro_available:
server, task_id, token = sys.argv[1:4]
bot.maestro = BotMaestroSDK(server=server)
Expand Down

0 comments on commit 4eef921

Please sign in to comment.