Skip to content

Commit

Permalink
black config
Browse files Browse the repository at this point in the history
  • Loading branch information
mergemaven11 committed May 19, 2024
1 parent eb3f4c9 commit 6e3ee40
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 36 deletions.
40 changes: 24 additions & 16 deletions cli/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@ def add_entry_to_file(entry_obj: t.Dict[str, t.Any], entries_file: str) -> None:
None
"""
# Load existing entries from the file
with open(entries_file, 'r') as f:
with open(entries_file, "r") as f:
entries_data: list = json.load(f)

# Generate a unique ID and timestamp for the new entry
entry_obj['id'] = str(uuid.uuid4())
entry_obj['timestamp'] = utils.current_datetime()
entry_obj["id"] = str(uuid.uuid4())
entry_obj["timestamp"] = utils.current_datetime()

# Append the new entry object to the existing list
entries_data.append(entry_obj)

# Write the updated entry data back to the file
with open(entries_file, 'w') as f:
with open(entries_file, "w") as f:
json.dump(entries_data, f, indent=4) # indent for pretty printing


# Searching an entry


def search_entries(keyword: str):
""" Search Entries by title, dropdown selection for multiple finds.
"""Search Entries by title, dropdown selection for multiple finds.
View all vs view one
Expand All @@ -61,18 +63,25 @@ def search_entries(keyword: str):
# Use InquirerPy to present a dropdown selection
questions = [
{
'type': 'list',
'name': 'selected_entry',
'message': "Select an entry to view:",
'choices': choices
"type": "list",
"name": "selected_entry",
"message": "Select an entry to view:",
"choices": choices,
}
]

answers = prompt(questions)

# Find the selected entry
selected_entry = next((item for item in search_items if f"{item['title']} - {item['timestamp']}" == answers['selected_entry']), None)

selected_entry = next(
(
item
for item in search_items
if f"{item['title']} - {item['timestamp']}" == answers["selected_entry"]
),
None,
)

if selected_entry:
# Pretty-print the selected entry using rich
console.print("[bold green]Selected Entry:[/bold green]")
Expand All @@ -81,11 +90,10 @@ def search_entries(keyword: str):
console.print("[bold red]No entry selected.[/bold red]")



# Viewing an entry
# Viewing an entry

# Delete Entries

# Edit Entries

# Export Entries
# Export Entries
25 changes: 13 additions & 12 deletions cli/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@

from datetime import datetime, timezone

ENTRIES_DIR = './entries'
ENTRIES_FILE = './entries/entries.json'
ENTRIES_DIR = "./entries"
ENTRIES_FILE = "./entries/entries.json"


def check_entries_dir():
"""
Check if entries directory is present, and if not, create the directory and an initial JSON file.
"""
if not os.path.exists(ENTRIES_DIR):
os.makedirs(ENTRIES_DIR)
with open(ENTRIES_FILE, 'w') as f:
with open(ENTRIES_FILE, "w") as f:
# Create an empty array to store entries initially
json.dump([], f)

return ENTRIES_FILE



def current_datetime():
"""
Get the current UTC datetime and format it to a more human-readable format.
"""
# Get the current datetime in UTC timezone
utc_datetime = datetime.now(timezone.utc) # TODO handle tz
utc_datetime = datetime.now(timezone.utc) # TODO handle tz

# Format the datetime object to a more human-readable format
formatted_datetime = utc_datetime.strftime("%m/%d/%Y @ %I:%M:%S %p")
Expand All @@ -40,24 +40,25 @@ def search(keyword: str) -> t.List[t.Dict[str, str]]:
Args:
keyword (str): Keyword used to query JSON file.
Returns:
search_data (List): List of entries matching the keyword.
TODO: - Fix permissions issue
"""
search_data = []

if os.path.exists(ENTRIES_DIR):
with open(ENTRIES_DIR, 'r') as file:
with open(ENTRIES_DIR, "r") as file:
entries = json.load(file)

for entry in entries:
if re.search(keyword, entry['title'], re.IGNORECASE) or re.search(keyword, entry['content'], re.IGNORECASE):
if re.search(keyword, entry["title"], re.IGNORECASE) or re.search(
keyword, entry["content"], re.IGNORECASE
):
search_data.append(entry)

if not search_data:
print(f"There are no entries with keyword: {keyword}")

return search_data

return search_data
15 changes: 8 additions & 7 deletions journal.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
# def main():
# print("Hello World")


# Add imported commands to the main Typer application instance
@app.command()
def add():
title = typer.prompt("Please enter a title")
entry = typer.prompt("Please enter your journal entry")
# Send data to add handler
handler.add_entry_to_file({"Title": title, "Entry": entry})


@app.command()
def view(name: str):
typer.echo(f"Hello {name}")
Expand All @@ -26,12 +27,12 @@ def view(name: str):
def export(name: str):
typer.echo(f"Hello {name}")


@app.command()
def search(keyword: str):
def search(keyword: str):
handler.search_entries(keyword)



@app.command()
def delete(name: str):
typer.echo(f"Hello {name}")
Expand All @@ -43,6 +44,6 @@ def edit(name: str):


if __name__ == "__main__":
# Check if first entry, if so create the dir and file.
util.check_entries_dir()
app()
# Check if first entry, if so create the dir and file.
util.check_entries_dir()
app()
84 changes: 83 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,26 @@ freezegun = "^1.5.1"
inquirerpy = "^0.3.4"
rich = "^13.7.1"

[tool.poetry.group.dev.dependencies]
black = "^24.4.2"

[tool.black]
line-length = 88
target-version = ['py38', 'py39', 'py312']
exclude = '''
/(
\.git
| \.hg
| \.mypy_cache
| \.tox
| \.venv
| _build
| buck-out
| build
| dist
| (?!cli|journal\.py$).* # Exclude everything except cli directory and journal.py
)/
'''

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit 6e3ee40

Please sign in to comment.