Пример #1
0
def process_bunq_accounts_callback(accounts):
    """ Process bunq accounts with the callback dataset of accounts """
    global _BUNQ_ACCOUNTS_CALLBACK
    get_bunq_accounts_callback()

    for acc2 in accounts:
        found = False
        for acc in _BUNQ_ACCOUNTS_CALLBACK:
            if acc["iban"] == acc2["iban"]:
                # update account
                found = True
                acc["id"] = acc2["id"]
                acc["name"] = acc2["name"]
                acc["type"] = acc2["type"]
                acc["description"] = acc2["description"]
                acc["enableMutation"] = acc2["enableMutation"]
                acc["enableRequest"] = acc2["enableRequest"]
                acc["callbackMutation"] = acc2["callbackMutation"]
                acc["callbackRequest"] = acc2["callbackRequest"]
                acc["callbackOther"] = acc2["callbackOther"]
                storage.store("account_callback", acc["iban"], {"value": acc})
        if not found:
            # new account
            storage.store("account_callback", acc2["iban"], {"value": acc2})
            _BUNQ_ACCOUNTS_CALLBACK.append(acc2)

    # remove deleted
    newaccs = []
    ibans = [x["iban"] for x in accounts]
    for acc in _BUNQ_ACCOUNTS_CALLBACK:
        if acc["iban"] in ibans:
            newaccs.append(acc)
        else:
            storage.remove("account_callback", acc["iban"])
    _BUNQ_ACCOUNTS_CALLBACK = sorted(newaccs, key=lambda k: k["description"])
Пример #2
0
def save_app_master_url(value):
    """ Save the URL of the master instance """
    global _APP_MASTER_URL
    _APP_MASTER_URL = value
    if value is None:
        storage.remove("config", "app_master_url")
    else:
        storage.store("config", "app_master_url", {"value": value})
Пример #3
0
def save_app_mode(value):
    """ Save the app mode """
    global _APP_MODE
    _APP_MODE = value
    if value is None:
        storage.remove("config", "app_mode")
    else:
        storage.store("config", "app_mode", {"value": value})
Пример #4
0
def save_bunq_security_mode(value):
    """ Save the bunq security mode """
    global _BUNQ_SECURITY_MODE
    _BUNQ_SECURITY_MODE = value
    if value is None:
        storage.remove("config", "bunq_security_mode")
    else:
        storage.store("config", "bunq_security_mode", {"value": value})
Пример #5
0
def trigger_request_delete(identity):
    """ Delete a specific trigger identity for IFTTT trigger bunq_request """
    try:
        for index in storage.query_indexes("request_" + identity):
            storage.remove("request_" + identity, index)
        storage.remove("trigger_request", identity)

        return ""
    except Exception:
        traceback.print_exc()
        print("[trigger_request_delete] ERROR: cannot delete trigger")
        return json.dumps({"errors": [{"message": "Cannot delete trigger"}]}),\
               400
Пример #6
0
def process_bunq_accounts_local(accounts):
    """ Process retrieved bunq accounts with the local dataset of accounts """
    global _BUNQ_ACCOUNTS_LOCAL
    get_bunq_accounts_local()

    default_internal = True
    default_draft = True
    for acc in _BUNQ_ACCOUNTS_LOCAL:
        # leave defaults True only if all existing accounts have True
        default_internal &= acc["enableInternal"]
        default_draft &= acc["enableDraft"]

    for acc2 in accounts:
        found = False
        for acc in _BUNQ_ACCOUNTS_LOCAL:
            if acc["iban"] == acc2["iban"]:
                # update account
                found = True
                acc["id"] = acc2["id"]
                acc["name"] = acc2["name"]
                acc["type"] = acc2["type"]
                acc["description"] = acc2["description"]
                storage.store("account_local", acc["iban"], {"value": acc})
        if not found:
            # new account
            acc2["enableInternal"] = default_internal
            acc2["enableDraft"] = default_draft
            acc2["enableExternal"] = False
            storage.store("account_local", acc2["iban"], {"value": acc2})
            _BUNQ_ACCOUNTS_LOCAL.append(acc2)

    # remove deleted
    newaccs = []
    ibans = [x["iban"] for x in accounts]
    for acc in _BUNQ_ACCOUNTS_LOCAL:
        if acc["iban"] in ibans:
            newaccs.append(acc)
        else:
            storage.remove("account_local", acc["iban"])
    _BUNQ_ACCOUNTS_LOCAL = sorted(newaccs, key=lambda k: k["description"])
