]> git.sommitrealweird.co.uk Git - curses-crossword.git/blobdiff - curses-crossword.py
Start reformatting for better pylint coverage
[curses-crossword.git] / curses-crossword.py
index a396105a983d4198742153496c14ca11faa21b41..662fa17b920611aed25160602e4351781e0f3be3 100644 (file)
@@ -4,6 +4,9 @@ import curses
 import curses.ascii
 import locale
 import codecs
+import getopt
+import os
+import sys
 
 locale.setlocale(locale.LC_ALL, '')
 code = locale.getpreferredencoding()
@@ -24,16 +27,43 @@ try:
         "9": unichr(8313).encode(code),
     }
 except:
-    for number in range(0,10):
+    for number in range(0, 10):
         superscript_numbers[str(number)] = str(number)
 
-crossworddata = codecs.open("crossword.output", "r", "utf-8").read()
+filename = None
+
+try:
+    (options, args) = getopt.getopt(sys.argv[1:], "f:", "file=")
+except getopt.GetoptError, err:
+    print str(err)
+    sys.exit(2)
+
+for option in options:
+    if option[0] == "-f" or option[0] == "--file":
+        filename = option[1]
+
+if not filename and len(args) > 0:
+    filename = args[0]
+
+if not filename:
+    sys.stderr.write("No crossword file specified, exiting.\n")
+    sys.exit(0)
+
+if os.path.exists(filename) and os.path.isfile(filename):
+    crossworddata = codecs.open(filename, "r", "utf-8").read()
+else:
+    sys.stderr.write("Couldn't open file %s\n" %(filename))
+    sys.exit(0)
 
 def parsecrossword(crossworddata):
     ingrid = False
     inacross = False
     indown = False
-    crossword = {"grid": [], "across": {}, "down": {}, "grid_questions_start": [], "grid_questions_end": []}
+    crossword = {"grid": [],
+        "across": {},
+        "down": {},
+        "grid_questions_start": [],
+        "grid_questions_end": []}
 
     for line in crossworddata.split("\n"):
         line = line.strip("\n")
@@ -58,23 +88,24 @@ def parsecrossword(crossworddata):
                     parts = line.split()
                     question_number = int(parts[0])
                     clue = " ".join(parts[1:])
-                    crossword["across"][int(question_number)] = clue
+                    crossword["across"][int(question_number)] = \
+                        clue.encode(code)
             if indown:
                 if line != "":
                     parts = line.split()
                     question_number = int(parts[0])
                     clue = " ".join(parts[1:])
-                    crossword["down"][int(question_number)] = clue
+                    crossword["down"][int(question_number)] = clue.encode(code)
     num_cols = len(crossword["grid"][0])
     num_rows = len(crossword["grid"])
 
     current_clue_number = 1
 
-    for row in range(0,num_rows):
+    for row in range(0, num_rows):
         crossword["grid_questions_start"].append([])
 
-    for row in range(0,num_rows):
-        for col in range(0,num_cols):
+    for row in range(0, num_rows):
+        for col in range(0, num_cols):
             have_clue = False
             if col > 0 \
                 and crossword["grid"][row][col - 1] == "x" \
@@ -94,7 +125,8 @@ def parsecrossword(crossworddata):
                 and crossword["grid"][row + 1][col] != "x":
                 have_clue = True
             if have_clue:
-                crossword["grid_questions_start"][row].append(current_clue_number)
+                crossword["grid_questions_start"][row] \
+                    .append(current_clue_number)
                 current_clue_number += 1
             else:
                 crossword["grid_questions_start"][row].append(0)
@@ -117,11 +149,11 @@ def crossword(stdscr, crossworddata):
     curx -= 1
     stdscr.addch(cury, curx, curses.ACS_URCORNER)
     cury += 1
-    curgridy=0
+    curgridy = 0
     for line in crossword["grid"]:
         datastr = ""
         curx = 0
-        curgridx=0
+        curgridx = 0
         for c in line:
             stdscr.addch(cury, curx, curses.ACS_VLINE)
             if curx > 0:
@@ -137,7 +169,13 @@ def crossword(stdscr, crossworddata):
                 stdscr.addch(cury, curx+1, curses.ACS_BLOCK)
                 stdscr.addch(cury, curx+2, curses.ACS_BLOCK)
             elif crossword["grid_questions_start"][curgridy][curgridx] > 0:
-                stdscr.addstr(cury, curx, ''.join([superscript_numbers[x].decode(code) for x in str(crossword["grid_questions_start"][curgridy][curgridx])]).encode(code))
+                stdscr.addstr(cury, curx, \
+                    ''.join( \
+                        [superscript_numbers[x].decode(code) \
+                        for x in str( \
+                            crossword["grid_questions_start"] \
+                            [curgridy][curgridx])]\
+                        ).encode(code))
             curx += 3
             curgridx += 1
         else:
@@ -163,14 +201,16 @@ def crossword(stdscr, crossworddata):
     stdscr.addstr(cury, curx, "Across")
     cury += 1
     for cluenumber in crossword["across"].keys():
-        stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["across"][cluenumber]))
+        stdscr.addstr(cury, curx, "%3s: %s" \
+            %(str(cluenumber), crossword["across"][cluenumber]))
         cury += 1
 
     cury += 1
     stdscr.addstr(cury, curx, "Down")
     cury += 1
     for cluenumber in crossword["down"].keys():
-        stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["down"][cluenumber]))
+        stdscr.addstr(cury, curx, "%3s: %s" \
+            %(str(cluenumber), crossword["down"][cluenumber]))
         cury += 1
 
     curx = 3
@@ -182,7 +222,7 @@ def crossword(stdscr, crossworddata):
         curx += 4
         gridx += 1
 
-    stdscr.move(cury,curx)
+    stdscr.move(cury, curx)
 
     while 1:
         c = stdscr.getch()
@@ -206,8 +246,8 @@ def crossword(stdscr, crossworddata):
                 gridx -= 1
                 while gridx > 0 \
                     and crossword["grid"][gridy][gridx] == "x":
-                        gridx -= 1
-                        curx -= 4
+                    gridx -= 1
+                    curx -= 4
                 while crossword["grid"][gridy][gridx] == "x":
                     gridx += 1
                     curx += 4
@@ -218,8 +258,8 @@ def crossword(stdscr, crossworddata):
                 cury -= 2
                 while gridy > 0 \
                     and crossword["grid"][gridy][gridx] == "x":
-                        gridy -= 1
-                        cury -= 2
+                    gridy -= 1
+                    cury -= 2
                 while crossword["grid"][gridy][gridx] == "x":
                     gridy += 1
                     cury += 2