<commit_before># -*- coding: utf-8 -*-

"""
file: io_json_format.py

Functions for reading and writing graph files in the lie_graph .jgf JSON format

This is a propitiatory format in which the graph meta-data, the nodes, edges
and their data dictionaries are stored in JSON format.
"""

import json
import logging

from .. import __version__
from lie_graph.graph import Graph
from lie_graph.graph_axis.graph_axis_class import GraphAxis
from lie_graph.graph_io.io_helpers import check_lie_graph_version, open_anything


def read_jgf(jgf_format, graph=None):
    """
    Read JSON graph format (.jgf)

    This is a propitiatory format in which the graph meta-data, the nodes,
    edges and their data dictionaries are stored in JSON format.

    Format description. Primary key/value pairs:
    * graph: Graph class meta-data. Serializes all class attributes of type
             int, float, bool, long, str or unicode.
    * nodes: Graph node identifiers (keys) and attributes (values)
    * edges: Graph enumerated edge identifiers
    * edge_attr: Graph edge attributes

    :param jgf_format:  JSON encoded graph data to parse
    :type jgf_format:   :py:str
    :param graph:       Graph object to import TGF data in
    :type graph:        :lie_graph:Graph

    :return:            Graph object
    :rtype:             Graph or GraphAxis object
    """

    # Try parsing the string using default Python json parser
    jgf_format = open_anything(jgf_format)
    try:
        parsed = json.load(jgf_format)
    except IOError:
        logging.error('Unable to decode JSON string')
        return

    # Check lie_graph version and format validity
    if not check_lie_graph_version(parsed.get('lie_graph_version')):
        return
    keywords = ['graph', 'nodes', 'edges', 'edge_attr']
    if not set(keywords).issubset(set(parsed.keys())):
        logging.error('JSON format does not contain required graph data')
        return

    # Determine graph class to use
    if not isinstance(graph, Graph):
        if parsed['graph'].get('root') is not None:
            graph = GraphAxis()
        else:
            graph = Graph()
<commit_msg>Handle JSON as input to jgf export<commit_after># -*- coding: utf-8 -*-

"""
file: io_json_format.py

Functions for reading and writing graph files in the lie_graph .jgf JSON format

This is a propitiatory format in which the graph meta-data, the nodes, edges
and their data dictionaries are stored in JSON format.
"""

import json
import logging

from .. import __version__
from lie_graph.graph import Graph
from lie_graph.graph_axis.graph_axis_class import GraphAxis
from lie_graph.graph_io.io_helpers import check_lie_graph_version, open_anything


def read_jgf(jgf_format, graph=None):
    """
    Read JSON graph format (.jgf)

    This is a propitiatory format in which the graph meta-data, the nodes,
    edges and their data dictionaries are stored in JSON format.

    Format description. Primary key/value pairs:
    * graph: Graph class meta-data. Serializes all class attributes of type
             int, float, bool, long, str or unicode.
    * nodes: Graph node identifiers (keys) and attributes (values)
    * edges: Graph enumerated edge identifiers
    * edge_attr: Graph edge attributes

    :param jgf_format:  JSON encoded graph data to parse
    :type jgf_format:   :py:str
    :param graph:       Graph object to import TGF data in
    :type graph:        :lie_graph:Graph

    :return:            Graph object
    :rtype:             Graph or GraphAxis object
    """

    # Try parsing the string using default Python json parser
    if isinstance(jgf_format, dict):
        parsed = jgf_format
    else:
        jgf_format = open_anything(jgf_format)
        try:
            parsed = json.load(jgf_format)
        except IOError:
            logging.error('Unable to decode JSON string')
            return

    # Check lie_graph version and format validity
    if not check_lie_graph_version(parsed.get('lie_graph_version')):
        return
    keywords = ['graph', 'nodes', 'edges', 'edge_attr']
    if not set(keywords).issubset(set(parsed.keys())):
        logging.error('JSON format does not contain required graph data')
        return

    # Determine graph class to use
    if not isinstance(graph, Graph):
        if parsed['graph'].get('root') is not None:
            graph = GraphAxis()
        else:
            graph = Graph()

{'fraction_non_alphanumeric': 0.076121431807884, 'fraction_numerical': 0.00045310376076121433, 'mean_word_length': 2.9209591474245116, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5041030', 'n_tokens_mistral': 1259, 'n_tokens_neox': 1202, 'n_words': 468}