Implemented the AcrossLite(tm) exporter
authorHarishankar <v.harishankar@gmail.com>
Sun, 5 Dec 2010 13:10:02 +0000 (18:40 +0530)
committerHarishankar <v.harishankar@gmail.com>
Sun, 5 Dec 2010 13:10:02 +0000 (18:40 +0530)
Implemented the AcrossLite(tm) format exporter

crosswordpuzzle.py
crosswordpuzzlecreator.py

index 4e291db..012450f 100644 (file)
@@ -73,6 +73,52 @@ class CrosswordPuzzle:
                        for j in range (cols):
                                self.data[i].append (GridItem ())
 
+       # get the AcrossLite(TM) data for exporting
+       def export_acrosslite (self, title, author, copyright):
+               # don't export if grid is frozen
+               if self.frozen_grid is False:
+                       raise FrozenGridException
+
+               across_data = []
+               across_data.append ("<ACROSS PUZZLE>\n")
+               across_data.append ("<TITLE>\n")
+               across_data.append (title + "\n")
+               across_data.append ("<AUTHOR>\n")
+               across_data.append (author + "\n")
+               across_data.append ("<COPYRIGHT>\n")
+               across_data.append (copyright + "\n")
+               across_data.append ("<SIZE>\n")
+               str_size = str (self.cols) + "x" + str (self.rows)
+               across_data.append (str_size + "\n")
+               across_data.append ("<GRID>\n")
+               for row in range (self.rows):
+                       for col in range (self.cols):
+                               if (self.data[row][col].occupied_across is True or
+                                       self.data[row][col].occupied_down is True):
+                                       across_data.append (self.data[row][col].char)
+                               else:
+                                       across_data.append (".")
+                       across_data.append ("\n")
+
+               across_data.append ("<ACROSS>\n")
+               clues_across = self.get_clues_across ()
+               for word, clue in clues_across:
+                       if clue:
+                               across_data.append (clue + "\n")
+                       else:
+                               across_data.append ("(No clue yet)\n")
+
+               across_data.append ("<DOWN>\n")
+               clues_down = self.get_clues_down ()
+               for word, clue in clues_down:
+                       if clue:
+                               across_data.append (clue + "\n")
+                       else:
+                               across_data.append ("(No clue yet\n")
+
+               acrosslite_str = "".join (across_data)
+               return acrosslite_str
+
        # get all the clues for across
        def get_clues_across (self):
                clues = []
index cdd233f..710de09 100644 (file)
@@ -94,7 +94,7 @@ class CrosswordPuzzleCreator:
                        raw_input (self.BRICKRED + "Press <return> to continue" + self.ENDCOL)
 
        # display existing clues
-       def display_clues (self):
+       def on_display_clues (self):
                try:
                        aclues = self.puzzle.get_clues_across ()
                        sys.stdout.write (self.BOLD + "\n------------\n")
@@ -128,7 +128,7 @@ class CrosswordPuzzleCreator:
                raw_input (self.BRICKRED + "Press <return> to continue" + self.ENDCOL)
 
        # set a clue to a word
-       def set_clue (self):
+       def on_set_clue (self):
                self.print_puzzle ()
                # get the row and column
                srow = raw_input (self.BRICKRED + "At row: " + self.ENDCOL)
@@ -164,7 +164,7 @@ class CrosswordPuzzleCreator:
                        sys.stderr.write ("No down word found at that position\n")
 
        # add a word to the puzzle
-       def add_word (self, across=True):
+       def on_add_word (self, across=True):
                # first display the grid
                self.print_puzzle ()
                # get the row and column
@@ -193,6 +193,27 @@ class CrosswordPuzzleCreator:
                except crosswordpuzzle.FrozenGridException:
                        sys.stderr.write ("Word cannot be added to a frozen puzzle.\n")
 
+       # Export to across lite
+       def on_export_acrosslite (self):
+               try:
+                       sys.stdout.write (self.BLUE + "Exporting to AcrossLite(tm) Format\n" +
+                                               self.ENDCOL)
+                       title = raw_input (self.BRICKRED + "Puzzle title: " + self.ENDCOL)
+                       name = raw_input (self.BRICKRED + "Author name: " + self.ENDCOL)
+                       copyright = raw_input (self.BRICKRED + "Copyright: " + self.ENDCOL)
+                       exportfile = raw_input (self.BRICKRED + "Export to file: " + self.ENDCOL)
+
+                       acrosslite_str = self.puzzle.export_acrosslite (title, name, copyright)
+                       fexport = open (exportfile, "w")
+                       fexport.write (acrosslite_str)
+                       fexport.close ()
+                       sys.stdout.write (self.BLUE + "Exported AcrossLite(tm) File: " +
+                               exportfile + "\n" + self.ENDCOL)
+               except crosswordpuzzle.FrozenGridException:
+                       sys.stderr.write ("Cannot export as grid is not frozen/finalized\n")
+               except crosswordpuzzle.NoWordsException:
+                       sys.stderr.write ("No words to export!\n")
+
        # Puzzle loop
        def do_puzzle_loop (self):
                # there is a current file
@@ -211,24 +232,27 @@ class CrosswordPuzzleCreator:
                                sys.stdout.write ("8. Set clue for word\n")
                                sys.stdout.write ("9. Display clues\n")
                                sys.stdout.write ("S. Save puzzle\n")
+                               sys.stdout.write ("E. Export to AcrossLite(TM) format\n")
                                sys.stdout.write ("X. Exit to main menu\n" + self.ENDCOL)
                                ch = raw_input (self.BRICKRED + "Your choice: " + self.ENDCOL)
                                if ch == "1":
                                        self.print_puzzle ()
                                elif ch == "2":
-                                       self.add_word ()
+                                       self.on_add_word ()
                                elif ch == "3":
-                                       self.add_word (False)
+                                       self.on_add_word (False)
                                elif ch == "6":
                                        self.puzzle.freeze_grid ()
                                elif ch == "7":
                                        self.puzzle.unfreeze_grid ()
                                elif ch == "8":
-                                       self.set_clue ()
+                                       self.on_set_clue ()
                                elif ch == "9":
-                                       self.display_clues ()
+                                       self.on_display_clues ()
                                elif ch == "S" or ch == "s":
                                        self.save_puzzle ()
+                               elif ch == "E" or ch == "e":
+                                       self.on_export_acrosslite ()
                                elif ch == "X" or ch == "x":
                                        break