home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / gnome-games-data / glchess / history.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  4.8 KB  |  161 lines

  1. # -*- coding: utf-8 -*-
  2. __author__ = 'Robert Ancell <bob27@users.sourceforge.net>'
  3. __license__ = 'GNU General Public License Version 2'
  4. __copyright__ = 'Copyright 2005-2006  Robert Ancell'
  5.  
  6. import os
  7. import errno
  8.  
  9. import chess.pgn
  10. from defaults import *
  11.  
  12. class GameHistory:
  13.     
  14.     def __init__(self):
  15.         try:
  16.             os.makedirs(HISTORY_DIR)
  17.         except OSError, e:
  18.             if e.errno != errno.EEXIST:
  19.                 print 'Failed to make history directory: %s' % e.strerror
  20.     
  21.     def getUnfinishedGame(self):
  22.         """Get the last game that is unfinished.
  23.         
  24.         Returns the (PGN game, fileName, inHistory) or (None, None, False) if no unfinished games.
  25.         """
  26.         g = None
  27.         fileName = None
  28.         inHistory = False
  29.         try:
  30.             f = file(UNFINISHED_FILE, 'r')
  31.             lines = f.readlines()
  32.             f.close()
  33.  
  34.             index = 0
  35.             while len(lines) > 0:
  36.                 fileName = lines[0].strip()
  37.  
  38.                 try:
  39.                     p = chess.pgn.PGN(fileName, 1)
  40.                 except chess.pgn.Error, e:
  41.                     print e.message
  42.                 except IOError, e:
  43.                     print e.strerror
  44.                 else:
  45.                     result = p[0].getTag(chess.pgn.TAG_RESULT)
  46.                     if result == chess.pgn.RESULT_INCOMPLETE:
  47.                         g = p[0]
  48.                         inHistory = fileName.startswith(HISTORY_DIR)
  49.                         break
  50.                 lines = lines[1:]
  51.  
  52.         except IOError, e:
  53.             if e.errno != errno.ENOENT:
  54.                 print e.errno
  55.                 print 'Failed to read unfinished list'
  56.                 return (None, None, False)
  57.             lines = []
  58.  
  59.         # Write the list back
  60.         try:
  61.             f = file(UNFINISHED_FILE, 'w')
  62.             f.writelines(lines)
  63.             f.close()
  64.         except IOError:
  65.             print 'Failed to write unfinished list'
  66.  
  67.         return (g, fileName, inHistory)
  68.  
  69.     def load(self, date):
  70.         return
  71.     
  72.     def _getFilename(self, game):
  73.         date = game.getTag(chess.pgn.TAG_DATE)
  74.         try:
  75.             (year, month, day) = date.split('.')
  76.         except ValueError:
  77.             directory = HISTORY_DIR
  78.         else:
  79.             directory = os.path.join(HISTORY_DIR, year, month, day)
  80.  
  81.         # Create the directory
  82.         try:
  83.             os.makedirs(directory)
  84.         except OSError, e:
  85.             if e.errno != errno.EEXIST:
  86.                 return None # FIXME
  87.  
  88.         # Get a unique name for the file
  89.         count = 0
  90.         fileName = os.path.join(directory, date)
  91.         while os.path.exists(fileName):
  92.             count += 1
  93.             fileName = os.path.join(directory, '%s-%d' % (date, count))
  94.         
  95.         return fileName
  96.     
  97.     def rename(self, oldName, newName):
  98.         try:
  99.             os.unlink(oldName)
  100.         except OSError:
  101.             print 'Failed to remove game from history'
  102.  
  103.         try:
  104.             f = file(UNFINISHED_FILE, 'r')
  105.             lines = f.readlines()
  106.             f.close()
  107.             
  108.             f = file(UNFINISHED_FILE, 'w')
  109.             for line in lines:
  110.                 l = line.strip()
  111.                 if l == oldName:
  112.                     f.write(newName + '\n')
  113.                 else:
  114.                     f.write(l + '\n')
  115.             f.close()
  116.         except IOError:
  117.             print 'Failed to update unfinished list'
  118.     
  119.     def save(self, g, fileName):
  120.         """Save a game in the history.
  121.         
  122.         'g' is the game to save
  123.         'fileName' is the history file to write to or None to create a new one
  124.         """
  125.         if fileName is None:
  126.             fileName = self._getFilename(g)
  127.             if fileName is None:
  128.                 # FIXME: This should be in a dialog
  129.                 print 'Unable to find location to save to'
  130.                 return
  131.  
  132.         lines = g.getLines()
  133.         try:
  134.             f = file(fileName, 'w')
  135.             for line in lines:
  136.                 f.write(line + '\n')
  137.             f.write('\n')
  138.             f.close()
  139.         except IOError, e:
  140.             # FIXME: This should be in a dialog
  141.             print 'Unable to autosave to %s: %s' % (fileName, str(e))
  142.             
  143.         # Update unfinished list
  144.         result = g.getTag(chess.pgn.TAG_RESULT)
  145.         try:
  146.             f = file(UNFINISHED_FILE, 'r')
  147.             lines = f.readlines()
  148.             f.close()
  149.             
  150.             f = file(UNFINISHED_FILE, 'w')
  151.             if result == chess.pgn.RESULT_INCOMPLETE:
  152.                 f.write(fileName + '\n')
  153.             for line in lines:
  154.                 l = line.strip()
  155.                 if l == fileName and result == chess.pgn.RESULT_INCOMPLETE:
  156.                     continue
  157.                 f.write(l + '\n')
  158.             f.close()
  159.         except IOError:
  160.             print 'Failed to update unfinished list'
  161.