Skip to content

Commit

Permalink
load ressources (e.g. models) not only based on current direcory
Browse files Browse the repository at this point in the history
Closes: #101
  • Loading branch information
sumpfralle committed Dec 31, 2017
1 parent ffb23ca commit 8689dec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pycam/Utils/locations.py
Expand Up @@ -61,8 +61,12 @@ def get_ui_file_location(filename, silent=False):
return get_data_file_location(os.path.join(UI_SUBDIR, filename), silent=silent)


def get_data_file_location(filename, silent=False):
for base_dir in DATA_BASE_DIRS:
def get_data_file_location(filename, silent=False, priority_directories=None):
if priority_directories is None:
scan_dirs = DATA_BASE_DIRS
else:
scan_dirs = tuple(priority_directories) + tuple(DATA_BASE_DIRS)
for base_dir in scan_dirs:
test_path = os.path.join(base_dir, filename)
if os.path.exists(test_path):
return test_path
Expand Down
13 changes: 13 additions & 0 deletions pycam/workspace/data_models.py
Expand Up @@ -43,6 +43,7 @@
from pycam.Utils import get_application_key, get_type_name, MultiLevelDictionaryAccess
from pycam.Utils.events import get_event_handler
from pycam.Utils.progress import ProgressContext
from pycam.Utils.locations import get_data_file_location
import pycam.Utils.log
from pycam.workspace import (
BoundsSpecification, CollectionName, FormatType, GCodeDialect, ModelScaleTarget,
Expand Down Expand Up @@ -670,6 +671,18 @@ def _get_source_copy(self, related_collection_name):
def _get_source_location(self, source_type):
location = self.get_value("location")
if source_type == SourceType.FILE:
if not os.path.isabs(location):
# try to guess the absolute location
# TODO: add the directory of the most recently loaded workspace file
# guess the git base directory
git_checkout_dir = os.path.join(os.path.dirname(__file__),
os.path.pardir, os.path.pardir)
search_directories = [os.getcwd(), git_checkout_dir]
abs_location = get_data_file_location(location, silent=True,
priority_directories=search_directories)
# hopefully it worked - otherwise normal error handling will happen
if abs_location is not None:
location = abs_location
location = "file://" + os.path.abspath(location)
detected_filetype = detect_file_type(location)
if detected_filetype:
Expand Down

0 comments on commit 8689dec

Please sign in to comment.