Ejemplo n.º 1
0
def solve(arg, usenext = False):
    """Uses Grid.getSolution() or Grid.getNextSolution() to get a solution.
    Then uses Grid.writeSvg() to export the solution"""
    global current
    ask = config["askConfirm"]
    print("-"*width())
    if current == None :
        raise mio.caught('No Loaded Grid.')
    if usenext:
        s = current.getNextSolution()
        if s == None:
            if current.getSolution() == "UNSAT":
                raise mio.caught("The grid has no solution")
            if not config["restart"] and not mio.readCommand({"y": lambda: True,"n": lambda: False}, 
                "All the solutions of this grid have been explored. Do you want to export the first solution? (y/n)\n", error = 'Enter "y" or "n".', haveArgs = False, width = width()): 
                printw("Aborting. Note: the solutions have been reinitialized, so the next sol. will be the first one.", width = width())
                return
            s = current.getNextSolution()
    else:
        s = current.getSolution()
        if s == "UNSAT":
            raise mio.caught("The grid has no solution")

    arg = arg.lstrip().split(" ",1)
    fname = arg[0]
    if fname == "": fname = inputw("\nIn which file would you like to write the output?\n").strip()
    if not fname.endswith(".svg"): fname += ".svg"

    if ask and os.path.isfile(os.path.join(config["svgOutPath"], fname)) :
        printw("Warning : There already exists a file named",fname, end = ".\n", width = width())
        exit = "n" == mio.readCommand({
            "y": lambda: "y",
            "n": lambda: "n"
        }, "Rewrite the file? (y/n)\n", error = 'Enter "y" or "n".', haveArgs = False, width = width())
        if exit:
            print("Aborting")
            return

    try:
        f = open(os.path.join(config["svgOutPath"], fname), "w") 
        current.writeSvg(f,s)
        f.close()
    except ValueError as e:
        raise mio.caught(str(e))
    else :
        printw("Export done successfully.", width = width())
        openFile = None
        if len(arg) > 1:
            if not arg[1] in ["y","n"]:
                printw("Warning : the argument",repr(arg[1]),"is ignored.", width = width())
            else :
                openFile = arg[1] == "y"
        if openFile == None: 
            openFile = mio.readCommand({
                "y": lambda: True,
                "n": lambda: False
            }, "Do you want to open the file? (y/n)\n", error = 'Enter "y" for yes or "n" for no.', haveArgs = False, width = width())
        if openFile:
            cmd = [config["openSvg"], os.path.join(config["svgOutPath"], fname)]
            subprocess.Popen(cmd,stdin=None, stdout=None, stderr=None, close_fds=True)
Ejemplo n.º 2
0
def load(arg):
    """Uses Grid() to load a grid, and adds it to the grids"""
    global current
    global currentName
    args = arg.lstrip().split(" ",1)
    fname = args[0]
    if fname == "":
        nameLength = 0
        files = []
        for f in os.scandir(config["gridsPath"]) :
            if os.path.isfile(os.path.join(config["gridsPath"], f)) :
                continue
            f = f.name
            files.append(f)
            if nameLength < len(f):
                nameLength = len(f)

        printw("Files available : ", width = width())
        lineLen = 0
        for f in files:
            if lineLen > 0 and lineLen+nameLength > config["width"]:
                print()
                lineLen = 0
            printw(f," "*(nameLength-len(f)), end = " ", width = width())
            lineLen += nameLength+1
        print()
        fname = inputw("\nWhich file would you like to open?\n").strip()

    try:
        f = open(os.path.join(config["gridsPath"], fname)) 
        newGrid = Grid(f)
        f.close()
    except (FileNotFoundError, IsADirectoryError) as e:
        raise mio.caught("Error : file '"+fname+"' not found the directory '"+config["gridsPath"]+"'\n(You can change the directory in the configurations)")
    except ValueError as e:
        raise mio.caught(str(e))
    else : 
        if len(args) > 1:
            gname = args[1]
        else :
            alter = os.path.splitext(os.path.basename(fname))[0]
            gname = inputw("Give the grid a name : (empty for "+repr(alter)+")\n").strip()
            if gname == "": gname = alter
        grids[gname] = newGrid
        printw("The loading ended successfully!", width = width())
        if args[0] == "": 
            printw("Note : next time you can write the command 'load {} {}' to make this same loading.".format(fname, gname), width = width())
        current = grids[gname]
        currentName = gname
Ejemplo n.º 3
0
def iolist(arg):
    """List all the grids"""
    arg = arg.lstrip()
    print("-"*width())
    if len(grids) == 0 :
        raise mio.caught('No Loaded Grid.')
    else:
        printw("Loaded grids :", width = width())
        for k in grids:
            printw(" -> " if grids[k] == current else "    ",k,":",grids[k], width = width())
    if (arg != "") : 
        printw("\nWarning : the '{}' in the command has been ignored.".format(arg), width = width())
Ejemplo n.º 4
0
def switch(arg):
    """Changes current to the chosen grid"""
    arg = arg.lstrip()
    global current
    global currentName
    newGrid = arg
    if newGrid == "":
        iolist("")
        newGrid = inputw("\nWhich is the name of the grid to switch to?\n").strip()
    try:
        current = grids[newGrid]
        currentName = newGrid
        printw("Switch done successfully", width = width())
    except KeyError as e:
        raise mio.caught("Grid "+repr(newGrid)+" not found.")
Ejemplo n.º 5
0
def export(arg):
    """Uses Grid.writeSvg() to export a grid"""
    ask = config["askConfirm"]
    arg = arg.lstrip()
    print("-"*width())
    arg = arg.lstrip().split(" ",1)
    fname = arg[0]
    if fname == "": fname = inputw("\nIn which file would you like to write the output?\n").strip()
    if not fname.endswith(".svg"): fname += ".svg"

    if ask and os.path.isfile(os.path.join(config["svgOutPath"], fname)) :
        printw("Warning : There already exists a file named",fname, end = ".\n", width = width())
        exit = "n" == mio.readCommand({
            "y": lambda: "y",
            "n": lambda: "n"
        }, "Rewrite the file? (y/n)\n", error = 'Enter "y" or "n".', haveArgs = False, width = width())
        if exit:
            print("Aborting")
            return

    try:
        f = open(os.path.join(config["svgOutPath"], fname), "w") 
        current.writeSvg(f,[])
        f.close()
    except ValueError as e:
        raise mio.caught(str(e))
    else :
        printw("Export done successfully.", width = width())
        openFile = None
        if len(arg) > 1:
            if not arg[1] in ["y","n"]:
                printw("Warning : the argument",repr(arg[1]),"is ignored.", width = width())
            else :
                openFile = arg[1] == "y"
        if openFile == None: 
            openFile = mio.readCommand({
                "y": lambda: True,
                "n": lambda: False
            }, "Do you want to open the file? (y/n)\n", error = 'Enter "y" for yes or "n" for no.', haveArgs = False, width = width())
        if openFile:
            cmd = [config["openSvg"], os.path.join(config["svgOutPath"], fname)]
            subprocess.Popen(cmd,stdin=None, stdout=None, stderr=None, close_fds=True)