Merge fixes from branch 'xorn'
[geda-gaf.git] / xorn / src / gaf / netlist / backend.py
blob1b6b98e73df47277d4cea3ad67a2ed9b3d808d52
1 # gaf.netlist - gEDA Netlist Extraction and Generation
2 # Copyright (C) 1998-2010 Ales Hvezda
3 # Copyright (C) 1998-2010 gEDA Contributors (see ChangeLog for details)
4 # Copyright (C) 2013-2020 Roland Lutz
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.
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.
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.
20 ## \namespace gaf.netlist.backend
21 ## Handling netlist backends.
23 # After the netlist has been constructed, it is passed to a \a backend
24 # which creates some kind of output based on the information in the
25 # netlist. The backend is selected by the user via the command-line
26 # option <tt>-g \a BACKEND</tt>; a list of available options can be
27 # displayed using the command-line option `--list-backends`.
29 # A backend is a Python file in the special backend load path whose
30 # name starts with the prefix \c "gnet_". It should contain a special
31 # function \c run(f, netlist) as the main entry point.
33 import imp, os, sys
34 from gettext import gettext as _
36 ## Module name prefix for netlister backends.
38 # Only Python modules whose name starts with this prefix are
39 # considered netlister backends.
41 BACKEND_PREFIX = 'gnet_'
43 ## Backend load path.
45 # A list of directory names in which to search for netlister backends.
47 load_path = []
49 ## Get a sorted list of available backends.
51 # Returns a list of available netlister backends by searching for
52 # files in each of the directories given in \ref load_path. A module
53 # is considered to be a netlister backend if its name begins with
54 # \ref BACKEND_PREFIX ("gnet_").
56 def list_backends():
57 backend_names = set()
59 for dir_name in load_path:
60 try:
61 d_names = os.listdir(dir_name)
62 except OSError as e:
63 sys.stderr.write(_("Can't open directory %s: %s\n")
64 % (dir_name, e.strerror))
65 continue
67 for d_name in d_names:
68 # Check that filename has the right format to be a backend
69 if not d_name.startswith(BACKEND_PREFIX):
70 continue
72 for suffix, mode, type in imp.get_suffixes():
73 if d_name.endswith(suffix, len(BACKEND_PREFIX)):
74 # Remove prefix & suffix. Add to list of backend names.
75 backend_names.add(d_name[len(BACKEND_PREFIX):-len(suffix)])
76 break
78 return sorted(backend_names)
80 ## Load a specific netlister backend.
82 # Searches in the backend load path for a module called \c
83 # gnet_<em>backend_name</em>, loads it, and returns the module object.
85 # The backend module's path is added to the system load path.
87 # \throws ImportError if the module could not be loaded
89 def load(backend_name):
90 # Search for backend module in load path
91 f, pathname, description = imp.find_module(
92 BACKEND_PREFIX + backend_name, load_path)
93 if f is None:
94 # module is a package
95 raise ImportError
97 # Load backend code.
98 sys.path.insert(0, os.path.dirname(pathname))
99 try:
100 return imp.load_module(BACKEND_PREFIX + backend_name,
101 f, pathname, description)
102 finally:
103 f.close()