|
1 | | -# python-snacks |
| 1 | +# Snack Stash |
| 2 | + |
| 3 | +A personal CLI tool for managing a local stash of reusable Python code snippets. Browse, copy, and curate snippets across projects with a single command. |
| 4 | + |
| 5 | +```bash |
| 6 | +pipx install snack-stash |
| 7 | +snack stash create default ~/snack-stash |
| 8 | +snack unpack auth/google_oauth.py |
| 9 | +``` |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +**Recommended (pipx):** |
| 16 | +```bash |
| 17 | +pipx install snack-stash |
| 18 | +``` |
| 19 | + |
| 20 | +**Alternative (pip):** |
| 21 | +```bash |
| 22 | +pip install snack-stash |
| 23 | +``` |
| 24 | + |
| 25 | +Requires Python 3.10+. |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## Configuration |
| 30 | + |
| 31 | +### First-time setup |
| 32 | + |
| 33 | +Create a stash directory and register it: |
| 34 | + |
| 35 | +```bash |
| 36 | +snack stash create default ~/snack-stash |
| 37 | +``` |
| 38 | + |
| 39 | +This creates `~/snack-stash`, writes `~/.snackstashrc`, and sets `default` as the active stash. |
| 40 | + |
| 41 | +### Environment variable (overrides config file) |
| 42 | + |
| 43 | +```bash |
| 44 | +export SNACK_STASH=~/snack-stash |
| 45 | +``` |
| 46 | + |
| 47 | +Add to `~/.zshrc` or `~/.bashrc` to make it permanent. Takes priority over everything else. |
| 48 | + |
| 49 | +### Config file (`~/.snackstashrc`) |
| 50 | + |
| 51 | +Created automatically by `snack stash create`. You can also edit it by hand: |
| 52 | + |
| 53 | +```ini |
| 54 | +[config] |
| 55 | +active = default |
| 56 | + |
| 57 | +[stash.default] |
| 58 | +path = ~/snack-stash |
| 59 | + |
| 60 | +[stash.work] |
| 61 | +path = ~/work-stash |
| 62 | +``` |
| 63 | + |
| 64 | +**Priority order:** `SNACK_STASH` env var → `~/.snackstashrc` active stash → error. |
| 65 | + |
| 66 | +--- |
| 67 | + |
| 68 | +## Stash structure |
| 69 | + |
| 70 | +A stash is a plain directory of `.py` files, organized into subdirectories by category: |
| 71 | + |
| 72 | +``` |
| 73 | +~/snack-stash/ |
| 74 | +├── auth/ |
| 75 | +│ ├── google_oauth_fastapi.py |
| 76 | +│ ├── google_oauth_flask.py |
| 77 | +│ └── jwt_helpers.py |
| 78 | +├── forms/ |
| 79 | +│ ├── contact_form.py |
| 80 | +│ └── newsletter_signup.py |
| 81 | +└── email/ |
| 82 | + └── smtp_sender.py |
| 83 | +``` |
| 84 | + |
| 85 | +You can manage the directory with Git, Dropbox, or any sync tool independently of this CLI. |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## Commands |
| 90 | + |
| 91 | +### `snack list [category]` |
| 92 | + |
| 93 | +List all snippets in the active stash. |
| 94 | + |
| 95 | +```bash |
| 96 | +snack list # all snippets |
| 97 | +snack list auth # filtered by category (subdirectory) |
| 98 | +``` |
| 99 | + |
| 100 | +### `snack search <keyword>` |
| 101 | + |
| 102 | +Search snippet filenames for a keyword (case-insensitive). |
| 103 | + |
| 104 | +```bash |
| 105 | +snack search oauth |
| 106 | +# auth/google_oauth_fastapi.py |
| 107 | +# auth/google_oauth_flask.py |
| 108 | +``` |
| 109 | + |
| 110 | +### `snack unpack <snippet>` |
| 111 | + |
| 112 | +Copy a snippet **from the stash** into the current working directory. |
| 113 | + |
| 114 | +```bash |
| 115 | +snack unpack auth/google_oauth_fastapi.py |
| 116 | +# → ./auth/google_oauth_fastapi.py |
| 117 | + |
| 118 | +snack unpack auth/google_oauth_fastapi.py --flat |
| 119 | +# → ./google_oauth_fastapi.py (no subdirectory) |
| 120 | + |
| 121 | +snack unpack auth/google_oauth_fastapi.py --force |
| 122 | +# Overwrites without prompting |
| 123 | +``` |
| 124 | + |
| 125 | +### `snack pack <snippet>` |
| 126 | + |
| 127 | +Copy a file **from the current directory** back into the stash. Use this when you've improved a snippet on a project and want to update the canonical version. |
| 128 | + |
| 129 | +```bash |
| 130 | +snack pack auth/google_oauth_fastapi.py |
| 131 | +snack pack auth/google_oauth_fastapi.py --force |
| 132 | +``` |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## Stash management |
| 137 | + |
| 138 | +### `snack stash create <name> <path>` |
| 139 | + |
| 140 | +Register a new named stash. Creates the directory if it doesn't exist. |
| 141 | + |
| 142 | +```bash |
| 143 | +snack stash create default ~/snack-stash |
| 144 | +snack stash create work ~/work-stash --no-activate |
| 145 | +``` |
| 146 | + |
| 147 | +The first stash created is automatically set as active. Use `--no-activate` to add a stash without switching to it. |
| 148 | + |
| 149 | +### `snack stash list` |
| 150 | + |
| 151 | +Show all configured stashes and which one is active. |
| 152 | + |
| 153 | +```bash |
| 154 | +snack stash list |
| 155 | +# default /Users/you/snack-stash ← active |
| 156 | +# work /Users/you/work-stash |
| 157 | +``` |
| 158 | + |
| 159 | +### `snack stash move <name> <new-path>` |
| 160 | + |
| 161 | +Move a stash to a new location. Moves the directory on disk and updates the config. |
| 162 | + |
| 163 | +```bash |
| 164 | +snack stash move default ~/new-location/snack-stash |
| 165 | +``` |
| 166 | + |
| 167 | +### `snack stash add-remote <repo>` |
| 168 | + |
| 169 | +Copy Python snippets from a public GitHub repository into the active stash. Downloads the repo as a tarball and copies all `.py` files, preserving directory structure. |
| 170 | + |
| 171 | +```bash |
| 172 | +snack stash add-remote owner/repo |
| 173 | +snack stash add-remote https://github.com/owner/repo |
| 174 | +snack stash add-remote owner/repo --subdir auth # only copy files under auth/ |
| 175 | +snack stash add-remote owner/repo --force # overwrite without prompting |
| 176 | +``` |
| 177 | + |
| 178 | +--- |
| 179 | + |
| 180 | +## Error handling |
| 181 | + |
| 182 | +| Situation | Behaviour | |
| 183 | +|---|---| |
| 184 | +| No stash configured | Error with setup instructions | |
| 185 | +| Stash path doesn't exist | Error with the attempted path | |
| 186 | +| Snippet not found in stash | Error — use `snack list` or `snack search` | |
| 187 | +| Source file not found | Error | |
| 188 | +| Destination file already exists | Prompt to confirm, or skip with `--force` | |
| 189 | +| Named stash already exists | Error | |
| 190 | +| Move target already exists | Error | |
| 191 | +| GitHub repo not found (HTTP 404) | Error with status code | |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | +## Project structure |
| 196 | + |
| 197 | +``` |
| 198 | +python-snacks/ |
| 199 | +├── pyproject.toml |
| 200 | +├── snacks/ |
| 201 | +│ ├── main.py # Typer app, all command definitions |
| 202 | +│ ├── config.py # SnackConfig class, stash path resolution |
| 203 | +│ └── ops.py # File copy logic (pack, unpack, add_remote) |
| 204 | +└── tests/ |
| 205 | + ├── test_commands.py # snippet commands |
| 206 | + └── test_stash_commands.py # stash management commands |
| 207 | +``` |
| 208 | + |
| 209 | +--- |
| 210 | + |
| 211 | +## CI / CD |
| 212 | + |
| 213 | +- **CI:** Tests run on every push and PR to `main` across Python 3.10, 3.11, and 3.12. |
| 214 | +- **Publish:** Push a `v*` tag to trigger a build, PyPI publish, and GitHub Release. |
| 215 | + |
| 216 | +```bash |
| 217 | +git tag v0.2.0 && git push origin v0.2.0 |
| 218 | +``` |
0 commit comments