Xorn
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
backend.py
Go to the documentation of this file.
1 # xorn.geda.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-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.netlist.backend
21 ## Handling netlist backends.
22 #
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`.
28 #
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.
32 
33 import imp, os, sys
34 from gettext import gettext as _
35 
36 ## Module name prefix for netlister backends.
37 #
38 # Only Python modules whose name starts with this prefix are
39 # considered netlister backends.
40 
41 BACKEND_PREFIX = 'gnet_'
42 
43 ## Backend load path.
44 #
45 # A list of directory names in which to search for netlister backends.
46 
47 load_path = []
48 
49 ## Get a sorted list of available backends.
50 #
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_").
55 
57  backend_names = set()
58 
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
66 
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
71 
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
77 
78  return sorted(backend_names)
79 
80 ## Load a specific netlister backend.
81 #
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.
84 #
85 # The backend module's path is added to the system load path.
86 #
87 # \throws ImportError if the module could not be loaded
88 
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
96 
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()
def list_backends
Get a sorted list of available backends.
Definition: backend.py:56
def load
Load a specific netlister backend.
Definition: backend.py:89