Exemplo n.º 1
0
def main():
    serversJSON = PyAl.nonvolatile("servers.json")
    bundleID = PyAl.bundle()

    with open(serversJSON) as f:
        servers = json.load(f)

    if sys.argv[1] == "add":
        name = sys.argv[2]
        user = sys.argv[3]
        pw = sys.argv[4]
        address = sys.argv[5]
        path = sys.argv[6]
        if len(sys.argv) == 8:
            url = sys.argv[7]
            if not url.endswith("/"):
                url = url + "/"
        else:
            url = None

        if not path.endswith("/"):
            path = path + "/"

        serverData = {"user": user, "address": address, "path": path, "url": url}
        servers[name] = serverData

        k = PyAl.Keychain("%s.%s" % (bundleID, address))
        k.storePassword(user, pw)

        with open(serversJSON, "w") as f:
            json.dump(servers, f)

        print "Added %s (%s@%s)" % (name, user, address)

    if sys.argv[1] == "remove":
        name = sys.argv[2]
        delServerData = servers.pop(name)

        k = PyAl.Keychain("%s.%s" % (bundleID, delServerData["address"]))
        k.deletePassword(delServerData["user"])

        with open(serversJSON, "w") as f:
            json.dump(servers, f)

        print "Deleted %s (%s@%s)" % (name, delServerData["user"], delServerData["address"])

    if sys.argv[1] == "finder":
        name = sys.argv[2]
        serverData = servers[name]

        k = PyAl.Keychain("%s.%s" % (bundleID, serverData["address"]))
        serverData["pw"] = k.retrievePassword(serverData["user"])

        pathList = subprocess.check_output(["osascript", "getFinder.applescript"])
        pathList = pathList.split(" , ")

        cleanPaths = []
        for path in pathList:
            if len(path):
                newPath = path[1:]
                if "'" in newPath:
                    newPath = newPath.replace("'", "")
                if ", " in newPath:
                    newPath = newPath.replace(", ", "")
                if "\n" in newPath:
                    newPath = newPath.replace("\n", "")
                cleanPaths.append(newPath)
        doUpload(serverData, cleanPaths)

    if sys.argv[1] == "path":
        name = sys.argv[2]
        pathList = sys.argv[3]

        pathSplit = re.compile(" (?=/|~)")
        pathList = re.split(pathSplit, pathList)

        for path in pathList:
            if "~" in path:
                pathList.remove(path)
                path = os.path.expanduser(path)
                pathList.append(path)

        serverData = servers[name]

        k = PyAl.Keychain("%s.%s" % (bundleID, serverData["address"]))
        serverData["pw"] = k.retrievePassword(serverData["user"])

        doUpload(serverData, pathList)

    if sys.argv[1] == "setdefault":
        name = sys.argv[2]
        serverData = servers[name]

        servers["default"] = serverData

        with open(serversJSON, "w") as f:
            json.dump(servers, f)

        print "Designated %s (%s@%s) default." % (name, serverData["user"], serverData["address"])

    if sys.argv[1] == "default":
        path = sys.argv[2]

        pathList = path.split("\t")

        try:
            serverData = servers["default"]
        except KeyError:
            print "No default server designated."
            return

        k = PyAl.Keychain("%s.%s" % (bundleID, serverData["address"]))
        serverData["pw"] = k.retrievePassword(serverData["user"])

        doUpload(serverData, pathList)
Exemplo n.º 2
0
def main():
    verb = sys.argv[1]

    if verb == "add":
        name = sys.argv[2]
        path = sys.argv[3]

        if "~" in path:
            path = os.path.expanduser(path)

        with open(PyAl.nonvolatile("scratches.json"), "r") as f:
            scratchList = json.load(f)
            f.close()

        if not os.path.isabs(path):
            path = os.path.join(PyAl.nonvolatile(), path)

        if os.path.isdir(path):
            path = os.path.join(path, name)

        scratchList[name] = path

        with open(PyAl.nonvolatile("scratches.json"), "w") as f:
            json.dump(scratchList, f)
            f.close()

        with open(path, "w") as f:
            f.write("\n")
            f.close()

        print "Added %s (%s)." % (name, path)

    elif verb == "remove":
        with open(PyAl.nonvolatile("scratches.json"), "r") as f:
            scratchList = json.load(f)
            f.close()

        try:
            name = sys.argv[2]
            deleted = scratchList.pop(name)
        except Exception as e:
            print "%s does not exist: %s" % (name, e)
            return

        path = deleted
        os.remove(path)

        with open(PyAl.nonvolatile("scratches.json"), "w") as f:
            json.dump(scratchList, f)

            f.close()

        print "Deleted %s (%s)." % (name, path)

    elif verb == "scratch":
        name = sys.argv[2]
        toScratch = sys.argv[3]

        with open(PyAl.nonvolatile("scratches.json"), "r") as f:
            scratchList = json.load(f)
            f.close()
        try:
            path = scratchList[name]
        except Exception as e:
            print "%s does not exist: %s" % (name, e)
            return

        cmd = 'sed -i "" -e "1i\\ \n - %s" "%s"' % (toScratch, path)

        subprocess.call(cmd, shell=True)

        # print "Scratched to %s (%s)." % (name, path)
        print toScratch

    else:
        print "Unknown error in act.py"
