-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatefile.py
33 lines (28 loc) · 1013 Bytes
/
statefile.py
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
#!/usr/bin/python
# - coding: utf-8 -
import os, codecs
_PRINT = True
_statefile = '/tmp/allemeineentchen.state'
def set(statefile, statefiledir='/tmp'):
global _statefile
_statefile = ((statefiledir + os.path.sep) if not os.path.sep in statefile else "") + statefile
try:
with codecs.open(_statefile, "a", "utf-8") as f:
pass
except Exception as e:
if _PRINT: print(("Could not create statefile %s: %s" % (_statefile, e)))
def has(s):
states = []
try:
with codecs.open(_statefile, "r", "utf-8") as f:
states = [ l.strip() for l in f.readlines() ]
except Exception as e:
if _PRINT: print(("Could not read statefile %s: %s" % (_statefile, e)))
return False
return s.strip() in states
def save(s):
try:
with codecs.open(_statefile, "a", "utf-8") as f:
f.write(s.strip() + "\n")
except Exception as e:
if _PRINT: print(("Could not write statefile %s: %s" % (_statefile, e)))