Source code for winspsrc.scripts.generate_source

#!/usr/bin/env python3
"""Script to generate Windows serialized property source code."""

import argparse
import logging
import os
import sys

import winspsrc

from winspsrc import yaml_definitions_file


[docs] def Main(): """Entry point of console script to generate property source code. Returns: int: exit code that is provided to sys.exit(). """ argument_parser = argparse.ArgumentParser( description=("Generated Windows serialized property source code.") ) argument_parser.add_argument( "-f", "--format", dest="format", action="store", metavar="FORMAT", default="python", help="source format.", ) options = argument_parser.parse_args() if options.format not in ("c_header", "c_source", "python"): print("Unsupported format.") print("") argument_parser.print_help() print("") return 1 logging.basicConfig(level=logging.INFO, format="[%(levelname)s] %(message)s") definitions_file = yaml_definitions_file.YAMLPropertiesDefinitionsFile() property_definitions = {} data_path = os.path.join(os.path.dirname(winspsrc.__file__), "data") path = os.path.join(data_path, "defined_properties.yaml") for property_definition in definitions_file.ReadFromFile(path): lookup_key = property_definition.lookup_key if lookup_key in property_definitions: property_definitions[lookup_key].Merge(property_definition) else: property_definitions[lookup_key] = property_definition path = os.path.join(data_path, "observed_properties.yaml") for property_definition in definitions_file.ReadFromFile(path): lookup_key = property_definition.lookup_key if lookup_key in property_definitions: property_definitions[lookup_key].Merge(property_definition) else: property_definitions[lookup_key] = property_definition path = os.path.join(data_path, "third_party_properties.yaml") for property_definition in definitions_file.ReadFromFile(path): lookup_key = property_definition.lookup_key if lookup_key in property_definitions: property_definitions[lookup_key].Merge(property_definition) else: property_definitions[lookup_key] = property_definition if options.format == "python": print("#!/usr/bin/env python") print('"""Windows serialized property to shell property key mappings.') print("") print("This file was generated by the winsps-kb project.") print('"""') print("") print("") print("SHELL_PROPERTY_KEYS = {") first_definition = True for lookup_key, property_definition in sorted(property_definitions.items()): if property_definition.shell_property_keys: if first_definition: first_definition = False else: if options.format == "python": print(",") shell_property_key = list(property_definition.shell_property_keys)[0] if options.format == "python": line = f" '{lookup_key:s}': '{shell_property_key:s}'" if len(line) < 80: print(line, end="") else: print(f" '{lookup_key:s}': (") print(f" '{shell_property_key:s}')", end="") if options.format == "python": print("}") return 0
if __name__ == "__main__": sys.exit(Main())