/usr/share/bluefish/lorem-ipsum-generator is in bluefish-data 2.2.7-2.
This file is owned by root:root, with mode 0o755.
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 | #!/usr/bin/env python
import sys
from os.path import abspath, exists
from optparse import OptionParser
from lipsum import MarkupGenerator, InvalidSampleError, InvalidDictionaryError
class LipsumGUI:
def __init__(self, options = None, generator=MarkupGenerator()):
import pygtk
pygtk.require("2.0")
import gtk
import gtk.glade
import pkg_resources
self.__generator = generator
filename = pkg_resources.resource_filename('lipsum', 'data/lipsum.xml')
self.__builder = gtk.Builder()
self.__builder.add_from_file(filename)
self.__main = self.__builder.get_object("main")
self.__spinbutton_sentence_mean = self.__builder.get_object("spinbutton_sentence_mean")
self.__spinbutton_sentence_mean.connect('changed', self.__set_sentence_mean)
self.__spinbutton_sentence_sigma = self.__builder.get_object("spinbutton_sentence_sigma")
self.__spinbutton_sentence_sigma.connect('changed', self.__set_sentence_sigma)
self.__spinbutton_paragraph_mean = self.__builder.get_object("spinbutton_paragraph_mean")
self.__spinbutton_paragraph_mean.connect('changed', self.__set_paragraph_mean)
self.__spinbutton_paragraph_sigma = self.__builder.get_object("spinbutton_paragraph_sigma")
self.__spinbutton_paragraph_sigma.connect('changed', self.__set_paragraph_sigma)
self.__button_reset = self.__builder.get_object("button_reset")
self.__button_reset.connect("clicked", self.__reset_statistics)
self.__button_generate = self.__builder.get_object("button_generate")
self.__button_generate.connect("clicked", self.__generate_output)
self.__button_copy = self.__builder.get_object("button_copy")
self.__button_copy.connect("clicked", self.__copy_output)
self.__button_copy_exit = self.__builder.get_object("button_copy_exit")
self.__button_copy_exit.connect("clicked", self.__copy_exit_output)
self.__spinbutton_quantity = self.__builder.get_object("spinbutton_quantity")
self.__radiobutton_quantity_paragraphs = self.__builder.get_object("radiobutton_quantity_paragraphs")
self.__radiobutton_quantity_sentences = self.__builder.get_object("radiobutton_quantity_sentences")
self.__radiobutton_format_plain = self.__builder.get_object("radiobutton_format_plain")
self.__radiobutton_format_html_paragraphs = self.__builder.get_object("radiobutton_format_html_paragraphs")
self.__radiobutton_format_html_list = self.__builder.get_object("radiobutton_format_html_list")
self.__checkbutton_start_with_lorem = self.__builder.get_object("checkbutton_start_with_lorem")
self.__filechooserbutton_sample = self.__builder.get_object("filechooserbutton_sample")
self.__filechooserbutton_sample.connect('selection-changed', self.__set_sample_file)
self.__filechooserbutton_dictionary = self.__builder.get_object("filechooserbutton_dictionary")
self.__filechooserbutton_dictionary.connect('selection-changed', self.__set_dictionary_file)
self.__textbuffer_output = gtk.TextBuffer()
self.__textview_output = self.__builder.get_object("textview_output")
self.__textview_output.set_buffer(self.__textbuffer_output)
self.__clipboard = gtk.Clipboard()
self.__update_statistic_fields()
self.__set_defaults(options)
self.__main.connect('destroy', gtk.main_quit)
self.__main.show()
gtk.main()
def __generate_output(self, w):
output = ''
quantity = self.__spinbutton_quantity.get_value()
start_with_lorem = self.__checkbutton_start_with_lorem.get_active()
if self.__radiobutton_quantity_paragraphs.get_active():
generate_plain = self.__generator.generate_paragraphs_plain
generate_html_p = self.__generator.generate_paragraphs_html_p
generate_html_li = self.__generator.generate_paragraphs_html_li
else:
generate_plain = self.__generator.generate_sentences_plain
generate_html_p = self.__generator.generate_sentences_html_p
generate_html_li = self.__generator.generate_sentences_html_li
if self.__radiobutton_format_html_paragraphs.get_active():
output = generate_html_p(quantity, start_with_lorem)
elif self.__radiobutton_format_html_list.get_active():
output = generate_html_li(quantity, start_with_lorem)
else:
output = generate_plain(quantity, start_with_lorem)
self.__textbuffer_output.set_text(output)
def __copy_output(self, w):
self.__clipboard.set_text(
self.__textbuffer_output.get_text(
self.__textbuffer_output.get_start_iter(),
self.__textbuffer_output.get_end_iter()
)
)
def __copy_exit_output(self, w):
self.__copy_output(w)
self.__main.destroy()
def __set_sample_file(self, w):
filename = self.__filechooserbutton_sample.get_filename()
if filename:
try:
sample = load_contents(filename)
except IOError:
self.__ioerror(filename)
try:
self.__generator.sample = sample
self.__update_statistic_fields()
except InvalidSampleError:
pass
def __set_dictionary_file(self, w):
filename = self.__filechooserbutton_dictionary.get_filename()
if filename:
try:
dictionary = load_contents(filename).split()
except IOError:
self.__ioerror(filename)
try:
self.__generator.dictionary = dictionary
except InvalidDictionaryError:
pass
def __set_defaults(self, options):
if options:
if options.paragraphs:
self.__spinbutton_quantity.set_value(options.paragraphs)
self.__radiobutton_quantity_paragraphs.set_active(True)
elif options.sentences:
self.__spinbutton_quantity.set_value(options.sentences)
self.__radiobutton_quantity_sentences.set_active(True)
else:
self.__spinbutton_quantity.set_value(5)
self.__radiobutton_quantity_paragraphs.set_active(True)
if options.format == "html-p":
self.__radiobutton_format_html_paragraphs.set_active(True)
elif options.format == "html-li":
self.__radiobutton_format_html_list.set_active(True)
else:
self.__radiobutton_format_plain.set_active(True)
if options.lorem:
self.__checkbutton_start_with_lorem.set_active(True)
else:
self.__checkbutton_start_with_lorem.set_active(False)
if options.sample_path:
self.__filechooserbutton_sample.set_filename(options.sample_path)
if options.dictionary_path:
self.__filechooserbutton_dictionary.set_filename(options.dictionary_path)
if options.paragraphs or options.sentences:
self.__button_generate.clicked()
def __error(self, title, message):
error = gtk.MessageDialog(
type=gtk.MESSAGE_ERROR,
message_format = title,
buttons=gtk.BUTTONS_OK
)
def close_error(widget, data=None):
widget.destroy()
error.format_secondary_markup(message)
error.connect('response', close_error)
error.show()
def __ioerror(self, filename):
self.__error('Missing data files', 'The following file could not be found: <b>%s</b>' % filename)
def __set_sentence_mean(self, w):
self.__generator.sentence_mean = self.__spinbutton_sentence_mean.get_value()
def __set_sentence_sigma(self, w):
self.__generator.sentence_sigma = self.__spinbutton_sentence_sigma.get_value()
pass
def __set_paragraph_mean(self, w):
self.__generator.paragraph_mean = self.__spinbutton_paragraph_mean.get_value()
def __set_paragraph_sigma(self, w):
self.__generator.paragraph_sigma = self.__spinbutton_paragraph_sigma.get_value()
def __reset_statistics(self, w):
self.__generator.reset_statistics()
self.__update_statistic_fields()
def __update_statistic_fields(self):
self.__spinbutton_sentence_mean.set_value(self.__generator.sentence_mean)
self.__spinbutton_sentence_sigma.set_value(self.__generator.sentence_sigma)
self.__spinbutton_paragraph_mean.set_value(self.__generator.paragraph_mean)
self.__spinbutton_paragraph_sigma.set_value(self.__generator.paragraph_sigma)
def load_contents(file):
file = open(abspath(file), 'r')
contents = file.read()
file.close()
return contents
def main():
(options, args) = parse_args()
generator = init_generator(options)
if (options.sentences or options.paragraphs) and not options.gui:
start_cli(options, generator)
else:
start_gui(options, generator)
def parse_args():
parser = OptionParser()
parser.add_option("-p", "--paragraphs", dest="paragraphs", help="generate NUM paragraphs", metavar="NUM", type="int")
parser.add_option("-s", "--sentences", dest="sentences", help="generate NUM sentences", metavar="NUM", type="int")
parser.add_option("--sample", dest="sample_path", help="use FILE as the sample text", metavar="FILE")
parser.add_option("--dictionary", dest="dictionary_path", help="use FILE as the dictionary text", metavar="FILE")
parser.add_option("--sentence-mean", dest="sentence_mean", help="set the mean sentence length to NUM", metavar="NUM", type="float")
parser.add_option("--paragraph-mean", dest="paragraph_mean", help="set the mean paragraph length to NUM", metavar="NUM", type="float")
parser.add_option("--sentence-sigma", dest="sentence_sigma", help="set the standard deviation sentence length to NUM", metavar="NUM", type="float")
parser.add_option("--paragraph-sigma", dest="paragraph_sigma", help="set the standard deviation paragraph length to NUM", metavar="NUM", type="float")
parser.add_option("-l", "--lorem", dest="lorem", action="store_true", help="start with \"Lorem ipsum dolor...\"")
parser.add_option("-f", "--format", metavar="FORMAT", dest="format", action="store", help="produce format in plain, html-p, or html-li format", choices=("plain", "html-p", "html-li"))
parser.add_option("-g", "--gui", dest="gui", action="store_true", help="force GUI to start")
return parser.parse_args()
def init_generator(options):
generator = MarkupGenerator()
# Set the sample and dictionary texts
if options.sample_path:
try:
generator.sample = load_contents(options.sample_path)
except IOError:
error('Unable to load sample file "%s".\n' % options.sample_path)
except InvalidSampleError:
error('Invalid sample file "%s".\n' % options.sample_path)
if options.dictionary_path:
try:
generator.dictionary = load_contents(options.dictionary_path).split()
except IOError:
error('Unable to load dictionary file "%s".\n' % options.dictionary_path)
except InvalidDictionaryError:
error('Invalid sample file "%s".\n' % options.dictionary_path)
# Set statistics
try:
if options.sentence_mean:
generator.sentence_mean = options.sentence_mean
if options.paragraph_mean:
generator.paragraph_mean = options.paragraph_mean
if options.sentence_sigma:
generator.sentence_sigma = options.sentence_sigma
if options.paragraph_sigma:
generator.paragraph_sigma = options.paragraph_sigma
except ValueError, e:
error("%s\n" % e)
return generator
def error(message):
sys.stderr.write(message)
exit()
def start_gui(options, generator):
window = LipsumGUI(options, generator)
def start_cli(options, generator):
output = ""
if options.paragraphs:
if options.format == "html-p":
output = generator.generate_paragraphs_html_p(options.paragraphs, options.lorem)
elif options.format == "html-li":
output = generator.generate_paragraphs_html_li(options.paragraphs, options.lorem)
else:
output = generator.generate_paragraphs_plain(options.paragraphs, options.lorem)
elif options.sentences:
if options.format == "html-p":
output = generator.generate_sentences_html_p(options.sentences, options.lorem)
elif options.format == "html-li":
output = generator.generate_sentences_html_li(options.sentences, options.lorem)
else:
output = generator.generate_sentences_plain(options.sentences, options.lorem)
print output
if __name__ == '__main__':
main()
|