Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
formatter.AbstractFormatter Class Reference

List of all members.

Public Member Functions

def __init__
def end_paragraph
def add_line_break
def add_hor_rule
def add_label_data
def format_counter
def format_letter
def format_roman
def add_flowing_data
def add_literal_data
def flush_softspace
def push_alignment
def pop_alignment
def push_font
def pop_font
def push_margin
def pop_margin
def set_spacing
def push_style
def pop_style
def assert_line_data

Public Attributes

 writer
 align
 align_stack
 font_stack
 margin_stack
 spacing
 style_stack
 nospace
 softspace
 para_end
 parskip
 hard_break
 have_label

Detailed Description

The standard formatter.

This implementation has demonstrated wide applicability to many writers,
and may be used directly in most circumstances.  It has been used to
implement a full-featured World Wide Web browser.

Definition at line 61 of file formatter.py.


Constructor & Destructor Documentation

def formatter.AbstractFormatter.__init__ (   self,
  writer 
)

Definition at line 75 of file formatter.py.

00075 
00076     def __init__(self, writer):
00077         self.writer = writer            # Output device
00078         self.align = None               # Current alignment
00079         self.align_stack = []           # Alignment stack
00080         self.font_stack = []            # Font state
00081         self.margin_stack = []          # Margin state
00082         self.spacing = None             # Vertical spacing state
00083         self.style_stack = []           # Other state, e.g. color
00084         self.nospace = 1                # Should leading space be suppressed
00085         self.softspace = 0              # Should a space be inserted
00086         self.para_end = 1               # Just ended a paragraph
00087         self.parskip = 0                # Skipped space between paragraphs?
00088         self.hard_break = 1             # Have a hard break
00089         self.have_label = 0

Here is the caller graph for this function:


Member Function Documentation

Definition at line 177 of file formatter.py.

00177 
00178     def add_flowing_data(self, data):
00179         if not data: return
00180         prespace = data[:1].isspace()
00181         postspace = data[-1:].isspace()
00182         data = " ".join(data.split())
00183         if self.nospace and not data:
00184             return
00185         elif prespace or self.softspace:
00186             if not data:
00187                 if not self.nospace:
00188                     self.softspace = 1
00189                     self.parskip = 0
00190                 return
00191             if not self.nospace:
00192                 data = ' ' + data
00193         self.hard_break = self.nospace = self.para_end = \
00194                           self.parskip = self.have_label = 0
00195         self.softspace = postspace
00196         self.writer.send_flowing_data(data)

Here is the call graph for this function:

def formatter.AbstractFormatter.add_hor_rule (   self,
  args,
  kw 
)

Definition at line 108 of file formatter.py.

00108 
00109     def add_hor_rule(self, *args, **kw):
00110         if not self.hard_break:
00111             self.writer.send_line_break()
00112         self.writer.send_hor_rule(*args, **kw)
00113         self.hard_break = self.nospace = 1
00114         self.have_label = self.para_end = self.softspace = self.parskip = 0

def formatter.AbstractFormatter.add_label_data (   self,
  format,
  counter,
  blankline = None 
)

Definition at line 115 of file formatter.py.

00115 
00116     def add_label_data(self, format, counter, blankline = None):
00117         if self.have_label or not self.hard_break:
00118             self.writer.send_line_break()
00119         if not self.para_end:
00120             self.writer.send_paragraph((blankline and 1) or 0)
00121         if isinstance(format, str):
00122             self.writer.send_label_data(self.format_counter(format, counter))
00123         else:
00124             self.writer.send_label_data(format)
00125         self.nospace = self.have_label = self.hard_break = self.para_end = 1
00126         self.softspace = self.parskip = 0

Here is the call graph for this function:

Definition at line 101 of file formatter.py.

00101 
00102     def add_line_break(self):
00103         if not (self.hard_break or self.para_end):
00104             self.writer.send_line_break()
00105             self.have_label = self.parskip = 0
00106         self.hard_break = self.nospace = 1
00107         self.softspace = 0

