Skip to content
Snippets Groups Projects
app.py 5.6 KiB
Newer Older
import os
import logging
import tarfile
from cutecoin.tools.exceptions import NameAlreadyExists, BadAccountFile
from cutecoin.core.account import Account
class Application(object):
    '''
    Managing core application datas :
    Accounts list and general configuration
    '''
    def __init__(self, argv):
        '''
        Constructor
        '''
inso's avatar
inso committed
        self.accounts = {}
inso's avatar
inso committed
        self.default_account = ""
        self.current_account = None
        config.parse_arguments(argv)
    def get_account(self, name):
inso's avatar
inso committed
        if not self.accounts[name]:
            self.load_account(name)
inso's avatar
inso committed
        if name in self.accounts.keys():
            return self.accounts[name]
        else:
            return None
    def create_account(self, name):
            if a == name:
                raise NameAlreadyExists(a)

        account_path = os.path.join(config.parameters['home'], name)
        if not os.path.exists(account_path):
            logging.info("Creating account directory")
            os.makedirs(account_path)
        account = Account.create(name,
                                 config.parameters)
        self.accounts[name] = account
        self.current_account = account
        return account
    def del_account(self, account):
inso's avatar
inso committed
    def change_current_account(self, account):
        if self.current_account is not None:
            self.save_cache(self.current_account)
inso's avatar
inso committed
        self.current_account = account
        self.load_cache(account)
inso's avatar
inso committed

        if not os.path.exists(config.parameters['home']):
            logging.info("Creating home directory")
            os.makedirs((config.parameters['home']))

        if (os.path.exists(config.parameters['data'])
                and os.path.isfile(config.parameters['data'])):
            logging.debug("Loading data...")
inso's avatar
inso committed
            with open(config.parameters['data'], 'r') as json_data:
                data = json.load(json_data)
inso's avatar
inso committed
                json_data.close()
inso's avatar
inso committed
                if 'default_account' in data.keys():
                    self.default_account = data['default_account']
inso's avatar
inso committed
                for account_name in data['local_accounts']:
                    self.accounts[account_name] = None

    def load_account(self, account_name):
        account_path = os.path.join(config.parameters['home'],
                                    account_name, 'properties')
        with open(account_path, 'r') as json_data:
            data = json.load(json_data)
            account = Account.load(data)
            self.accounts[account_name] = account

    def load_cache(self, account):
        for wallet in account.wallets:
            wallet_path = os.path.join(config.parameters['home'],
inso's avatar
inso committed
                                        account.name, '__cache__', wallet.pubkey)
            if os.path.exists(wallet_path):
                json_data = open(wallet_path, 'r')
                data = json.load(json_data)
                wallet.cache.load_from_json(data)
inso's avatar
inso committed
            for community in account.communities:
                wallet.cache.refresh(community)
    def save(self, account):
        with open(config.parameters['data'], 'w') as outfile:
            json.dump(self.jsonify(), outfile, indent=4, sort_keys=True)
        account_path = os.path.join(config.parameters['home'],
                                    account.name, 'properties')
        with open(account_path, 'w') as outfile:
            json.dump(account.jsonify(), outfile, indent=4, sort_keys=True)

    def save_cache(self, account):
inso's avatar
inso committed
        if not os.path.exists(os.path.join(config.parameters['home'],
                                        account.name, '__cache__')):
            os.makedirs(os.path.join(config.parameters['home'],
                                        account.name, '__cache__'))
        for wallet in account.wallets:
            wallet_path = os.path.join(config.parameters['home'],
inso's avatar
inso committed
                                        account.name, '__cache__', wallet.pubkey)
            with open(wallet_path, 'w') as outfile:
                json.dump(wallet.cache.jsonify(), outfile, indent=4, sort_keys=True)

    def import_account(self, file, name):
        with tarfile.open(file, "r") as tar:
            path = os.path.join(config.parameters['home'],
                                name)
            for obj in ["properties"]:
                try:
                    tar.getmember(obj)
                except KeyError:
                    raise BadAccountFile(file)
            tar.extractall(path)

        account_path = os.path.join(config.parameters['home'],
                                    name, 'properties')
        json_data = open(account_path, 'r')
        data = json.load(json_data)
        account = Account.load(data)
inso's avatar
inso committed
        account.name = name
        self.accounts.append(account)
        self.save(account)

    def export_account(self, file, account):
        with tarfile.open(file, "w") as tar:
            for file in ["properties"]:
                path = os.path.join(config.parameters['home'],
                                    account.name, file)
                tar.add(path, file)

    def jsonify_accounts(self):
inso's avatar
inso committed
        logging.debug("{0}".format(self.accounts))
inso's avatar
inso committed
            data.append(account)
inso's avatar
inso committed
        data = {'default_account': self.default_account,
                'local_accounts': self.jsonify_accounts()}