Commit 5586da59 authored by Aziz Berkay Yesilyurt's avatar Aziz Berkay Yesilyurt
Browse files

add folder logic

parent 37896465
Showing with 27 additions and 8 deletions
+27 -8
......@@ -20,9 +20,7 @@ def make_message(content):
# A better way would be to preassign a timestamp for each message
time.sleep(0.001)
return Message(
service=SERVICE, content=content, dateSent=datetime.datetime.now()
)
return Message(service=SERVICE, content=content, dateSent=datetime.datetime.now())
def create_chat(
......@@ -47,18 +45,39 @@ def create_chat(
items.extend((make_message(msg) for msg in messages))
json_items = [item.to_json() for item in items]
return json.dumps(json_items, indent=2)
return json_items
def main(file : pathlib.Path):
def write_json(file, output_file):
with open(file, "r") as f:
data = json.load(f)
chat_items = create_chat(
messages=data["messages"],
user_names=data["user_names"],
channel_name=data["channel_name"],
)
with open(output_file, "w") as f:
json.dump(chat_items, f, indent=2)
def main(file: pathlib.Path = None, folder: pathlib.Path = None):
"""
Read json from file and create a sample database with two users and a channel.
Args:
file : path to json file
"""
with open(file, 'r') as f:
data = json.load(f)
return create_chat(messages=data['messages'], user_names=data['user_names'], channel_name=data['channel_name'])
file = pathlib.Path(file) if file else None
folder = pathlib.Path(folder) if folder else None
if file is None and folder is None:
raise RuntimeError("Either file or folder must be specified")
output_folder = pathlib.Path("output")
if file:
write_json(file, output_folder / file.name)
if folder:
for file in folder.glob("*.json"):
write_json(file, output_folder / file.name)
if __name__ == "__main__":
# Make Python Fire not use a pager when it prints a help text
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment