def __init__(self, palabra_window, properties): PalabraDialog.__init__(self, palabra_window, u"Appearance") self.palabra_window = palabra_window hbox = gtk.HBox() hbox.set_spacing(18) hbox.pack_start(self.create_content(properties)) mode = constants.VIEW_MODE_PREVIEW_SOLUTION self.preview = GridPreview(mode=mode, cell_size=None) self.preview.set_size_request(200, 200) hbox.pack_start(self.preview, False, False, 0) self.pack(hbox) g = Grid(3, 3) g.set_block(0, 2, True) g.set_void(2, 0, True) g.set_char(0, 0, "A") g.assign_numbers() self.preview.display(g) self.on_update() self.add_button(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL) self.add_button(gtk.STOCK_APPLY, gtk.RESPONSE_OK)
def read_ipuz(filename, warnings=True): results = [] content = None with open(filename, 'r') as f: content = f.read() content = content.strip('\n') if content is not None: data = json.loads(content[5:-1]) keys = data.keys() if "version" not in keys: raise IPUZParserError(u"Mandatory version element missing in ipuz file.") if "kind" not in keys: raise IPUZParserError(u"Mandatory kind element missing in ipuz file.") n_kinds = len(data["kind"]) if n_kinds == 0: raise IPUZParserError(u"Mandatory kind element has no content.") if n_kinds > 1: raise IPUZParserError(u"Mandatory kind element has more than one puzzle kind.") kind = data["kind"][0] if kind != "http://ipuz.org/crossword#1": raise IPUZParserError(u"This type of puzzle (" + kind + ") is not supported.") r_meta = {} r_width = None r_height = None r_grid = None r_notepad = "" r_styles = {} r_gstyles = {} r_number_mode = constants.NUMBERING_AUTO for m in IPUZ_MY_META_ELEMS: if m in data: r_meta[IPUZ_MY_META_ELEMS[m]] = data[m] for m in IPUZ_META_ELEMS: if m in data: r_meta[m] = data[m] c_block = IPUZ_BLOCK_CHAR c_empty = IPUZ_EMPTY_CHAR for t in IPUZ_TECH_ELEMS: if t in data: if t == "block": r_meta[t] = c_block = data[t] elif t == "empty": r_meta[t] = c_empty = data[t] else: pass # TODO for e in IPUZ_CROSS_ELEMS: if e in data: if e == "dimensions": r_width = data[e]["width"] r_height = data[e]["height"] elif e == "puzzle": assert r_width >= 0 assert r_height >= 0 r_grid = Grid(r_width, r_height) x, y = 0, 0 for row in data[e]: for x, c in enumerate(row): if isinstance(c, dict): if "style" in c: style = c["style"] if "shapebg" in style: if style["shapebg"] == "circle": if (x, y) not in r_styles: r_styles[x, y] = CellStyle() r_styles[x, y]["circle"] = True if "color" in style: if (x, y) not in r_styles: r_styles[x, y] = CellStyle() rgb = hex_to_color('#' + style["color"]) r_styles[x, y]["cell", "color"] = rgb if "colortext" in style: if (x, y) not in r_styles: r_styles[x, y] = CellStyle() rgb = hex_to_color(style["colortext"]) r_styles[x, y]["char", "color"] = rgb if "cell" in c: c = c["cell"] # fall-through if c == None: r_grid.set_void(x, y, True) elif c == c_block: r_grid.set_block(x, y, True) elif c == c_empty: pass y += 1 elif e == "solution": assert r_grid is not None x, y = 0, 0 for row in data[e]: for x, c in enumerate(row): if isinstance(c, list) or isinstance(c, dict): print "TODO" elif c is not None and c != c_block and c != c_empty: r_grid.set_char(x, y, c) y += 1 elif e == "clues": assert r_grid is not None clues = {} for md, d in [("across", "Across"), ("down", "Down")]: if d in data[e]: for n, clue in data[e][d]: clues[n, md] = clue if r_number_mode == constants.NUMBERING_AUTO: r_grid.assign_numbers() for d in ["across", "down"]: for n, x, y in r_grid.words_by_direction(d): if (n, d) in clues: r_grid.store_clue(x, y, d, "text", clues[n, d]) p = Puzzle(r_grid, r_styles, r_gstyles) p.metadata = r_meta p.type = constants.PUZZLE_IPUZ p.filename = filename p.notepad = r_notepad results.append(p) return results