Skip to content

Commit

Permalink
exporting updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaya-Sem committed Jul 14, 2024
1 parent cc6a570 commit 9cc0506
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 42 deletions.
43 changes: 43 additions & 0 deletions src/export_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import click
from database import get_collection

@click.command()
@click.argument('collection_id', type=int)
@click.argument('output_file', type=click.Path(writable=True))
def checklist(collection_id, output_file):
"""
Export a collection of items as a markdown checklist.
COLLECTION_ID: The ID of the collection to export.
OUTPUT_FILE: The file to write the markdown checklist to.
"""
try:
collection = get_collection(collection_id)
markdown_content = generate_markdown_checklist(collection)

with open(output_file, 'w') as file:
file.write(markdown_content)

click.echo(f"Collection {collection_id} exported to {output_file} successfully.")
except Exception as e:
click.echo(f"An error occurred: {e}")

def generate_markdown_checklist(collection):
"""
Generate a markdown checklist for a given collection.
"""
checklist = f"# {collection.name}\n"
checklist += f"{collection.description}\n\n"

for category, items_list in collection.items.items():
checklist += f"### {category}\n\n"
for item in items_list:
name = pad_string(item.name, 30)
checklist += f"- [ ] {name} ({item.note})\n"
checklist += f"\n"

return checklist

def pad_string(s, width):
return s + ' ' * (width - len(s))

98 changes: 56 additions & 42 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
delete_collection,
)

from export_commands import checklist

# I am not totally sure what this rich console does or out it works.
console = Console()

Expand All @@ -27,18 +29,53 @@
def cli():
pass

# Register the checklist command
cli.add_command(checklist)


# NOTE: subcommands for 'add'
@cli.group()
def add():
"""Add items to collections"""
pass


@add.command()
@click.argument('item_ids', nargs=-1, type=int, required=False)
@click.option('--collection', 'collection_id', type=int, help='Collection ID to add items to')
@click.option('--interactive', is_flag=True, help='Interactive mode to select items and collections')
def item(item_ids, collection_id, interactive):
"""
Add items to a collection.
"""
if interactive:
handle_interactive_add()
else:
if not item_ids:
click.echo("You must provide at least one item ID.")
return

if not collection_id:
click.echo("You must provide a collection ID.")
return

try:
add_items_to_collection(collection_id, item_ids)
except Exception as e:
click.echo(f"An error occurred: {e}")


@cli.group()
def view():
"""View items, collections, etc."""
"""View items and collections."""
pass


# NOTE: subcommands for 'list'

@cli.group()
def list():
"""List items, collections, etc."""
"""List items and collections."""
pass


Expand Down Expand Up @@ -71,12 +108,12 @@ def collections():
# NOTE: subcommands for 'add'

@cli.group()
def add():
"""Add items, collections, etc."""
def create():
"""Create items and collections."""
pass


@add.command()
@create.command()
def item():
name = click.prompt("Enter the name of the item", type=str)
weight = click.prompt("Enter the weight of the item", type=float)
Expand All @@ -86,7 +123,7 @@ def item():
create_item(name, weight, category, note)


@add.command()
@create.command()
def collection():
name = click.prompt("Enter the name of the collection", type=str)
description = click.prompt("Enter the description of the collection", type=str)
Expand All @@ -98,8 +135,8 @@ def collection():

@view.command()
@click.argument("id", required=False, type=int)
def collection(col_id):
if col_id is None:
def collection(id):
if id is None:
collections = get_collections()
if collections:
print(f"\nAvailable collections [dim]({len(collections)})[/dim]:\n")
Expand All @@ -108,10 +145,10 @@ def collection(col_id):
else:
click.echo("No collections found in the database")

col_id = click.prompt("Enter the ID of the collection you want to view", type=int)
id = click.prompt("Enter the ID of the collection you want to view", type=int)

try:
collection = get_collection(col_id)
collection = get_collection(id)
view_collection(collection)
except ValueError as e:
click.echo(str(e))
Expand Down Expand Up @@ -144,8 +181,8 @@ def print_collection_items(items):

@view.command()
@click.argument("id", required=False, type=int)
def item(it_id):
if it_id is None:
def item(id):
if id is None:
items = get_items()
if items:
click.echo("Available items:")
Expand All @@ -155,10 +192,10 @@ def item(it_id):
click.echo("No items found in the database.")
sys.exit()

it_id = click.prompt("Enter the ID of the item you want to view", type=int)
id = click.prompt("Enter the ID of the item you want to view", type=int)

try:
item = get_item(it_id)
item = get_item(id)
print(
f"Item ID: {item.id}, Name: {item.name}, Weight: {item.weight}, Category: {item.category}"
)
Expand All @@ -170,14 +207,14 @@ def item(it_id):

@cli.group()
def delete():
"""Delete items, collections, etc."""
"""Delete items and collections."""
pass


@delete.command()
@click.argument("id", required=False, type=int)
def item(it_id):
if it_id is None:
def item(id):
if id is None:
items = get_items()
if items:
print(f"{len(items)} available items:")
Expand All @@ -187,10 +224,10 @@ def item(it_id):
print("No items found in the database.")
sys.exit()

it_id = click.prompt("Enter the ID of the item you want to delete", type=int)
id = click.prompt("Enter the ID of the item you want to delete", type=int)

try:
delete_item(it_id)
delete_item(id)
except ValueError as e:
click.echo(str(e))

Expand All @@ -216,28 +253,5 @@ def collection(collection_id):
click.echo(str(e))


# NOTE: subcommands for "collection"

@cli.group()
def collection():
"""Add and remove items to collections."""
pass


# TODO: implement improved "collection" logic
@collection.command("add-item")
@click.argument("collection_id", type=int)
@click.argument("item_ids", nargs=-1, type=int) # Accept multiple item_ids
def add_item_to_collection(collection_id, item_ids):
if not item_ids:
click.echo("You must provide at least one item ID.")
return

try:
add_items_to_collection(collection_id, item_ids)
except Exception as e:
click.echo(f"An error occurred: {e}")


if __name__ == "__main__":
cli()
13 changes: 13 additions & 0 deletions src/test1
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Kungsleden

Zweden met Milan ♥︎

## Items

### Shelter
- [ ] Bonfus Middus 2 (900.0 Shelter)
- [ ] Mongar 2 (2400.0 Shelter)
- [ ] Hilleberg Soulo (2800.0 Shelter)
### Sleep
- [ ] Thermarest XTherm (580.0 Sleep)
- [ ] Cumulus XLite 200 (380.0 Sleep)
12 changes: 12 additions & 0 deletions src/test2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Kungsleden
Zweden met Milan ♥︎

## Items

### Shelter
- [ ] Bonfus Middus 2 (900.0 Shelter)
- [ ] Mongar 2 (2400.0 Shelter)
- [ ] Hilleberg Soulo (2800.0 Shelter)
### Sleep
- [ ] Thermarest XTherm (580.0 Sleep)
- [ ] Cumulus XLite 200 (380.0 Sleep)

0 comments on commit 9cc0506

Please sign in to comment.