/usr/share/pyshared/tvtk/indenter.py is in mayavi2 4.1.0-1.
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 | """This module defines an indenter class that handles indentation
levels for automatic code generation. It also defines other
miscellaneous classes useful for the tvtk code generation.
"""
# Author: Prabhu Ramachandran
# Copyright (c) 2004, Enthought, Inc.
# License: BSD Style.
import re
# Local imports (there is a good reason for the relative imports).
from common import get_tvtk_name, camel2enthought
######################################################################
# `Indent` class.
######################################################################
class Indent:
"""This class manages indentation levels for dynamically generated
Python code. The class also provides a method that formats a text
string suitably at a given indentation level.
"""
def __init__(self, nspace=4):
"""Initializes the object.
Parameters
----------
- nspace : `int`
Specifies the number of spaces to use for each indentation
level. Defaults to 4.
"""
self.tab = ''
self.txt = ''
self.nspace = 0
self.set_tab(nspace)
self.space_re = re.compile(r'^\s*$')
self.find_space_re = re.compile(r'\s*(\S)')
def __repr__(self):
return self.txt
def set_tab(self, nspace):
"""Set the number of spaces a tab represents."""
self.nspace = nspace
self.tab = ' '*nspace
def reset(self):
"""Reset the indentation level to 0."""
self.txt = ''
def incr(self):
"""Increase the indentation level."""
self.txt += self.tab
def decr(self):
"""Decrease the indentation level."""
self.txt = self.txt[:-self.nspace]
def format(self, txt):
"""Formats given text as per current indentation levels.
Note that the returned string always ends with a newline to
avoid problems for subsequent lines in the output that have to
deal trailing garbage in the last line.
Parameters
----------
- txt : `string`
Input text string to be formatted. Can contain newlines.
If the input text is a single line of text then leading
space is stripped and the current indentation is added to
the left along with a newline and the resulting string is
returned.
If the input text is multi-line input the indentation of the
first line is ignored, and the second line is considered.
All subsequent lines have the current indentation followed
by any extra space from the default indentation.
"""
space_re = self.space_re
find_space_re = self.find_space_re
d = txt.split('\n')
one_liner = 1
if len(d) > 1:
for i in d[1:]:
if not space_re.match(i):
one_liner = 0
break
elif len(d) == 0:
return '\n'
if one_liner:
return '%s%s\n'%(repr(self), d[0].strip())
else:
strip_idx = 0
m = find_space_re.match(d[1])
try:
strip_idx = m.start(1)
except AttributeError:
strip_idx = 0
ret = []
if not space_re.match(d[0]):
ret.append('%s%s'%(repr(self), d[0]))
for i in d[1:]:
if i:
ret.append('%s%s'%(repr(self), i[strip_idx:]))
else:
ret.append(repr(self))
if space_re.match(ret[-1]):
ret[-1] = ''
else:
ret.append('')
return '\n'.join(ret)
######################################################################
# `VTKDocMassager` class.
######################################################################
class VTKDocMassager:
"""This class massages the documentation strings suitably for
inclusion in the TVTK code. The names of all the VTK classes are
changed suitably and when possible the method names are also
changed.
This class is *not* generic and is *very* specific to VTK
documentation strings.
"""
def __init__(self):
self.renamer = re.compile(r'(vtk[A-Z0-9]\S+)')
self.ren_func = lambda m: get_tvtk_name(m.group(1))
self.func_re = re.compile(r'([a-z0-9]+[A-Z])')
self.cpp_method_re = re.compile(r'C\+\+: .*?;\n*')
#################################################################
# `VTKDocMassager` interface.
#################################################################
def write_class_doc(self, doc, out, indent):
"""Write processed class documentation string into `out`.
Parameters
----------
- doc : `string`
The documentation string.
- out : file line object.
- indent : `Indent`
"""
ret = self.massage(doc)
indent.incr()
out.write(indent.format('"""'))
out.write(indent.format('\n' + ret))
out.write(indent.format('"""'))
indent.decr()
def write_trait_doc(self, doc, out, indent):
"""Write processed trait documentation string into `out`.
This method removes the call signature information from the
method.
Parameters
----------
- doc : `string`
The documentation string.
- out : file line object.
- indent : `Indent`
"""
ret = self._remove_sig(doc)
indent.incr()
out.write(indent.format('"""'))
out.write(indent.format('\n'+self.massage(ret)))
out.write(indent.format('"""'))
indent.decr()
def write_method_doc(self, doc, out, indent):
"""Write processed method documentation string into `out`.
The method signature is appopriately massaged.
Parameters
----------
- doc : `string`
The documentation string.
- out : file line object.
- indent : `Indent`
"""
orig_name = doc[2:doc.find('(')]
name = camel2enthought(orig_name)
my_sig = self._rename_class(doc[:doc.find('\n\n')])
my_sig = self.cpp_method_re.sub('', my_sig)
my_sig = my_sig.replace('V.'+orig_name, 'V.'+name)
indent.incr()
out.write(indent.format('"""'))
out.write(indent.format(my_sig))
ret = self._remove_sig(doc)
if ret:
out.write('\n')
out.write(indent.format('\n'+self.massage(ret)))
out.write(indent.format('"""'))
indent.decr()
def get_method_doc(self, doc):
"""Return processed method documentation string from `doc`.
The method signature is appopriately massaged.
Parameters
----------
- doc : `string`
The documentation string.
"""
orig_name = doc[2:doc.find('(')]
name = camel2enthought(orig_name)
my_sig = self._rename_class(doc[:doc.find('\n\n')])
my_sig = self.cpp_method_re.sub('', my_sig)
my_sig = my_sig.replace('V.'+orig_name, 'V.'+name)
ret = self.massage(self._remove_sig(doc))
if ret:
return my_sig + '\n' + ret
else:
return my_sig
def massage(self, doc):
"""Returns massaged documentation string from passed
docstring, `doc`. This method basically renames the methods
and classes in the docstring.
"""
ret = self._rename_methods(doc)
ret = self._rename_class(ret)
return ret
#################################################################
# Non-public interface.
#################################################################
def _rename_class(self, doc):
return self.renamer.sub(self.ren_func, doc)
def _remove_sig(self, doc):
idx = doc.find('\n\n') + 2
if len(doc) > idx:
return doc[idx:]
else:
return ''
def _rename_methods(self, doc):
lines = doc.split('\n')
nl = []
for line in lines:
words = line.split(' ')
nw = []
for word in words:
if word[:3] == 'vtk':
nw.append(word)
else:
if self.func_re.search(word):
nw.append(camel2enthought(word))
else:
nw.append(word)
nl.append(' '.join(nw))
return '\n'.join(nl)
|