Definition at line 197 of file formatter.py.

00197 
00198     def add_literal_data(self, data):
00199         if not data: return
00200         if self.softspace:
00201             self.writer.send_flowing_data(" ")
00202         self.hard_break = data[-1:] == '\n'
00203         self.nospace = self.para_end = self.softspace = \
00204                        self.parskip = self.have_label = 0
00205         self.writer.send_literal_data(data)

def formatter.AbstractFormatter.assert_line_data (   self,
  flag = 1 
)

Definition at line 290 of file formatter.py.

00290 
00291     def assert_line_data(self, flag=1):
00292         self.nospace = self.hard_break = not flag
00293         self.para_end = self.parskip = self.have_label = 0
00294 

def formatter.AbstractFormatter.end_paragraph (   self,
  blankline 
)

Definition at line 90 of file formatter.py.

00090 
00091     def end_paragraph(self, blankline):
00092         if not self.hard_break:
00093             self.writer.send_line_break()
00094             self.have_label = 0
00095         if self.parskip < blankline and not self.have_label:
00096             self.writer.send_paragraph(blankline - self.parskip)
00097             self.parskip = blankline
00098             self.have_label = 0
00099         self.hard_break = self.nospace = self.para_end = 1
00100         self.softspace = 0

Definition at line 206 of file formatter.py.

00206 
00207     def flush_softspace(self):
00208         if self.softspace:
00209             self.hard_break = self.para_end = self.parskip = \
00210                               self.have_label = self.softspace = 0
00211             self.nospace = 1
00212             self.writer.send_flowing_data(' ')

def formatter.AbstractFormatter.format_counter (   self,
  format,
  counter 
)

Definition at line 127 of file formatter.py.

00127 
00128     def format_counter(self, format, counter):
00129         label = ''
00130         for c in format:
00131             if c == '1':
00132                 label = label + ('%d' % counter)
00133             elif c in 'aA':
00134                 if counter > 0:
00135                     label = label + self.format_letter(c, counter)
00136             elif c in 'iI':
00137                 if counter > 0:
00138                     label = label + self.format_roman(c, counter)
00139             else:
00140                 label = label + c
00141         return label

Here is the call graph for this function:

Here is the caller graph for this function:

def formatter.AbstractFormatter.format_letter (   self,
  case,
  counter 
)

Definition at line 142 of file formatter.py.

00142 
00143     def format_letter(self, case, counter):
00144         label = ''
00145         while counter > 0:
00146             counter, x = divmod(counter-1, 26)
00147             # This makes a strong assumption that lowercase letters
00148             # and uppercase letters form two contiguous blocks, with
00149             # letters in order!
00150             s = chr(ord(case) + x)
00151             label = s + label
00152         return label

Here is the call graph for this function:

Here is the caller graph for this function:

def formatter.AbstractFormatter.format_roman (   self,
  case,
  counter 
)

Definition at line 153 of file formatter.py.

00153 
00154     def format_roman(self, case, counter):
00155         ones = ['i', 'x', 'c', 'm']
00156         fives = ['v', 'l', 'd']
00157         label, index = '', 0
00158         # This will die of IndexError when counter is too big
00159         while counter > 0:
00160             counter, x = divmod(counter, 10)
00161             if x == 9:
00162                 label = ones[index] + ones[index+1] + label
00163             elif x == 4:
00164                 label = ones[index] + fives[index] + label
00165             else:
00166                 if x >= 5:
00167                     s = fives[index]
00168                     x = x-5
00169                 else:
00170                     s = ''
00171                 s = s + ones[index]*x
00172                 label = s + label
00173             index = index + 1
00174         if case == 'I':
00175             return label.upper()
00176         return label

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 221 of file formatter.py.

00221 
00222     def pop_alignment(self):
00223         if self.align_stack:
00224             del self.align_stack[-1]
00225         if self.align_stack:
00226             self.align = align = self.align_stack[-1]
00227             self.writer.new_alignment(align)
00228         else:
00229             self.align = None
00230             self.writer.new_alignment(None)

