예제 #1
0
def _group_age(x):
    y = parse(x.opened_on)
    z = parse(x.date_savings_started_this_cycle)
    a = y - z.replace(tzinfo=tz.tzutc())
    return a.days
예제 #2
0
def _group_age(x):
    y = parse(x.opened_on)
    z = parse(x.date_savings_started_this_cycle)
    a = y - z.replace(tzinfo=tz.tzutc())
    return a.days
예제 #3
0
def client(parameters):
    cmd = parameters[0]

    if cmd == "ls":
        path = parameters[1]
        to_file = parameters[2]

        resp = api_client.metadata(path)
        file = open(to_file, "w")

        if 'contents' in resp:
            for f in resp['contents']:
                name = os.path.basename(f['path'])
                encoding = locale.getdefaultlocale()[1]
                file.write(('%s\n' % name).encode(encoding))
        file.close()

    if cmd == "share":
        param = parameters[1]
        f = api_client.share(param)
        url = str(f['url'])
        print(" > Generated link: " + url)

    elif cmd == "delta":
        state = load_state()
        cursor = state.get('cursor')
        tree = state['tree']
        page = 0
        changed = False
        page_limit = 15

        try:
            delta_switch = parameter[1]
        except:
            delta_switch = 0

        while (page_limit is None) or (page < page_limit):
            # Make an int for progress/total
            progress = 0
            total = 0
            # Get /delta results from Dropbox
            result = api_client.delta(cursor)
            page += 1
            if result['reset'] == True:
                sys.stdout.write('reset\n')
                changed = True
                tree = {}
            cursor = result['cursor']
            # Apply the entries one by one to our cached tree.
            for delta_entry in result['entries']:
                total = total + 1
            for delta_entry in result['entries']:
                changed = True
                progress = progress + 1
                print("Current entry: " + str(progress) + "/" + str(total))
                apply_delta(tree, delta_entry)
                cursor = result['cursor']
                #if not result['has_more']: break

        if not changed:
            sys.stdout.write('No updates.\n')
        else:
            # Save state
            state['cursor'] = cursor
            state['tree'] = tree
            save_state(state)

    elif cmd == "put":
        from_path = parameters[1]
        to_path = parameters[2]
        notify = parameters[3]  # it can be 'add' or 'upd'

        from_file = open(os.path.expanduser(from_path), 'rb')
        # try:
        api_client.put_file("/" + to_path, from_file)
        # except:
        #	print(" x Unable to upload file. ")
        common.orphiliaNotify(notify, from_path)

    elif cmd == "unlink":
        sess.unlink()
        print(" > Unlinked :C")

    elif cmd == "cat":
        f = api_client.get_file("/" + path)
        stdout.write(f.read())
        stdout.write("\n")

    elif cmd == "mkdir":
        path = parameters[1]
        try:
            api_client.file_create_folder("/" + path)
        except:
            print(" x Unable to make directory " + path)
        print(" > Directory \'" + path + "\' created")

    elif cmd == "rm":
        path = path_rewrite.rewritepath('posix', parameters[1])
        try:
            api_client.file_delete("/" + path)
            common.orphiliaNotify('rm', path)
        except:
            print(" x Unable to remove file " + path)

    elif cmd == "mv":
        from_path = parameters[1]
        to_path = parameters[2]
        try:
            api_client.file_move("/" + from_path, "/" + to_path)
        except:
            print(" x Unable to move file " + from_path + " to " + to_path)

    elif cmd == "account_info":
        f = api_client.account_info()
        pprint.PrettyPrinter(indent=2).pprint(f)

    elif cmd == "uid":
        f = api_client.account_info()
        uid = str(f['uid'])
        return uid

    elif cmd == "get":
        from_path = parameters[1]
        to_path = parameters[2]

        resp = api_client.metadata(from_path)
        modified = resp['client_mtime']

        try:
            open(os.path.expanduser(to_path), 'rb')
        except:
            pass

        date1 = time.mktime(parse(modified).timetuple())
        f = api_client.get_file("/" + from_path)
        dirname = os.path.dirname(os.path.abspath(to_path))

        if not os.path.exists(dirname):
            os.makedirs(dirname)

        file = open(to_path, "wb")
        try:
            file.write(f.read())
            file.close()
            os.utime(os.path.normpath(to_path), (date1, date1))
        except:
            print(" x Unable to save file.")

    elif cmd == "sync":
        """
        Command which syncs files.
            - if both file exists, compare the dates:
                - local is older? download from server
                - local is newer? remove from server and upload local
            - if only local exists, upload
            - if exists only on the server, download
        """
        # get path that was sent to client
        path = parameters[1]
        localPath = os.path.normpath(dropboxPath + '/' + path)
        # assume that both files actually exist
        change = 'upd'

        # check if file exists on the server, try to get response data
        try:
            resp = api_client.metadata(path)
            modified = resp['client_mtime']
            dropboxTime = parse(modified)
        except:
            # ok, it doesn't, so we're going to upload that file
            dropboxTime = 0
            change = 'add'

        # check if local file exists and try to get it's modified date
        try:
            localTime = datetime.datetime.fromtimestamp(
                os.path.getmtime(localPath), tz=tz.tzutc())
        except:
            change = 'get'
            localTime = 0

        # uhm, this was not supposed to happen...
        if dropboxTime == 0 and localTime == 0:
            print(
                " x WTF?! It looks like there is no file on the server nor locally..."
            )
            exit()
        else:
            if change == "upd":
                # both file exists, decide what's next
                if localTime < dropboxTime:
                    # local file is older, download from server
                    change = "get"
                else:
                    # local file is newer, remove from server
                    tmp = ['rm', path]
                    client(tmp)
                    # and reupload
                    change = "add"

            # push tasks to client
            if change == "get":
                tmp = ['get', path, localPath]
                client(tmp)
            elif change == "add":
                tmp = ['put', localPath, path, change]
                client(tmp)

    print(" > Command '" + parameters[0] + "' executed")
