Package prompt :: Module base_class
[hide private]
[frames] | no frames]

Source Code for Module prompt.base_class

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2009-2012 Edward d'Auvergne                                   # 
  4  #                                                                             # 
  5  # This file is part of the program relax.                                     # 
  6  #                                                                             # 
  7  # relax is free software; you can redistribute it and/or modify               # 
  8  # it under the terms of the GNU General Public License as published by        # 
  9  # the Free Software Foundation; either version 2 of the License, or           # 
 10  # (at your option) any later version.                                         # 
 11  #                                                                             # 
 12  # relax is distributed in the hope that it will be useful,                    # 
 13  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 14  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 15  # GNU General Public License for more details.                                # 
 16  #                                                                             # 
 17  # You should have received a copy of the GNU General Public License           # 
 18  # along with relax; if not, write to the Free Software                        # 
 19  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 20  #                                                                             # 
 21  ############################################################################### 
 22   
 23  # Module docstring. 
 24  """The base class for all the user function classes.""" 
 25   
 26  # Python module imports. 
 27  import platform 
 28  from re import split 
 29  import sys 
 30  from textwrap import wrap 
 31   
 32  # relax module imports. 
 33  import ansi 
 34  import help 
 35  from status import Status; status = Status() 
 36  from string import split, strip 
 37   
 38  # The width of the text. 
 39  if platform.uname()[0] in ['Windows', 'Microsoft']: 
 40      width = 80 
 41  else: 
 42      width = 100 
 43   
 44   
