|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +# MIT License |
| 4 | +# |
| 5 | +# Copyright (c) 2021 Eugenio Parodi <ceccopierangiolieugenio AT googlemail DOT com> |
| 6 | +# |
| 7 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | +# of this software and associated documentation files (the "Software"), to deal |
| 9 | +# in the Software without restriction, including without limitation the rights |
| 10 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | +# copies of the Software, and to permit persons to whom the Software is |
| 12 | +# furnished to do so, subject to the following conditions: |
| 13 | +# |
| 14 | +# The above copyright notice and this permission notice shall be included in all |
| 15 | +# copies or substantial portions of the Software. |
| 16 | +# |
| 17 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | +# SOFTWARE. |
| 24 | + |
| 25 | +__all__ = ['TloggCfg'] |
| 26 | + |
| 27 | +import os |
| 28 | +import yaml |
| 29 | + |
| 30 | +from .. import __version__ |
| 31 | + |
| 32 | +class TloggCfg: |
| 33 | + version=__version__ |
| 34 | + name="tlogg" |
| 35 | + cfgVersion = '1.0' |
| 36 | + pathCfg="." |
| 37 | + colors=[] |
| 38 | + filters=[] |
| 39 | + options={} |
| 40 | + searches=[] |
| 41 | + maxsearches=200 |
| 42 | + |
| 43 | + @staticmethod |
| 44 | + def save(searches=True, filters=True, colors=True, options=True): |
| 45 | + os.makedirs(TloggCfg.pathCfg, exist_ok=True) |
| 46 | + colorsPath = os.path.join(TloggCfg.pathCfg,'colors.yaml') |
| 47 | + filtersPath = os.path.join(TloggCfg.pathCfg,'filters.yaml') |
| 48 | + optionsPath = os.path.join(TloggCfg.pathCfg,'options.yaml') |
| 49 | + searchesPath = os.path.join(TloggCfg.pathCfg,'searches.yaml') |
| 50 | + |
| 51 | + def writeCfg(path, cfg): |
| 52 | + fullCfg = { |
| 53 | + 'version':TloggCfg.cfgVersion, |
| 54 | + 'cfg':cfg } |
| 55 | + with open(path, 'w') as f: |
| 56 | + yaml.dump(fullCfg, f, sort_keys=False, default_flow_style=False) |
| 57 | + |
| 58 | + if colors: writeCfg(colorsPath, TloggCfg.colors) |
| 59 | + if filters: writeCfg(filtersPath, TloggCfg.filters) |
| 60 | + if options: writeCfg(optionsPath, TloggCfg.options) |
| 61 | + if searches: writeCfg(searchesPath, TloggCfg.searches) |
| 62 | + |
| 63 | + @staticmethod |
| 64 | + def load(): |
| 65 | + colorsPath = os.path.join(TloggCfg.pathCfg,'colors.yaml') |
| 66 | + filtersPath = os.path.join(TloggCfg.pathCfg,'filters.yaml') |
| 67 | + optionsPath = os.path.join(TloggCfg.pathCfg,'options.yaml') |
| 68 | + searchesPath = os.path.join(TloggCfg.pathCfg,'searches.yaml') |
| 69 | + |
| 70 | + if os.path.exists(colorsPath): |
| 71 | + with open(colorsPath) as f: |
| 72 | + TloggCfg.colors = yaml.load(f, Loader=yaml.SafeLoader)['cfg'] |
| 73 | + if os.path.exists(filtersPath): |
| 74 | + with open(filtersPath) as f: |
| 75 | + TloggCfg.filters = yaml.load(f, Loader=yaml.SafeLoader)['cfg'] |
| 76 | + if os.path.exists(optionsPath): |
| 77 | + with open(optionsPath) as f: |
| 78 | + TloggCfg.options = yaml.load(f, Loader=yaml.SafeLoader)['cfg'] |
| 79 | + if os.path.exists(searchesPath): |
| 80 | + with open(searchesPath) as f: |
| 81 | + TloggCfg.searches = yaml.load(f, Loader=yaml.SafeLoader)['cfg'] |
0 commit comments