commit:     e40f31c10a79499a822596948b68fcf6ea3f647f
Author:     Matt Turner <mattst88 <AT> gentoo <DOT> org>
AuthorDate: Mon Nov  2 17:43:34 2020 +0000
Commit:     Matt Turner <mattst88 <AT> gentoo <DOT> org>
CommitDate: Fri Jan 29 23:48:02 2021 +0000
URL:        https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=e40f31c1

catalyst: Switch spec files to TOML

Signed-off-by: Matt Turner <mattst88 <AT> gentoo.org>

 catalyst/config.py | 120 -----------------------------------------------------
 catalyst/main.py   |  26 ++++--------
 2 files changed, 9 insertions(+), 137 deletions(-)

diff --git a/catalyst/config.py b/catalyst/config.py
deleted file mode 100644
index e1963f71..00000000
--- a/catalyst/config.py
+++ /dev/null
@@ -1,120 +0,0 @@
-
-import re
-
-from catalyst import log
-from catalyst.support import CatalystError
-
-
-class ParserBase():
-
-    filename = ""
-    lines = None
-    values = None
-    key_value_separator = "="
-    multiple_values = False
-    empty_values = True
-    eval_none = False
-
-    def __getitem__(self, key):
-        return self.values[key]
-
-    def get_values(self):
-        return self.values
-
-    def dump(self):
-        dump = ""
-        for x in self.values:
-            dump += x + " = " + repr(self.values[x]) + "\n"
-        return dump
-
-    def parse_file(self, filename):
-        try:
-            with open(filename, "r") as myf:
-                self.lines = myf.readlines()
-        except:
-            raise CatalystError("Could not open file " + filename,
-                                print_traceback=True)
-        self.filename = filename
-        self.parse()
-
-    def parse_lines(self, lines):
-        self.lines = lines
-        self.parse()
-
-    def parse(self):
-        values = {}
-        cur_array = []
-
-        trailing_comment = re.compile(r'\s*#.*$')
-
-        for x, myline in enumerate(self.lines):
-            myline = myline.strip()
-
-            # Force the line to be clean
-            # Remove Comments ( anything following # )
-            myline = trailing_comment.sub("", myline)
-
-            # Skip any blank lines
-            if not myline:
-                continue
-
-            if self.key_value_separator in myline:
-                # Split on the first occurence of the separator creating two 
strings in the array mobjs
-                mobjs = myline.split(self.key_value_separator, 1)
-                mobjs[1] = mobjs[1].strip().strip('"')
-
-                # Start a new array using the first element of mobjs
-                cur_array = [mobjs[0]]
-                if mobjs[1]:
-                    # do any variable substitiution embeded in it with
-                    # the values already obtained
-                    mobjs[1] = mobjs[1] % values
-                    if self.multiple_values:
-                        # split on white space creating additional array 
elements
-                        subarray = mobjs[1].split()
-                        cur_array += subarray
-                    else:
-                        cur_array += [mobjs[1]]
-
-            # Else add on to the last key we were working on
-            else:
-                if self.multiple_values:
-                    cur_array += myline.split()
-                else:
-                    raise CatalystError("Syntax error: %s" %
-                                        x, print_traceback=True)
-
-            # XXX: Do we really still need this "single value is a string" 
behavior?
-            if len(cur_array) == 2:
-                values[cur_array[0]] = cur_array[1]
-            else:
-                values[cur_array[0]] = cur_array[1:]
-
-        if not self.empty_values:
-            # Make sure the list of keys is static since we modify inside the 
loop.
-            for x in list(values.keys()):
-                # Delete empty key pairs
-                if not values[x]:
-                    log.warning('No value set for key "%s"; deleting', x)
-                    del values[x]
-
-        if self.eval_none:
-            # Make sure the list of keys is static since we modify inside the 
loop.
-            for x in list(values.keys()):
-                # reset None values
-                if isinstance(values[x], str) and values[x].lower() in 
['none']:
-                    log.info('None value found for key "%s"; reseting', x)
-                    values[x] = None
-        self.values = values
-
-
-class SpecParser(ParserBase):
-
-    key_value_separator = ':'
-    multiple_values = True
-    empty_values = False
-    eval_none = True
-
-    def __init__(self, filename=""):
-        if filename:
-            self.parse_file(filename)

diff --git a/catalyst/main.py b/catalyst/main.py
index 0de1040f..81495c62 100644
--- a/catalyst/main.py
+++ b/catalyst/main.py
@@ -13,7 +13,6 @@ from DeComp.definitions import (COMPRESS_DEFINITIONS, 
DECOMPRESS_DEFINITIONS,
 from DeComp.contents import ContentsMap
 
 from catalyst import log
-import catalyst.config
 from catalyst.context import namespace
 from catalyst.defaults import (confdefaults, option_messages,
                                DEFAULT_CONFIG_FILE, valid_config_file_values)
@@ -276,11 +275,6 @@ def _main(parser, opts):
         myconfigs = [DEFAULT_CONFIG_FILE]
     myspecfile = opts.file
 
-    mycmdline = list()
-    if opts.snapshot:
-        mycmdline.append('target: snapshot')
-        mycmdline.append('snapshot_treeish: ' + opts.snapshot)
-
     conf_values['DEBUG'] = opts.debug
     conf_values['VERBOSE'] = opts.debug or opts.verbose
 
@@ -299,7 +293,7 @@ def _main(parser, opts):
         options.append('enter-chroot')
 
     # Make sure we have some work before moving further.
-    if not myspecfile and not mycmdline:
+    if not myspecfile and not opts.snapshot:
         parser.error('please specify one of either -f or -C or -s')
 
     # made it this far so start by outputting our version info
@@ -320,7 +314,6 @@ def _main(parser, opts):
     # initialize our (de)compression definitions
     conf_values['decompress_definitions'] = DECOMPRESS_DEFINITIONS
     conf_values['compress_definitions'] = COMPRESS_DEFINITIONS
-    # TODO add capability to config/spec new definitions
 
     if "digests" in conf_values:
         valid_digests = hashlib.algorithms_available
@@ -338,16 +331,15 @@ def _main(parser, opts):
 
     if myspecfile:
         log.notice("Processing spec file: %s", myspecfile)
-        spec = catalyst.config.SpecParser(myspecfile)
-        addlargs.update(spec.get_values())
-
-    if mycmdline:
         try:
-            cmdline = catalyst.config.SpecParser()
-            cmdline.parse_lines(mycmdline)
-            addlargs.update(cmdline.get_values())
-        except CatalystError:
-            log.critical('Could not parse commandline')
+            addlargs.update(toml.load(myspecfile))
+        except Exception as e:
+            log.critical('Could not find parse spec file: %s: %s',
+                         myspecfile, e)
+
+    if opts.snapshot:
+        addlargs['target'] = 'snapshot'
+        addlargs['snapshot_treeish'] = opts.snapshot
 
     if "target" not in addlargs:
         raise CatalystError("Required value \"target\" not specified.")

Reply via email to