From: Ondrej Lichtner olichtne@redhat.com
This module will contain helper functions for PerfRepo integration. The first two functions are: parse_id_mapping(filename) get_id(mapping, key)
These will be used to work with mapping files that are required for full support of PerfRepo. The plan is to have this functionality in a single class, but for now these two functions are sufficient.
Signed-off-by: Ondrej Lichtner olichtne@redhat.com --- lnst/Controller/PerfRepoUtils.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lnst/Controller/PerfRepoUtils.py
diff --git a/lnst/Controller/PerfRepoUtils.py b/lnst/Controller/PerfRepoUtils.py new file mode 100644 index 0000000..4ae38b1 --- /dev/null +++ b/lnst/Controller/PerfRepoUtils.py @@ -0,0 +1,33 @@ +""" +This module contains helper functions useful when writing recipes +that use PerfRepo. + +Copyright 2015 Red Hat, Inc. +Licensed under the GNU General Public License, version 2 as +published by the Free Software Foundation; see COPYING for details. +""" + +__author__ = """ +olichtne@redhat.com (Ondrej Lichtner) +""" + +import re + +def parse_id_mapping(filename): + line_re = re.compile(r"^(\w+)\s*=\s*(\w+)$") + res_dict = {} + try: + with open(filename) as f: + for line in f: + match = line_re.match(line) + if match is not None: + res_dict[match.group(1)] = match.group(2) + except: + return None + return res_dict + +def get_id(mapping, key): + try: + return mapping[key] + except (KeyError, TypeError): + return None