Commit f411d3a4 authored by sumpfralle's avatar sumpfralle

mainly fixed the filename/URI conversion - some testing is still required


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@1025 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 2976c8b8
This diff is collapsed.
...@@ -23,6 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -23,6 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
from pycam.Toolpath import Bounds from pycam.Toolpath import Bounds
import pycam.Cutters import pycam.Cutters
import pycam.Utils.log import pycam.Utils.log
import pycam.Utils
import pycam.Toolpath import pycam.Toolpath
import ConfigParser import ConfigParser
import StringIO import StringIO
...@@ -331,17 +332,19 @@ process: 3 ...@@ -331,17 +332,19 @@ process: 3
self.config.readfp(config_text) self.config.readfp(config_text)
def load_file(self, filename): def load_file(self, filename):
uri = pycam.Utils.URIHandler(filename)
try: try:
content = file(filename).read() handle = uri.open()
content = handle.read()
except IOError, err_msg: except IOError, err_msg:
log.error("Settings: Failed to read config file '%s': %s" \ log.error("Settings: Failed to read config file '%s': %s" \
% (filename, err_msg)) % (uri, err_msg))
return False return False
try: try:
self.reset(content) self.reset(content)
except ConfigParser.ParsingError, err_msg: except ConfigParser.ParsingError, err_msg:
log.error("Settings: Failed to parse config file '%s': %s" \ log.error("Settings: Failed to parse config file '%s': %s" \
% (filename, err_msg)) % (uri, err_msg))
return False return False
return True return True
...@@ -357,9 +360,10 @@ process: 3 ...@@ -357,9 +360,10 @@ process: 3
def write_to_file(self, filename, tools=None, processes=None, bounds=None, def write_to_file(self, filename, tools=None, processes=None, bounds=None,
tasks=None): tasks=None):
uri = pycam.Utils.URIHandler(filename)
text = self.get_config_text(tools, processes, bounds, tasks) text = self.get_config_text(tools, processes, bounds, tasks)
try: try:
handle = open(filename, "w") handle = open(uri.get_local_path(), "w")
handle.write(text) handle.write(text)
handle.close() handle.close()
except IOError, err_msg: except IOError, err_msg:
......
...@@ -25,7 +25,7 @@ from pycam.Geometry.Line import Line ...@@ -25,7 +25,7 @@ from pycam.Geometry.Line import Line
from pycam.Geometry.Point import Point from pycam.Geometry.Point import Point
from pycam.Geometry import get_points_of_arc from pycam.Geometry import get_points_of_arc
import pycam.Utils.log import pycam.Utils.log
from pycam.Utils import open_url import pycam.Utils
log = pycam.Utils.log.get_logger() log = pycam.Utils.log.get_logger()
...@@ -174,7 +174,7 @@ class CXFParser(object): ...@@ -174,7 +174,7 @@ class CXFParser(object):
def import_font(filename, callback=None): def import_font(filename, callback=None):
try: try:
infile = open_url(filename) infile = pycam.Utils.URIHandler(filename).open()
except IOError, err_msg: except IOError, err_msg:
log.error("CXFImporter: Failed to read file (%s): %s" \ log.error("CXFImporter: Failed to read file (%s): %s" \
% (filename, err_msg)) % (filename, err_msg))
......
...@@ -27,7 +27,7 @@ import pycam.Geometry.Model ...@@ -27,7 +27,7 @@ import pycam.Geometry.Model
import pycam.Geometry.Matrix import pycam.Geometry.Matrix
import pycam.Geometry import pycam.Geometry
import pycam.Utils.log import pycam.Utils.log
from pycam.Utils import open_url import pycam.Utils
import math import math
import re import re
import os import os
...@@ -852,8 +852,9 @@ def import_model(filename, color_as_height=False, fonts_cache=None, ...@@ -852,8 +852,9 @@ def import_model(filename, color_as_height=False, fonts_cache=None,
infile = filename infile = filename
else: else:
try: try:
infile = open_url(filename) infile = pycam.Utils.URIHandler(filename).open()
except IOError, err_msg: except IOError, err_msg:
print pycam.Utils.URIHandler(filename)
log.error("DXFImporter: Failed to read file (%s): %s" \ log.error("DXFImporter: Failed to read file (%s): %s" \
% (filename, err_msg)) % (filename, err_msg))
return None return None
......
...@@ -22,7 +22,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -22,7 +22,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
from pycam.Importers.SVGImporter import convert_eps2dxf from pycam.Importers.SVGImporter import convert_eps2dxf
import pycam.Importers.DXFImporter import pycam.Importers.DXFImporter
from pycam.Utils import check_uri_exists, retrieve_uri import pycam.Utils
import tempfile import tempfile
import os import os
...@@ -45,17 +45,17 @@ def import_model(filename, program_locations=None, unit="mm", callback=None, ...@@ -45,17 +45,17 @@ def import_model(filename, program_locations=None, unit="mm", callback=None,
return return
filename = ps_file_name filename = ps_file_name
else: else:
if not check_uri_exists(filename): uri = pycam.Utils.URIHandler(filename)
if not uri.exists():
log.error("PSImporter: file (%s) does not exist" % filename) log.error("PSImporter: file (%s) does not exist" % filename)
return None return None
if not os.path.isfile(filename): if not uri.is_local():
# non-local file - write it to a temporary file first # non-local file - write it to a temporary file first
uri = filename
ps_file_handle, ps_file_name = tempfile.mkstemp(suffix=".ps") ps_file_handle, ps_file_name = tempfile.mkstemp(suffix=".ps")
os.close(ps_file_handle) os.close(ps_file_handle)
log.debug("Retrieving PS file for local access: %s -> %s" % \ log.debug("Retrieving PS file for local access: %s -> %s" % \
(uri, ps_file_name)) (uri, ps_file_name))
if not retrieve_uri(uri, ps_file_name, callback=callback): if not uri.retrieve_remote_file(ps_file_name, callback=callback):
log.error("PSImporter: Failed to retrieve the PS model file: " + \ log.error("PSImporter: Failed to retrieve the PS model file: " + \
"%s -> %s" % (uri, ps_file_name)) "%s -> %s" % (uri, ps_file_name))
return return
......
...@@ -27,7 +27,7 @@ from pycam.Geometry.PointKdtree import PointKdtree ...@@ -27,7 +27,7 @@ from pycam.Geometry.PointKdtree import PointKdtree
from pycam.Geometry.utils import epsilon from pycam.Geometry.utils import epsilon
from pycam.Geometry.Model import Model from pycam.Geometry.Model import Model
import pycam.Utils.log import pycam.Utils.log
from pycam.Utils import open_url import pycam.Utils
from struct import unpack from struct import unpack
import StringIO import StringIO
...@@ -63,10 +63,10 @@ def ImportModel(filename, use_kdtree=True, callback=None, **kwargs): ...@@ -63,10 +63,10 @@ def ImportModel(filename, use_kdtree=True, callback=None, **kwargs):
if hasattr(filename, "read"): if hasattr(filename, "read"):
f = filename f = filename
# useful for later error messages # useful for later error messages
filename = "input data" filename = "input stream"
else: else:
try: try:
url_file = open_url(filename) url_file = pycam.Utils.URIHandler(filename).open()
# urllib.urlopen objects do not support "seek" - so we need to read # urllib.urlopen objects do not support "seek" - so we need to read
# the whole file at once. This is ugly - anyone with a better idea? # the whole file at once. This is ugly - anyone with a better idea?
f = StringIO.StringIO(url_file.read()) f = StringIO.StringIO(url_file.read())
......
...@@ -21,7 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -21,7 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
import pycam.Importers.DXFImporter import pycam.Importers.DXFImporter
from pycam.Utils import check_uri_exists, retrieve_uri import pycam.Utils
import tempfile import tempfile
import subprocess import subprocess
import os import os
...@@ -97,17 +97,17 @@ def import_model(filename, program_locations=None, unit="mm", callback=None, ...@@ -97,17 +97,17 @@ def import_model(filename, program_locations=None, unit="mm", callback=None,
return return
filename = svg_file_name filename = svg_file_name
else: else:
if not check_uri_exists(filename): uri = pycam.Utils.URIHandler(filename)
if not uri.exists():
log.error("SVGImporter: file (%s) does not exist" % filename) log.error("SVGImporter: file (%s) does not exist" % filename)
return None return None
if not os.path.isfile(filename): if not uri.is_local():
# non-local file - write it to a temporary file first # non-local file - write it to a temporary file first
uri = filename
svg_file_handle, svg_file_name = tempfile.mkstemp(suffix=".svg") svg_file_handle, svg_file_name = tempfile.mkstemp(suffix=".svg")
os.close(svg_file_handle) os.close(svg_file_handle)
log.debug("Retrieving SVG file for local access: %s -> %s" % \ log.debug("Retrieving SVG file for local access: %s -> %s" % \
(uri, svg_file_name)) (uri, svg_file_name))
if not retrieve_uri(uri, svg_file_name, callback=callback): if not uri.retrieve_remote_file(svg_file_name, callback=callback):
log.error("SVGImporter: Failed to retrieve the SVG model " + \ log.error("SVGImporter: Failed to retrieve the SVG model " + \
"file: %s -> %s" % (uri, svg_file_name)) "file: %s -> %s" % (uri, svg_file_name))
filename = svg_file_name filename = svg_file_name
......
...@@ -23,7 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -23,7 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import pycam.Gui.Settings import pycam.Gui.Settings
import pycam.Gui.Project import pycam.Gui.Project
import pycam.Utils.log import pycam.Utils.log
from pycam.Utils import open_url import pycam.Utils
import re import re
import os import os
import sys import sys
...@@ -59,7 +59,7 @@ def parse_toolpath_settings(filename): ...@@ -59,7 +59,7 @@ def parse_toolpath_settings(filename):
else: else:
# open the file # open the file
try: try:
infile = open_url(filename) infile = pycam.Utils.URIHandler(filename).open()
except IOError, err_msg: except IOError, err_msg:
log.warn("ToolpathSettingsParser: Failed to read file (%s): %s" % \ log.warn("ToolpathSettingsParser: Failed to read file (%s): %s" % \
(filename, err_msg)) (filename, err_msg))
......
...@@ -21,13 +21,15 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>. ...@@ -21,13 +21,15 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
""" """
__all__ = ["iterators", "polynomials", "ProgressCounter", "threading", __all__ = ["iterators", "polynomials", "ProgressCounter", "threading",
"get_platform", "get_external_program_location", "PLATFORM_WINDOWS", "get_platform", "get_external_program_location", "URIHandler",
"PLATFORM_MACOS", "PLATFORM_LINUX", "PLATFORM_UNKNOWN"] "PLATFORM_WINDOWS", "PLATFORM_MACOS", "PLATFORM_LINUX",
"PLATFORM_UNKNOWN"]
import sys import sys
import os import os
import socket import socket
import urllib import urllib
import urlparse
# this is imported below on demand # this is imported below on demand
#import win32com #import win32com
#import win32api #import win32api
...@@ -56,33 +58,102 @@ def get_platform(): ...@@ -56,33 +58,102 @@ def get_platform():
return PLATFORM_UNKNOWN return PLATFORM_UNKNOWN
def open_url(uri): class URIHandler(object):
if (get_platform() == PLATFORM_WINDOWS) and (uri[1:3] == ":\\"):
# We are on Windows and a local path is given. Open the file DEFAULT_PREFIX = "file://"
# normally. Otherwise "C:\\" is misinterpreted as a protocol.
return open(uri) def __init__(self, location):
else: self._uri = None
return urllib.urlopen(uri) self.set_location(location)
def __str__(self):
url = self._uri.geturl()
if url.startswith(self.DEFAULT_PREFIX):
return url[len(self.DEFAULT_PREFIX):]
else:
return url
def set_location(self, location):
if isinstance(location, URIHandler):
self._uri = location._uri
elif (get_platform() == PLATFORM_WINDOWS) and (location[1:3] == ":\\"):
self._uri = urlparse.urlparse(self.DEFAULT_PREFIX + location)
else:
self._uri = urlparse.urlparse(location)
if not self._uri.scheme:
# always fill the "scheme" field - some functions expect this
self._uri = urlparse.urlparse(self.DEFAULT_PREFIX + \
os.path.realpath(os.path.abspath(location)))
def is_local(self):
return bool(self and not self._uri.scheme or \
(self._uri.scheme == "file"))
def get_local_path(self):
if self.is_local():
print "LOCAL:", self._uri.path
return self._uri.path
else:
return None
def get_path(self):
return self._uri.path
def get_url(self):
return self._uri.geturl()
def open(self):
if self.is_local():
return open(self.get_local_path())
else:
return urllib.urlopen(self._uri.geturl())
def retrieve_remote_file(uri, destination, callback=None):
if callback:
download_callback = lambda current_blocks, block_size, \
num_of_blocks: callback()
else:
download_callback = None
try:
urllib.urlretrieve(uri, destination, download_callback)
return True
except IOError:
return False
def __eq__(self, other):
if isinstance(other, basestring):
return self == URIHandler(other)
elif self.__class__ == other.__class__:
if self.is_local() and other.is_local():
return self._uri.path == other._uri.path
else:
return tuple(self) == tuple(other)
else:
return hash(self) == hash(other)
def __ne__(self, other):
return not self == other
def __nonzero__(self):
return self.get_url() != self.DEFAULT_PREFIX
def exists(self):
if not self:
return False
elif self.is_local():
return os.path.exists(self._uri.path)
else:
try:
handle = self.open()
handle.close()
return True
except IOError:
return False
def is_writable(self):
return bool(self.is_local() and os.path.isfile(self._uri.path) and \
os.access(self._uri.path, os.W_OK))
def check_uri_exists(uri):
try:
handle = open_url(uri)
handle.close()
return True
except IOError:
return False
def retrieve_uri(uri, filename, callback=None):
if callback:
download_callback = lambda current_blocks, block_size, num_of_blocks: \
callback()
else:
download_callback = None
try:
urllib.urlretrieve(uri, filename, download_callback)
return True
except IOError:
return False
def get_all_ips(): def get_all_ips():
""" try to get all IPs of this machine """ try to get all IPs of this machine
......
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