Debian Codegraph @

crazy_complete/json_source.py

raw
1 # SPDX-License-Identifier: GPL-3.0-or-later
2 # Copyright (C) 2025-2026 Benjamin Abendroth <[email protected]>
3
4 """
5 This module provides functions for creating CommandLine objects from JSON
6 and vice versa.
7 """
8
9 import json
10
11 from . import dictionary_source
12
13
14 def load_from_file(file):
15 '''Load a JSON file and turn it into a cli.CommandLine object.'''
16
17 with open(file, 'r', encoding='utf-8') as fh:
18 dictionaries = json.load(fh)
19 return dictionary_source.dictionaries_to_commandline(dictionaries)
20
21
22 def commandline_to_json(commandline):
23 '''Convert a cli.CommandLine object into JSON.'''
24
25 dictionaries = dictionary_source.commandline_to_dictionaries(commandline)
26 json_string = json.dumps(dictionaries, indent=None)
27 return json_string