예제 #4
0
def client(parameters):
    cmd = parameters[0]

    if cmd == "ls":
        path = parameters[1]
        to_file = parameters[2]

        resp = api_client.metadata(path)
        file = open(to_file, "w")

        if 'contents' in resp:
            for f in resp['contents']:
                name = os.path.basename(f['path'])
                encoding = locale.getdefaultlocale()[1]
                file.write(('%s\n' % name).encode(encoding))
        file.close()

    if cmd == "share":
        param = parameters[1]
        f = api_client.share(param)
        url = str(f['url'])
        print(" > Generated link: " + url)

    elif cmd == "delta":
        state = load_state()
        cursor = state.get('cursor')
        tree = state['tree']
        page = 0
        changed = False
        page_limit = 15

        try:
            delta_switch = parameter[1]
        except:
            delta_switch = 0

        while (page_limit is None) or (page < page_limit):
            # Make an int for progress/total
            progress = 0
            total = 0
            # Get /delta results from Dropbox
            result = api_client.delta(cursor)
            page += 1
            if result['reset'] == True:
                sys.stdout.write('reset\n')
                changed = True
                tree = {}
            cursor = result['cursor']
            # Apply the entries one by one to our cached tree.
            for delta_entry in result['entries']:
                total = total + 1
            for delta_entry in result['entries']:
                changed = True
                progress = progress + 1
                print("Current entry: " + str(progress) + "/" + str(total))
                apply_delta(tree, delta_entry)
                cursor = result['cursor']
                #if not result['has_more']: break

        if not changed:
            sys.stdout.write('No updates.\n')
        else:
            # Save state
            state['cursor'] = cursor
            state['tree'] = tree
            save_state(state)

    elif cmd == "put":
        from_path = parameters[1]
        to_path = parameters[2]
        notify = parameters[3]  # it can be 'add' or 'upd'

        from_file = open(os.path.expanduser(from_path), 'rb')
        # try:
        api_client.put_file("/" + to_path, from_file)
        # except:
        #	print(" x Unable to upload file. ")
        common.orphiliaNotify(notify, from_path)

    elif cmd == "unlink":
        sess.unlink()
        print(" > Unlinked :C")

    elif cmd == "cat":
        f = api_client.get_file("/" + path)
        stdout.write(f.read())
        stdout.write("\n")

    elif cmd == "mkdir":
        path = parameters[1]
        try:
            api_client.file_create_folder("/" + path)
        except:
            print(" x Unable to make directory " + path)
        print(" > Directory \'" + path + "\' created")

    elif cmd == "rm":
        path = path_rewrite.rewritepath('posix', parameters[1])
        try:
            api_client.file_delete("/" + path)
            common.orphiliaNotify('rm', path)
        except:
            print(" x Unable to remove file " + path)

    elif cmd == "mv":
        from_path = parameters[1]
        to_path = parameters[2]
        try:
            api_client.file_move("/" + from_path, "/" + to_path)
        except:
            print(" x Unable to move file " + from_path + " to " + to_path)

    elif cmd == "account_info":
        f = api_client.account_info()
        pprint.PrettyPrinter(indent=2).pprint(f)

    elif cmd == "uid":
        f = api_client.account_info()
        uid = str(f['uid'])
        return uid

    elif cmd == "get":
        from_path = parameters[1]
        to_path = parameters[2]

        resp = api_client.metadata(from_path)
        modified = resp['client_mtime']

        try:
            open(os.path.expanduser(to_path), 'rb')
        except:
            pass

        date1 = time.mktime(parse(modified).timetuple())
        f = api_client.get_file("/" + from_path)
        dirname = os.path.dirname(os.path.abspath(to_path))

        if not os.path.exists(dirname):
            os.makedirs(dirname)

        file = open(to_path, "wb")
        try:
            file.write(f.read())
            file.close()
            os.utime(os.path.normpath(to_path), (date1, date1))
        except:
            print(" x Unable to save file.")

    elif cmd == "sync":
        """
        Command which syncs files.
            - if both file exists, compare the dates:
                - local is older? download from server
                - local is newer? remove from server and upload local
            - if only local exists, upload
            - if exists only on the server, download
        """
        # get path that was sent to client
        path = parameters[1]
        localPath = os.path.normpath(dropboxPath + '/' + path)
        # assume that both files actually exist
        change = 'upd'

        # check if file exists on the server, try to get response data
        try:
            resp = api_client.metadata(path)
            modified = resp['client_mtime']
            dropboxTime = parse(modified)
        except:
            # ok, it doesn't, so we're going to upload that file
            dropboxTime = 0
            change = 'add'

        # check if local file exists and try to get it's modified date
        try:
            localTime = datetime.datetime.fromtimestamp(os.path.getmtime(localPath), tz=tz.tzutc())
        except:
            change = 'get'
            localTime = 0

        # uhm, this was not supposed to happen...
        if dropboxTime == 0 and localTime == 0:
            print(" x WTF?! It looks like there is no file on the server nor locally...")
            exit()
        else:
            if change == "upd":
                # both file exists, decide what's next
                if localTime < dropboxTime:
                    # local file is older, download from server
                    change = "get"
                else:
                    # local file is newer, remove from server
                    tmp = ['rm', path]
                    client(tmp)
                    # and reupload
                    change = "add"

            # push tasks to client
            if change == "get":
                tmp = ['get', path, localPath]
                client(tmp)
            elif change == "add":
                tmp = ['put', localPath, path, change]
                client(tmp)

    print(" > Command '" + parameters[0] + "' executed")