1 from os
.path
import abspath
, exists
4 from config
import OUTPUT_DIR
6 from utils
.colors
import *
9 """Utility class to generate a randomly named (without collisions)
10 file to output i.e. pattern files from pachi. """
13 def __init__(self
, filename
, create_empty
=False):
14 self
.filename
= filename
16 assert not self
.exists()
17 open(self
.filename
,'w').close()
19 def exists(self
, warn
=False):
20 status
= exists(self
.filename
)
21 if not status
and warn
:
22 logging
.warn("File '%s' does not exist."%(self
.filename
,))
26 return "ResultFile('%s')"%(self
.filename
,)
28 def get_random_output_base(sub_len
=3, levels
=2):
29 h
= misc
.unique_hash()
30 assert len(h
) > levels
* sub_len
33 for x
in xrange(levels
):
34 l
.append( h
[ x
* sub_len
: (x
+1) * sub_len
] )
37 if not os
.path
.isdir(d
):
39 return os
.path
.join(d
, h
)
41 def get_output_resultfile(suffix
=''):
42 ret
= ResultFile( get_random_output_base() + suffix
)
44 raise RuntimeError("New output result file '%s' already exists, unique hash not really unique..."%(ret))
47 def get_output_resultpair(suffix
=''):
48 basename
= get_random_output_base()
49 ret1
= ResultFile(basename
+ '_B' + suffix
)
50 ret2
= ResultFile(basename
+ '_W' + suffix
)
51 rettup
= BlackWhite(ret1
, ret2
)
54 raise RuntimeError("New output result file '%s' already exists, unique hash not really unique..."%(ret))