<commit_before>import sys
import time
import logging
from decimal import *

logger = logging.getLogger("faircoins")

from django.conf import settings
from django.db.models import Q

import faircoin_nrp.electrum_fair_nrp as efn

from valuenetwork.valueaccounting.models import EconomicAgent, EconomicEvent, EconomicResource
from valuenetwork.valueaccounting.lockfile import FileLock, AlreadyLocked, LockTimeout, LockFailed

#FAIRCOIN_DIVISOR = int(1000000)

def init_electrum_fair():
    try:
        daemon = efn.daemon_is_up()
    except:
        logger.critical("Cannot connect with daemon.")
        return False

    if not daemon or (daemon == 'ERROR'):
        return False

    try:
        network = efn.is_connected()
    except:
        logger.critical("Cannot connect with electrum-server.")
        return False

    return network and (network != 'ERROR')

def acquire_lock():
    lock = FileLock("broadcast-faircoins")
    logger.debug("acquiring lock...")
    try:
        #lock.acquire(settings.BROADCAST_FAIRCOINS_LOCK_WAIT_TIMEOUT)
        lock.acquire(1)
    except AlreadyLocked:
        logger.warning("lock already in place. quitting.")
        return False
    except LockTimeout:
        logger.warning("waiting for the lock timed out. quitting.")
        return False
    logger.debug("lock acquired.")
    return lock

def create_address_from_file(entity_id, entity):
    filename = settings.NEW_FAIRCOIN_ADDRESSES_FILE
    address = None
    with open(filename, 'r') as fin:
        try:
            data = fin.read().splitlines(True)
            address_in_file, privkey = data[0].strip().split(',')
        except:
            logger.critical("Error reading new faircoin addresses file.")
            return None
    try:
        status, address = efn.import_key(privkey, entity_id, entity)
    except:
        logger.critical("Error importing key: " + address)
        return None
    if status == 'ERROR':
        logger.critical("Error importing key: " + address)
        return None
    if address != address_in_file:
        logger.warning("Address returned on importing key is different from the file one.")
    with open(filename, 'w') as fout:
        try:
           fout.writelines(data[1:])
       except:
           logger.critical("Error writting new faircoin addresses file.")
    logger.debug("Private key succesfully imported to wallet for "
        + str(entity) + ": " + str(entity_id))
    return address

def create_address_for_agent(agent):
    address = None
    if hasattr(settings, 'NEW_FAIRCOIN_ADDRESSES_FILE'):<commit_msg>Stupid identation bug<commit_after>import sys
import time
import logging
from decimal import *

logger = logging.getLogger("faircoins")

from django.conf import settings
from django.db.models import Q

import faircoin_nrp.electrum_fair_nrp as efn

from valuenetwork.valueaccounting.models import EconomicAgent, EconomicEvent, EconomicResource
from valuenetwork.valueaccounting.lockfile import FileLock, AlreadyLocked, LockTimeout, LockFailed

#FAIRCOIN_DIVISOR = int(1000000)

def init_electrum_fair():
    try:
        daemon = efn.daemon_is_up()
    except:
        logger.critical("Cannot connect with daemon.")
        return False

    if not daemon or (daemon == 'ERROR'):
        return False

    try:
        network = efn.is_connected()
    except:
        logger.critical("Cannot connect with electrum-server.")
        return False

    return network and (network != 'ERROR')

def acquire_lock():
    lock = FileLock("broadcast-faircoins")
    logger.debug("acquiring lock...")
    try:
        #lock.acquire(settings.BROADCAST_FAIRCOINS_LOCK_WAIT_TIMEOUT)
        lock.acquire(1)
    except AlreadyLocked:
        logger.warning("lock already in place. quitting.")
        return False
    except LockTimeout:
        logger.warning("waiting for the lock timed out. quitting.")
        return False
    logger.debug("lock acquired.")
    return lock

def create_address_from_file(entity_id, entity):
    filename = settings.NEW_FAIRCOIN_ADDRESSES_FILE
    address = None
    with open(filename, 'r') as fin:
        try:
            data = fin.read().splitlines(True)
            address_in_file, privkey = data[0].strip().split(',')
        except:
            logger.critical("Error reading new faircoin addresses file.")
            return None
    try:
        status, address = efn.import_key(privkey, entity_id, entity)
    except:
        logger.critical("Error importing key: " + address)
        return None
    if status == 'ERROR':
        logger.critical("Error importing key: " + address)
        return None
    if address != address_in_file:
        logger.warning("Address returned on importing key is different from the file one.")
    with open(filename, 'w') as fout:
        try:
           fout.writelines(data[1:])
        except:
           logger.critical("Error writting new faircoin addresses file.")
    logger.debug("Private key succesfully imported to wallet for "
        + str(entity) + ": " + str(entity_id))
    return address

def create_address_for_agent(agent):
    address = None
    if hasattr(settings, 'NEW_FAIRCOIN_ADDRESSES_FILE'):
{'fraction_non_alphanumeric': 0.09254598257502421, 'fraction_numerical': 0.003872216844143272, 'mean_word_length': 2.979969183359014, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, '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': '4624550', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1458, 'n_words': 403}