Definition at line 247 of file formatter.py.

00247 
00248     def pop_font(self):
00249         if self.font_stack:
00250             del self.font_stack[-1]
00251         if self.font_stack:
00252             font = self.font_stack[-1]
00253         else:
00254             font = None
00255         self.writer.new_font(font)

Definition at line 263 of file formatter.py.

00263 
00264     def pop_margin(self):
00265         if self.margin_stack:
00266             del self.margin_stack[-1]
00267         fstack = [m for m in self.margin_stack if m]
00268         if fstack:
00269             margin = fstack[-1]
00270         else:
00271             margin = None
00272         self.writer.new_margin(margin, len(fstack))

def formatter.AbstractFormatter.pop_style (   self,
  n = 1 
)

Definition at line 286 of file formatter.py.

00286 
00287     def pop_style(self, n=1):
00288         del self.style_stack[-n:]
00289         self.writer.new_styles(tuple(self.style_stack))

def formatter.AbstractFormatter.push_alignment (   self,
  align 
)

Definition at line 213 of file formatter.py.

00213 
00214     def push_alignment(self, align):
00215         if align and align != self.align:
00216             self.writer.new_alignment(align)
00217             self.align = align
00218             self.align_stack.append(align)
00219         else:
00220             self.align_stack.append(self.align)

def formatter.AbstractFormatter.push_font (   self,
  font 
)

Definition at line 231 of file formatter.py.

00231 
00232     def push_font(self, font):
00233         size, i, b, tt = font
00234         if self.softspace:
00235             self.hard_break = self.para_end = self.softspace = 0
00236             self.nospace = 1
00237             self.writer.send_flowing_data(' ')
00238         if self.font_stack:
00239             csize, ci, cb, ctt = self.font_stack[-1]
00240             if size is AS_IS: size = csize
00241             if i is AS_IS: i = ci
00242             if b is AS_IS: b = cb
00243             if tt is AS_IS: tt = ctt
00244         font = (size, i, b, tt)
00245         self.font_stack.append(font)
00246         self.writer.new_font(font)

def formatter.AbstractFormatter.push_margin (   self,
  margin 
)

Definition at line 256 of file formatter.py.

00256 
00257     def push_margin(self, margin):
00258         self.margin_stack.append(margin)
00259         fstack = [m for m in self.margin_stack if m]
00260         if not margin and fstack:
00261             margin = fstack[-1]
00262         self.writer.new_margin(margin, len(fstack))

def formatter.AbstractFormatter.push_style (   self,
  styles 
)

Definition at line 277 of file formatter.py.

00277 
00278     def push_style(self, *styles):
00279         if self.softspace:
00280             self.hard_break = self.para_end = self.softspace = 0
00281             self.nospace = 1
00282             self.writer.send_flowing_data(' ')
00283         for style in styles:
00284             self.style_stack.append(style)
00285         self.writer.new_styles(tuple(self.style_stack))

def formatter.AbstractFormatter.set_spacing (   self,
  spacing 
)

Definition at line 273 of file formatter.py.

00273 
00274     def set_spacing(self, spacing):
00275         self.spacing = spacing
00276         self.writer.new_spacing(spacing)


Member Data Documentation

Definition at line 77 of file formatter.py.

Definition at line 78 of file formatter.py.

Definition at line 79 of file formatter.py.

Definition at line 87 of file formatter.py.

Definition at line 88 of file formatter.py.

Definition at line 80 of file formatter.py.

Definition at line 83 of file formatter.py.

Definition at line 85 of file formatter.py.

Definition at line 86 of file formatter.py.

Definition at line 84 of file formatter.py.

Definition at line 81 of file formatter.py.

Definition at line 82 of file formatter.py.

Definition at line 76 of file formatter.py.


The documentation for this class was generated from the following file: