Skip to content

Commit

Permalink
rename 'main.py' -> 'cli.py'
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed-chs committed Oct 19, 2023
1 parent 9bf9e25 commit 5223c11
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ The [Project Todo](TODO.md) and [JavaScript Todo](js/how_to_use.md#still-working
See also a rough internal dependency graph of the project [here](assets/deps_graph.png) (the graph is not complete, but it gives a general idea of the project's structure). Generated using :

```bash
pydeps main.py -o assets/deps_graph.png -T png --noshow --reverse --rankdir BT --exclude-exact models views controllers
pydeps cli.py -o assets/deps_graph.png -T png --noshow --reverse --rankdir BT --exclude-exact models views controllers
```

## Documentation
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ powershell -ExecutionPolicy ByPass -File .venv\Scripts\Activate.ps1
With the environment set up, you can now run the script. In the terminal or command prompt, execute:

```bash
python main.py
python cli.py
```

Now, follow the instructions on screen and choose your desired options, the script will handle the rest.
Expand Down
16 changes: 7 additions & 9 deletions main.py → cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Main file for testing the program."""
"""Main file for running the program from the command line."""

import sys
from pathlib import Path
Expand All @@ -11,12 +11,12 @@
update_config_file,
)
from controllers.file_system import (
conversation_set_from_json,
conversation_set_from_zip,
create_n_save_all_weekwise_graphs,
generate_n_save_all_wordclouds,
get_bookmarklet_json_filepath,
load_conversations_from_bookmarklet_json,
load_conversations_from_openai_zip,
save_conversation_set_to_dir,
save_conversation_set,
save_custom_instructions_to_file,
)
from models.conversation_set import ConversationSet
Expand Down Expand Up @@ -51,16 +51,14 @@ def main() -> None:

zip_filepath = Path(configs_dict["zip_file"])

all_conversations_set: ConversationSet = load_conversations_from_openai_zip(
all_conversations_set: ConversationSet = conversation_set_from_zip(
zip_filepath=zip_filepath,
)

bookmarklet_json_filepath: Path | None = get_bookmarklet_json_filepath()
if bookmarklet_json_filepath:
print("Found bookmarklet download, loading 📂 ...\n")
bookmarklet_conversations_set: (
ConversationSet
) = load_conversations_from_bookmarklet_json(
bookmarklet_conversations_set: ConversationSet = conversation_set_from_json(
json_filepath=bookmarklet_json_filepath,
)
all_conversations_set.update(conv_set=bookmarklet_conversations_set)
Expand All @@ -76,7 +74,7 @@ def main() -> None:
markdown_folder: Path = output_folder / "Markdown"
markdown_folder.mkdir(parents=True, exist_ok=True)

save_conversation_set_to_dir(
save_conversation_set(
conv_set=all_conversations_set,
dir_path=markdown_folder,
)
Expand Down
4 changes: 2 additions & 2 deletions controllers/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from models.node import Node
from views.prompt_user import prompt_user

from .file_system import default_output_folder, get_openai_zip_filepath
from .file_system import default_output_folder, most_recently_downloaded_zip


def get_user_configs() -> dict[str, Any]:
Expand All @@ -21,7 +21,7 @@ def get_user_configs() -> dict[str, Any]:
default_configs = json_load(fp=file)

if not default_configs["zip_file"]:
default_configs["zip_file"] = get_openai_zip_filepath()
default_configs["zip_file"] = most_recently_downloaded_zip()

if not default_configs["output_folder"]:
default_configs["output_folder"] = default_output_folder()
Expand Down
34 changes: 11 additions & 23 deletions controllers/file_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)


def load_conversations_from_openai_zip(zip_filepath: Path) -> ConversationSet:
def conversation_set_from_zip(zip_filepath: Path) -> ConversationSet:
"""Load the conversations from the OpenAI zip export file."""
with ZipFile(file=zip_filepath, mode="r") as file:
file.extractall(path=zip_filepath.with_suffix(suffix=""))
Expand All @@ -44,15 +44,15 @@ def load_conversations_from_openai_zip(zip_filepath: Path) -> ConversationSet:
return ConversationSet(conversations=conversations)


def load_conversations_from_bookmarklet_json(json_filepath: Path) -> ConversationSet:
def conversation_set_from_json(json_filepath: Path) -> ConversationSet:
"""Load the conversations from the bookmarklet json export file."""
with open(file=json_filepath, encoding="utf-8") as file:
conversations = json_load(fp=file)

return ConversationSet(conversations=conversations)


def save_conversation_to_file(conversation: Conversation, filepath: Path) -> None:
def save_conversation(conversation: Conversation, filepath: Path) -> None:
"""Save a conversation to a file, with added modification time."""
base_file_name: str = filepath.stem

Expand All @@ -68,14 +68,14 @@ def save_conversation_to_file(conversation: Conversation, filepath: Path) -> Non
utime(path=filepath, times=(conversation.update_time, conversation.update_time))


def save_conversation_set_to_dir(conv_set: ConversationSet, dir_path: Path) -> None:
def save_conversation_set(conv_set: ConversationSet, dir_path: Path) -> None:
"""Save a conversation set to a directory, one markdown file per conversation."""
for conversation in tqdm(
iterable=conv_set.conversation_list,
desc="Writing Markdown 📄 files",
):
file_path: Path = dir_path / f"{conversation.sanitized_title()}.md"
save_conversation_to_file(conversation=conversation, filepath=file_path)
save_conversation(conversation=conversation, filepath=file_path)


def save_weekwise_graph_from_conversation_set(
Expand Down Expand Up @@ -103,8 +103,6 @@ def save_weekwise_graph_from_conversation_set(
fname=dir_path / file_name,
dpi=300,
)
else:
raise ValueError("Invalid time period for weekwise graph")


def create_n_save_all_weekwise_graphs(
Expand Down Expand Up @@ -212,37 +210,27 @@ def save_custom_instructions_to_file(conv_set: ConversationSet, filepath: Path)


def default_output_folder() -> str:
"""Returns the default output folder path.
(put the function in a separate file to isolate file system operations)
"""
"""Returns the default output folder path : ~/Documents/ChatGPT Data"""
# put the function here to isolate file system operations
return str(object=Path.home() / "Documents" / "ChatGPT Data")


def get_openai_zip_filepath() -> str:
"""Returns the path to the most recent zip file in the Downloads folder,
excluding those containing 'bookmarklet'.
"""
def most_recently_downloaded_zip() -> str:
"""Path to the most recently created zip file in the Downloads folder."""
downloads_folder: Path = Path.home() / "Downloads"

# Filter out zip files with names that contain "bookmarklet"
zip_files: list[Path] = [
x for x in downloads_folder.glob(pattern="*.zip") if "bookmarklet" not in x.name
]
zip_files: list[Path] = [x for x in downloads_folder.glob(pattern="*.zip")]

if not zip_files:
return ""

# Most recent zip file in downloads folder, excluding those containing "bookmarklet"
default_zip_filepath: Path = max(zip_files, key=lambda x: x.stat().st_ctime)

return str(object=default_zip_filepath)


def get_bookmarklet_json_filepath() -> Path | None:
"""Returns the path to the most recent json file in the Downloads folder,
containing 'bookmarklet'.
"""
"""Path to the most recently downloaded JSON file, with "bookmarklet" in the name."""
downloads_folder: Path = Path.home() / "Downloads"

# Filter out json files with names that do not contain "bookmarklet"
Expand Down
2 changes: 1 addition & 1 deletion js/how_to_use.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Alternatively, you can create a bookmarklet with the code in [this file](bookmar

(You should refresh the page after the download finishes, to clear the UI widget and the console logs.)

Now, if you run the `main.py` script, it should recognize the new downloaded json file and add the conversations to the ones from the OpenAI export, that way ALL the conversations are converted to markdown files, as well as the other data visualizations stuff.
Now, if you run the `cli.py` script, it should recognize the new downloaded json file and add the conversations to the ones from the OpenAI export, that way ALL the conversations are converted to markdown files, as well as the other data visualizations stuff.

This is a very rudimentary js script, and it needs more error handling. I've tried it on Chrome, and it works so far.
Could break at anytime if OpenAI changes their data permissions or the `/backend-api/` API.
Expand Down
2 changes: 1 addition & 1 deletion playground.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"\n",
"But I get the worry of accidentally breaking things when contributing code. So, I made this notebook to help with that. It's a work in progress, aimed at letting you easily see specific outputs of interest for smoother development.\n",
"\n",
"Previously, my basic testing meant using a `test.py` file to generate a few markdowns and then checking them manually. Or, for a deeper look, I'd run the `main.py` and wait a while to see everything, which isn't quick on my laptop.\n",
"Previously, my basic testing meant using a `test.py` file to generate a few markdowns and then checking them manually. Or, for a deeper look, I'd run the `cli.py` and wait a while to see everything, which isn't quick on my laptop.\n",
"\n",
"This notebook aims to streamline that process, letting you test and inspect targeted parts of the output without the fear of breaking things."
]
Expand Down

0 comments on commit 5223c11

Please sign in to comment.