Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
AbstractFormatter Class Reference

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
 

Data Fields

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

Detailed Description

Definition at line 54 of file formatter.py.

Constructor & Destructor Documentation

def __init__ (   self,
  writer 
)

Definition at line 61 of file formatter.py.

61 
62  def __init__(self, writer):
63  self.writer = writer # Output device
64  self.align = None # Current alignment
65  self.align_stack = [] # Alignment stack
66  self.font_stack = [] # Font state
67  self.margin_stack = [] # Margin state
68  self.spacing = None # Vertical spacing state
69  self.style_stack = [] # Other state, e.g. color
70  self.nospace = 1 # Should leading space be suppressed
71  self.softspace = 0 # Should a space be inserted
72  self.para_end = 1 # Just ended a paragraph
73  self.parskip = 0 # Skipped space between paragraphs?
74  self.hard_break = 1 # Have a hard break
75  self.have_label = 0

Member Function Documentation

def add_flowing_data (   self,
  data,
  whitespace = string.whitespace,
  join = string.join,
  split = string.split 
)

Definition at line 166 of file formatter.py.

References AbstractFormatter.hard_break, AbstractFormatter.have_label, dospath.join(), AbstractFormatter.nospace, AbstractFormatter.para_end, AbstractFormatter.parskip, AbstractFormatter.softspace, and dospath.split().

167  join = string.join, split = string.split):
168  if not data: return
169  # The following looks a bit convoluted but is a great improvement over
170  # data = regsub.gsub('[' + string.whitespace + ']+', ' ', data)
171  prespace = data[:1] in whitespace
172  postspace = data[-1:] in whitespace
173  data = join(split(data))
174  if self.nospace and not data:
175  return
176  elif prespace or self.softspace:
177  if not data:
178  if not self.nospace:
179  self.softspace = 1
180  self.parskip = 0
181  return
182  if not self.nospace:
183  data = ' ' + data
184  self.hard_break = self.nospace = self.para_end = \
185  self.parskip = self.have_label = 0
186  self.softspace = postspace
187  self.writer.send_flowing_data(data)
def add_hor_rule (   self,
  args,
  kw 
)

Definition at line 94 of file formatter.py.

References AbstractFormatter.hard_break, AbstractFormatter.have_label, AbstractFormatter.nospace, AbstractFormatter.para_end, AbstractFormatter.parskip, and AbstractFormatter.softspace.

94 
95  def add_hor_rule(self, *args, **kw):
96  if not self.hard_break:
97  self.writer.send_line_break()
98  apply(self.writer.send_hor_rule, args, kw)
99  self.hard_break = self.nospace = 1
100  self.have_label = self.para_end = self.softspace = self.parskip = 0
def add_label_data (   self,
  format,
  counter,
  blankline = None 
)

Definition at line 101 of file formatter.py.

References AbstractFormatter.format_counter(), AbstractFormatter.hard_break, AbstractFormatter.have_label, AbstractFormatter.nospace, AbstractFormatter.para_end, AbstractFormatter.parskip, and AbstractFormatter.softspace.

102  def add_label_data(self, format, counter, blankline = None):
103  if self.have_label or not self.hard_break:
104  self.writer.send_line_break()
105  if not self.para_end:
106  self.writer.send_paragraph((blankline and 1) or 0)
107  if type(format) is StringType:
108  self.writer.send_label_data(self.format_counter(format, counter))
109  else:
110  self.writer.send_label_data(format)
111  self.nospace = self.have_label = self.hard_break = self.para_end = 1
112  self.softspace = self.parskip = 0
def add_line_break (   self)

Definition at line 87 of file formatter.py.

References AbstractFormatter.hard_break, AbstractFormatter.have_label, AbstractFormatter.nospace, AbstractFormatter.para_end, AbstractFormatter.parskip, and AbstractFormatter.softspace.

87 
88  def add_line_break(self):
89  if not (self.hard_break or self.para_end):
90  self.writer.send_line_break()
91  self.have_label = self.parskip = 0
92  self.hard_break = self.nospace = 1
93  self.softspace = 0
def add_literal_data (   self,
  data 
)

Definition at line 188 of file formatter.py.

References AbstractFormatter.hard_break, AbstractFormatter.have_label, AbstractFormatter.nospace, AbstractFormatter.para_end, AbstractFormatter.parskip, and AbstractFormatter.softspace.