Exemplo n.º 3
0
def main():
    serversJSON = PyAl.nonvolatile("servers.json")
    errorItem = I(valid=False, title="No saved servers!", subtitle="You must store a server first.")

    if os.path.exists(serversJSON):
        with open(serversJSON) as f:
            servers = json.load(f)
    else:
        empty = {}
        with open(serversJSON, "w") as f:
            json.dump(empty, f)
        servers = {}

    if len(sys.argv) == 1:
        items = [I(valid=False, autocomplete="add ",
            title="Add Server to Favorites", subtitle="Store server information securely in the Keychain.")]
        if len(servers) > 0:
            items.append(I(valid=False, autocomplete="upload ",
                title="Upload to Server", subtitle="Select a server to upload to."))
            items.append(I(valid=False, autocomplete="remove ",
                title="Remove Server from Favorites", subtitle="Forget a server's information."))
            items.append(I(valid=False, autocomplete="default ",
                title="Set Default Server", subtitle="Designate a default server for file actions."))
        print F(items)

    elif sys.argv[1] == "add":
        if len(sys.argv) == 2:
            name = "[name]"
            user = "******"
            pw = ""
            masked = "[password]"
            address = "[address]"
            path = "[path]"
            url = "[baseurl]"
            item = I(valid=False, title="Save %s" % name, subtitle="%s %s %s %s %s %s" % (name, user, masked, address, path, url))

        elif len(sys.argv) == 3:
            name = sys.argv[2]
            user = "******"
            pw = ""
            masked = "[password]"
            address = "[address]"
            path = "[path]"
            url = "[baseurl]"
            item = I(valid=False, title="Save %s" % name, subtitle="%s %s %s %s %s %s" % (name, user, masked, address, path, url))

        elif len(sys.argv) == 4:
            name = sys.argv[2]
            user = sys.argv[3]
            pw = ""
            masked = "[password]"
            address = "[address]"
            path = "[path]"
            url = "[baseurl]"
            item = I(valid=False, title="Save %s" % name, subtitle="%s %s %s %s %s %s" % (name, user, masked, address, path, url))

        elif len(sys.argv) == 5:
            name = sys.argv[2]
            user = sys.argv[3]
            pw = sys.argv[4]
            masked = u"\u2022" * len(pw)
            address = "[address]"
            path = "[path]"
            url = "[baseurl]"
            item = I(valid=False, title="Save %s" % name, subtitle="%s %s %s %s %s %s" % (name, user, masked, address, path, url))

        elif len(sys.argv) == 6:
            name = sys.argv[2]
            user = sys.argv[3]
            pw = sys.argv[4]
            masked = u"\u2022" * len(pw)
            address = sys.argv[5]
            path = "[path]"
            url = "[baseurl]"
            item = I(valid=False, title="Save %s" % name, subtitle="%s %s %s %s %s %s" % (name, user, masked, address, path, url))

        elif len(sys.argv) == 7:
            name = sys.argv[2]
            user = sys.argv[3]
            pw = sys.argv[4]
            masked = u"\u2022" * len(pw)
            address = sys.argv[5]
            path = sys.argv[6]
            url = "[baseurl]"
            item = I(valid=True, title="Save %s" % name, subtitle="%s %s %s %s %s %s" % (name, user, masked, address, path, url),
                arg="add \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"" % (name, user, pw, address, path))

        elif len(sys.argv) == 8:
            name = sys.argv[2]
            user = sys.argv[3]
            pw = sys.argv[4]
            masked = u"\u2022" * len(pw)
            address = sys.argv[5]
            path = sys.argv[6]
            url = sys.argv[7]
            item = I(valid=True, arg="add \"%s\" \"%s\" \"%s\" \"%s\" \"%s\" \"%s\"" % (name, user, pw, address, path, url),
                title="Save %s" % name, subtitle="%s %s %s %s %s %s" % (name, user, masked, address, path, url))

        print F(item)

    elif sys.argv[1] == "remove":
        if len(servers) < 1:
            items = errorItem
        else:
            items = []
            for server in servers.keys():
                name = server
                serverDict = servers[name]
                address = serverDict["address"]
                user = serverDict["user"]
                items.append(I(valid=True, arg="remove \"%s\"" % name, uid=name,
                    title="Remove " + name, subtitle="%s@%s" % (user, address)))
        print F(items)

    elif sys.argv[1] == "upload":
        if len(servers) < 1:
            items = errorItem
        else:
            if len(sys.argv) == 2:
                items = []
                for server in servers.keys():
                    if server != "default":
                        name = server
                        serverDict = servers[name]
                        address = serverDict["address"]
                        user = serverDict["user"]
                        items.append(I(valid=False, autocomplete="upload %s" % name,
                            title="Upload to %s" % name, subtitle="%s@%s" % (user, address)))
            elif len(sys.argv) == 3:
                name = sys.argv[2]
                items = I(valid=True, arg="finder \"%s\"" % name, uid=name,
                    title="Finder to %s" % name, subtitle="Upload Finder selection to %s." % name)
            elif len(sys.argv) == 4:
                name = sys.argv[2]
                path = sys.argv[3]
                items = I(valid=True, arg="path \"%s\" \"%s\"" % (name, path), uid=name, icon=path, fileIcon=True,
                    title="Path to %s" % name, subtitle="Upload '%s' to %s." % (path, name))

        print F(items)

    elif sys.argv[1] == "default":
        if len(servers) < 1:
            items = errorItem
        else:
            items = []
            for server in servers.keys():
                name = server
                serverDict = servers[name]
                address = serverDict["address"]
                user = serverDict["user"]
                items.append(I(valid=True, arg="setdefault %s" % name,
                    title="Set '%s' as Default" % name, subtitle="%s@%s" % (user, address)))

        print F(items)
Exemplo n.º 4
0
def main():
    serversJSON = PyAl.nonvolatile("servers.json")
    bundleID = PyAl.bundle()

    with open(serversJSON) as f:
        servers = json.load(f)

    if sys.argv[1] == "add":
        name = sys.argv[2]
        user = sys.argv[3]
        pw = sys.argv[4]
        address = sys.argv[5]
        path = sys.argv[6]
        if len(sys.argv) == 8:
            url = sys.argv[7]
            if not url.endswith("/"):
                url = url + "/"
        else:
            url = None

        if not path.endswith("/"):
            path = path + "/"

        serverData = {
            "user": user,
            "address": address,
            "path": path,
            "url": url
        }
        servers[name] = serverData

        k = PyAl.Keychain("%s.%s" % (bundleID, address))
        k.storePassword(user, pw)

        with open(serversJSON, "w") as f:
            json.dump(servers, f)

        print "Added %s (%s@%s)" % (name, user, address)

    if sys.argv[1] == "remove":
        name = sys.argv[2]
        delServerData = servers.pop(name)

        k = PyAl.Keychain("%s.%s" % (bundleID, delServerData["address"]))
        k.deletePassword(delServerData["user"])

        with open(serversJSON, "w") as f:
            json.dump(servers, f)

        print "Deleted %s (%s@%s)" % (name, delServerData["user"],
                                      delServerData["address"])

    if sys.argv[1] == "finder":
        name = sys.argv[2]
        serverData = servers[name]

        k = PyAl.Keychain("%s.%s" % (bundleID, serverData["address"]))
        serverData["pw"] = k.retrievePassword(serverData["user"])

        pathList = subprocess.check_output(
            ["osascript", "getFinder.applescript"])
        pathList = pathList.split(" , ")

        cleanPaths = []
        for path in pathList:
            if len(path):
                newPath = path[1:]
                if "'" in newPath:
                    newPath = newPath.replace("'", "")
                if ", " in newPath:
                    newPath = newPath.replace(", ", "")
                if "\n" in newPath:
                    newPath = newPath.replace("\n", "")
                cleanPaths.append(newPath)
        doUpload(serverData, cleanPaths)

    if sys.argv[1] == "path":
        name = sys.argv[2]
        pathList = sys.argv[3]

        pathSplit = re.compile(" (?=/|~)")
        pathList = re.split(pathSplit, pathList)

        for path in pathList:
            if "~" in path:
                pathList.remove(path)
                path = os.path.expanduser(path)
                pathList.append(path)

        serverData = servers[name]

        k = PyAl.Keychain("%s.%s" % (bundleID, serverData["address"]))
        serverData["pw"] = k.retrievePassword(serverData["user"])

        doUpload(serverData, pathList)

    if sys.argv[1] == "setdefault":
        name = sys.argv[2]
        serverData = servers[name]

        servers["default"] = serverData

        with open(serversJSON, "w") as f:
            json.dump(servers, f)

        print "Designated %s (%s@%s) default." % (name, serverData["user"],
                                                  serverData["address"])

    if sys.argv[1] == "default":
        path = sys.argv[2]

        pathList = path.split("\t")

        try:
            serverData = servers["default"]
        except KeyError:
            print "No default server designated."
            return

        k = PyAl.Keychain("%s.%s" % (bundleID, serverData["address"]))
        serverData["pw"] = k.retrievePassword(serverData["user"])

        doUpload(serverData, pathList)
Exemplo n.º 5
0
def main():
    q = sys.argv[1]
    args = q.split(" ")

    if os.path.exists(PyAl.nonvolatile("scratches.json")):
        with open(PyAl.nonvolatile("scratches.json"), "r") as f:
            scratchList = json.load(f)
            f.close()
    else:
        with open(PyAl.nonvolatile("scratches.json"), "w") as f:
            scratchList = {}
            json.dump(scratchList, f)
            f.close()

    if len(args) == 0 or args[0] == "":
        emptyFeedback = PyAl.Feedback()
        emptyFeedback.addInvalidItem(
            uid="com.danielsh.alfred.scratch.addscratch",
            autocomplete="add ",
            itemDict={
                "title": "Add Scratch File",
                "subtitle": "A name, a space, and a path."
            })
        if len(scratchList) > 0:
            emptyFeedback.addInvalidItem(
                uid="com.danielsh.alfred.scratch.removescratch",
                autocomplete="remove ",
                itemDict={
                    "title": "Remove Scratch File",
                    "subtitle": "List scratches to delete."
                })

            for (name, path) in scratchList.iteritems():
                emptyFeedback.addInvalidItem(
                    uid="com.danielsh.alfred.scratch.%s" % name,
                    autocomplete="%s " % name,
                    itemDict={
                        "title": name,
                        "subtitle": path
                    })

        print emptyFeedback

    elif args[0] == "add":
        addFeedback = PyAl.Feedback()
        if len(args) == 2:
            name = args[1] if len(args[1]) else "[name]"
            path = ""

            addFeedback.addInvalidItem(
                uid="com.danielsh.alfred.scratch.addscratch.%s" % name,
                autocomplete="add %s" % name,
                itemDict={
                    "title": "Add %s" % name,
                    "subtitle": "At [path]"
                })
        elif len(args) == 3:
            name = args[1]
            path = args[2]

            addFeedback.addValidItem(
                uid="com.danielsh.alfred.scratch.addscratch.%s" % name,
                arg="add \"%s\" \"%s\"" % (name, path),
                itemDict={
                    "title": "Add %s" % name,
                    "subtitle": "At %s" % path
                })
        elif len(args) > 3:
            name = args[1]
            path = args[2:]
            path = ' '.join(path)

            addFeedback.addValidItem(
                uid="com.danielsh.alfred.scratch.addscratch.%s" % name,
                arg="add \"%s\" \"%s\"" % (name, path),
                itemDict={
                    "title": "Add %s" % name,
                    "subtitle": "At %s" % path
                })
        print addFeedback

    elif args[0] == "remove":
        removeFeedback = PyAl.Feedback()
        if len(scratchList) > 0:
            for (name, path) in scratchList.iteritems():
                removeFeedback.addValidItem(
                    uid="com.danielsh.alfred.scratch.removescratch.%s" % name,
                    arg="remove \"%s\"" % name,
                    itemDict={
                        "title": "Remove %s" % name,
                        "subtitle": "From %s" % path
                    })
        else:
            return

        print removeFeedback

    else:
        if len(scratchList) == 0:
            return
        else:
            scratchFeedback = PyAl.Feedback()
            if args[0] in scratchList.keys():
                name = args[0]
                path = scratchList[name]
                if len(args) >= 2:
                    toScratch = args[1:]
                    toScratch = ' '.join(toScratch)

                    if '"' in toScratch:
                        toScratch = re.sub("\"", "'", toScratch)
                else:
                    toScratch = ""

                scratchFeedback.addValidItem(
                    uid="com.danielsh.alfred.scratch.%s" % name,
                    arg="scratch \"%s\" \"%s\"" % (name, toScratch),
                    itemDict={
                        "title": "Scratch to %s:" % name,
                        "subtitle": toScratch
                    })
            else:
                return

            print scratchFeedback
Exemplo n.º 6
0
def main():
    q = sys.argv[1]
    args = q.split(" ")

    if os.path.exists(PyAl.nonvolatile("scratches.json")):
        with open(PyAl.nonvolatile("scratches.json"), "r") as f:
            scratchList = json.load(f)
            f.close()
    else:
        with open(PyAl.nonvolatile("scratches.json"), "w") as f:
            scratchList = {}
            json.dump(scratchList, f)
            f.close()

    if len(args) == 0 or args[0] == "":
        emptyFeedback = PyAl.Feedback()
        emptyFeedback.addInvalidItem(uid="com.danielsh.alfred.scratch.addscratch", autocomplete="add ",
                                    itemDict={"title": "Add Scratch File", "subtitle": "A name, a space, and a path."})
        if len(scratchList) > 0:
            emptyFeedback.addInvalidItem(uid="com.danielsh.alfred.scratch.removescratch", autocomplete="remove ",
                                        itemDict={"title": "Remove Scratch File", "subtitle": "List scratches to delete."})

            for (name, path) in scratchList.iteritems():
                emptyFeedback.addInvalidItem(uid="com.danielsh.alfred.scratch.%s" % name, autocomplete="%s " % name,
                                        itemDict={"title": name, "subtitle": path})

        print emptyFeedback

    elif args[0] == "add":
        addFeedback = PyAl.Feedback()
        if len(args) == 2:
            name = args[1] if len(args[1]) else "[name]"
            path = ""

            addFeedback.addInvalidItem(uid="com.danielsh.alfred.scratch.addscratch.%s" % name, autocomplete="add %s" % name,
                                itemDict={"title": "Add %s" % name, "subtitle": "At [path]"})
        elif len(args) == 3:
            name = args[1]
            path = args[2]

            addFeedback.addValidItem(uid="com.danielsh.alfred.scratch.addscratch.%s" % name, arg="add \"%s\" \"%s\"" % (name, path),
                                itemDict={"title": "Add %s" % name, "subtitle": "At %s" % path})
        elif len(args) > 3:
            name = args[1]
            path = args[2:]
            path = ' '.join(path)

            addFeedback.addValidItem(uid="com.danielsh.alfred.scratch.addscratch.%s" % name, arg="add \"%s\" \"%s\"" % (name, path),
                                itemDict={"title": "Add %s" % name, "subtitle": "At %s" % path})
        print addFeedback

    elif args[0] == "remove":
        removeFeedback = PyAl.Feedback()
        if len(scratchList) > 0:
            for (name, path) in scratchList.iteritems():
                removeFeedback.addValidItem(uid="com.danielsh.alfred.scratch.removescratch.%s" % name, arg="remove \"%s\"" % name,
                                itemDict={"title": "Remove %s" % name, "subtitle": "From %s" % path})
        else:
            return

        print removeFeedback

    else:
        if len(scratchList) == 0:
            return
        else:
            scratchFeedback = PyAl.Feedback()
            if args[0] in scratchList.keys():
                name = args[0]
                path = scratchList[name]
                if len(args) >= 2:
                    toScratch = args[1:]
                    toScratch = ' '.join(toScratch)

                    if '"' in toScratch:
                        toScratch = re.sub("\"", "'", toScratch)
                else:
                    toScratch = ""

                scratchFeedback.addValidItem(uid="com.danielsh.alfred.scratch.%s" % name, arg="scratch \"%s\" \"%s\"" % (name, toScratch),
                                itemDict={"title": "Scratch to %s:" % name, "subtitle": toScratch})
            else:
                return

            print scratchFeedback