File: json_source.py

package info (click to toggle)
crazy-complete 0.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 2,404 kB
  • sloc: python: 7,949; sh: 4,636; makefile: 74
file content (24 lines) | stat: -rw-r--r-- 657 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
This module provides functions for creating CommandLine objects from JSON
and vice versa.
"""

import json

from . import dictionary_source


def load_from_file(file):
    '''Load a JSON file and turn it into a cli.CommandLine object.'''

    with open(file, 'r', encoding='utf-8') as fh:
        dictionaries = json.load(fh)
        return dictionary_source.dictionaries_to_commandline(dictionaries)


def commandline_to_json(commandline):
    '''Convert a cli.CommandLine object into JSON.'''

    dictionaries = dictionary_source.commandline_to_dictionaries(commandline)
    json_string = json.dumps(dictionaries, indent=None)
    return json_string