-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_converter.py
83 lines (76 loc) · 2.54 KB
/
config_converter.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Converts Text simulation config created by STELA to Shlex configs.
"""
import io
import re
import sys
from logger import Logger
from version import Version
class Converter:
def __init__(self, log_level="ERROR"):
"""
Initial Model adds Loging
"""
logger = Logger(log_level)
self.log = logger.get_logger()
def read_file(self, file_address):
"""
Reads the file, if not found breaks and logs an
ERROR and breaks the program.
"""
try:
f = open(file_address, "r+")
except IOError as e:
self.log.error("I/O error({0}): {1}".format(e.errno, e.strerror))
raise
return f
def get_file_container(self, f):
"""
Get the open file as a dict of extension and name
"""
file_name = f.name
name_ext = file_name.split(".")
ext = name_ext[len(name_ext) - 1]
del name_ext[-1]
name = ".".join(name_ext)
file_container = {"name": name, "ext": ext}
return file_container
def convert_file(self, file_address, written_file_name=False):
"""
Get the txt file and convert it to the
shelx config file, returns the generated
config file name as string. suffix will be added
if a file with a same name exist.
"""
version = Version()
version_tag = version.get_version()
f = self.read_file(file_address)
if(written_file_name):
written_file = open(written_file_name, 'w')
else:
written_file = open(
self.get_file_container(f)["name"] + ".cfg", 'w')
file_name = written_file.name
written_file.write(
"#Config file generated by SatGen V. " + version_tag)
for line in list(f):
if(not re.search("Date", line)):
line = re.sub(":", "=", line)
if(re.search("Date", line)):
line = re.sub("(?<=[^0-9]):", "=", line.strip())
line = re.sub("\[ ", "[", line)
line = re.sub(" \]", "]", line)
line = line.strip()
line = line + "\n"
try:
written_file.write(line)
self.log.debug(written_file.name + " is written to the disk!")
except IOError as e:
self.log.error(
"I/O error({0}): {1}".format(e.errno, e.strerror))
raise
written_file.close()
f.close
return file_name