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/>.
from pycam.Toolpath import Bounds
import pycam.Cutters
import pycam.Utils.log
import pycam.Utils
import pycam.Toolpath
import ConfigParser
import StringIO
......@@ -331,17 +332,19 @@ process: 3
self.config.readfp(config_text)
def load_file(self, filename):
uri = pycam.Utils.URIHandler(filename)
try:
content = file(filename).read()
handle = uri.open()
content = handle.read()
except IOError, err_msg:
log.error("Settings: Failed to read config file '%s': %s" \
% (filename, err_msg))
% (uri, err_msg))
return False
try:
self.reset(content)
except ConfigParser.ParsingError, err_msg:
log.error("Settings: Failed to parse config file '%s': %s" \
% (filename, err_msg))
% (uri, err_msg))
return False
return True
......@@ -357,9 +360,10 @@ process: 3
def write_to_file(self, filename, tools=None, processes=None, bounds=None,
tasks=None):
uri = pycam.Utils.URIHandler(filename)
text = self.get_config_text(tools, processes, bounds, tasks)
try:
handle = open(filename, "w")
handle = open(uri.get_local_path(), "w")
handle.write(text)
handle.close()
except IOError, err_msg:
......
......@@ -25,7 +25,7 @@ from pycam.Geometry.Line import Line
from pycam.Geometry.Point import Point
from pycam.Geometry import get_points_of_arc
import pycam.Utils.log
from pycam.Utils import open_url
import pycam.Utils
log = pycam.Utils.log.get_logger()
......@@ -174,7 +174,7 @@ class CXFParser(object):
def import_font(filename, callback=None):
try:
infile = open_url(filename)
infile = pycam.Utils.URIHandler(filename).open()
except IOError, err_msg:
log.error("CXFImporter: Failed to read file (%s): %s" \
% (filename, err_msg))
......
......@@ -27,7 +27,7 @@ import pycam.Geometry.Model
import pycam.Geometry.Matrix
import pycam.Geometry
import pycam.Utils.log
from pycam.Utils import open_url
import pycam.Utils
import math
import re
import os
......@@ -852,8 +852,9 @@ def import_model(filename, color_as_height=False, fonts_cache=None,
infile = filename
else:
try:
infile = open_url(filename)
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
......
......@@ -22,7 +22,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
from pycam.Importers.SVGImporter import convert_eps2dxf
import pycam.Importers.DXFImporter
from pycam.Utils import check_uri_exists, retrieve_uri
import pycam.Utils
import tempfile
import os
......@@ -45,17 +45,17 @@ def import_model(filename, program_locations=None, unit="mm", callback=None,
return
filename = ps_file_name
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)
return None
if not os.path.isfile(filename):
if not uri.is_local():
# non-local file - write it to a temporary file first
uri = filename
ps_file_handle, ps_file_name = tempfile.mkstemp(suffix=".ps")
os.close(ps_file_handle)
log.debug("Retrieving PS file for local access: %s -> %s" % \
(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: " + \
"%s -> %s" % (uri, ps_file_name))
return
......
......@@ -27,7 +27,7 @@ from pycam.Geometry.PointKdtree import PointKdtree
from pycam.Geometry.utils import epsilon
from pycam.Geometry.Model import Model
import pycam.Utils.log
from pycam.Utils import open_url
import pycam.Utils
from struct import unpack
import StringIO
......@@ -63,10 +63,10 @@ def ImportModel(filename, use_kdtree=True, callback=None, **kwargs):
if hasattr(filename, "read"):
f = filename
# useful for later error messages
filename = "input data"
filename = "input stream"
else:
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
# the whole file at once. This is ugly - anyone with a better idea?
f = StringIO.StringIO(url_file.read())
......
......@@ -21,7 +21,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
import pycam.Importers.DXFImporter
from pycam.Utils import check_uri_exists, retrieve_uri
import pycam.Utils
import tempfile
import subprocess
import os
......@@ -97,17 +97,17 @@ def import_model(filename, program_locations=None, unit="mm", callback=None,
return
filename = svg_file_name
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)
return None
if not os.path.isfile(filename):
if not uri.is_local():
# non-local file - write it to a temporary file first
uri = filename
svg_file_handle, svg_file_name = tempfile.mkstemp(suffix=".svg")
os.close(svg_file_handle)
log.debug("Retrieving SVG file for local access: %s -> %s" % \
(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 " + \
"file: %s -> %s" % (uri, svg_file_name))
filename = svg_file_name
......
......@@ -23,7 +23,7 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
import pycam.Gui.Settings
import pycam.Gui.Project
import pycam.Utils.log
from pycam.Utils import open_url
import pycam.Utils
import re
import os
import sys
......@@ -59,7 +59,7 @@ def parse_toolpath_settings(filename):
else:
# open the file
try:
infile = open_url(filename)
infile = pycam.Utils.URIHandler(filename).open()
except IOError, err_msg:
log.warn("ToolpathSettingsParser: Failed to read file (%s): %s" % \
(filename, err_msg))
......
......@@ -21,13 +21,15 @@ along with PyCAM. If not, see <http://www.gnu.org/licenses/>.
"""
__all__ = ["iterators", "polynomials", "ProgressCounter", "threading",
"get_platform", "get_external_program_location", "PLATFORM_WINDOWS",
"PLATFORM_MACOS", "PLATFORM_LINUX", "PLATFORM_UNKNOWN"]
"get_platform", "get_external_program_location", "URIHandler",
"PLATFORM_WINDOWS", "PLATFORM_MACOS", "PLATFORM_LINUX",
"PLATFORM_UNKNOWN"]
import sys
import os
import socket
import urllib
import urlparse
# this is imported below on demand
#import win32com
#import win32api
......@@ -56,34 +58,103 @@ def get_platform():
return PLATFORM_UNKNOWN
def open_url(uri):
if (get_platform() == PLATFORM_WINDOWS) and (uri[1:3] == ":\\"):
# We are on Windows and a local path is given. Open the file
# normally. Otherwise "C:\\" is misinterpreted as a protocol.
return open(uri)
class URIHandler(object):
DEFAULT_PREFIX = "file://"
def __init__(self, location):
self._uri = None
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 urllib.urlopen(uri)
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 check_uri_exists(uri):
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:
handle = open_url(uri)
handle.close()
urllib.urlretrieve(uri, destination, download_callback)
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()
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:
download_callback = None
try:
urllib.urlretrieve(uri, filename, download_callback)
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 get_all_ips():
""" 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