diff -r 707ddd7a7912 -r 9234f5ae765b index.py --- a/index.py Tue May 05 19:39:44 2009 +0300 +++ b/index.py Tue May 05 21:49:56 2009 +0300 @@ -126,7 +126,12 @@ """ data = [] - + + # if no chars given, don't insert any + if not chars : + chars = [] + + # randomize char order across lines? if random_chars : chars = randomize(chars) @@ -135,11 +140,16 @@ continue # pick position to place char - pos = random.randint(1, len(line) - 1) - + if len(line) >= 2 : + pos = random.randint(1, len(line) - 1) + else : + pos = random.randint(0, 1) + + # default color if not color : color = "#000000" - + + # randomize text in some way? if random_text : line = ''.join(randomize(line)) @@ -251,21 +261,92 @@ return data -def arg_bool (val) : - if val.lower() in ('true', 't', '1', 'yes', 'y') : - return True - elif val.lower() in ('false', 'f', '0', 'no', 'n') : - return False - else : - raise ValueError(val) +class OptionType (object) : + def parse (self, val) : + """ + Unicode value -> object + """ -def arg_color (val) : - if val.beginswith('#') : - int(val[1:], 16) + abstract - return val - else : - raise ValueError(val) + def build (self, val) : + """ + object -> unicode value + """ + + return unicode(val) + + def input (self, val) : + """ + HTML input item + """ + + abstract + +class StringOption (OptionType) : + def parse (self, val) : + return unicode(val) + + def build (self, val) : + if val is None : + return "" + + else : + return val + + def input (self, opt, val) : + return """""" % dict( + name = opt.name, + value = self.build(val), + ) + + def select (self, opt, val) : + return """""" % dict( + name = opt.name, + options = '\n'.join( + "\t" % dict( + value = self.build(optval), + selected = 'selected="selected"' if val == optval else "", + ) for optval in opt.range + ), + ) + +class BoolOption (OptionType) : + def parse (self, val) : + if val.lower() in ('true', 't', '1', 'yes', 'y') : + return True + + elif val.lower() in ('false', 'f', '0', 'no', 'n') : + return False + + else : + raise ValueError(val) + + def input (self, opt, val) : + return """""" % dict( + name = opt.name, + checked = 'checked="checked"' if val else '', + ) + +class IntOption (StringOption) : + def parse (self, val) : + return int(val) + +class FloatOption (StringOption) : + def parse (self, val) : + return float(val) + +class ColorOption (StringOption) : + def _parse (self, val) : + if val.startswith('#') : + int(val[1:], 16) + + return val + else : + raise ValueError(val) + class Option (object) : def __init__ (self, name, is_list, type, default, range) : @@ -275,43 +356,94 @@ self.default = default self.range = range - def parse (self, args) : + def parse (self, args, force_bool=False) : if self.is_list : if self.name in args : - return args.getlist(self.name, self.type) + l = args.getlist(self.name, self.type.parse) + + # special-case to handle a single param with a newline-separtated list + if len(l) == 1 : + if not l[0] : + return None + + else : + return l[0].split('\r\n') + + else : + return l + else : return self.default + else : - if self.type == arg_bool and not self.default and self.name in args : + if isinstance(self.type, BoolOption) and force_bool : + return self.name in args + + elif isinstance(self.type, BoolOption) and not self.default and self.name in args : return True else : - return args.get(self.name, self.default, self.type) + return args.get(self.name, self.default, self.type.parse) + + def build_list (self, value) : + if self.is_list and value : + return [self.type.build(val) for val in value] + + else : + return [self.type.build(value)] + + def _build_input (self, value) : + if self.is_list : + return """\ +""" % dict( + name = self.name, + data = '\n'.join(self.type.build(val) for val in value) if value else '', + ) + + elif self.range : + return self.type.select(self, value) + + else : + return self.type.input(self, value) + + def build_form (self, opts) : + value = opts[self.name] + + return """\ +
Aaltologotin pulauttaa sulle uuden, sattumanvaraisesti valitun aalto-logon!
+ + +