]> git.sommitrealweird.co.uk Git - curses-crossword.git/blobdiff - curses-crossword.py
Be slightly nicer when option parsing fails
[curses-crossword.git] / curses-crossword.py
index a396105a983d4198742153496c14ca11faa21b41..19ee754ef125f87a3bcf9692701ce1fd6dbd1c2e 100644 (file)
@@ -4,6 +4,9 @@ import curses
 import curses.ascii
 import locale
 import codecs
 import curses.ascii
 import locale
 import codecs
+import getopt
+import os
+import sys
 
 locale.setlocale(locale.LC_ALL, '')
 code = locale.getpreferredencoding()
 
 locale.setlocale(locale.LC_ALL, '')
 code = locale.getpreferredencoding()
@@ -27,7 +30,30 @@ except:
     for number in range(0,10):
         superscript_numbers[str(number)] = str(number)
 
     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
 
 def parsecrossword(crossworddata):
     ingrid = False
@@ -58,13 +84,13 @@ def parsecrossword(crossworddata):
                     parts = line.split()
                     question_number = int(parts[0])
                     clue = " ".join(parts[1:])
                     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('utf-8')
             if indown:
                 if line != "":
                     parts = line.split()
                     question_number = int(parts[0])
                     clue = " ".join(parts[1:])
             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('utf-8')
     num_cols = len(crossword["grid"][0])
     num_rows = len(crossword["grid"])
 
     num_cols = len(crossword["grid"][0])
     num_rows = len(crossword["grid"])