/usr/share/sgml/misc/sgmltools/python/SGMLtools.py is in sgmltools-lite 3.0.3.0.cvs.20010909-15.1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | #
# SGMLtools.py - SGMLtools main routine.
#
# $Id: SGMLtools.py,v 1.4 2000/10/25 06:00:05 cdegroot Exp $
#
# SGMLtools - an SGML toolkit.
# Copyright (C)1998 Cees A. de Groot
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
This module contains the main logic for SGMLtools. It is wrapped
in a class so that once somebody cooks up a use for it, you can
actually have multiple copies active (although we'd need to factor
initialization into repeatable and non-repeatable parts, then).
"""
import sys, os, glob, imp, getopt
import Backend, utils
class SGMLtools:
_globals = {}
_classes = {}
_autoconf = {}
def __init__(self, autoconf):
"""Create an SGMLtools object.
This method hunts for backend modules and does some other
assorted initialization things. The autoconf argument
contains some assorted settings that are passed down from
autoconf.
"""
self._autoconf = autoconf
#
# Expand path
#
sys.path.append(os.path.join(autoconf['shrdir'], 'python'))
sys.path = sys.path + autoconf['backends']
#
# Import backends, instantiate a BackendGlobals object for
# each of them, and stash it away.
#
files = []
for dir in autoconf['backends']:
pattern = os.path.join(dir, '*.py')
files = files + glob.glob(pattern)
for file in files:
name, junk = os.path.splitext(file)
dir, module = os.path.split(name)
cmd = 'from %s import %s, %s' % (module, module, module + 'Globals')
exec cmd
cmd = 'glob = %sGlobals()' % module
exec cmd
self._globals[glob.getName()] = glob
cmd = 'cls = %s' % module
exec cmd
self._classes[glob.getName()] = cls
#
# Read alias file
#
self._aliases = utils.readAliases(autoconf)
#
# Setup SGML environment
#
if not os.environ.has_key('SGML_CATALOG_FILES'):
os.environ['SGML_CATALOG_FILES'] = \
os.path.join(autoconf['etcdir'], 'catalog') \
+ ":" + "/usr/share/sgml/stylesheets/sgmltools/sgmltools.cat" \
+ ":" + "/usr/share/sgml/stylesheet/dsssl/sgmltools/sgmltools.cat" \
+ ":" + "/usr/share/sgml/sgmltools-lite/stylesheet/dsssl/sgmltools.cat" \
+ ":" + "/usr/share/sgml/CATALOG.docbkdsl" \
+ ":" + "/etc/sgml/catalog"
def processOptions(self, args):
"""Process command line options.
Process command line options, dynamically expanding them
based on the --backend option, and returning the list of
files that's left.
"""
#
# Hunt down the backend option. The first test tests for
# "-b x", the second for "-bx" (or the equivalend long versions).
#
numArgs = len(args)
for i in range(numArgs):
arg = args[i]
if arg in ["-b", "--backend"]:
if i+1 >= numArgs:
raise getopt.error, "option %s requires an argument" % arg
miniargs = [arg, args[i+1]]
break
if arg[:2] == "-b" or arg[:10] == "--backend=":
miniargs = [arg]
break
else:
#
# Default to the HTML backend.
#
miniargs = [ "--backend=onehtml" ];
#
# We should have a backend option now. Ask getopt to parse it. Once
# we have it, ask the backend for extra options so we can get
# down to business.
#
opt, junk = getopt.getopt(miniargs, 'b:', ['backend='])
#
# if opt = 'txt', check for 'w3m' else fallback to 'lynx'
#
if opt[0][1] == "txt":
if not self._autoconf['progs']['w3m'] == 'N/A':
self._curbackend = "w3m"
else:
self._curbackend = "lynx"
else:
self._curbackend = opt[0][1]
try:
self._curglobal = self._globals[self._curbackend]
except KeyError:
utils.usage(None, "Unknown backend " + self._curbackend)
if not self._globals.has_key(self._curbackend):
utils.usage(None, "Unknown backend " + self._curbackend)
#
# Merge all the options and parse them. Return whatever is
# left (the list of files we need to run).
#
shortopts, longopts = utils.makeOpts(self._curglobal)
try:
options, retval = getopt.getopt(args, shortopts, longopts)
except getopt.error, e:
utils.usage(self._curglobal, 'Error parsing arguments: ' + `e`)
self._options = utils.normalizeOpts(self._curglobal, options)
#
# Check for help/version/... options
#
if utils.findOption(self._options, 'help'):
utils.version(self._autoconf['shrdir'])
print
utils.usage(self._curglobal, None)
if utils.findOption(self._options, 'version'):
utils.version(self._autoconf['shrdir'])
sys.exit(0)
if utils.findOption(self._options, 'license'):
utils.license()
return retval
def processFile(self, file):
"""Process the indicated file"""
#
# Some filename munching so the user can invoke us with our
# without the .sgml/.SGML extension.
#
filepath, filename = os.path.split(file)
filename, fileext = os.path.splitext(filename)
if filepath == '':
filepath = '.'
if os.path.isfile(file):
self._fileinfo = (filename, filepath, fileext)
elif os.path.isfile(os.path.join(filepath, filename + '.sgml')):
self._fileinfo = (filename, filepath, '.sgml')
elif os.path.isfile(os.path.join(filepath, filename + '.SGML')):
self._fileinfo = (filename, filepath, '.SGML')
elif os.path.isfile(os.path.join(filepath, filename)):
self._fileinfo = (filename, filepath, '')
else:
raise IOError, "file %s not found" % file
self._filename = os.path.join(self._fileinfo[1],
self._fileinfo[0] + self._fileinfo[2])
#
# Create a backend instance.
#
if utils.findOption(self._options, 'verbose') != None:
dotrace = 1
else:
dotrace = 0
self._tracer = utils.Tracer(dotrace)
be = self._classes[self._curbackend](self._filename, self._fileinfo,
self._curglobal, self._tracer, self._autoconf)
#
# Make SGML_SEARCH_PATH absolute.
#
savdir = os.getcwd()
os.chdir(filepath)
envname = 'SGML_SEARCH_PATH'
if os.environ.has_key(envname):
os.environ[envname] = os.environ[envname] + ':' + os.getcwd()
os.environ[envname] = os.environ[envname] + ':' + "/usr/share/sgml"
else:
os.environ[envname] = os.getcwd()
os.environ[envname] = os.environ[envname] + ':' + "/usr/share/sgml"
os.chdir(savdir)
#
# Get the Jade parameters and see whether the stylesheet was
# overriden. Translate the stylesheet to an absolute filename
#
stylesheet, jadebe = self._curglobal.getJadeSettings()
userSheet = utils.findOption(self._options, 'dsssl-spec')
if userSheet != None:
stylesheet = userSheet
dssslfile = utils.findStylesheet(stylesheet, self._aliases)
addJadeOpt = ''
userJadeOpt = utils.findOption(self._options, 'jade-opt')
if userJadeOpt != None:
addJadeOpt = ' ' + userJadeOpt
#
# the "nochunks hack"
# "-b html -j 'nochunks'" = "-b onehtml"
if self._curbackend == 'html':
optlist_nochunks_hack, args_nochunks_hack = getopt.getopt(addJadeOpt.split(), 'V:')
for curopt_nochunks_hack in optlist_nochunks_hack:
if (curopt_nochunks_hack[0] == '-V') and (curopt_nochunks_hack[1] == 'nochunks'):
print """The html backend tries to convert a sgml file
into one-file-for-one-section html format, but the
-j '-V nochunks' does not allow it. Assuming -b onehtml instead.
If you really want to use the html backend with -j '-V nochunks', try:
sgmltools -b html -j '-V "nochunks"'
"""
self._curbackend = 'onehtml'
be = self._classes[self._curbackend](self._filename, self._fileinfo, self._curglobal, self._tracer, self._autoconf)
#
# Open the input file and give the pre-Jade routine a shot.
#
infile = open(self._filename, 'r')
nextfile = be.preJade(infile)
jadeinputfile = utils.makeTemp()
# dirty hack to avoid the problem with openjade and pipe
# OK, Me, a bustard, don't know python at all.
# If you can provide me a correct patch, I'll appreciate.
catcmd = '/bin/cat >' + jadeinputfile
catpipe = os.popen(catcmd, 'w')
catpipe.writelines(nextfile.readlines())
catpipe.close()
#
# Run Jade attached to a pipe
#
jadecmd = self._autoconf['progs']['jade']
jadecmd = jadecmd + ' -t ' + jadebe
jadecmd = jadecmd + ' -d ' + dssslfile
jadeoutfile = utils.makeTemp()
jadecmd = jadecmd + ' -o ' + jadeoutfile
jadecmd = jadecmd + addJadeOpt
jadecmd = jadecmd + ' ' + jadeinputfile + ' '
jadestdoutfile = utils.makeTemp()
jadecmd = jadecmd + ' >' + jadestdoutfile
self._tracer.trace(jadecmd)
#jadepipe = os.popen(jadecmd, 'w')
self._tracer.system(jadecmd)
#
# Pump nextfile->jadepipe, and close all files.
#
#jadepipe.writelines(nextfile.readlines())
try:
jadepipe.close();
infile.close();
nextfile.close();
except:
pass
#
# Run the postJade stage.
#
be.postJade(jadeoutfile, jadestdoutfile)
|