Skip to content
Snippets Groups Projects
Commit 8d1fe3bf authored by Hugo Trentesaux's avatar Hugo Trentesaux Committed by Vincent Texier
Browse files

[fix] diverse localblockchain fixes

use pathlib instead of os.path / glob
fix too long lines
use default variables
parent 211038ff
No related branches found
No related tags found
2 merge requests!128Release 0.62.0,!124#130: Support reading Duniter's local json blockchain
# imports locally stored blockchain in the chunk format # imports locally stored blockchain in the chunk format
# example usage : # example usage :
# ``` # ```
# from duniterpy.localblockchain import load # from duniterpy.helpers.blockchain import load
# bc = load() # gets blockchain iterator # bc = load() # gets blockchain iterator
# b = next(bc) # gets block # b = next(bc) # gets block
# b.number # should return 0
# next(bc).number # should return 1 (and so on)
# ``` # ```
import json import json
import os import pathlib
import glob from ..documents import Block
from .documents import Block
CHUNK_SIZE = 250 CHUNK_SIZE = 250
DEFAULT_PATH = ".config/duniter/duniter_default/g1/"
class JsonBlockchain: class JsonBlockchain:
def __init__(self, folder): def __init__(self, folder):
self.folder = folder # folder where chunks are stored self.folder = folder # folder where chunks are stored
self.chunks = len( # number of files starting with "chunk_"
glob.glob(os.path.join(folder, "chunk_*")) self.chunks = len(list(folder.glob("chunk_*")))
) # number of files starting with "chunk_" # number from 0 to 249 equal to current_block // current_chunk
self.current_block_in_chunk = ( self.current_block_in_chunk = 0
0 # number from 0 to 249 equal to current_block // current_chunk
)
self.current_chunk = 0 # current chunk number self.current_chunk = 0 # current chunk number
self.chunk = [] # parsed json for current chunk (length = 250) self.chunk = [] # parsed json for current chunk (length = 250)
self.parsechunk() # parse first chunk self.parsechunk() # parse first chunk
...@@ -31,7 +31,9 @@ class JsonBlockchain: ...@@ -31,7 +31,9 @@ class JsonBlockchain:
def parsechunk(self): def parsechunk(self):
"""parse a json chunk file""" """parse a json chunk file"""
with open( with open(
os.path.join(self.folder, "chunk_" + str(self.current_chunk) + "-250.json") self.folder.joinpath(
"chunk_" + str(self.current_chunk) + "-" + str(CHUNK_SIZE) + ".json"
)
) as f: ) as f:
s = f.read() s = f.read()
p = json.loads(s) p = json.loads(s)
...@@ -41,16 +43,18 @@ class JsonBlockchain: ...@@ -41,16 +43,18 @@ class JsonBlockchain:
return self return self
def __next__(self): def __next__(self):
"""if current block is outside current chunk, parse next one, otherwise, return current block parsed json""" """
if self.current_block_in_chunk == 250: # block outside chunk if current block is outside current chunk, parse next one,
otherwise, return current block parsed json
"""
if self.current_block_in_chunk == CHUNK_SIZE: # block outside chunk
self.current_block_in_chunk = 0 # reset to next chunk start self.current_block_in_chunk = 0 # reset to next chunk start
self.current_chunk += 1 # increment current chunk number self.current_chunk += 1 # increment current chunk number
if self.current_chunk >= self.chunks: # outside range if self.current_chunk >= self.chunks: # outside range
raise StopIteration() raise StopIteration()
self.parsechunk() # parse this chunk self.parsechunk() # parse this chunk
self.current_block_in_chunk += ( # increment current block number for next iteration
1 # increment current block number for next iteration self.current_block_in_chunk += 1
)
return self.chunk[ return self.chunk[
self.current_block_in_chunk - 1 self.current_block_in_chunk - 1
] # return block (before incrementation) ] # return block (before incrementation)
...@@ -63,10 +67,10 @@ def Blockchain(json_blockchain): ...@@ -63,10 +67,10 @@ def Blockchain(json_blockchain):
yield Block.from_parsed_json(jb) yield Block.from_parsed_json(jb)
def load(path=".config/duniter/duniter_default/g1/"): def load(path=DEFAULT_PATH):
"""returns an iterator allowing to browse all the blockchain """returns an iterator allowing to browse all the blockchain
in practice, it will load chunk by chunk and only keep one in memory at a time""" in practice, it will load chunk by chunk and only keep one in memory at a time"""
path = os.path.join(os.path.expanduser("~"), path) # expand path path = pathlib.Path("~").joinpath(path).expanduser() # expand path
jbc = JsonBlockchain(path) # gets an iterator over json blockchain jbc = JsonBlockchain(path) # gets an iterator over json blockchain
bc = Blockchain(jbc) # convert it to an iterator over blocks bc = Blockchain(jbc) # convert it to an iterator over blocks
return bc # returns return bc # returns
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment