예제 #1
0
    def test_to_puz_only_include_puz_specific_data(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        puz_object = crossword.to_puz(puzzle)
        self.assertFalse(hasattr(puz_object, "kind"))
예제 #2
0
    def test_to_puz_only_works_if_numbering_system_matches(self):
        with open('fixtures/ipuz/first.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        with self.assertRaises(crossword.CrosswordException):
            crossword.to_puz(puzzle)
예제 #3
0
    def test_read_first_ipuz_fixture(self):
        with open('fixtures/ipuz/first.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        self.assertEqual(puzzle.width, 13)
        self.assertEqual(puzzle.height, 13)
        self.assertEqual(puzzle.meta.creator, "Arthur Wynne")
        self.assertEqual(puzzle.meta.date, "12/21/1913")
        self.assertEqual(puzzle.meta.title, "FUN's Word-Cross Puzzle")
        self.assertEqual(
            puzzle.clues.across["2-3"],
            "What bargain hunters enjoy"
        )
        self.assertIsNone(puzzle.block)
        self.assertIsNone(puzzle.empty)

        self.assertEqual(puzzle.clues.down["1-32"], "To govern")
        self.assertEqual(puzzle[0, 0].puzzle, None)
        self.assertEqual(puzzle[0, 0].solution, None)
        self.assertEqual(puzzle[6, 0].puzzle, 1)
        self.assertEqual(puzzle[6, 0].solution, "R")

        puzzle[6, 0]['solution'] = "X"
        self.assertEqual(puzzle[6, 0].solution, "X")
        puzzle[6, 0].solution = "Y"
        self.assertEqual(puzzle[6, 0]['solution'], "Y")
        setattr(puzzle[6, 0], "solution", "Z")
        self.assertEqual(puzzle[6, 0].solution, "Z")
예제 #4
0
    def test_read_example_ipuz_fixture(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        self.assertEqual(puzzle.meta.rights, "2011 Puzzazz")
        self.assertEqual(puzzle.empty, "0")
예제 #5
0
    def test_read_first_ipuz_fixture(self):
        with open('fixtures/ipuz/first.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        self.assertEqual(puzzle.width, 13)
        self.assertEqual(puzzle.height, 13)
        self.assertEqual(puzzle.meta.creator, "Arthur Wynne")
        self.assertEqual(puzzle.meta.date, "12/21/1913")
        self.assertEqual(puzzle.meta.title, "FUN's Word-Cross Puzzle")
        self.assertEqual(puzzle.clues.across["2-3"],
                         "What bargain hunters enjoy")
        self.assertIsNone(puzzle.block)
        self.assertIsNone(puzzle.empty)

        self.assertEqual(puzzle.clues.down["1-32"], "To govern")
        self.assertEqual(puzzle[0, 0].puzzle, None)
        self.assertEqual(puzzle[0, 0].solution, None)
        self.assertEqual(puzzle[6, 0].puzzle, 1)
        self.assertEqual(puzzle[6, 0].solution, "R")

        puzzle[6, 0]['solution'] = "X"
        self.assertEqual(puzzle[6, 0].solution, "X")
        puzzle[6, 0].solution = "Y"
        self.assertEqual(puzzle[6, 0]['solution'], "Y")
        setattr(puzzle[6, 0], "solution", "Z")
        self.assertEqual(puzzle[6, 0].solution, "Z")
예제 #6
0
    def test_to_puz_only_works_if_numbering_system_matches(self):
        with open('fixtures/ipuz/first.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        with self.assertRaises(crossword.CrosswordException):
            crossword.to_puz(puzzle)
예제 #7
0
    def test_read_example_ipuz_fixture(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        self.assertEqual(puzzle.meta.rights, "2011 Puzzazz")
        self.assertEqual(puzzle.empty, "0")
예제 #8
0
    def test_to_puz_only_include_puz_specific_data(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        puz_object = crossword.to_puz(puzzle)
        self.assertFalse(hasattr(puz_object, "kind"))
예제 #9
0
    def test_read_example_may_not_have_solution(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        del ipuz_dict["solution"]
        puzzle = crossword.from_ipuz(ipuz_dict)
        self.assertEqual(puzzle[0, 0].puzzle, 1)
        self.assertEqual(puzzle[0, 0].solution, None)
예제 #10
0
    def test_read_example_may_not_have_solution(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        del ipuz_dict["solution"]
        puzzle = crossword.from_ipuz(ipuz_dict)
        self.assertEqual(puzzle[0, 0].puzzle, 1)
        self.assertEqual(puzzle[0, 0].solution, None)
예제 #11
0
    def test_read_example_may_not_have_all_cells_defined(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        ipuz_dict["puzzle"] = [row[:5] for row in ipuz_dict["puzzle"]]
        ipuz_dict["solution"] = [row[:5] for row in ipuz_dict["solution"]]
        puzzle = crossword.from_ipuz(ipuz_dict)
        self.assertEqual(puzzle[10, 10].puzzle, None)
        self.assertEqual(puzzle[10, 10].solution, None)
예제 #12
0
    def test_read_example_may_not_have_clues(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        del ipuz_dict["clues"]["Across"]
        del ipuz_dict["clues"]["Down"]
        puzzle = crossword.from_ipuz(ipuz_dict)
        self.assertEqual(list(puzzle.clues.across()), [])
        self.assertEqual(list(puzzle.clues.down()), [])
예제 #13
0
    def test_read_example_may_not_have_all_cells_defined(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        ipuz_dict["puzzle"] = [row[:5] for row in ipuz_dict["puzzle"]]
        ipuz_dict["solution"] = [row[:5] for row in ipuz_dict["solution"]]
        puzzle = crossword.from_ipuz(ipuz_dict)
        self.assertEqual(puzzle[10, 10].puzzle, None)
        self.assertEqual(puzzle[10, 10].solution, None)
예제 #14
0
    def test_read_example_may_not_have_clues(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        del ipuz_dict["clues"]["Across"]
        del ipuz_dict["clues"]["Down"]
        puzzle = crossword.from_ipuz(ipuz_dict)
        self.assertEqual(list(puzzle.clues.across()), [])
        self.assertEqual(list(puzzle.clues.down()), [])
예제 #15
0
    def test_write_to_ipuz_only_includes_empty_block(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        puzzle.block = None
        puzzle.empty = None
        new_ipuz_dict = crossword.to_ipuz(puzzle)
        self.assertNotIn("empty", new_ipuz_dict)
        self.assertNotIn("block", new_ipuz_dict)
예제 #16
0
    def test_write_to_ipuz_only_includes_empty_block(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        puzzle.block = None
        puzzle.empty = None
        new_ipuz_dict = crossword.to_ipuz(puzzle)
        self.assertNotIn("empty", new_ipuz_dict)
        self.assertNotIn("block", new_ipuz_dict)
예제 #17
0
    def test_read_and_write_round_trip(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        new_ipuz_dict = crossword.to_ipuz(puzzle)
        for key, value in ipuz_dict.items():
            self.assertEqual(value, new_ipuz_dict[key])
        for key, value in new_ipuz_dict.items():
            if key in ipuz_dict:
                self.assertEqual(value, ipuz_dict[key])
            else:
                self.assertIsNone(value)
예제 #18
0
    def test_read_and_write_round_trip(self):
        with open('fixtures/ipuz/example.ipuz') as f:
            ipuz_dict = ipuz.read(f.read())

        puzzle = crossword.from_ipuz(ipuz_dict)
        new_ipuz_dict = crossword.to_ipuz(puzzle)
        for key, value in ipuz_dict.items():
            self.assertEqual(value, new_ipuz_dict[key])
        for key, value in new_ipuz_dict.items():
            if key in ipuz_dict:
                self.assertEqual(value, ipuz_dict[key])
            else:
                self.assertIsNone(value)
예제 #19
0
def viewer(ipuz_file_name, rendered_file_name):
    print('Converting "{}" to "{}"'.format(ipuz_file_name, rendered_file_name))
    with open(ipuz_file_name) as puzzle_file:
        ipuz_dict = ipuz.read(
            puzzle_file.read())  # may raise ipuz.IPUZException
    puzzle = crossword.from_ipuz(ipuz_dict)

    with HtmlPage(rendered_file_name) as draw:
        draw.heading(1, 'Crossword example')
        draw.heading(
            2, '{} by {}'.format(puzzle.meta['title'],
                                 puzzle.meta['publisher']))

        # draw the empty crossword board
        with draw.crossword(puzzle.block) as grid:
            for row in puzzle:
                grid.add_row([(get_literal(cell.puzzle), ' ') for cell in row])

        # container to hold clues
        with draw.column_container() as _:
            # disply ACROSS clues
            with draw.column() as _:
                draw.heading(3, 'ACROSS')
                for number, clue in puzzle.clues.across():
                    draw.line('{} {}'.format(number, clue))

            # disply DOWN clues
            with draw.column() as _:
                draw.heading(3, 'DOWN')
                for number, clue in puzzle.clues.down():
                    draw.line('{} {}'.format(number, clue))

        # draw solution
        draw.heading(2, 'Solution')
        with draw.crossword(puzzle.block) as grid:
            for row in puzzle:
                grid.add_row([(get_literal(cell.puzzle), cell.solution)
                              for cell in row])

        draw.rights(puzzle.meta['rights'])

    print('Done!')
예제 #20
0
 def test_from_ipuz_raise_exception_for_non_crossword(self):
     ipuz_dict = {
         "kind": ["http://ipuz.org/sudoku#1"],
     }
     with self.assertRaises(crossword.CrosswordException):
         crossword.from_ipuz(ipuz_dict)
예제 #21
0
def parse_ipuz(contents, filename):
    rebus_shorthands = list("⚷⚳♇♆⛢♄♃♂♁♀☿♹♸♷♶♵♴♳⅘⅗⅖⅕♚♛♜♝♞♟⚅⚄⚃⚂⚁⚀♣♦♥♠+&%$@?*zyxwvutsrqponmlkjihgfedcba0987654321")

    # i need a .load to create the ipuz_dict, and then maybe i am home free
    ipuz_dict = ipuz.read(contents.decode("utf-8"))
    puzzle = crossword.from_ipuz(ipuz_dict)

    grid_dict = dict(list(zip(string.ascii_uppercase, string.ascii_uppercase)))

    xd = xdfile.xdfile('', filename)

    xd.set_header("Author", puzzle.meta.creator)
    xd.set_header("Editor", puzzle.meta.contributor)
    xd.set_header("Copyright", puzzle.meta.rights)
    dt = parse_date_from_filename(parse_pathname(filename).base)
    if dt:
        xd.set_header("Date", dt)
    xd.set_header("Notes", puzzle.meta.description)
    #xd.set_header("Postscript", "".join(x for x in puzobj.postscript if ord(x) >= ord(' ')))
    #xd.set_header("Preamble", puzobj.preamble)

    xd.set_header("Title", puzzle.meta.title)


    for r, row in enumerate(puzzle):
        rowstr = ""
        for c, cell in enumerate(row):
            if puzzle.block is None and cell.solution == '#':
                rowstr += xdfile.BLOCK_CHAR
            elif cell.solution == puzzle.block:
                rowstr += xdfile.BLOCK_CHAR
            elif cell.solution == ':':
                rowstr += xdfile.OPEN_CHAR
            elif cell == puzzle.empty:
                rowstr += xdfile.UNKNOWN_CHAR
            else:
                n = r * puzzle.width + c
                ch = cell.solution
                if ch not in grid_dict:
                    if ch in rebus_shorthands:
                        cellch = ch
                        rebus_shorthands.remove(ch)
                        warn("%s: unknown grid character '%s', assuming rebus of itself" % (filename, ch))
                    else:
                        cellch = rebus_shorthands.pop()
                        warn("%s: unknown grid character '%s', assuming rebus (as '%s')" % (filename, ch, cellch))
                    xd.set_header("Rebus", xd.get_header("Rebus") + " %s=%s" % (cellch, ch))


                    grid_dict[ch] = cellch
                rowstr += grid_dict[ch]

        xd.grid.append(rowstr)

    assert xd.size() == (puzzle.width, puzzle.height), "non-matching grid sizes"

    # clues
    answers = {}

    for posdir, posnum, answer in xd.iteranswers():
        answers[posdir[0] + str(posnum)] = answer

    try:
        for number, clue in puzzle.clues.across():
            cluenum = "A" + str(number)
            if cluenum not in answers:
                raise xdfile.IncompletePuzzleParse(xd, "Clue number doesn't match grid: " + cluenum)
            xd.clues.append((("A", number), decode(clue), answers.get(cluenum, "")))

        # xd.append_clue_break()

        for number, clue in puzzle.clues.down():
            cluenum = "D" + str(number)
            if cluenum not in answers:
                raise xdfile.IncompletePuzzleParse(xd, "Clue doesn't match grid: " + cluenum)
            xd.clues.append((("D", number), decode(clue), answers.get(cluenum, "")))
    except KeyError as e:
        raise xdfile.IncompletePuzzleParse(xd, "Clue doesn't match grid: " + str(e))

    return xd
예제 #22
0
 def test_from_ipuz_raise_exception_for_non_crossword(self):
     ipuz_dict = {
         "kind": ["http://ipuz.org/sudoku#1"],
     }
     with self.assertRaises(crossword.CrosswordException):
         crossword.from_ipuz(ipuz_dict)