| 20 | import shutil |
| 21 | from .utilities import bash |
| 22 | class configFileOps: |
| 23 | class entry: |
| 24 | def __init__(self, name, value, op, separator): |
| 25 | self.name = name |
| 26 | self.value = value |
| 27 | self.state = "new" |
| 28 | self.op = op |
| 29 | self.separator = separator |
| 30 | def setState(self, state): |
| 31 | self.state = state |
| 32 | def getState(self): |
| 33 | return self.state |
| 34 | |
| 35 | def __init__(self, fileName, cfg=None): |
| 36 | self.fileName = fileName |
| 37 | self.entries = [] |
| 38 | self.backups = [] |
| 39 | |
| 40 | if cfg is not None: |
| 41 | cfg.cfoHandlers.append(self) |
| 42 | |
| 43 | def addEntry(self, name, value, separator="="): |
| 44 | e = self.entry(name, value, "add", separator) |
| 45 | self.entries.append(e) |
| 46 | |
| 47 | def rmEntry(self, name, value, separator="="): |
| 48 | entry = self.entry(name, value, "rm", separator) |
| 49 | self.entries.append(entry) |
| 50 | |
| 51 | def getEntry(self, name, separator="="): |
| 52 | try: |
| 53 | ctx = open(self.fileName).read(-1) |
| 54 | match = re.search("^" + name + ".*", ctx, re.MULTILINE) |
| 55 | if match is None: |
| 56 | return "" |
| 57 | line = match.group(0).split(separator, 1) |
| 58 | return line[1] |
| 59 | except: |
| 60 | return "" |
| 61 | |
| 62 | def save(self): |
| 63 | newLines = [] |
| 64 | if os.path.exists(self.fileName) and os.path.isfile(self.fileName): |
| 65 | fp = open(self.fileName, "r") |
| 66 | for line in fp.readlines(): |
| 67 | matched = False |
| 68 | for entry in self.entries: |
| 69 | if entry.op == "add": |
| 70 | if entry.separator == "=": |
| 71 | matchString = r"^\ *" + entry.name + ".*" |
| 72 | elif entry.separator == " ": |
| 73 | matchString = r"^\ *" + entry.name + r"\ *" + entry.value |
| 74 | else: |
| 75 | if entry.separator == "=": |
| 76 | matchString = r"^\ *" + entry.name + r"\ *=\ *" + entry.value |
| 77 | else: |
| 78 | matchString = r"^\ *" + entry.name + r"\ *" + entry.value |
| 79 |
no outgoing calls
no test coverage detected