Skip to content

Commit

Permalink
Merge pull request #5 from berrysauce/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
berrysauce committed Sep 25, 2023
2 parents 65a8ce1 + 8add450 commit 8f246af
Show file tree
Hide file tree
Showing 7 changed files with 1,073 additions and 141 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<h1 align="center">detapack 📄</h1>
<h1 align="center">📄 detapack</h1>
<p align="center"><strong>Import/Export data from/to Deta Bases</strong></p>
<p align="center">
<img alt="PyPI - Python Version" src="https://img.shields.io/pypi/pyversions/detapack">
Expand All @@ -17,3 +17,19 @@ detapack can be installed from the Python Package Index.
pip install detapack
```
Run `detapack version` to check if detapack was installed successfully. You may need to add detapack to your shell configuration.

## Commands

Detapack is mainly used with two commands:

```bash
detapack export <BASE NAME>
```

```bash
detapack import <BASE NAME> <PATH TO JSON>
```

Both commands will ask you for your Deta project key, which will be used to access your bases. For security reasons, detapack asks for this key every time and doesn't store it anywhere.

You can read more about each command by putting `-- help` at the end.
109 changes: 67 additions & 42 deletions detapack/main.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import os
import json
import typer
from rich.progress import Progress, SpinnerColumn, TextColumn, track
from deta import Deta
from progress.bar import ChargingBar


# app configuration
app = typer.Typer(help="detapack: Import/Export data from/to Deta Bases")
APP_NAME = "detapack"
APP_VERSION = "0.1.2"
APP_VERSION = "0.1.3"


@app.command(name="version")
Expand All @@ -29,58 +29,83 @@ def version_command():
github.com/berrysauce/detapack\n""",
fg=typer.colors.BRIGHT_BLACK)


@app.command(name="export")
def export_command(key: str, basename: str):
def export_command(
basename: str,
):
"""
Export Deta Base to a local .json file
"""

typer.echo("Connecting to Deta...")
try:
deta = Deta(key)
base = deta.Base(basename)
except AssertionError:
typer.secho("ERROR! Bad project key provided", fg=typer.colors.BRIGHT_RED)
return

typer.echo("Receiving data...")
res = base.fetch().items

typer.echo("Writing data...")
target = "export.json"
destination = str(os.getcwd()) + "/" + target

with open(destination, "w") as f:
f.write(json.dumps(res))

key = typer.prompt("Enter your project key", hide_input=True, type=str)

with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True
) as progress:
progress.add_task("Connecting to Deta...")

try:
deta = Deta(key)
base = deta.Base(basename)
except AssertionError:
typer.secho("ERROR! Bad project key provided", fg=typer.colors.BRIGHT_RED)
return

progress.add_task("Receiving data...")

res = base.fetch().items

progress.add_task("Writing data...")

target = "export.json"
destination = str(os.getcwd()) + "/" + target

with open(destination, "w") as f:
f.write(json.dumps(res, indent=4))

typer.secho(f"DONE! Saved as {target} in {str(os.getcwd())}", fg=typer.colors.BRIGHT_GREEN)


@app.command(name="import")
def import_command(key: str, basename: str, path: str):
def import_command(
basename: str,
path: str
):
"""
Import local .json file to Deta Base
"""

typer.echo("Connecting to Deta...")
try:
deta = Deta(key)
base = deta.Base(basename)
except AssertionError:
typer.secho("ERROR! Bad project key provided", fg=typer.colors.BRIGHT_RED)
return

typer.echo(f"Reading data from {path}...")
data = ""
try:
with open(path, "r") as f:
data = f.read()
except FileNotFoundError:
typer.secho("ERROR! File to import doesn't exist", fg=typer.colors.BRIGHT_RED)
return

typer.echo(f"Importing to Deta Base...")

key = typer.prompt("Enter your project key", hide_input=True, type=str)

with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True
) as progress:
progress.add_task("Connecting to Deta...")

try:
deta = Deta(key)
base = deta.Base(basename)
except AssertionError:
typer.secho("ERROR! Bad project key provided", fg=typer.colors.BRIGHT_RED)
return

progress.add_task(f"Reading data from {path}...")

data = ""
try:
with open(path, "r") as f:
data = f.read()
except FileNotFoundError:
typer.secho("ERROR! File to import doesn't exist", fg=typer.colors.BRIGHT_RED)
return

data = json.loads(data)
for item in ChargingBar("Importing").iter(data):
for item in track(iter(data), description="Importing..."):
base.put(item)

typer.secho(f"DONE! Imported data to {basename}", fg=typer.colors.BRIGHT_GREEN)
Loading

0 comments on commit 8f246af

Please sign in to comment.