dbpush 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dbpush/__init__.py ADDED
@@ -0,0 +1 @@
1
+ from .dbpush import Dbpush
dbpush/cli.py ADDED
@@ -0,0 +1,294 @@
1
+ from rich.console import Console
2
+ from rich.panel import Panel
3
+ from rich.table import Table
4
+ from .dbpush import dbpush
5
+ from .os_sys import mkdir,rmdir,remove,path,listDir,touch,read_file,clear_screen
6
+ from pathlib import Path
7
+ import getpass
8
+
9
+ console = Console()
10
+
11
+
12
+ def show_help():
13
+ table = Table(title="dbpush Commands", border_style="cyan")
14
+ table.add_column("Command", style="bold yellow")
15
+ table.add_column("Description", style="white")
16
+ table.add_row("config", "configure database connection")
17
+ table.add_row("config_file", "configure database connection file")
18
+ table.add_row("read_config", "read configure database connection file")
19
+ table.add_row("create_db", "create database")
20
+ table.add_row("connect", "connect database")
21
+ table.add_row("create_table <column , data type>", "create table from excel")
22
+ table.add_row("insert", "insert excel rows")
23
+ table.add_row("drop_table", "drop current table")
24
+ table.add_row("truncate", "truncate current table")
25
+ table.add_row("select", "select all rows")
26
+ table.add_row("row_count", "show excel row count")
27
+ table.add_row("col_count", "show excel column count")
28
+ table.add_row("find <column> <value>", "find value in excel")
29
+ table.add_row("header", "show table header")
30
+ table.add_row("sheet", "show table sheet")
31
+ table.add_row("ls", "show file and dir list")
32
+ table.add_row("mkdir", "create folder")
33
+ table.add_row("rmdir", "remove folder")
34
+ table.add_row("touch", "create file")
35
+ table.add_row("remove", "remove file")
36
+ table.add_row("path", "show file absulute path")
37
+ table.add_row("edit", "edit files ")
38
+ table.add_row("read_file", "read files data")
39
+ table.add_row("cls", "clear screen")
40
+ table.add_row("exit", "quit cli")
41
+
42
+ console.print(table)
43
+
44
+
45
+ def build_db(config):
46
+ return dbpush.create(
47
+ db_type=config["db_type"],
48
+ host=config["host"],
49
+ user=config["user"],
50
+ password=config["password"],
51
+ database=config["database"],
52
+ port=int(config.get("port", 5432)),
53
+ file_name=config["file_name"],
54
+ sheet_name=config["sheet_name"],
55
+ )
56
+
57
+
58
+ def run():
59
+ console.print(
60
+ Panel(
61
+ "[bold green]Welcome to dbpush CLI[/bold green]\nType [yellow]help[/yellow]",
62
+ border_style="green",
63
+ )
64
+ )
65
+
66
+ config = {}
67
+ config_file = {}
68
+ cf_file = None
69
+ db = None
70
+ unqic_col = None
71
+ unqic_dtype = None
72
+
73
+ while True:
74
+ try:
75
+ raw = console.input("[bold cyan]> [/bold cyan]").strip()
76
+ except (KeyboardInterrupt, EOFError):
77
+ console.print("\n[bold red]Goodbye[/bold red]")
78
+ break
79
+
80
+ if not raw:
81
+ continue
82
+
83
+ parts = raw.split()
84
+ cmd = parts[0].lower()
85
+
86
+ if cmd in ("exit", "quit"):
87
+ console.print("[bold red]Goodbye[/bold red]")
88
+ break
89
+
90
+ elif cmd == "help":
91
+ show_help()
92
+
93
+ elif cmd == "config":
94
+ # ── db type ──────────────────────────────────────────
95
+ db_type = console.input("db type (mysql/postgres): ").strip().lower()
96
+ config["db_type"] = db_type
97
+
98
+ # ── auto default port based on db type ───────────────
99
+ default_port = "5432" if db_type == "postgres" else "3306"
100
+
101
+ config["host"] = console.input("host: ").strip()
102
+ config["user"] = console.input("user: ").strip()
103
+
104
+ # ── hidden password (shows **** while typing) ─────────
105
+ config["password"] = getpass.getpass("password: ")
106
+
107
+ config["database"] = console.input("database: ").strip()
108
+
109
+ # ── port with smart default ───────────────────────────
110
+ port_input = console.input(
111
+ f"port (press Enter for default '{default_port}'): "
112
+ ).strip()
113
+ config["port"] = port_input or default_port
114
+
115
+ config["file_name"] = console.input("excel file: ").strip()
116
+ config["sheet_name"] = console.input("sheet name: ").strip()
117
+
118
+ db = build_db(config)
119
+ console.print("[green]configuration loaded[/green]")
120
+
121
+ elif cmd == "config_file":
122
+ raw_path = console.input("config_file: ").strip()
123
+ cf_file = Path(raw_path).expanduser().resolve()
124
+ if not cf_file.exists():
125
+ console.print(f"[red]File not found:[/red] {cf_file}")
126
+ continue
127
+ try:
128
+ config_file = {}
129
+ with cf_file.open("r", encoding="utf-8") as f:
130
+ for line in f:
131
+ line = line.strip()
132
+ if not line or line.startswith("#"):
133
+ continue
134
+ if ":" not in line:
135
+ continue
136
+ key, value = line.split(":", 1)
137
+ config_file[key.strip()] = value.strip()
138
+
139
+ required = [
140
+ "db_type",
141
+ "host",
142
+ "user",
143
+ "password",
144
+ "database",
145
+ "port",
146
+ "file_name",
147
+ "sheet_name",
148
+ ]
149
+
150
+ missing = [k for k in required if k not in config_file]
151
+
152
+ if missing:
153
+ console.print(
154
+ f"[red]Missing config keys:[/red] {', '.join(missing)}"
155
+ )
156
+ continue
157
+
158
+ # ── auto fill port if missing or empty ────────────
159
+ if not config_file.get("port"):
160
+ config_file["port"] = (
161
+ "5432" if config_file.get("db_type") == "postgres" else "3306"
162
+ )
163
+
164
+ db = build_db(config_file)
165
+ console.print(
166
+ f"[green]config loaded from[/green] [bold]{cf_file}[/bold]"
167
+ )
168
+ except Exception as e:
169
+ console.print(f"[red]Config error:[/red] {e}")
170
+
171
+ elif cmd == "create_db":
172
+ db.create_db()
173
+
174
+ elif cmd == "read_config":
175
+ if not db:
176
+ console.print("[red]No config loaded[/red]")
177
+ else:
178
+ table = Table(title="Current Config", border_style="cyan")
179
+ table.add_column("Key", style="bold yellow")
180
+ table.add_column("Value", style="white")
181
+
182
+ for key in ("host", "user", "password", "database", "port", "file_name", "sheet_name"):
183
+ raw_value = config.get(key) or config_file.get(key, "")
184
+ # ── mask password in read_config display ──────
185
+ display_value = "********" if key == "password" else str(raw_value)
186
+ table.add_row(key, display_value)
187
+
188
+ console.print(table)
189
+
190
+ elif cmd == "connect":
191
+ db.connect()
192
+
193
+ elif cmd == "create_table":
194
+ value = console.input(
195
+ "unique column (optional, press Enter for default 'id'): "
196
+ ).strip()
197
+ dtype_value = console.input(
198
+ "unique column data type (optional, press Enter for default 'TEXT'): "
199
+ ).strip()
200
+
201
+ unqic_col = value or "id"
202
+ unqic_dtype = dtype_value or "TEXT"
203
+ try:
204
+ db.drop_table()
205
+ except Exception:
206
+ pass
207
+ db.create_table(unqic_col, unqic_dtype)
208
+ console.print(
209
+ f"[green]table created with unique column:[/green] [bold]{unqic_col}[/bold]"
210
+ )
211
+
212
+ elif cmd == "insert":
213
+ db.insert()
214
+
215
+ elif cmd == "header":
216
+ db.show_header()
217
+
218
+ elif cmd == "sheet":
219
+ db.show_sheet()
220
+
221
+ elif cmd == "drop_table":
222
+ db.drop_table()
223
+
224
+ elif cmd == "truncate":
225
+ db.truncate_table()
226
+
227
+ elif cmd == "select":
228
+ db.select_all()
229
+
230
+ elif cmd == "row_count":
231
+ db.row_count()
232
+
233
+ elif cmd == "col_count":
234
+ db.col_count()
235
+
236
+ elif cmd == "ls":
237
+ value = console.input("enter path (./): ").strip()
238
+ listDir(value)
239
+
240
+ elif cmd == "mkdir":
241
+ value = console.input("enter folder name & path (./): ").strip()
242
+ mkdir(value)
243
+
244
+ elif cmd == "rmdir":
245
+ value = console.input("enter folder name & path (./): ").strip()
246
+ rmdir(value)
247
+
248
+ elif cmd == "remove":
249
+ value = console.input("enter folder name & path (./): ").strip()
250
+ remove(value)
251
+
252
+ elif cmd == "touch":
253
+ value = console.input("enter folder name & path (./): ").strip()
254
+ touch(value)
255
+
256
+ elif cmd == "path":
257
+ value = console.input("enter folder name & path (./): ").strip()
258
+ path(value)
259
+
260
+ elif cmd == "read_file":
261
+ value = console.input("enter folder name & path (./): ").strip()
262
+ read_file(value)
263
+
264
+ elif cmd == "cls":
265
+ clear_screen()
266
+ console.print(
267
+ Panel(
268
+ "[bold green]Welcome to dbpush CLI[/bold green]\nType [yellow]help[/yellow]",
269
+ border_style="green",
270
+ )
271
+ )
272
+
273
+ elif cmd == "find":
274
+ col = console.input("column name: ").strip() # handles spaces like "Joining Date"
275
+ value = console.input("value: ").strip()
276
+ db.find(col, value)
277
+
278
+ elif cmd == "edit":
279
+ try:
280
+ from .editor import config_editor
281
+ filename = console.input("enter file name & path (./): ").strip()
282
+ config_editor(filename)
283
+ except ImportError:
284
+ console.print("[red]curses is not available. Install:[/red] pip install windows-curses")
285
+
286
+ elif not db:
287
+ console.print("[red]Run 'config' first[/red]")
288
+
289
+ else:
290
+ console.print(f"[red]Unknown command:[/red] {cmd}")
291
+
292
+
293
+ if __name__ == "__main__":
294
+ run()