forked from FUEL4EP/consumption_analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_module.py
74 lines (65 loc) · 2.21 KB
/
io_module.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
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
#io_module.py
# -*- coding: utf-8 -*-
# $Rev: 42 $:
# $Author: ewald $:
# $Date: 2013-03-03 11:30:40 +0100 (So, 03. Mär 2013) $:
# $Id: io_module.py 42 2013-03-03 10:30:40Z ewald $
__version__ = "$Revision: 42 $"
import io
import os
from messaging import stdMsg, dbgMsg, warnMsg, errMsg, setDebugging
import sys
import re
def isWritable(directory):
try:
tmp_prefix = "tmp_file_for_write_testing";
count = 0
filename = os.path.join(directory, tmp_prefix)
while(os.path.exists(filename)):
filename = "{}.{}".format(os.path.join(directory, tmp_prefix),count)
count = count + 1
f = open(filename,"w")
f.close()
os.remove(filename)
return True
except Exception as e:
dbgMsg("specified working directory \'%s\' is not writable!\n" % directory)
return False
def check_working_directory (directory):
dbgMsg("checking working directory %s" % directory)
if not os.path.exists(directory):
errMsg("specified working directory \'%s\' does not exist!\n" % directory)
sys.exit(1)
if not os.path.isdir(directory):
errMsg("specified working directory \'%s\' is not a directory!\n" % directory)
sys.exit(1)
if not isWritable(directory):
errMsg("specified working directory \'%s\' is not writable!\n" % directory)
sys.exit()
return True
def check_database_file (directory, file_name):
fn = directory+r'/'+ file_name
if not os.path.isfile(fn):
errMsg("Specified data base file \'%s\' does not exist!\n\n" % fn)
sys.exit(1)
if not os.access(fn, os.R_OK):
errMsg("Specified data base file \'%s\' is not readable!\n\n" % fn)
sys.exit(1)
if os.stat(fn).st_size==0:
errMsg("Specified data base file \'%s\' has size of 0!\n\n" % fn)
sys.exit(1)
return True
def check_float(string):
floatpattern=r'^[+-]?(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?$'
m=re.match(floatpattern, string)
if m is not None:
return True
else:
return False
def input_float(string):
done=False
while not done:
input=raw_input(string)
done=check_float(input)
fp=float(input)
return fp