# Path: paperbroker/accounts.py
# class Account():
# 
#     def __init__(self, positions=None, account_id:str=None):
#         self.account_id = account_id if account_id is not None else 'account' + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))
#         self.cash = 10000
#         self.maintenance_margin = 0.0
#         self.positions = positions if positions is not None else []
# 
# def account_factory(account):
#     if isinstance(account, Account):
#         return account
#     return Account(account_id=account)


from ...accounts import Account, account_factory
from os import listdir
from os.path import isfile, join
import tempfile
import os
import pickle
class LocalFileSystemAccountAdapter():

    def __init__(self, root=None):
        if root is None: root = tempfile.gettempdir()
        if not os.path.exists(root+"/accounts/"):
            os.makedirs(root+"/accounts/")
        self.root = root

    def get_account(self, account_id: str, current_date=None):
        with open(self.root + "/accounts/" + account_id + ".pickle", 'rb') as f:
            return pickle.load(file=f)

    def has_account(self, account_id: str, current_date=None):
        try:
            pickle.load(file=self.root + "/accounts/" + account_id + ".pickle")
            return True
        except:
            return False

    def put_account(self, account: Account, current_date=None):
        with open(self.root + "/accounts/" + account.account_id + ".pickle", 'wb') as f:
            pickle.dump(account, file=f)

    def get_account_ids(self, current_date=None):
        mypath = self.root + "/accounts/"
        return [f.split(".")[0] for f in listdir(mypath) if isfile(join(mypath, f))]

    def delete_account(self, account, current_date=None):
        try:
            os.remove(self.root + "/accounts/" + account_factory(account).account_id + ".pickle")

# Path: paperbroker/accounts.py
# class Account():
# 
#     def __init__(self, positions=None, account_id:str=None):
#         self.account_id = account_id if account_id is not None else 'account' + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))
#         self.cash = 10000
#         self.maintenance_margin = 0.0
#         self.positions = positions if positions is not None else []
# 
# def account_factory(account):
#     if isinstance(account, Account):
#         return account
#     return Account(account_id=account)


# Path: paperbroker/adapters/accounts/LocalFileSystemAccountAdapter.py
from ...accounts import Account, account_factory
from os import listdir
from os.path import isfile, join
import tempfile
import os
import pickle

class LocalFileSystemAccountAdapter():

    def __init__(self, root=None):
        if root is None: root = tempfile.gettempdir()
        if not os.path.exists(root+"/accounts/"):
            os.makedirs(root+"/accounts/")
        self.root = root

    def get_account(self, account_id: str, current_date=None):
        with open(self.root + "/accounts/" + account_id + ".pickle", 'rb') as f:
            return pickle.load(file=f)

    def has_account(self, account_id: str, current_date=None):
        try:
            pickle.load(file=self.root + "/accounts/" + account_id + ".pickle")
            return True
        except:
            return False

    def put_account(self, account: Account, current_date=None):
        with open(self.root + "/accounts/" + account.account_id + ".pickle", 'wb') as f:
            pickle.dump(account, file=f)

    def get_account_ids(self, current_date=None):
        mypath = self.root + "/accounts/"
        return [f.split(".")[0] for f in listdir(mypath) if isfile(join(mypath, f))]

    def delete_account(self, account, current_date=None):
        try:
            os.remove(self.root + "/accounts/" + account_factory(account).account_id + ".pickle")

        except: