def ParseJSONFile(filename):
    with open(filename) as json_file:
        try:
            return json.loads(json_comment_eater.Nom(json_file.read()))
        except ValueError as e:
            print "%s is not a valid JSON document" % filename
            raise e
Beispiel #2
0
def _Load(filename):
  """Loads a JSON file int a Python object and return this object.
  """
  # TODO(beaudoin): When moving to Python 2.7 use object_pairs_hook=OrderedDict.
  with open(filename, 'r') as handle:
    result = json.loads(json_comment_eater.Nom(handle.read()))
  return result
def _Load(filename):
    """Loads a JSON file into a Python object and return this object.
  """
    with open(filename, 'r') as handle:
        result = json.loads(json_comment_eater.Nom(handle.read()))
    return result
Beispiel #4
0
  Returns:
    A Python object representing the data encoded in the file.

  Raises:
    Exception: If the file could not be read or its contents could not be
        parsed as JSON data.
  """
    try:
        json_file = open(path, 'r')
    except IOError, msg:
        raise Exception("Failed to read the file at %s: %s" % (path, msg))

    try:
        json_str = json_file.read()
        json_obj = json.loads(json_comment_eater.Nom(json_str), encoding)
    except ValueError, msg:
        raise Exception("Failed to parse JSON out of file %s: %s" %
                        (path, msg))
    finally:
        json_file.close()

    return json_obj


def parse_idl_file(path):
    """ Load the specified file and parse it as IDL.

  Args:
    path: Path to a file containing JSON-encoded data.
  """
Beispiel #5
0
def Load(filename):
  with open(filename, 'r') as handle:
    return DeleteNocompileNodes(
        json.loads(json_comment_eater.Nom(handle.read())))
Beispiel #6
0
def Load(filename):
    with open(filename, 'r') as handle:
        schemas = json.loads(json_comment_eater.Nom(handle.read()))
    schema_util.PrefixSchemasWithNamespace(schemas)
    return schemas
Beispiel #7
0
 def Parse(json_str):
     return simplejson.loads(json_comment_eater.Nom(json_str),
                             object_pairs_hook=OrderedDict)
Beispiel #8
0
def main():
    parser = argparse.ArgumentParser(
        description="Generates a C++ constant containing a catalog manifest.")
    parser.add_argument("--root-manifest")
    parser.add_argument("--submanifest-info")
    parser.add_argument("--output-filename-base")
    parser.add_argument("--output-function-name")
    parser.add_argument("--module-path")
    args, _ = parser.parse_known_args()

    if args.submanifest_info is None:
        raise Exception("--submanifest-info required")
    if args.output_filename_base is None:
        raise Exception("--output-filename-base is required")
    if args.output_function_name is None:
        raise Exception("--output-function-name is required")
    if args.module_path is None:
        args.module_path = args.output_filename_base

    if args.root_manifest:
        with open(args.root_manifest, "r") as input_file:
            root_manifest = json.loads(
                json_comment_eater.Nom(input_file.read()))
    else:
        root_manifest = None

    qualified_function_name = args.output_function_name.split("::")
    namespaces = qualified_function_name[0:-1]
    function_name = qualified_function_name[-1]

    def raise_error(error, value):
        raise Exception(error)

    overlays = []
    packaged_services = []
    with open(args.submanifest_info, "r") as info_file:
        for line in info_file.readlines():
            submanifest_type, namespace_file, header_base = line.strip().split(
                "@", 3)
            with open(namespace_file, "r") as namespace_file:
                namespace = namespace_file.readline().strip()
            info = {"namespace": namespace, "header": header_base + ".h"}
            if submanifest_type == "overlay":
                overlays.append(info)
            else:
                packaged_services.append(info)

    global_vars = {
        "root_manifest": root_manifest,
        "overlays": overlays,
        "packaged_services": packaged_services,
        "function_name": function_name,
        "namespaces": namespaces,
        "path": args.module_path,
        "raise": raise_error,
    }

    input_h_filename = _H_FILE_TEMPLATE
    output_h_filename = "%s.h" % args.output_filename_base
    ApplyTemplate(input_h_filename, output_h_filename, global_vars)

    input_cc_filename = _CC_FILE_TEMPLATE
    output_cc_filename = "%s.cc" % args.output_filename_base
    ApplyTemplate(input_cc_filename, output_cc_filename, global_vars)

    return 0