def GetBlockshash_part(): os.chdir(get_config()["main_folder"]) if not os.path.exists(TEMP_BLOCKSHASH_PART_PATH): return [] else: with open(TEMP_BLOCKSHASH_PART_PATH, "rb") as block_file: return pickle.load(block_file)
def GetBlockshash(): os.chdir(get_config()["main_folder"]) if not os.path.exists(TEMP_BLOCKSHASH_PATH): return None else: with open(TEMP_BLOCKSHASH_PATH, 'rb') as block_file: return pickle.load(block_file)
def get_ledger(): from lib.config_system import get_config import os os.chdir(get_config().main_folder) with open(LEDGER_PATH, 'rb') as ledger_file: return pickle.load(ledger_file)
def save_ledger(self): from lib.config_system import get_config import os os.chdir(get_config().main_folder) with open(LEDGER_PATH, 'wb') as ledger_file: pickle.dump(self, ledger_file, protocol=2)
def save_connected_node(host, port): node_list = get_connected_node() already_in_list = False for element in node_list: if element[0] == host and element[1] == port: already_in_list = True if not already_in_list: new_node_list = [] new_node_list.append(host) new_node_list.append(port) node_list.append(new_node_list) import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), "..")) from lib.config_system import get_config old_cwd = os.getcwd() os.chdir(get_config().main_folder) with open(CONNECTED_NODE_PATH, 'wb') as connected_node_file: pickle.dump(node_list, connected_node_file) os.chdir(old_cwd)
def save_new_unl_node(id): id = id node = None from node.myownp2pn import MyOwnPeer2PeerNode for inbound_node in MyOwnPeer2PeerNode.main_node.nodes_inbound: if id in (inbound_node.id): node = inbound_node for outbound_node in MyOwnPeer2PeerNode.main_node.nodes_outbound: if id in (outbound_node.id): node = outbound_node if node != None: nodes_list = get_unl_nodes() already_in_list = False for element in nodes_list: if element == node.id: already_in_list = True if not already_in_list: nodes_list[node.id] = {} nodes_list[node.id]["host"] = node.host nodes_list[node.id]["port"] = node.port sys.path.append(os.path.join(os.path.dirname(__file__), "..")) from lib.config_system import get_config old_cwd = os.getcwd() os.chdir(get_config()["main_folder"]) with open(UNL_NODES_PATH, 'w') as unl_nodes_file: json.dump(nodes_list, unl_nodes_file) os.chdir(old_cwd)
def GetAccounts(): os.chdir(get_config()["main_folder"]) if not os.path.exists(TEMP_ACCOUNTS_PATH): return [] else: with open(TEMP_ACCOUNTS_PATH, 'rb') as block_file: return pickle.load(block_file)
def save_new_unl_node(id): id = id.replace('\n', '') node = None from node.myownp2pn import MyOwnPeer2PeerNode for inbound_node in MyOwnPeer2PeerNode.main_node.nodes_inbound: if id in (inbound_node.id).replace('\n', ''): node = inbound_node for outbound_node in MyOwnPeer2PeerNode.main_node.nodes_outbound: if id in (outbound_node.id).replace('\n', ''): node = outbound_node if node != None: nodes_list = get_unl_nodes() already_in_list = False for element in nodes_list: if element == node.id: already_in_list = True if not already_in_list: nodes_list.append(node.id) import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), "..")) from lib.config_system import get_config old_cwd = os.getcwd() os.chdir(get_config().main_folder) with open(UNL_NODES_PATH, 'wb') as unl_nodes_file: pickle.dump(nodes_list, unl_nodes_file) os.chdir(old_cwd)
def save_connected_node(host,port,id): node_list = get_connected_node() already_in_list = False for element in node_list: if node_list[element]["host"] == host and node_list[element]["port"] == port: already_in_list = True if not already_in_list: node_list[id] = {} node_list[id]["host"] = host node_list[id]["port"] = port sys.path.append(os.path.join(os.path.dirname(__file__), "..")) from lib.config_system import get_config os.chdir(get_config()["main_folder"]) with open(CONNECTED_NODE_PATH, 'w') as connected_node_file: json.dump(node_list, connected_node_file, indent=4)
def the_settings(): os.chdir(get_config()["main_folder"]) if not os.path.exists(SETTING_PATH): return settings_class() with open(SETTING_PATH, 'rb') as settings_file: return json.load(settings_file)
def save_settings(new_settings): """ Saves the settings. """ os.chdir(get_config()["main_folder"]) with open(SETTING_PATH, 'w') as settings_file: json.dump(new_settings, settings_file, indent=4)
def connected_node_delete(node): saved_nodes = get_connected_node() if node in saved_nodes: del saved_nodes[node] from lib.config_system import get_config os.chdir(get_config()["main_folder"]) with open(CONNECTED_NODE_PATH, 'w') as connected_node_file: json.dump(saved_nodes, connected_node_file, indent=4)
def unl_node_delete(node_id): saved_nodes = get_unl_nodes() if node_id in saved_nodes: del saved_nodes[node_id] from lib.config_system import get_config os.chdir(get_config()["main_folder"]) with open(UNL_NODES_PATH, 'w') as connected_node_file: json.dump(saved_nodes, connected_node_file)
def Wallet_Delete(account): saved_wallet = get_saved_wallet() if account in saved_wallet: del saved_wallet[account] from lib.config_system import get_config os.chdir(get_config()["main_folder"]) with open(WALLETS_PATH, 'w') as wallet_list_file: json.dump(saved_wallet, wallet_list_file)
def get_saved_wallet(): from lib.config_system import get_config os.chdir(get_config()["main_folder"]) if not os.path.exists(WALLETS_PATH): return {} with open(WALLETS_PATH, 'rb') as wallet_list_file: return json.load(wallet_list_file)
def Wallet_Delete(account): saved_wallet = get_saved_wallet() if len(saved_wallet) != 0: saved_wallet.remove(account) from lib.config_system import get_config import os os.chdir(get_config().main_folder) with open(WALLETS_PATH, 'wb') as wallet_list_file: pickle.dump(saved_wallet, wallet_list_file)
def SavetoMyTransaction(tx): """ Saves the transaction to the transaction db. """ currently_list = GetMyTransaction() currently_list.append(tx) os.chdir(get_config()["main_folder"]) with open(MY_TRANSACTION_PATH, "wb") as my_transaction_file: pickle.dump(currently_list, my_transaction_file, protocol=2)
def get_saved_wallet(): from lib.config_system import get_config import os if not os.path.exists(WALLETS_PATH): return [] os.chdir(get_config().main_folder) with open(WALLETS_PATH, 'rb') as wallet_list_file: return pickle.load(wallet_list_file)
def get_unl_nodes(): """ Returns the UNL nodes list from UNL_NODES_PATH. """ if not os.path.exists(UNL_NODES_PATH): return {} os.chdir(get_config()["main_folder"]) with open(UNL_NODES_PATH, "rb") as unl_nodes_file: return json.load(unl_nodes_file)
def get_unl_nodes(): sys.path.append(os.path.join(os.path.dirname(__file__), "..")) from lib.config_system import get_config if not os.path.exists(UNL_NODES_PATH): return {} os.chdir(get_config()["main_folder"]) with open(UNL_NODES_PATH, 'rb') as unl_nodes_file: return json.load(unl_nodes_file)
def unl_node_delete(node_id): """ Deletes the UNL node """ saved_nodes = get_unl_nodes() if node_id in saved_nodes: del saved_nodes[node_id] os.chdir(get_config()["main_folder"]) with open(UNL_NODES_PATH, "w") as connected_node_file: json.dump(saved_nodes, connected_node_file, indent=4)
def get_unl_nodes(): import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), "..")) from lib.config_system import get_config if not os.path.exists(UNL_NODES_PATH): return [] os.chdir(get_config().main_folder) with open(UNL_NODES_PATH, 'rb') as unl_nodes_file: return pickle.load(unl_nodes_file)
def test_getting_and_saving_config(self): finded_true_folder = False temp_config = get_config() os.chdir(temp_config["main_folder"]) obj = os.scandir() for entry in obj : if entry.name == "db": finded_true_folder = True self.assertEqual(finded_true_folder,True,"A problem on the config.")
def the_settings(): """ Returns the settings. If it doesn't exist, it creates, saves and returns. """ os.chdir(get_config()["main_folder"]) if not os.path.exists(SETTING_PATH): return create_and_save_the_settings() else: with open(SETTING_PATH, 'rb') as settings_file: return json.load(settings_file)
def get_connected_node(): sys.path.append(os.path.join(os.path.dirname(__file__), "..")) from lib.config_system import get_config if not os.path.exists(CONNECTED_NODE_PATH): temp_json = {} return temp_json os.chdir(get_config()["main_folder"]) with open(CONNECTED_NODE_PATH, 'rb') as connected_node_file: return json.load(connected_node_file)
def get_connected_node(): import os import sys sys.path.append(os.path.join(os.path.dirname(__file__), "..")) from lib.config_system import get_config if not os.path.exists(CONNECTED_NODE_PATH): return [] os.chdir(get_config().main_folder) with open(CONNECTED_NODE_PATH, 'rb') as connected_node_file: return pickle.load(connected_node_file)
def GetMyTransaction(): """ Returns the transaction db. """ os.chdir(get_config()["main_folder"]) if not os.path.exists(MY_TRANSACTION_PATH): return [] with open(MY_TRANSACTION_PATH, "rb") as my_transaction_file: obj = pickle.load(my_transaction_file) if not len(obj) == 1: obj.remove(obj[0]) return obj
def saveBlockstoBlockchainDB(block): """ Adds the block to the blockchain database at BLOCKS_PATH. """ os.chdir(get_config()["main_folder"]) with open(BLOCKS_PATH+str(block.sequance_number)+".block", 'wb') as block_file: pickle.dump(block, block_file, protocol=2) with open(BLOCKS_PATH+str(block.sequance_number)+".accounts", 'wb') as block_file: pickle.dump(GetAccounts(), block_file, protocol=2)
def save_wallet_list(publicKey, privateKey): wallet_list = get_saved_wallet() wallet_list[publicKey] = {} wallet_list[publicKey]["publickey"] = publicKey wallet_list[publicKey]["privatekey"] = privateKey from lib.config_system import get_config old_cwd = os.getcwd() os.chdir(get_config()["main_folder"]) with open(WALLETS_PATH, 'w') as wallet_list_file: json.dump(wallet_list, wallet_list_file) os.chdir(old_cwd)
def export_to_csv(obj, filename): """ Export a list of objects to a CSV file. """ if not len(obj) == 0: os.chdir(get_config()["main_folder"]) with open(filename, 'w', newline='', encoding='utf-8') as csvfile: fieldnames = [i for i in obj[0].__dict__.keys()] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() for obj in obj: writer.writerow(obj.__dict__) return True else: return False