Пример #7
0
def trigger_request():
    """ Callback for IFTTT trigger bunq_request """
    try:
        data = request.get_json()
        print("[trigger_request] input: {}".format(json.dumps(data)))

        if "triggerFields" not in data or \
                "account" not in data["triggerFields"]:
            print("[trigger_request] ERROR: account field missing!")
            return json.dumps({"errors": [{"message": "Invalid data"}]}), 400
        account = data["triggerFields"]["account"]
        fields = data["triggerFields"]
        fieldsstr = json.dumps(fields)

        if "trigger_identity" not in data:
            print("[trigger_request] ERROR: trigger_identity field missing!")
            return json.dumps({"errors": [{"message": "Invalid data"}]}), 400
        identity = data["trigger_identity"]

        limit = 50
        if "limit" in data:
            limit = data["limit"]

        if account == "NL42BUNQ0123456789":
            return trigger_request_test(limit)

        timezone = "UTC"
        if "user" in data and "timezone" in data["user"]:
            timezone = data["user"]["timezone"]

        entity = storage.retrieve("trigger_request", identity)
        if entity is not None:
            if entity["account"] != account or \
                    json.dumps(entity["fields"]) != fieldsstr:
                storage.store("trigger_request", identity, {
                    "account": account,
                    "identity": identity,
                    "fields": fields
                })
                print("[trigger_request] updating trigger {} {}".format(
                    account, fieldsstr))
        else:
            storage.store("trigger_request", identity, {
                "account": account,
                "identity": identity,
                "fields": fields
            })
            storage.store(
                "request_" + identity, "0", {
                    "value": {
                        "created_at": "2018-01-05T11:25:15+00:00",
                        "date": "2018-01-05",
                        "amount": "0.00",
                        "account": account,
                        "counterparty_account": "NL11BANK1111111111",
                        "counterparty_name": "Dummy Transaction",
                        "description": "This is a dummy transaction",
                        "request_id": "123e4567-e89b-12d3-a456-426655440001",
                        "meta": {
                            "id": "0",
                            "timestamp": "1515151515"
                        }
                    }
                })
            print("[trigger_request] storing new trigger {} {}".format(
                account, fieldsstr))

        transactions = []
        for entity in storage.query_all("request_" + identity):
            entity["value"]["created_at"] = arrow.get(\
                entity["value"]["created_at"]).to(timezone).isoformat()
            transactions.append(entity["value"])
        transactions = sorted(transactions,
                              key=lambda k: -int(k["meta"]["timestamp"]))

        if len(transactions) > 50:
            for trans in transactions[50:]:
                storage.remove("request_" + identity, str(trans["meta"]["id"]))

        print("[trigger_request] Found {} transactions".format(
            len(transactions)))
        return json.dumps({"data": transactions[:limit]})
    except Exception:
        traceback.print_exc()
        print("[trigger_request] ERROR: cannot retrieve requests")
        return json.dumps({"errors": [{"message": \
                           "Cannot retrieve requests"}]}), 400
Пример #8
0
def delete_event(request):
    storage.remove(model.event_to_json(event))
    return 200, ''
Пример #9
0
def main():
    if len(sys.argv) == 1:
        parser.print_help()
        exit
    args = parser.parse_args()

    if args.fib:
        for x in fib.generate_fib_sequence(args.fib):
            print x

    if args.sto:
        storage = storage.Storage()
        while input != "exit": 
            input = raw_input("Enter command:")
            items = input.split()
            if len(items) == 0:
                print "Command required"
                continue
            command = items[0]
            del(items[0])
            if command == "add":
                for x in items:
                    storage.add(x)
            elif command == "remove":
                for x in items:
                    storage.remove(x)
            elif command == "find":
                for x in items:
                    storage.find(x)
            elif command == "list":
                storage.list()
            elif command == "save":
                print items[0]
                storage.save(items[0])
            elif command == "load":
                storage.load(items[0])
            elif command == "grep":
                storage.grep(items[0])    
            elif command != "exit":
                print "Invalid command"

    if args.text:
        try:
            with open(args.text, "r") as file:
                text = file.read()
        except IOError:
            print "File {} doesn't exist".format(args.text)

        print "Average wordcount:{}".format(text_statistics.average_wordcount(text))
        print "Median wordcount: {}".format(text_statistics.median_wordcount(text))
        print "Word count: {}".format(text_statistics.count_words(text))
        try: 
            n = raw_input("Enter N:")
            number = raw_input("Enter K:")
            print "Top {0} of {1}-grams is:".format(number, n) 
            print text_statistics.top_ngrams(text, int(n), int(number))
        except ValueError:
            print "Invalid input. Integer required!"

    if args.sort:
        try: 
            with open(args.sort, "r") as file:
                ara = file.read().replace("\n", "").replace(" ", "").split(",")
                ara = map(int, ara)
        except IOError:
            print "File {} doesn't exist".format(args.sort)
        print "quick sort"
        sort.demo(ara, sort.quick_sort)
        print "merge sort"
        sort.demo(ara, sort.merge_sort)
        print "radix sort"
        sort.demo(ara, sort.radix_sort)