Commit 6581a46c authored by sumpfralle's avatar sumpfralle

fixed various small problems of the filename/URI handling


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1026 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent f411d3a4
......@@ -100,10 +100,10 @@ def show_gui(inputfile=None, task_settings_file=None):
gui = gui_class()
# load the given model or the default
if inputfile is None:
if not inputfile:
default_model = get_default_model()
if isinstance(default_model, basestring):
gui.load_model_file(filename=default_model)
if isinstance(default_model, (basestring, pycam.Utils.URIHandler)):
gui.load_model_file(filename=default_model, store_filename=False)
else:
gui.load_model(default_model)
else:
......@@ -113,6 +113,9 @@ def show_gui(inputfile=None, task_settings_file=None):
if not task_settings_file is None:
gui.open_task_settings_file(task_settings_file)
# tell the GUI to empty the "undo" queue
gui.finish_startup()
# open the GUI
gui.mainloop()
# no error -> return no error code
......@@ -134,14 +137,16 @@ def get_default_model():
return pycam.Importers.TestModel.get_test_model()
def load_model_file(filename, program_locations, unit=None):
filename = os.path.expanduser(filename)
if not os.path.isfile(filename):
log.warn("The input file ('%s') was not found!" % filename)
uri = pycam.Utils.URIHandler(filename)
if uri.is_local():
uri = pycam.Utils.URIHandler(os.path.expanduser(str(filename)))
if not uri.exists():
log.warn("The input file ('%s') was not found!" % uri)
return None
importer = pycam.Importers.detect_file_type(filename)[1]
model = importer(filename, program_locations=program_locations, unit=unit)
importer = pycam.Importers.detect_file_type(uri)[1]
model = importer(uri, program_locations=program_locations, unit=unit)
if model is None:
log.warn("Failed to load the model file (%s)." % filename)
log.warn("Failed to load the model file (%s)." % uri)
return None
else:
return model
......@@ -167,7 +172,7 @@ def execute(parser, opts, args, pycam):
pycam.Utils.setproctitle("pycam")
if len(args) > 0:
inputfile = os.path.expanduser(args[0])
inputfile = pycam.Utils.URIHandler(args[0])
else:
inputfile = None
......
......@@ -1053,18 +1053,28 @@ caused by careless DXF/SVG exporting programs.</property>
</packing>
</child>
<child>
<object class="GtkLinkButton" id="ModelTransformationsHelp">
<property name="label">gtk-help</property>
<object class="GtkVButtonBox" id="vbuttonbox6">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="relief">none</property>
<property name="use_stock">True</property>
<property name="uri">http://sourceforge.net/apps/mediawiki/pycam/index.php?title=ModelTransformations</property>
<property name="orientation">vertical</property>
<property name="layout_style">end</property>
<child>
<object class="GtkLinkButton" id="ModelTransformationsHelp">
<property name="label">gtk-help</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="relief">none</property>
<property name="use_stock">True</property>
<property name="uri">http://sourceforge.net/apps/mediawiki/pycam/index.php?title=ModelTransformations</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
......@@ -3253,18 +3263,27 @@ Usually you will want to use the cutter radius here to cut around the outline.</
</packing>
</child>
<child>
<object class="GtkLinkButton" id="BoundsHelp">
<property name="label">gtk-help</property>
<object class="GtkVButtonBox" id="vbuttonbox7">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="has_tooltip">True</property>
<property name="relief">none</property>
<property name="use_stock">True</property>
<property name="uri">http://sourceforge.net/apps/mediawiki/pycam/index.php?title=BoundsSettings</property>
<property name="orientation">vertical</property>
<property name="layout_style">end</property>
<child>
<object class="GtkLinkButton" id="BoundsHelp">
<property name="label">gtk-help</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="relief">none</property>
<property name="use_stock">True</property>
<property name="uri">http://sourceforge.net/apps/mediawiki/pycam/index.php?title=BoundsSettings</property>
</object>
<packing>
<property name="expand">False</property>
<property name="position">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="position">1</property>
</packing>
</child>
......
......@@ -1289,7 +1289,7 @@ class ProjectGui:
"""
uri = pycam.Utils.URIHandler(filename)
self.last_model_uri = uri
if self.last_model_uri:
if not self.last_model_uri:
self.window.set_title("PyCAM")
else:
short_name = os.path.basename(uri.get_path())
......@@ -2017,7 +2017,7 @@ class ProjectGui:
def export_from_font_dialog(self, widget=None):
text_model = self.get_font_dialog_text_rendered()
if text_model and (not text_model.maxx is None):
self.save_model(model=text_model)
self.save_model(model=text_model, store_filename=False)
def copy_font_dialog_to_clipboard(self, widget=None):
text_model = self.get_font_dialog_text_rendered()
......@@ -2713,7 +2713,8 @@ class ProjectGui:
return filename
@gui_activity_guard
def save_model(self, widget=None, filename=None, model=None):
def save_model(self, widget=None, filename=None, model=None,
store_filename=True):
if model is None:
model = self.model
if not model.is_export_supported():
......@@ -2723,7 +2724,8 @@ class ProjectGui:
# get the filename
if callable(filename):
filename = filename()
if not isinstance(filename, basestring):
uri = None
if not isinstance(filename, (basestring, pycam.Utils.URIHandler)):
# we open a dialog
# determine the file type
# TODO: this needs to be decided by the exporter code
......@@ -2740,21 +2742,30 @@ class ProjectGui:
mode_load=False, type_filter=type_filter,
filename_templates=(self.last_model_uri,))
if filename:
self.set_model_filename(filename)
uri = pycam.Utils.URIHandler(filename)
if uri.is_local() and store_filename:
self.set_model_filename(filename)
else:
uri = pycam.Utils.URIHandler(filename)
# no filename given -> exit
if not filename:
if not uri:
return
try:
file_in = open(filename, "w")
model.export(comment=self.get_meta_data(),
unit=self.settings.get("unit")).write(file_in)
file_in.close()
except IOError, err_msg:
log.error("Failed to save model file: %s" % err_msg)
if not uri.is_local():
log.error("Unable to write file to a non-local " + \
"destination: %s" % uri)
else:
log.info("Successfully stored the current model as '%s'." % \
str(filename))
self.add_to_recent_file_list(filename)
try:
file_in = open(uri.get_local_path(), "w")
model.export(comment=self.get_meta_data(),
unit=self.settings.get("unit")).write(file_in)
file_in.close()
except IOError, err_msg:
log.error("Failed to save model file: %s" % err_msg)
else:
log.info("Successfully stored the current model as '%s'." % \
str(filename))
self.update_save_actions()
self.add_to_recent_file_list(filename)
@gui_activity_guard
def reset_preferences(self, widget=None):
......@@ -3049,7 +3060,7 @@ class ProjectGui:
@gui_activity_guard
@progress_activity_guard
def load_model_file(self, widget=None, filename=None):
def load_model_file(self, widget=None, filename=None, store_filename=True):
if callable(filename):
filename = filename()
if not filename:
......@@ -3066,7 +3077,8 @@ class ProjectGui:
unit=self.settings.get("unit"),
fonts_cache=self._fonts_cache,
callback=self.update_progress_bar)):
self.set_model_filename(filename)
if store_filename:
self.set_model_filename(filename)
self.add_to_recent_file_list(filename)
return True
else:
......@@ -3095,6 +3107,14 @@ class ProjectGui:
else:
self.add_to_recent_file_list(filename)
def finish_startup(self):
""" This function is called by the pycam script after everything is
set up properly.
"""
# empty the "undo" states (accumulated by loading the defualt model)
while self._undo_states:
self._undo_states.pop(0)
def open_task_settings_file(self, filename):
""" This function is used by the commandline handler """
self.last_task_settings_uri = pycam.Utils.URIHandler(filename)
......@@ -3610,7 +3630,7 @@ class ProjectGui:
def save_task_settings_file(self, widget=None, filename=None):
if callable(filename):
filename = filename()
if not isinstance(filename, basestring):
if not isinstance(filename, (basestring, pycam.Utils.URIHandler)):
# we open a dialog
filename = self.get_filename_via_dialog("Save settings to ...",
mode_load=False, type_filter=FILTER_CONFIG,
......@@ -3626,7 +3646,9 @@ class ProjectGui:
self.process_list, self.bounds_list, self.task_list):
log.error("Failed to save settings file")
else:
log.info("Task settings written to %s" % filename)
self.add_to_recent_file_list(filename)
self.update_save_actions()
def toggle_progress_bar(self, status):
# always hide the progress button - it will be enabled later
......@@ -4049,8 +4071,10 @@ class ProjectGui:
valid_templates = []
if filename_templates:
for template in filename_templates:
if hasattr(template, "get_local_path"):
valid_templates.append(template.get_local_path())
if not template:
continue
elif hasattr(template, "get_path"):
valid_templates.append(template.get_path())
else:
valid_templates.append(template)
if valid_templates:
......
......@@ -854,7 +854,6 @@ def import_model(filename, color_as_height=False, fonts_cache=None,
try:
infile = pycam.Utils.URIHandler(filename).open()
except IOError, err_msg:
print pycam.Utils.URIHandler(filename)
log.error("DXFImporter: Failed to read file (%s): %s" \
% (filename, err_msg))
return None
......
......@@ -24,6 +24,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
__all__ = ["STLImporter", "DXFImporter", "SVGImporter", "TestModel"]
import pycam.Utils.log
import pycam.Utils
import pycam.Importers.DXFImporter
import pycam.Importers.STLImporter
import pycam.Importers.SVGImporter
......@@ -35,6 +36,9 @@ log = pycam.Utils.log.get_logger()
def detect_file_type(filename, quiet=False):
# also accept URI input
uri = pycam.Utils.URIHandler(filename)
filename = uri.get_path()
failure = (None, None)
# check all listed importers
# TODO: this should be done by evaluating the header of the file
......
......@@ -76,6 +76,8 @@ class URIHandler(object):
def set_location(self, location):
if isinstance(location, URIHandler):
self._uri = location._uri
elif not location:
self._uri = urlparse.urlparse(self.DEFAULT_PREFIX)
elif (get_platform() == PLATFORM_WINDOWS) and (location[1:3] == ":\\"):
self._uri = urlparse.urlparse(self.DEFAULT_PREFIX + location)
else:
......@@ -91,7 +93,6 @@ class URIHandler(object):
def get_local_path(self):
if self.is_local():
print "LOCAL:", self._uri.path
return self._uri.path
else:
return None
......@@ -184,7 +185,6 @@ def get_all_ips():
return cmp(ip1, ip2)
# non-local IPs first
filtered_result.sort(cmp=sort_ip_by_relevance)
print filtered_result
return filtered_result
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment