Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
DuniterPy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
clients
python
DuniterPy
Commits
2eee7028
Commit
2eee7028
authored
4 years ago
by
Hugo Trentesaux
Committed by
Vincent Texier
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[fix] formatting
parent
5b7e37c3
No related branches found
No related tags found
2 merge requests
!128
Release 0.62.0
,
!124
#130: Support reading Duniter's local json blockchain
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
duniterpy/localblockchain.py
+32
-21
32 additions, 21 deletions
duniterpy/localblockchain.py
with
32 additions
and
21 deletions
duniterpy/localblockchain.py
+
32
−
21
View file @
2eee7028
...
@@ -14,18 +14,25 @@ from .documents import Block
...
@@ -14,18 +14,25 @@ from .documents import Block
CHUNK_SIZE
=
250
CHUNK_SIZE
=
250
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
(
glob
.
glob
(
os
.
path
.
join
(
folder
,
"
chunk_*
"
)))
# number of files starting with "chunk_"
self
.
chunks
=
len
(
self
.
current_block_in_chunk
=
0
# number from 0 to 249 equal to current_block // current_chunk
glob
.
glob
(
os
.
path
.
join
(
folder
,
"
chunk_*
"
))
self
.
current_chunk
=
0
# current chunk number
)
# number of files starting with "chunk_"
self
.
chunk
=
[]
# parsed json for current chunk (length = 250)
self
.
current_block_in_chunk
=
(
self
.
parsechunk
()
# parse first chunk
0
# number from 0 to 249 equal to current_block // current_chunk
)
self
.
current_chunk
=
0
# current chunk number
self
.
chunk
=
[]
# parsed json for current chunk (length = 250)
self
.
parsechunk
()
# parse first chunk
def
parsechunk
(
self
):
def
parsechunk
(
self
):
"""
parse a json chunk file
"""
"""
parse a json chunk file
"""
with
open
(
os
.
path
.
join
(
self
.
folder
,
f
"
chunk_
{
self
.
current_chunk
}
-250.json
"
))
as
f
:
with
open
(
os
.
path
.
join
(
self
.
folder
,
f
"
chunk_
{
self
.
current_chunk
}
-250.json
"
)
)
as
f
:
s
=
f
.
read
()
s
=
f
.
read
()
p
=
json
.
loads
(
s
)
p
=
json
.
loads
(
s
)
self
.
chunk
=
p
[
"
blocks
"
]
self
.
chunk
=
p
[
"
blocks
"
]
...
@@ -35,14 +42,19 @@ class JsonBlockchain():
...
@@ -35,14 +42,19 @@ class JsonBlockchain():
def
__next__
(
self
):
def
__next__
(
self
):
"""
if current block is outside current chunk, parse next one, otherwise, return current block parsed json
"""
"""
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
self
.
current_block_in_chunk
==
250
:
# 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
+=
1
# increment current block number for next iteration
self
.
current_block_in_chunk
+=
(
return
self
.
chunk
[
self
.
current_block_in_chunk
-
1
]
# return block (before incrementation)
1
# increment current block number for next iteration
)
return
self
.
chunk
[
self
.
current_block_in_chunk
-
1
]
# return block (before incrementation)
def
Blockchain
(
json_blockchain
):
def
Blockchain
(
json_blockchain
):
"""
convert json to duniterpy block document
"""
"""
convert json to duniterpy block document
"""
...
@@ -50,12 +62,11 @@ def Blockchain(json_blockchain):
...
@@ -50,12 +62,11 @@ def Blockchain(json_blockchain):
for
jb
in
jbc
:
for
jb
in
jbc
:
yield
Block
.
from_parsed_json
(
jb
)
yield
Block
.
from_parsed_json
(
jb
)
def
load
(
path
=
"
.config/duniter/duniter_default/g1/
"
):
def
load
(
path
=
"
.config/duniter/duniter_default/g1/
"
):
"""
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
=
os
.
path
.
join
(
os
.
path
.
expanduser
(
"
~
"
),
path
)
# 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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment