askrobots / dbbasic-content
Public
WordPress-style content management with block-based pages, stored as JSON files on disk instead of MySQL.
cli.py · 8.9 KB · 320 lines
| 1 | """ |
| 2 | CLI tools for dbbasic-content. |
| 3 | |
| 4 | Unix-style command-line interface for content management. |
| 5 | """ |
| 6 | |
| 7 | import sys |
| 8 | import argparse |
| 9 | from pathlib import Path |
| 10 | from .content import ContentDB |
| 11 | from .wordpress_importer import WordPressImporter |
| 12 | from .blocks import validate_blocks |
| 13 | |
| 14 | |
| 15 | def cmd_init(args): |
| 16 | """Initialize a new content directory.""" |
| 17 | content_path = Path(args.path) |
| 18 | |
| 19 | if content_path.exists() and any(content_path.iterdir()): |
| 20 | print(f"Error: Directory {args.path} already exists and is not empty", file=sys.stderr) |
| 21 | return 1 |
| 22 | |
| 23 | # Create ContentDB (will create directories) |
| 24 | ContentDB(args.path) |
| 25 | |
| 26 | print(f"Initialized content directory at {args.path}") |
| 27 | print(f" articles/ - Article JSON files") |
| 28 | |
| 29 | return 0 |
| 30 | |
| 31 | |
| 32 | def cmd_list(args): |
| 33 | """List all posts.""" |
| 34 | db = ContentDB(args.path) |
| 35 | posts = db.get_posts() |
| 36 | |
| 37 | if not posts: |
| 38 | print("No posts found") |
| 39 | return 0 |
| 40 | |
| 41 | # Display posts |
| 42 | for post in posts: |
| 43 | status = post.get('status', 'unknown') |
| 44 | date = post.get('date', 'no-date') |
| 45 | title = post.get('title', 'Untitled') |
| 46 | slug = post.get('slug', 'no-slug') |
| 47 | |
| 48 | print(f"{slug:30} {date:12} {status:10} {title}") |
| 49 | |
| 50 | print(f"\nTotal: {len(posts)} posts") |
| 51 | return 0 |
| 52 | |
| 53 | |
| 54 | def cmd_show(args): |
| 55 | """Show post details.""" |
| 56 | db = ContentDB(args.path) |
| 57 | post = db.get_post(args.slug) |
| 58 | |
| 59 | if not post: |
| 60 | print(f"Error: Post '{args.slug}' not found", file=sys.stderr) |
| 61 | return 1 |
| 62 | |
| 63 | # Display post details |
| 64 | print(f"Slug: {post.get('slug')}") |
| 65 | print(f"Title: {post.get('title')}") |
| 66 | print(f"Date: {post.get('date')}") |
| 67 | print(f"Author: {post.get('author')}") |
| 68 | print(f"Status: {post.get('status', 'published')}") |
| 69 | |
| 70 | if 'categories' in post: |
| 71 | print(f"Categories: {', '.join(post['categories'])}") |
| 72 | |
| 73 | if 'tags' in post: |
| 74 | print(f"Tags: {', '.join(post['tags'])}") |
| 75 | |
| 76 | if 'description' in post: |
| 77 | print(f"\nDescription:\n{post['description']}") |
| 78 | |
| 79 | blocks = post.get('blocks', []) |
| 80 | print(f"\nBlocks: {len(blocks)}") |
| 81 | for i, block in enumerate(blocks): |
| 82 | print(f" {i+1}. {block['type']}") |
| 83 | |
| 84 | return 0 |
| 85 | |
| 86 | |
| 87 | def cmd_validate(args): |
| 88 | """Validate content structure.""" |
| 89 | db = ContentDB(args.path) |
| 90 | posts = db.get_posts() |
| 91 | |
| 92 | if not posts: |
| 93 | print("No posts to validate") |
| 94 | return 0 |
| 95 | |
| 96 | errors = [] |
| 97 | |
| 98 | for post in posts: |
| 99 | slug = post.get('slug', 'unknown') |
| 100 | |
| 101 | # Validate required fields |
| 102 | required = ['slug', 'title', 'blocks'] |
| 103 | for field in required: |
| 104 | if field not in post: |
| 105 | errors.append(f"{slug}: Missing required field '{field}'") |
| 106 | |
| 107 | # Validate blocks |
| 108 | blocks = post.get('blocks', []) |
| 109 | is_valid, block_errors = validate_blocks(blocks) |
| 110 | if not is_valid: |
| 111 | for error in block_errors: |
| 112 | errors.append(f"{slug}: {error}") |
| 113 | |
| 114 | if errors: |
| 115 | print(f"Validation failed with {len(errors)} errors:\n") |
| 116 | for error in errors: |
| 117 | print(f" - {error}") |
| 118 | return 1 |
| 119 | |
| 120 | print(f"Validation passed for {len(posts)} posts") |
| 121 | return 0 |
| 122 | |
| 123 | |
| 124 | def cmd_import_wordpress(args): |
| 125 | """Import from WordPress database.""" |
| 126 | print(f"Importing from WordPress database...") |
| 127 | print(f" Host: {args.host}") |
| 128 | print(f" Database: {args.database}") |
| 129 | print(f" Output: {args.path}") |
| 130 | print() |
| 131 | |
| 132 | try: |
| 133 | with WordPressImporter( |
| 134 | host=args.host, |
| 135 | database=args.database, |
| 136 | user=args.user, |
| 137 | password=args.password, |
| 138 | port=args.port, |
| 139 | table_prefix=args.prefix |
| 140 | ) as importer: |
| 141 | stats = importer.import_to(args.path) |
| 142 | |
| 143 | print("Import complete:") |
| 144 | print(f" Posts: {stats['posts']}") |
| 145 | print(f" Pages: {stats['pages']}") |
| 146 | print(f" Drafts: {stats['drafts']}") |
| 147 | print(f" Total: {sum(stats.values())}") |
| 148 | |
| 149 | return 0 |
| 150 | |
| 151 | except ImportError: |
| 152 | print("Error: pymysql not installed", file=sys.stderr) |
| 153 | print("Install with: pip install dbbasic-content[wordpress]", file=sys.stderr) |
| 154 | return 1 |
| 155 | |
| 156 | except Exception as e: |
| 157 | print(f"Error: {e}", file=sys.stderr) |
| 158 | return 1 |
| 159 | |
| 160 | |
| 161 | def cmd_categories(args): |
| 162 | """List all categories.""" |
| 163 | db = ContentDB(args.path) |
| 164 | categories = db.get_categories() |
| 165 | |
| 166 | if not categories: |
| 167 | print("No categories found") |
| 168 | return 0 |
| 169 | |
| 170 | for category in categories: |
| 171 | # Count posts in category |
| 172 | posts = db.get_posts(categories=[category]) |
| 173 | print(f"{category:30} ({len(posts)} posts)") |
| 174 | |
| 175 | return 0 |
| 176 | |
| 177 | |
| 178 | def cmd_tags(args): |
| 179 | """List all tags.""" |
| 180 | db = ContentDB(args.path) |
| 181 | tags = db.get_tags() |
| 182 | |
| 183 | if not tags: |
| 184 | print("No tags found") |
| 185 | return 0 |
| 186 | |
| 187 | for tag in tags: |
| 188 | # Count posts with tag |
| 189 | posts = db.get_posts(tags=[tag]) |
| 190 | print(f"{tag:30} ({len(posts)} posts)") |
| 191 | |
| 192 | return 0 |
| 193 | |
| 194 | |
| 195 | def cmd_search(args): |
| 196 | """Search posts.""" |
| 197 | db = ContentDB(args.path) |
| 198 | results = db.search(args.query) |
| 199 | |
| 200 | if not results: |
| 201 | print(f"No results found for: {args.query}") |
| 202 | return 0 |
| 203 | |
| 204 | print(f"Found {len(results)} results for: {args.query}\n") |
| 205 | |
| 206 | for post in results: |
| 207 | slug = post.get('slug', 'no-slug') |
| 208 | title = post.get('title', 'Untitled') |
| 209 | print(f" {slug:30} {title}") |
| 210 | |
| 211 | return 0 |
| 212 | |
| 213 | |
| 214 | def main(): |
| 215 | """Main CLI entry point.""" |
| 216 | parser = argparse.ArgumentParser( |
| 217 | description='dbbasic-content: Unix-foundation content management', |
| 218 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 219 | epilog=""" |
| 220 | Examples: |
| 221 | # Initialize new content directory |
| 222 | dbcontent init /var/app/content |
| 223 | |
| 224 | # List all posts |
| 225 | dbcontent list /var/app/content |
| 226 | |
| 227 | # Show post details |
| 228 | dbcontent show /var/app/content hello-world |
| 229 | |
| 230 | # Import from WordPress |
| 231 | dbcontent import wordpress \\ |
| 232 | --host localhost \\ |
| 233 | --database wordpress \\ |
| 234 | --user root \\ |
| 235 | --password secret \\ |
| 236 | /var/app/content |
| 237 | |
| 238 | # Validate content |
| 239 | dbcontent validate /var/app/content |
| 240 | |
| 241 | # Search posts |
| 242 | dbcontent search /var/app/content "python programming" |
| 243 | """ |
| 244 | ) |
| 245 | |
| 246 | subparsers = parser.add_subparsers(dest='command', help='Commands') |
| 247 | |
| 248 | # init command |
| 249 | parser_init = subparsers.add_parser('init', help='Initialize content directory') |
| 250 | parser_init.add_argument('path', help='Path to content directory') |
| 251 | |
| 252 | # list command |
| 253 | parser_list = subparsers.add_parser('list', help='List all posts') |
| 254 | parser_list.add_argument('path', help='Path to content directory') |
| 255 | |
| 256 | # show command |
| 257 | parser_show = subparsers.add_parser('show', help='Show post details') |
| 258 | parser_show.add_argument('path', help='Path to content directory') |
| 259 | parser_show.add_argument('slug', help='Post slug') |
| 260 | |
| 261 | # validate command |
| 262 | parser_validate = subparsers.add_parser('validate', help='Validate content structure') |
| 263 | parser_validate.add_argument('path', help='Path to content directory') |
| 264 | |
| 265 | # import wordpress command |
| 266 | parser_import = subparsers.add_parser('import', help='Import from WordPress') |
| 267 | import_subparsers = parser_import.add_subparsers(dest='import_type', help='Import type') |
| 268 | |
| 269 | parser_wp = import_subparsers.add_parser('wordpress', help='Import from WordPress database') |
| 270 | parser_wp.add_argument('path', help='Path to content directory') |
| 271 | parser_wp.add_argument('--host', default='localhost', help='MySQL host') |
| 272 | parser_wp.add_argument('--database', required=True, help='Database name') |
| 273 | parser_wp.add_argument('--user', required=True, help='MySQL user') |
| 274 | parser_wp.add_argument('--password', required=True, help='MySQL password') |
| 275 | parser_wp.add_argument('--port', type=int, default=3306, help='MySQL port') |
| 276 | parser_wp.add_argument('--prefix', default='wp_', help='Table prefix') |
| 277 | |
| 278 | # categories command |
| 279 | parser_categories = subparsers.add_parser('categories', help='List all categories') |
| 280 | parser_categories.add_argument('path', help='Path to content directory') |
| 281 | |
| 282 | # tags command |
| 283 | parser_tags = subparsers.add_parser('tags', help='List all tags') |
| 284 | parser_tags.add_argument('path', help='Path to content directory') |
| 285 | |
| 286 | # search command |
| 287 | parser_search = subparsers.add_parser('search', help='Search posts') |
| 288 | parser_search.add_argument('path', help='Path to content directory') |
| 289 | parser_search.add_argument('query', help='Search query') |
| 290 | |
| 291 | # Parse args |
| 292 | args = parser.parse_args() |
| 293 | |
| 294 | if not args.command: |
| 295 | parser.print_help() |
| 296 | return 1 |
| 297 | |
| 298 | # Dispatch to command handlers |
| 299 | commands = { |
| 300 | 'init': cmd_init, |
| 301 | 'list': cmd_list, |
| 302 | 'show': cmd_show, |
| 303 | 'validate': cmd_validate, |
| 304 | 'categories': cmd_categories, |
| 305 | 'tags': cmd_tags, |
| 306 | 'search': cmd_search, |
| 307 | } |
| 308 | |
| 309 | if args.command == 'import' and args.import_type == 'wordpress': |
| 310 | return cmd_import_wordpress(args) |
| 311 | |
| 312 | if args.command in commands: |
| 313 | return commands[args.command](args) |
| 314 | |
| 315 | print(f"Unknown command: {args.command}", file=sys.stderr) |
| 316 | return 1 |
| 317 | |
| 318 | |
| 319 | if __name__ == '__main__': |
| 320 | sys.exit(main()) |