-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConfigFileManager.py
90 lines (73 loc) · 3.76 KB
/
ConfigFileManager.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#! /usr/bin/env python3.4
########################################################################
# #
# #
# #
# Known issues: #
# #
# #
# #
# This script was written by Thomas Heavey in 2015. #
# #
# Copyright 2015 Thomas J. Heavey IV #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or #
# implied. #
# See the License for the specific language governing permissions and #
# limitations under the License. #
# #
########################################################################
# Import Statements
import os # Generally good for referencing files
# todo define as a class?
def configmanager(basename, filename=".config"):
"""Function for managing a file for the current state of a
calculation.
.config is the file name by default
It has a structure of keyword (whitespace) value on each line
lines starting with # are completely ignored, but also unrecognized
keywords are currently not interpretted as anything (I think)."""
configexists = checkforconfig(filename)
# todo print if one exists, and what is being done about it
pass
# todo Obviously need to do this
def checkforconfig(filename):
"""Return True if one exists, False if not.
Uses os"""
return(os.path.isfile(filename))
def makenewconfig(basename, filename, **starting_values):
""""""
with open(filename, 'x') as file:
file.write('basename {}\n'.format(basename))
for key in starting_values:
file.write('{} {}'.format(key, starting_values[key]))
print("Wrote new configuration file to {}".format(filename))
def readconfig(filename):
""""""
filedata = dict()
with open(filename, 'r') as file:
for line in file:
# take off leading and trailing whitespace
line = line.strip()
# ignore commented lines
if line.startswith("#"):
continue
# add first two parts of each line to the dictionary for
# output as key: value pairs
filedata.update(line.split()[0:1])
print("Read configuration file {}".format(filename))
return(filedata)
def updateconfig(filename, **added_values):
""""""
with open(filename, 'a') as file:
for key in added_values:
file.write('{} {}'.format(key, added_values[key]))