<commit_before>from __future__ import print_function

from invoke import run, task

import sys
import os

from decimal import Decimal as D, ROUND_HALF_UP
import hashlib
import hmac
import requests
import time
import json

from gratipay import wireup
from gratipay.exceptions import NegativeBalance
from gratipay.models.exchange_route import ExchangeRoute
from gratipay.models.participant import Participant

MINIMUM_COINBASE_PAYOUT = 1 # in USD

@task(
    help={
        'username': "Gratipay username. (required)",
        'email':    "PayPal email address. (required)",
        'api-key-fragment': "First 8 characters of user's API key.",
        'overwrite': "Override existing PayPal email?"
    }
)
def set_paypal_email(username='', email='', api_key_fragment='', overwrite=False):
    """
    Usage:

    [gratipay] $ env/bin/invoke set_paypal_email --username=username --email=<EMAIL> [--api-key-fragment=12e4s678] [--overwrite]
    """

    if not username or not email:
        print_help(set_paypal_email)
        sys.exit(1)

    if not os.environ.get('DATABASE_URL'):
        load_prod_envvars()

    if not api_key_fragment:
        first_eight = "unknown!"
    else:
        first_eight = api_key_fragment

    db = wireup.db(wireup.env())

    participant = Participant.from_username(username)
    if not participant:
        print("No Gratipay participant found with username '" + username + "'")
        sys.exit(2)

    route = ExchangeRoute.from_network(participant, 'paypal')

    # PayPal caps the MassPay fee at $20 for users outside the U.S., and $1 for
    # users inside the U.S. Most Gratipay users using PayPal are outside the U.S.
    # so we set to $20 and I'll manually adjust to $1 when running MassPay and
    # noticing that something is off.
    FEE_CAP = 20

    if route:
        print("PayPal email is already set to: " + route.address)
        if not overwrite:
            print("Not overwriting existing PayPal email.")
            sys.exit(3)

    if participant.api_key == None:
        assert first_eight == "None"
    else:
        assert participant.api_key[0:8] == first_eight

    print("Setting PayPal email for " + username + " to " + email)
    ExchangeRoute.insert(participant, 'paypal', email, fee_cap=FEE_CAP)
    print("All done.")

@task(
    help={
        'username': "Gratipay username. (required)",
        'amount': "Amount to send in USD. (required)",
        'api-key-fragment': "First 8 characters of user's API key.",
    }
)
def bitcoin_payout(username='', amount='', api_key_fragment=''):
    """
    Usage:

    [gratipay] $ env/bin/invoke bitcoin_payout --username=username --amount=amount [--api-key-fragment=12e4s678]
    """

    if not username or not amount:
        print_help(bitcoin_payout)
        sys.exit(1)

    if not os.environ.get('DATABASE_URL'):
        load_prod_envvars()

    amount = D(amount)
    assert amount >= MINIMUM_COINBASE_PAYOUT
    amount = subtract_fee(amount)

    if not api_key_fragment:
        first_eight = "unknown!"
    else:
        first_eight = api_key_fragment

    db = wireup.db(wireup.env())

    participant = Participant.from_username(username)
    if not participant:
        print("No Gratipay participant found with username '" + username + "'")
        sys.exit(2)

    route = ExchangeRoute.from_network(participant, 'bitcoin')
    if not route:
        print(username + " hasn't linked a bitcoin address to their profile!")
        sys.exit(3)

    bitcoin_address = route.address
    print("Fetched bitcoin_address from database: " + bitcoin_address)

    if D(participant.balance) < D(amount):
        print("Not enough balance. %s only has %f in their account!" % username, D(amount))
        sys.exit(4)

    if participant.api_key == None:
        assert first_eight == "None"<commit_msg>Fix string formatting error in coinbase script<commit_after>from __future__ import print_function

from invoke import run, task

import sys
import os

from decimal import Decimal as D, ROUND_HALF_UP
import hashlib
import hmac
import requests
import time
import json

from gratipay import wireup
from gratipay.exceptions import NegativeBalance
from gratipay.models.exchange_route import ExchangeRoute
from gratipay.models.participant import Participant

MINIMUM_COINBASE_PAYOUT = 1 # in USD

@task(
    help={
        'username': "Gratipay username. (required)",
        'email':    "PayPal email address. (required)",
        'api-key-fragment': "First 8 characters of user's API key.",
        'overwrite': "Override existing PayPal email?"
    }
)
def set_paypal_email(username='', email='', api_key_fragment='', overwrite=False):
    """
    Usage:

    [gratipay] $ env/bin/invoke set_paypal_email --username=username --email=<EMAIL> [--api-key-fragment=12e4s678] [--overwrite]
    """

    if not username or not email:
        print_help(set_paypal_email)
        sys.exit(1)

    if not os.environ.get('DATABASE_URL'):
        load_prod_envvars()

    if not api_key_fragment:
        first_eight = "unknown!"
    else:
        first_eight = api_key_fragment

    db = wireup.db(wireup.env())

    participant = Participant.from_username(username)
    if not participant:
        print("No Gratipay participant found with username '" + username + "'")
        sys.exit(2)

    route = ExchangeRoute.from_network(participant, 'paypal')

    # PayPal caps the MassPay fee at $20 for users outside the U.S., and $1 for
    # users inside the U.S. Most Gratipay users using PayPal are outside the U.S.
    # so we set to $20 and I'll manually adjust to $1 when running MassPay and
    # noticing that something is off.
    FEE_CAP = 20

    if route:
        print("PayPal email is already set to: " + route.address)
        if not overwrite:
            print("Not overwriting existing PayPal email.")
            sys.exit(3)

    if participant.api_key == None:
        assert first_eight == "None"
    else:
        assert participant.api_key[0:8] == first_eight

    print("Setting PayPal email for " + username + " to " + email)
    ExchangeRoute.insert(participant, 'paypal', email, fee_cap=FEE_CAP)
    print("All done.")

@task(
    help={
        'username': "Gratipay username. (required)",
        'amount': "Amount to send in USD. (required)",
        'api-key-fragment': "First 8 characters of user's API key.",
    }
)
def bitcoin_payout(username='', amount='', api_key_fragment=''):
    """
    Usage:

    [gratipay] $ env/bin/invoke bitcoin_payout --username=username --amount=amount [--api-key-fragment=12e4s678]
    """

    if not username or not amount:
        print_help(bitcoin_payout)
        sys.exit(1)

    if not os.environ.get('DATABASE_URL'):
        load_prod_envvars()

    amount = D(amount)
    assert amount >= MINIMUM_COINBASE_PAYOUT
    amount = subtract_fee(amount)

    if not api_key_fragment:
        first_eight = "unknown!"
    else:
        first_eight = api_key_fragment

    db = wireup.db(wireup.env())

    participant = Participant.from_username(username)
    if not participant:
        print("No Gratipay participant found with username '" + username + "'")
        sys.exit(2)

    route = ExchangeRoute.from_network(participant, 'bitcoin')
    if not route:
        print(username + " hasn't linked a bitcoin address to their profile!")
        sys.exit(3)

    bitcoin_address = route.address
    print("Fetched bitcoin_address from database: " + bitcoin_address)

    if D(participant.balance) < D(amount):
        print("Not enough balance. %s only has %f in their account!" % (username, D(amount)))
        sys.exit(4)

    if participant.api_key == None:
        assert first_eight == "None"
{'fraction_non_alphanumeric': 0.10715682440359797, 'fraction_numerical': 0.00834311041585191, 'mean_word_length': 3.027296587926509, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3902207', 'n_tokens_mistral': 2432, 'n_tokens_neox': 2312, 'n_words': 675}