189  def add_literal_data(self, data):
190  if not data: return
191  if self.softspace:
192  self.writer.send_flowing_data(" ")
193  self.hard_break = data[-1:] == '\n'
194  self.nospace = self.para_end = self.softspace = \
195  self.parskip = self.have_label = 0
196  self.writer.send_literal_data(data)
def assert_line_data (   self,
  flag = 1 
)

Definition at line 280 of file formatter.py.

References AbstractFormatter.hard_break, AbstractFormatter.have_label, AbstractFormatter.nospace, AbstractFormatter.para_end, and AbstractFormatter.parskip.

281  def assert_line_data(self, flag=1):
282  self.nospace = self.hard_break = not flag
283  self.para_end = self.parskip = self.have_label = 0
284 
def end_paragraph (   self,
  blankline 
)

Definition at line 76 of file formatter.py.

References AbstractFormatter.hard_break, AbstractFormatter.have_label, AbstractFormatter.nospace, AbstractFormatter.para_end, AbstractFormatter.parskip, and AbstractFormatter.softspace.

76 
77  def end_paragraph(self, blankline):
78  if not self.hard_break:
79  self.writer.send_line_break()
80  self.have_label = 0
81  if self.parskip < blankline and not self.have_label:
82  self.writer.send_paragraph(blankline - self.parskip)
83  self.parskip = blankline
84  self.have_label = 0
85  self.hard_break = self.nospace = self.para_end = 1
86  self.softspace = 0
def flush_softspace (   self)

Definition at line 197 of file formatter.py.

References AbstractFormatter.hard_break, AbstractFormatter.have_label, AbstractFormatter.nospace, AbstractFormatter.para_end, AbstractFormatter.parskip, and AbstractFormatter.softspace.

198  def flush_softspace(self):
199  if self.softspace:
200  self.hard_break = self.para_end = self.parskip = \
201  self.have_label = self.softspace = 0
202  self.nospace = 1
203  self.writer.send_flowing_data(' ')
def format_counter (   self,
  format,
  counter 
)

Definition at line 113 of file formatter.py.

References AbstractFormatter.format_letter(), and AbstractFormatter.format_roman().

114  def format_counter(self, format, counter):
115  label = ''
116  for c in format:
117  if c == '1':
118  label = label + ('%d' % counter)
119  elif c in 'aA':
120  if counter > 0:
121  label = label + self.format_letter(c, counter)
122  elif c in 'iI':
123  if counter > 0:
124  label = label + self.format_roman(c, counter)
125  else:
126  label = label + c
127  return label
def format_letter (   self,
  case,
  counter 
)

Definition at line 128 of file formatter.py.

129  def format_letter(self, case, counter):
130  label = ''
131  while counter > 0:
132  counter, x = divmod(counter-1, 26)
133  # This makes a strong assumption that lowercase letters
134  # and uppercase letters form two contiguous blocks, with
135  # letters in order!
136  s = chr(ord(case) + x)
137  label = s + label
138  return label
def format_roman (   self,
  case,
  counter 
)

Definition at line 139 of file formatter.py.

References AbstractFormatter.add_flowing_data().

140  def format_roman(self, case, counter):
141  ones = ['i', 'x', 'c', 'm']
142  fives = ['v', 'l', 'd']
143  label, index = '', 0
144  # This will die of IndexError when counter is too big
145  while counter > 0:
146  counter, x = divmod(counter, 10)
147  if x == 9:
148  label = ones[index] + ones[index+1] + label
149  elif x == 4:
150  label = ones[index] + fives[index] + label
151  else:
152  if x >= 5:
153  s = fives[index]
154  x = x-5
155  else:
156  s = ''
157  s = s + ones[index]*x
158  label = s + label
159  index = index + 1
160  if case == 'I':
161  return label.upper()
162  return label
def pop_alignment (   self)

Definition at line 212 of file formatter.py.

References Chunk.align, AbstractFormatter.align, and AbstractFormatter.align_stack.

213  def pop_alignment(self):
214  if self.align_stack:
215  del self.align_stack[-1]
216  if self.align_stack:
217  self.align = align = self.align_stack[-1]
218  self.writer.new_alignment(align)
219  else:
220  self.align = None
221  self.writer.new_alignment(None)
def pop_font (   self)

