def parse_path_groups_from_svg_file(filename, callback=None): """ parse SVG data from a file and return the resulting svg.path objects grouped by layer """ if callback is None: # we are not running interactively - use big chunks read_chunk_size = 1 * 1024**3 else: # read smaller 16 KB chunks (improving responsiveness of the GUI) read_chunk_size = 64 * 1024**2 target = SVGXMLParser() parser = xml.etree.ElementTree.XMLParser(target=target) try: with open_file_context(filename, "r", True) as svg_file: while True: chunk = svg_file.read(read_chunk_size) if not chunk: break parser.feed(chunk) if callback and callback(): raise AbortOperationException( "SVGImporter: load model operation was cancelled") except IOError as exc: log.error("SVGImporter: Failed to read svg file (%s): %s", filename, exc) return parser.close() return target.groups
def load_workspace_from_file(self, filename, remember_uri=True, default_content=None): if remember_uri: self.last_workspace_uri = pycam.Utils.URIHandler(filename) self.settings.get("set_last_filename")(filename) log.info("Loading workspace from file: %s", filename) try: with open_file_context(filename, "r", True) as in_file: content = in_file.read() except OSError as exc: if default_content: content = default_content else: log.error("Failed to read workspace file (%s): %s", filename, exc) return False try: return self.load_workspace_from_description(content) except PycamBaseException as exc: log.warning( "Failed to load workspace description from file (%s): %s", filename, exc) if default_content: log.info("Falling back to default workspace due to load error") self.load_workspace_from_description(default_content) return False
def save_workspace_to_file(self, filename, remember_uri=True): from pycam.Flow.parser import dump_yaml if remember_uri: self.last_workspace_uri = pycam.Utils.URIHandler(filename) self.settings.emit_event("notify-file-opened", filename) log.info("Storing workspace in file: %s", filename) try: with open_file_context(filename, "w", True) as out_file: dump_yaml(target=out_file) except OSError as exc: log.error("Failed to store workspace in file '%s': %s", filename, exc)
def open_workspace_file(mode="r"): return open_file_context(get_workspace_filename(), mode, True)
def open_preferences_file(mode="r"): return open_file_context(get_config_filename(), mode, True)