Package nMOLDYN :: Package GUI :: Package HTMLReader :: Module util |
[hide private]
[frames] | no frames]

# Source Code for Module nMOLDYN.GUI.HTMLReader.util

      1  ## vim:ts=4:et:nowrap
      2  ##
      3  ##---------------------------------------------------------------------------##
      4  ##
      5  ## PySol -- a Python Solitaire game
      6  ##
      7  ## Copyright (C) 2003 Markus Franz Xaver Johannes Oberhumer
      8  ## Copyright (C) 2002 Markus Franz Xaver Johannes Oberhumer
      9  ## Copyright (C) 2001 Markus Franz Xaver Johannes Oberhumer
     10  ## Copyright (C) 2000 Markus Franz Xaver Johannes Oberhumer
     11  ## Copyright (C) 1999 Markus Franz Xaver Johannes Oberhumer
     12  ## Copyright (C) 1998 Markus Franz Xaver Johannes Oberhumer
     13  ## All Rights Reserved.
     14  ##
     15  ## This program is free software; you can redistribute it and/or modify
     16  ## it under the terms of the GNU General Public License as published by
     17  ## the Free Software Foundation; either version 2 of the License, or
     18  ## (at your option) any later version.
     19  ##
     20  ## This program is distributed in the hope that it will be useful,
     21  ## but WITHOUT ANY WARRANTY; without even the implied warranty of
     22  ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     23  ## GNU General Public License for more details.
     24  ##
     25  ## You should have received a copy of the GNU General Public License
     26  ## along with this program; see the file COPYING.
     27  ## If not, write to the Free Software Foundation, Inc.,
     28  ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
     29  ##
     30  ## Markus F.X.J. Oberhumer
     31  ## <markus@oberhumer.com>
     32  ## http://www.oberhumer.com/pysol
     33  ##
     34  ##---------------------------------------------------------------------------##
     35
     36
     37  # imports
     38  import sys, os, re, string, time, types
     39
     40  # PySol imports
     41  from mfxtools import *
     42  from version import VERSION, VERSION_DATE, VERSION_TUPLE
     43  from mfxutil import Pickler, Unpickler, UnpicklingError
     44  from mfxutil import Struct, EnvError
     45
     46
     47  # /***********************************************************************
     48  # // constants
     49  # ************************************************************************/
     50
     51  PACKAGE = "PySol"                                                   #bundle#
     52  PACKAGE_URL = "http://www.oberhumer.com/pysol"                      #bundle#
     53
     54  # Suits values are 0-3. This maps to colors 0-1.
     55  SUITS = ("Club", "Spade", "Heart", "Diamond")
     56  COLORS = ("black", "red")
     57
     58  # Card ranks are 0-12.  We also define symbolic names for the picture cards.
     59  RANKS = ("Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King")
     60  ACE = 0
     61  JACK = 10
     62  QUEEN = 11
     63  KING = 12
     64
     65  # Special values for Stack.cap:
     66  ANY_SUIT = -1
     67  ANY_COLOR = -1
     68  ANY_RANK = -1
     69  NO_SUIT = 999999        # no card can ever match this suit
     70  NO_COLOR = 999999       # no card can ever match this color
     71  NO_RANK = 999999        # no card can ever match this rank
     72
     73  #
     74  NO_REDEAL = 0
     75  UNLIMITED_REDEALS = -1
     76  VARIABLE_REDEALS = -2
     77
     78  CARDSET = "cardset"
     79
     80  IMAGE_EXTENSIONS = (".gif", ".ppm",)
     81  if 1 and os.name == "nt":
     82      ## #$unbundle#IMAGE_EXTENSIONS = (".png", ".gif", ".ppm", ".jpg",)
     83      IMAGE_EXTENSIONS = (".png", ".gif", ".ppm", ".jpg",)
     84      pass
     85
     86  try:
     87      bundle
     88  except:
     89      bundle = 0
     90
     91 -def get_version_tuple(version_string):
     92      v = re.split(r"[^\d\.]", version_string)
     93      if not v or not v[0]:
     94          return (0,)
     95      v = string.split(v[0], ".")
     96      v = filter(lambda x: x != "", v)
     97      if not v or not v[0]:
     98          return (0,)
     99      return tuple(map(int, v))
    100
    101
    102  # /***********************************************************************
    103  # // simple benchmarking
    104  # ************************************************************************/
    105
    106 -class Timer:
    107 -    def __init__(self, msg = ""):
    108          self.msg = msg
    109          self.clock = time.time
    110          if os.name == "nt":
    111              self.clock = time.clock
    112          self.start = self.clock()
    113 -    def reset(self):
    114          self.start = self.clock()
    115 -    def get(self):
    116          return self.clock() - self.start
    117 -    def __repr__(self):
    118          return "%-20s %6.3f seconds" % (self.msg, self.clock() - self.start)
    119
    120
    121  # /***********************************************************************
    122  # // DataLoader
    123  # ************************************************************************/
    124
    125 -class DataLoader:
    126 -    def __init__(self, argv0, filenames, path=[]):
    127          self.dir = None
    128          if type(filenames) is types.StringType:
    129              filenames = (filenames,)
    130          assert type(filenames) in (types.TupleType, types.ListType)
    131          #$ init path
    132          path = path[:]
    133          head, tail = os.path.split(argv0)
    134          if not head:
    135              head = os.curdir
    136          path.append(head)
    137          path.append(os.path.join(head, "data"))
    138          path.append(os.path.join(head, os.pardir, "data"))          #bundle#
    139          #$ you can add your extra directories here
    140          if os.name == "posix":
    141              pass
    142          if os.name == "nt":
    143              pass
    144          if os.name == "mac":
    145              pass
    146          #$ add standard Unix directories to search path
    147          if 1 and os.name == "posix":
    148              for v in (VERSION, ""):
    149                  for prefix in ("@prefix@", "/usr/local", "/usr"):
    150                      try:
    151                          if os.path.isdir(prefix):
    152                              path.append(os.path.join(prefix,"share/pysol",v))
    153                              path.append(os.path.join(prefix,"lib/pysol",v))
    154                              path.append(os.path.join(prefix,"share/games/pysol",v))
    155                              path.append(os.path.join(prefix,"lib/games/pysol",v))
    156                              path.append(os.path.join(prefix,"games/share/pysol",v))
    157                              path.append(os.path.join(prefix,"games/lib/pysol",v))
    158                      except EnvError:
    159                          pass
    160          #$ check path for valid directories
    161          self.path = []
    162          for p in path:
    163              if not p: continue
    164              try:
    165                  np = os.path.normpath(p)
    166                  if np and (not np in self.path) and os.path.isdir(np):
    167                      self.path.append(np)
    168              except EnvError:
    169                  pass
    170          #$ now try to find all filenames along path
    171          for p in self.path:
    172              n = 0
    173              for filename in filenames:
    174                  try:
    175                      f = os.path.join(p, filename)
    176                      if os.path.isfile(f):
    177                          n = n + 1
    178                      else:
    179                          break
    180                  except EnvError:
    181                      pass
    182              if n == len(filenames):
    183                  self.dir = p
    184                  break
    185          else:
    186              raise os.error, str(argv0) + ": DataLoader could not find " + str(filenames)
    187          ##print path, self.path, self.dir
    188
    189
    190 -    def __findFile(self, func, filename, subdirs=None, do_raise=1):
    191          if subdirs is None:
    192              subdirs = ("",)
    193          elif type(subdirs) is types.StringType:
    194              subdirs = (subdirs,)
    195          for dir in subdirs:
    196              f = os.path.join(self.dir, dir, filename)
    197              f = os.path.normpath(f)
    198              if func(f):
    199                  return f
    200          if do_raise:
    201              raise os.error, "DataLoader could not find " + filename + " in " + self.dir + " " + str(subdirs)
    202          return None
    203
    204 -    def findFile(self, filename, subdirs=None):
    205          return self.__findFile(os.path.isfile, filename, subdirs)
    206
    207 -    def findImage(self, filename, subdirs=None):
    208          for ext in IMAGE_EXTENSIONS:
    209              f = self.__findFile(os.path.isfile, filename+ext, subdirs, 0)
    210              if f:
    211                  return f
    212          raise os.error, "DataLoader could not find image " + filename + " in " + self.dir + " " + str(subdirs)
    213
    214 -    def findIcon(self, filename=None, subdirs=None):
    215          if not filename:
    216              filename = string.lower(PACKAGE)
    217          root, ext = os.path.splitext(filename)
    218          if not ext:
    219              filename = filename + ".xbm"
    220          return self.findFile(filename, subdirs)
    221
    222 -    def findDir(self, filename, subdirs=None):
    223          return self.__findFile(os.path.isdir, filename, subdirs)
    224
    225
    226  # /***********************************************************************
    227  # // memory util/debugging
    228  # ************************************************************************/
    229
    230  cyclops = None
    231