Definition at line 237 of file formatter.py.

References AbstractFormatter.font_stack.

238  def pop_font(self):
239  if self.font_stack:
240  del self.font_stack[-1]
241  if self.font_stack:
242  font = self.font_stack[-1]
243  else:
244  font = None
245  self.writer.new_font(font)
def pop_margin (   self)

Definition at line 253 of file formatter.py.

References fnmatch.filter(), and AbstractFormatter.margin_stack.

254  def pop_margin(self):
255  if self.margin_stack:
256  del self.margin_stack[-1]
257  fstack = filter(None, self.margin_stack)
258  if fstack:
259  margin = fstack[-1]
260  else:
261  margin = None
262  self.writer.new_margin(margin, len(fstack))
def pop_style (   self,
  n = 1 
)

Definition at line 276 of file formatter.py.

References AbstractFormatter.style_stack, and log_faction_ships.tuple.

277  def pop_style(self, n=1):
278  del self.style_stack[-n:]
279  self.writer.new_styles(tuple(self.style_stack))
def push_alignment (   self,
  align 
)

Definition at line 204 of file formatter.py.

References Chunk.align, and AbstractFormatter.align.

205  def push_alignment(self, align):
206  if align and align != self.align:
207  self.writer.new_alignment(align)
208  self.align = align
209  self.align_stack.append(align)
210  else:
211  self.align_stack.append(self.align)
def push_font (   self,
  size,
  i,
  b,
  tt 
)

Definition at line 222 of file formatter.py.

References AbstractFormatter.font_stack, AbstractFormatter.hard_break, AbstractFormatter.nospace, AbstractFormatter.para_end, and AbstractFormatter.softspace.

223  def push_font(self, (size, i, b, tt)):
224  if self.softspace:
225  self.hard_break = self.para_end = self.softspace = 0
226  self.nospace = 1
227  self.writer.send_flowing_data(' ')
228  if self.font_stack:
229  csize, ci, cb, ctt = self.font_stack[-1]
230  if size is AS_IS: size = csize
231  if i is AS_IS: i = ci
232  if b is AS_IS: b = cb
233  if tt is AS_IS: tt = ctt
234  font = (size, i, b, tt)
235  self.font_stack.append(font)
236  self.writer.new_font(font)
def push_margin (   self,
  margin 
)

Definition at line 246 of file formatter.py.

References fnmatch.filter(), and AbstractFormatter.margin_stack.

247  def push_margin(self, margin):
248  self.margin_stack.append(margin)
249  fstack = filter(None, self.margin_stack)
250  if not margin and fstack:
251  margin = fstack[-1]
252  self.writer.new_margin(margin, len(fstack))
def push_style (   self,
  styles 
)

Definition at line 267 of file formatter.py.

References AbstractFormatter.hard_break, AbstractFormatter.nospace, AbstractFormatter.para_end, AbstractFormatter.softspace, AbstractFormatter.style_stack, and log_faction_ships.tuple.

268  def push_style(self, *styles):
269  if self.softspace:
270  self.hard_break = self.para_end = self.softspace = 0
271  self.nospace = 1
272  self.writer.send_flowing_data(' ')
273  for style in styles:
274  self.style_stack.append(style)
275  self.writer.new_styles(tuple(self.style_stack))
def set_spacing (   self,
  spacing 
)

Definition at line 263 of file formatter.py.

References AbstractFormatter.spacing.

264  def set_spacing(self, spacing):
265  self.spacing = spacing
266  self.writer.new_spacing(spacing)

Field Documentation

align

Definition at line 63 of file formatter.py.

align_stack

Definition at line 64 of file formatter.py.

font_stack

Definition at line 65 of file formatter.py.

hard_break

Definition at line 73 of file formatter.py.

have_label

Definition at line 74 of file formatter.py.

margin_stack

Definition at line 66 of file formatter.py.

nospace

Definition at line 69 of file formatter.py.

para_end

Definition at line 71 of file formatter.py.

parskip

Definition at line 72 of file formatter.py.

softspace

Definition at line 70 of file formatter.py.

spacing

Definition at line 67 of file formatter.py.

style_stack

Definition at line 68 of file formatter.py.

writer

Definition at line 62 of file formatter.py.


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