From 4df8e83808791d7aa15a2abdba59118e0014db13 Mon Sep 17 00:00:00 2001 From: Philipp Hahn Date: Mon, 27 Apr 2020 06:45:54 +0200 Subject: generator: Convert to defaultdict() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit for nested dictionaries allows to remove the case distinction for "first" from "all other" cases. Signed-off-by: Philipp Hahn Reviewed-by: Ján Tomko --- generator.py | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) (limited to 'generator.py') diff --git a/generator.py b/generator.py index c0eb588..8b9b4b8 100755 --- a/generator.py +++ b/generator.py @@ -8,13 +8,14 @@ import re import sys import xml.sax from contextlib import closing +from collections import defaultdict functions = {} lxc_functions = {} qemu_functions = {} -enums = {} # { enumType: { enumConstant: enumValue } } -lxc_enums = {} # { enumType: { enumConstant: enumValue } } -qemu_enums = {} # { enumType: { enumConstant: enumValue } } +enums = defaultdict(dict) # { enumType: { enumConstant: enumValue } } +lxc_enums = defaultdict(dict) # { enumType: { enumConstant: enumValue } } +qemu_enums = defaultdict(dict) # { enumType: { enumConstant: enumValue } } event_ids = [] params = [] # [ (paramName, paramValue)... ] @@ -186,8 +187,6 @@ def lxc_function(name, desc, ret, args, file, module, cond): def enum(type, name, value): - if type not in enums: - enums[type] = {} if (name.startswith('VIR_DOMAIN_EVENT_ID_') or name.startswith('VIR_NETWORK_EVENT_ID_')): event_ids.append(name) @@ -215,16 +214,12 @@ def enum(type, name, value): def lxc_enum(type, name, value): - if type not in lxc_enums: - lxc_enums[type] = {} if onlyOverrides and name not in lxc_enums[type]: return lxc_enums[type][name] = value def qemu_enum(type, name, value): - if type not in qemu_enums: - qemu_enums[type] = {} if value == 'VIR_DOMAIN_AGENT_RESPONSE_TIMEOUT_BLOCK': value = -2 elif value == 'VIR_DOMAIN_AGENT_RESPONSE_TIMEOUT_DEFAULT': @@ -342,7 +337,7 @@ py_types = { 'const virDomainSnapshot *': ('O', "virDomainSnapshot", "virDomainSnapshotPtr", "virDomainSnapshotPtr"), } -unknown_types = {} +unknown_types = defaultdict(list) ####################################################################### # @@ -701,11 +696,7 @@ def print_function_wrapper(module, name, output, export, include): else: if a_type in skipped_types: return 0 - if a_type in unknown_types: - lst = unknown_types[a_type] - lst.append(name) - else: - unknown_types[a_type] = [name] + unknown_types[a_type].append(name) return -1 if format: format += ":%s" % (name) @@ -738,11 +729,7 @@ def print_function_wrapper(module, name, output, export, include): else: if r_type in skipped_types: return 0 - if r_type in unknown_types: - lst = unknown_types[r_type] - lst.append(name) - else: - unknown_types[r_type] = [name] + unknown_types[r_type].append(name) return -1 if cond: -- cgit v1.2.1