def load_topology(path):
    """
    Open a topology file, patch it for last GNS3 release and return it
    """
    log.debug("Read topology %s", path)
    try:
        with open(path, encoding="utf-8") as f:
            topo = json.load(f)
    except (OSError, UnicodeDecodeError, ValueError) as e:
        raise aiohttp.web.HTTPConflict(text="Could not load topology {}: {}".format(path, str(e)))

    if topo.get("revision", 0) > GNS3_FILE_FORMAT_REVISION:
        raise aiohttp.web.HTTPConflict(text="This project is designed for a more recent version of GNS3 please update GNS3 to version {} or later".format(topo["version"]))

    changed = False
    if "revision" not in topo or topo["revision"] < GNS3_FILE_FORMAT_REVISION:
        # If it's an old GNS3 file we need to convert it
        # first we backup the file
        try:
            shutil.copy(path, path + ".backup{}".format(topo.get("revision", 0)))
        except (OSError) as e:
            raise aiohttp.web.HTTPConflict(text="Can't write backup of the topology {}: {}".format(path, str(e)))
        changed = True

    if "revision" not in topo or topo["revision"] < 5:
        topo = _convert_1_3_later(topo, path)

    # Version before GNS3 2.0 alpha 4
    if topo["revision"] < 6:
        topo = _convert_2_0_0_alpha(topo, path)

    # Version before GNS3 2.0 beta 3
    if topo["revision"] < 7:
        topo = _convert_2_0_0_beta_2(topo, path)

    # Version before GNS3 2.1
    if topo["revision"] < 8:
        topo = _convert_2_0_0(topo, path)

    try:
        _check_topology_schema(topo)
    except aiohttp.web.HTTPConflict as e:
        log.error("Can't load the topology %s", path)
        raise e

    if changed:
        try:
            with open(path, "w+", encoding="utf-8") as f:
                json.dump(topo, f, indent=4, sort_keys=True)
        except (OSError) as e:
            raise aiohttp.web.HTTPConflict(text="Can't write the topology {}: {}".format(path, str(e)))
    return topo
def load_topology ( path ) : log . debug ( "Read topology %s" , path ) try : with open ( path , encoding = "utf-8" ) as f : topo = json . load ( f ) except ( OSError , UnicodeDecodeError , ValueError ) as e : raise aiohttp . web . HTTPConflict ( text = "Could not load topology {}: {}" . format ( path , str ( e ) ) ) if topo . get ( "revision" , 0 ) > GNS3_FILE_FORMAT_REVISION : raise aiohttp . web . HTTPConflict ( text = "This project is designed for a more recent version of GNS3 please update GNS3 to version {} or later" . format ( topo [ "version" ] ) ) changed = False if "revision" not in topo or topo [ "revision" ] < GNS3_FILE_FORMAT_REVISION : try : shutil . copy ( path , path + ".backup{}" . format ( topo . get ( "revision" , 0 ) ) ) except ( OSError ) as e : raise aiohttp . web . HTTPConflict ( text = "Can't write backup of the topology {}: {}" . format ( path , str ( e ) ) ) changed = True if "revision" not in topo or topo [ "revision" ] < 5 : topo = _convert_1_3_later ( topo , path ) if topo [ "revision" ] < 6 : topo = _convert_2_0_0_alpha ( topo , path ) if topo [ "revision" ] < 7 : topo = _convert_2_0_0_beta_2 ( topo , path ) if topo [ "revision" ] < 8 : topo = _convert_2_0_0 ( topo , path ) try : _check_topology_schema ( topo ) except aiohttp . web . HTTPConflict as e : log . error ( "Can't load the topology %s" , path ) raise e if changed : try : with open ( path , "w+" , encoding = "utf-8" ) as f : json . dump ( topo , f , indent = 4 , sort_keys = True ) except ( OSError ) as e : raise aiohttp . web . HTTPConflict ( text = "Can't write the topology {}: {}" . format ( path , str ( e ) ) ) return topo
Open a topology file, patch it for last GNS3 release and return it