File: goto.py

package info (click to toggle)
golly 2.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 9,560 kB
  • ctags: 5,064
  • sloc: cpp: 38,119; python: 3,203; perl: 1,121; makefile: 58; java: 49; sh: 22
file content (99 lines) | stat: -rw-r--r-- 3,255 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Go to a requested generation.  The given generation can be an
# absolute number like 1,000,000 (commas are optional) or a number
# relative to the current generation like +9 or -6.  If the target
# generation is less than the current generation then we go back
# to the starting generation (normally 0) and advance to the target.
# Authors: Andrew Trevorrow and Dave Greene, April 2006.
# Updated Sept-Oct 2006 -- XRLE support and reusable default value.

from glife import validint
from time import time
import os
import golly

# --------------------------------------------------------------------

def goto(gen):
   currgen = int(golly.getgen())
   if gen[0] == '+':
      n = int(gen[1:])
      newgen = currgen + n
   elif gen[0] == '-':
      n = int(gen[1:])
      if currgen > n:
         newgen = currgen - n
      else:
         newgen = 0
   else:
      newgen = int(gen)
   
   if newgen < currgen:
      # try to go back to starting gen (not necessarily 0) and
      # then forwards to newgen; note that reset() also restores
      # algorithm and/or rule, so too bad if user changed those
      # after the starting info was saved;
      # first save current location and scale
      midx, midy = golly.getpos()
      mag = golly.getmag()
      golly.reset()
      # restore location and scale
      golly.setpos(midx, midy)
      golly.setmag(mag)
      # current gen might be > 0 if user loaded a pattern file
      # that set the gen count
      currgen = int(golly.getgen())
      if newgen < currgen:
         golly.error("Can't go back any further; pattern was saved "\
                     + "at generation " + str(currgen) + ".")
         return
   
   golly.show("Hit escape to abort...")
   oldsecs = time()
   while currgen < newgen:
      if golly.empty():
         golly.show("Pattern is empty.")
         return
      # using golly.step() as a faster method for getting to newgen
      # is left as an exercise for the reader :)
      golly.run(1)
      currgen += 1
      golly.dokey( golly.getkey() )    # allow keyboard interaction
      newsecs = time()
      if newsecs - oldsecs >= 1.0:     # do an update every sec
         oldsecs = newsecs
         golly.update()
   golly.show("")

# --------------------------------------------------------------------

# use same file name as in goto.pl
GotoINIFileName = golly.getdir("data") + "goto.ini"
previousgen = ""
if os.access(GotoINIFileName, os.R_OK):
   f = open(GotoINIFileName, 'r')
   previousgen = f.readline()
   f.close()
   if not validint(previousgen):
      previousgen = ""

gen = golly.getstring("Enter the desired generation number,\n" +
                      "or -n/+n to go back/forwards by n:",
                      previousgen, "Go to generation")
if len(gen) == 0:
   golly.exit()
elif gen == "+" or gen == "-":
   # clear the default
   previousgen = ""
elif not validint(gen):
   golly.exit('Sorry, but "' + gen + '" is not a valid integer.')
else:
   previousgen = gen
   goto(gen.replace(",",""))

if os.access(GotoINIFileName, os.W_OK) or not os.access(GotoINIFileName, os.F_OK):
   try:
      f = open(GotoINIFileName, 'w')
      f.write(previousgen)
      f.close()
   except:
      golly.show("Unable to save defaults to " + GotoINIFileName)