45 -def _build_doc(fn):
46 """Build the fn.__doc__ docstring. 47 48 @param fn: The user function to build the docstring for. 49 @type fn: method 50 """ 51 52 # Initialise. 53 fn.__doc__ = "" 54 55 # Add the title. 56 fn.__doc__ = "%s%s\n" % (fn.__doc__, fn._doc_title) 57 58 # Add the keyword args. 59 if hasattr(fn, '_doc_args'): 60 fn.__doc__ = fn.__doc__ + _build_subtitle("Keyword Arguments") 61 for arg, desc in fn._doc_args: 62 # The text. 63 text = "%s: %s" % (arg, desc) 64 65 # Format. 66 text = _format_text(text) 67 68 # Add to the docstring. 69 fn.__doc__ = "%s%s\n" % (fn.__doc__, text) 70 71 # Add the description. 72 if hasattr(fn, '_doc_desc'): 73 fn.__doc__ = fn.__doc__ + _build_subtitle("Description") 74 fn.__doc__ = fn.__doc__ + _format_text(fn._doc_desc) 75 76 # Add the examples. 77 if hasattr(fn, '_doc_examples'): 78 fn.__doc__ = fn.__doc__ + '\n' + _build_subtitle("Examples") 79 fn.__doc__ = fn.__doc__ + _format_text(fn._doc_examples) 80 81 # Add the additional sections. 82 if hasattr(fn, '_doc_additional'): 83 # Loop over each section. 84 for i in range(len(fn._doc_additional)): 85 fn.__doc__ = fn.__doc__ + '\n' + _build_subtitle(fn._doc_additional[i][0]) 86 fn.__doc__ = fn.__doc__ + _format_text(fn._doc_additional[i][1]) 87 88 # Convert the _doc_args list into a dictionary for easy argument description retrieval. 89 if hasattr(fn, '_doc_args'): 90 # Init. 91 fn._doc_args_dict = {} 92 93 # Loop over the args. 94 for arg, desc in fn._doc_args: 95 fn._doc_args_dict[arg] = desc
96 97 98
99 -def _build_subtitle(text):
100 """Create the formatted subtitle string. 101 102 @param text: The name of the subtitle. 103 @type text: str 104 @return: The formatted subtitle. 105 @rtype: str 106 """ 107 108 # Format and return. 109 return "\n%s\n%s\n\n" % (text, "~"*len(text))
110 111
112 -def _format_text(text):
113 """Format the text by stripping whitespace and wrapping. 114 115 @param text: The text to strip and wrap. 116 @type text: str 117 @return: The stripped and wrapped text. 118 @rtype: str 119 """ 120 121 # First strip whitespace. 122 stripped_text = _strip_lead(text) 123 124 # Remove the first characters if newlines. 125 while True: 126 if stripped_text[0] == "\n": 127 stripped_text = stripped_text[1:] 128 else: 129 break 130 131 # Remove the last character if a newline. 132 while True: 133 if stripped_text[-1] == "\n": 134 stripped_text = stripped_text[:-1] 135 else: 136 break 137 138 # Then split into lines. 139 lines = split(stripped_text, "\n") 140 141 # Then wrap each line. 142 new_text = "" 143 for line in lines: 144 # Empty line, so preserve. 145 if not len(line): 146 new_text = new_text + "\n" 147 148 # Wrap the line. 149 for wrapped_line in wrap(line, width): 150 new_text = new_text + wrapped_line + "\n" 151 152 # Return the formatted text. 153 return new_text
154 155
156 -def _strip_lead(text):
157 """Strip the leading whitespace from the given text. 158 159 @param text: The text to strip the leading whitespace from. 160 @type text: str 161 @return: The text with leading whitespace removed. 162 @rtype: str 163 """ 164 165 # Split by newline. 166 lines = split(text, '\n') 167 168 # Find the minimum whitespace. 169 min_white = 1000 170 for line in lines: 171 # Empty lines. 172 if strip(line) == '': 173 continue 174 175 # Count the whitespace for the current line. 176 num_white = 0 177 for i in range(len(line)): 178 if line[i] != ' ': 179 break 180 num_white = num_white + 1 181 182 # The min value. 183 min_white = min(min_white, num_white) 184 185 # Strip the whitespace. 186 new_text = '' 187 for line in lines: 188 new_text = new_text + line[min_white:] + '\n' 189 190 # Return the new text. 191 return new_text
192 193 194
195 -class Basic_class:
196 - def __init__(self, exec_info=None):
197 """All non-user function classes. 198 199 @keyword exec_info: The execution information container. This must contain at least the exec_info.intro boolean variable. If not supplied, an instance will be generated. 200 @type exec_info: None or class instance 201 """ 202 203 # Generate the execution info container. 204 if exec_info == None: 205 exec_info = Exec_info() 206 207 # Store the execution info container privately. 208 self._exec_info = exec_info
209 210 211
212 -class Exec_info:
213 """Container for execution information.""" 214
215 - def __init__(self):
216 """Initialise the data of this container. 217 218 This includes the introduction flag as well as the strings to change the Python prompts. 219 """ 220 221 # The user function intro flag. 222 self.intro = True 223 224 # The prompts (to change the Python prompt, as well as the function print outs). 225 self.ps1_orig = 'relax> ' 226 self.ps2_orig = 'relax| ' 227 self.ps3_orig = '\n%s' % self.ps1_orig 228 229 # Coloured text. 230 self.ps1_colour = "%s%s%s" % (ansi.relax_prompt, self.ps1_orig, ansi.end) 231 self.ps2_colour = "%s%s%s" % (ansi.relax_prompt, self.ps2_orig, ansi.end) 232 self.ps3_colour = "\n%s%s%s" % (ansi.relax_prompt, self.ps1_orig, ansi.end) 233 234 # Default to no colours. 235 self.prompt_colour_off()
236 237
238 - def prompt_colour_off(self):
239 """Turn the prompt colouring ANSI escape sequences off.""" 240 241 sys.ps1 = self.ps1 = self.ps1_orig 242 sys.ps2 = self.ps2 = self.ps2_orig 243 sys.ps3 = self.ps3 = self.ps3_orig
244 245
246 - def prompt_colour_on(self):
247 """Turn the prompt colouring ANSI escape sequences off.""" 248 249 sys.ps1 = self.ps1 = self.ps1_colour 250 sys.ps2 = self.ps2 = self.ps2_colour 251 sys.ps3 = self.ps3 = self.ps3_colour
252 253 254
255 -class User_fn_class:
256 - def __init__(self, exec_info=None):
257 """Initialise the user function class, compiling the help string. 258 259 @keyword exec_info: The execution information container. This must contain at least the exec_info.intro boolean variable. If not supplied, an instance will be generated. 260 @type exec_info: None or class instance 261 """ 262 263 # Generate the execution info container. 264 if exec_info == None: 265 exec_info = Exec_info() 266 267 # Store the execution info container privately 268 self._exec_info = exec_info 269 270 # Add the generic help string. 271 self.__relax_help__ = self.__doc__ + "\n" + help.relax_class_help 272 273 # Add a description to the help string. 274 if hasattr(self, '__description__'): 275 self.__relax_help__ = self.__relax_help__ + "\n\n" + _strip_lead(self.__description__)
276