Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 6def779

Browse files
authored
Use json.dump in FileExfiltrationWriter (#15095)
To directly write to the open file, instead of writing to an in-memory string first.
1 parent 91f8de7 commit 6def779

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

changelog.d/15095.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Refactor writing json data in `FileExfiltrationWriter`.

synapse/app/admin_cmd.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ def write_events(self, room_id: str, events: List[EventBase]) -> None:
149149

150150
with open(events_file, "a") as f:
151151
for event in events:
152-
print(json.dumps(event.get_pdu_json()), file=f)
152+
json.dump(event.get_pdu_json(), fp=f)
153153

154154
def write_state(
155155
self, room_id: str, event_id: str, state: StateMap[EventBase]
@@ -162,7 +162,7 @@ def write_state(
162162

163163
with open(event_file, "a") as f:
164164
for event in state.values():
165-
print(json.dumps(event.get_pdu_json()), file=f)
165+
json.dump(event.get_pdu_json(), fp=f)
166166

167167
def write_invite(
168168
self, room_id: str, event: EventBase, state: StateMap[EventBase]
@@ -178,7 +178,7 @@ def write_invite(
178178

179179
with open(invite_state, "a") as f:
180180
for event in state.values():
181-
print(json.dumps(event), file=f)
181+
json.dump(event, fp=f)
182182

183183
def write_knock(
184184
self, room_id: str, event: EventBase, state: StateMap[EventBase]
@@ -194,15 +194,15 @@ def write_knock(
194194

195195
with open(knock_state, "a") as f:
196196
for event in state.values():
197-
print(json.dumps(event), file=f)
197+
json.dump(event, fp=f)
198198

199199
def write_profile(self, profile: JsonDict) -> None:
200200
user_directory = os.path.join(self.base_directory, "user_data")
201201
os.makedirs(user_directory, exist_ok=True)
202202
profile_file = os.path.join(user_directory, "profile")
203203

204204
with open(profile_file, "a") as f:
205-
print(json.dumps(profile), file=f)
205+
json.dump(profile, fp=f)
206206

207207
def write_devices(self, devices: List[JsonDict]) -> None:
208208
user_directory = os.path.join(self.base_directory, "user_data")
@@ -211,7 +211,7 @@ def write_devices(self, devices: List[JsonDict]) -> None:
211211

212212
for device in devices:
213213
with open(device_file, "a") as f:
214-
print(json.dumps(device), file=f)
214+
json.dump(device, fp=f)
215215

216216
def write_connections(self, connections: List[JsonDict]) -> None:
217217
user_directory = os.path.join(self.base_directory, "user_data")
@@ -220,7 +220,7 @@ def write_connections(self, connections: List[JsonDict]) -> None:
220220

221221
for connection in connections:
222222
with open(connection_file, "a") as f:
223-
print(json.dumps(connection), file=f)
223+
json.dump(connection, fp=f)
224224

225225
def write_account_data(
226226
self, file_name: str, account_data: Mapping[str, JsonDict]
@@ -233,7 +233,7 @@ def write_account_data(
233233
account_data_file = os.path.join(account_data_directory, file_name)
234234

235235
with open(account_data_file, "a") as f:
236-
print(json.dumps(account_data), file=f)
236+
json.dump(account_data, fp=f)
237237

238238
def finished(self) -> str:
239239
return self.base_directory

0 commit comments

Comments
 (0)