Xorn
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
read.py
Go to the documentation of this file.
1 # xorn.geda - Python library for manipulating gEDA files
2 # Copyright (C) 1998-2010 Ales Hvezda
3 # Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
4 # Copyright (C) 2013-2016 Roland Lutz
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software Foundation,
18 # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20 ## \namespace xorn.geda.read
21 ## Reading schematic/symbol files.
22 
23 import os.path, sys
24 from gettext import gettext as _
25 import xorn.geda.clib
28 import xorn.geda.ref
29 import xorn.geda.xmlread
30 
31 ## Raised when parsing a malformed file.
32 
33 class ParseError(Exception):
34  pass
35 
36 ## Default behavior for handling file read errors and warnings.
37 #
38 # When reading a file, a log object can be specified which handles any
39 # errors and warnings which occur while reading the file. If no log
40 # object is specified, a new DefaultLog instance is used instead.
41 #
42 # The behavior of DefaultLog is to print any messages to \c sys.stderr
43 # along with the file name passed to the constructor, and to raise a
44 # ParseError exception on error.
45 
46 class DefaultLog:
47  def __init__(self, name):
48  self.name = name
49  self.lineno = 0
50 
51  def error(self, message):
52  sys.stderr.write("%s:%d: error: %s\n" % (
53  self.name, self.lineno + 1, message))
54  raise ParseError
55 
56  def warn(self, message):
57  sys.stderr.write("%s:%d: warning: %s\n" % (
58  self.name, self.lineno + 1, message))
59 
60 ## Read a symbol or schematic file.
61 #
62 # See \ref read_file for a description of the keyword arguments.
63 #
64 # \returns a transient xorn.proxy.RevisionProxy instance containing
65 # the file's contents
66 #
67 # \throws ParseError if the file is not a valid schematic/symbol file
68 # \throws xorn.geda.fileformat.UnknownFormatError if \a format is not
69 # specified and the format can't be guessed from \a path
70 
71 def read(path, format = None, **kwds):
72  if format is None:
74 
75  f = open(path, 'rb')
76  try:
77  return read_file(
78  f, path, format, pixmap_basepath = os.path.dirname(path), **kwds)
79  finally:
80  f.close()
81 
82 ## Read a symbol or schematic file from a file object.
83 #
84 # \param [in] f A file-like object from which to read
85 # \param [in] name The file name displayed in warning and
86 # error messages
87 # \param [in] format The file format to be expected
88 # \param [in] log An object to which errors are logged.
89 # If this is \c None (the default), a new
90 # DefaultLog instance is used which
91 # raises a ParseError exception on error
92 # and writes messages to \c sys.stderr
93 # \param [in] load_symbols Whether to load referenced symbol files as well
94 # \param [in] load_pixmaps Whether to load referenced pixmap files as well
95 # \param [in] pixmap_basepath Base directory for relative pixmap paths
96 #
97 # \returns a transient xorn.proxy.RevisionProxy instance containing
98 # the file's contents
99 #
100 # \throws ParseError if the file is not a valid schematic/symbol file
101 
102 def read_file(f, name, format, log = None,
103  load_symbols = False,
104  load_pixmaps = False,
105  pixmap_basepath = None, **kwds):
106  if log is None:
107  log = DefaultLog(name)
108 
109  # Mock-ups for referenced symbols if we aren't loading them
110  referenced_symbols = {}
111  # Mock-ups for or already loaded pixmaps
112  referenced_pixmaps = {}
113 
114  def load_symbol(basename, fallback_available):
115  if load_symbols:
116  # Look up the symbol from the component library, loading
117  # it if necessary.
118  try:
119  return xorn.geda.clib.lookup_symbol(basename)
120  except ValueError:
121  if fallback_available:
122  log.warn(
123  _("symbol \"%s\" not found in library") % basename)
124  else:
125  log.error(
126  _("symbol \"%s\" not found in library") % basename)
127  # fallthrough
128 
129  if fallback_available:
130  return None
131 
132  try:
133  return referenced_symbols[basename]
134  except KeyError:
135  symbol = xorn.geda.ref.Symbol(basename, None, False)
136  referenced_symbols[basename] = symbol
137  return symbol
138 
139  def load_pixmap(filename, fallback_available):
140  try:
141  pixmap = referenced_pixmaps[filename]
142  except KeyError:
143  pixmap = xorn.geda.ref.Pixmap(filename, None, False)
144  referenced_pixmaps[filename] = pixmap
145 
146  if load_pixmaps:
147  if pixmap_basepath is not None:
148  real_filename = os.path.join(pixmap_basepath, filename)
149  else:
150  real_filename = filename
151  try:
152  f = open(real_filename, 'rb')
153  try:
154  pixmap.data = f.read()
155  finally:
156  f.close()
157  except IOError as e:
158  if fallback_available:
159  log.warn(_("can't read pixmap file \"%s\": %s")
160  % (real_filename, e.strerror))
161  else:
162  log.error(_("can't read pixmap file \"%s\": %s")
163  % (real_filename, e.strerror))
164 
165  if pixmap.data is None and fallback_available:
166  return None
167  return pixmap
168 
169  if format == xorn.geda.fileformat.FORMAT_SYM or \
170  format == xorn.geda.fileformat.FORMAT_SCH:
172  f, name, log, load_symbol, load_pixmap, **kwds)
173  if format == xorn.geda.fileformat.FORMAT_SYM_XML or \
174  format == xorn.geda.fileformat.FORMAT_SCH_XML:
176  f, name, log, load_symbol, load_pixmap, **kwds)
177  raise ValueError
File formats.
Definition: fileformat.py:1
Raised when parsing a malformed file.
Definition: read.py:33
Reading gEDA schematic/symbol files in XML format.
Definition: xmlread.py:1
Reading gEDA schematic/symbol files.
Definition: plainread.py:1
def read
Read a symbol or schematic file.
Definition: read.py:71
Default behavior for handling file read errors and warnings.
Definition: read.py:46
The component library system.
Definition: clib.py:1
Referenced symbols and pixmaps.
Definition: ref.py:1
def read_file
Read a symbol or schematic file in libgeda format from a file object.
Definition: plainread.py:172
def lookup_symbol
Get symbol object for a given symbol name.
Definition: clib.py:456
def read_file
Read a symbol or schematic file from a file object.
Definition: read.py:105