This repository was archived by the owner on Aug 25, 2024. It is now read-only.
File tree 3 files changed +13
-10
lines changed
3 files changed +13
-10
lines changed Original file line number Diff line number Diff line change @@ -76,7 +76,7 @@ async def _close(self):
76
76
elif self .config .filename [::- 1 ].startswith ((".zip" )[::- 1 ]):
77
77
close = self .zip_closer_helper ()
78
78
else :
79
- close = open (self .config .filename , "w" )
79
+ close = open (self .config .filename , "w+ " )
80
80
with close as fd :
81
81
await self .dump_fd (fd )
82
82
Original file line number Diff line number Diff line change @@ -17,21 +17,24 @@ class JSONSource(FileSource, MemorySource):
17
17
stored in memory.
18
18
"""
19
19
20
- def __init__ (self , config ):
21
- super ().__init__ (config )
22
- self .repos = {}
23
-
24
20
async def load_fd (self , fd ):
25
- self . repos = json .load (fd )
21
+ repos = json .load (fd )
26
22
self .mem = {
27
23
src_url : Repo (src_url , data = data )
28
- for src_url , data in self . repos .get (self .config .label , {}).items ()
24
+ for src_url , data in repos .get (self .config .label , {}).items ()
29
25
}
30
26
LOGGER .debug ("%r loaded %d records" , self , len (self .mem ))
31
27
32
28
async def dump_fd (self , fd ):
33
- self .repos [self .config .label ] = {
29
+ repos = {}
30
+ if fd .seekable ():
31
+ # Empty Stream is not a Valid JSON
32
+ if fd .seek (0 ) is not 0 :
33
+ repos = json .load (fd )
34
+ fd .seek (0 )
35
+ fd .truncate (0 )
36
+ repos [self .config .label ] = {
34
37
repo .src_url : repo .dict () for repo in self .mem .values ()
35
38
}
36
- json .dump (self . repos , fd )
39
+ json .dump (repos , fd )
37
40
LOGGER .debug ("%r saved %d records" , self , len (self .mem ))
Original file line number Diff line number Diff line change @@ -181,7 +181,7 @@ async def test_close(self):
181
181
):
182
182
async with FakeFileSource (self .config ("testfile" )):
183
183
pass
184
- m_open .assert_called_once_with ("testfile" , "w" )
184
+ m_open .assert_called_once_with ("testfile" , "w+ " )
185
185
186
186
async def test_close_gz (self ):
187
187
m_open = mock_open ()
You can’t perform that action at this time.
0 commit comments