-
-
Notifications
You must be signed in to change notification settings - Fork 32.7k
gh-114576: Add command-line interface for dbm module #137893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d3044d8
ddfa563
b4c224a
31ad2c4
7059a0c
a547468
740f5bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -501,3 +501,70 @@ | |||||||||||||
that this factor changes for each :mod:`dbm` submodule. | ||||||||||||||
|
||||||||||||||
.. versionadded:: next | ||||||||||||||
|
||||||||||||||
|
||||||||||||||
.. _dbm-commandline: | ||||||||||||||
.. program:: dbm | ||||||||||||||
|
||||||||||||||
Command-line interface | ||||||||||||||
---------------------- | ||||||||||||||
|
||||||||||||||
.. module:: dbm.__main__ | ||||||||||||||
:synopsis: A command-line interface for DBM database operations. | ||||||||||||||
|
||||||||||||||
**Source code:** :source:`Lib/dbm/__main__.py` | ||||||||||||||
|
||||||||||||||
-------------- | ||||||||||||||
|
||||||||||||||
The :mod:`dbm` module can be invoked as a script via ``python -m dbm`` | ||||||||||||||
to identify, examine, and reorganize DBM database files. | ||||||||||||||
|
||||||||||||||
Command-line options | ||||||||||||||
^^^^^^^^^^^^^^^^^^^^ | ||||||||||||||
|
||||||||||||||
.. option:: --whichdb file [file ...] | ||||||||||||||
Comment on lines
+524
to
+525
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above
Suggested change
|
||||||||||||||
|
||||||||||||||
Identify the database type for one or more database files: | ||||||||||||||
|
||||||||||||||
.. code-block:: shell-session | ||||||||||||||
|
||||||||||||||
$ python -m dbm --whichdb *.db | ||||||||||||||
dbm.gnu - database1.db | ||||||||||||||
dbm.sqlite3 - database2.db | ||||||||||||||
UNKNOWN - corrupted.db | ||||||||||||||
|
||||||||||||||
This command uses the :func:`whichdb` function to determine the type | ||||||||||||||
of each database file. Files that cannot be identified are marked as | ||||||||||||||
``UNKNOWN``. | ||||||||||||||
|
||||||||||||||
.. option:: --dump file | ||||||||||||||
|
||||||||||||||
Display the contents of a database file: | ||||||||||||||
|
||||||||||||||
.. code-block:: shell-session | ||||||||||||||
|
||||||||||||||
$ python -m dbm --dump mydb.db | ||||||||||||||
username: john_doe | ||||||||||||||
email: john@example.com | ||||||||||||||
last_login: 2024-01-15 | ||||||||||||||
|
||||||||||||||
Keys and values are displayed in ``key: value`` format. Binary data | ||||||||||||||
is decoded using UTF-8 with error replacement for display purposes. | ||||||||||||||
|
||||||||||||||
.. option:: --reorganize file | ||||||||||||||
|
||||||||||||||
Reorganize and compact a database file to reduce disk space: | ||||||||||||||
|
||||||||||||||
.. code-block:: shell-session | ||||||||||||||
|
||||||||||||||
$ python -m dbm --reorganize mydb.db | ||||||||||||||
Reorganized database 'mydb.db' | ||||||||||||||
|
||||||||||||||
This operation uses the database's native :meth:`!reorganize` method | ||||||||||||||
when available (:mod:`dbm.sqlite3`, :mod:`dbm.gnu`, :mod:`dbm.dumb`). | ||||||||||||||
For database types that don't support reorganization, an error message | ||||||||||||||
is displayed. | ||||||||||||||
|
||||||||||||||
.. option:: -h, --help | ||||||||||||||
|
||||||||||||||
Show the help message. | ||||||||||||||
Comment on lines
+567
to
+570
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we should add help flag as option because it's not a common practice.
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import argparse | ||
import os | ||
import sys | ||
|
||
from . import open as dbm_open, whichdb, error | ||
|
||
|
||
def _whichdb_command(filenames): | ||
exit_code = 0 | ||
|
||
for filename in filenames: | ||
if os.path.exists(filename): | ||
db_type = whichdb(filename) | ||
print(f"{db_type or 'UNKNOWN'} {filename}") | ||
else: | ||
print(f"Error: File '{filename}' not found", file=sys.stderr) | ||
exit_code = 1 | ||
|
||
return exit_code | ||
|
||
|
||
def _dump_command(filename): | ||
try: | ||
with dbm_open(filename, "r") as db: | ||
for key in db: | ||
print(f"{key!r}: {db[key]!r}") | ||
return 0 | ||
except error: | ||
print(f"Error: Database '{filename}' not found", file=sys.stderr) | ||
return 1 | ||
|
||
|
||
def _reorganize_command(filename): | ||
try: | ||
with dbm_open(filename, "c") as db: | ||
if db.hasattr("reorganize"): | ||
db.reorganize() | ||
print(f"Reorganized database: '{filename}'", file=sys.stderr) | ||
else: | ||
print("Database type doesn't support reorganize method", | ||
file=sys.stderr) | ||
return 1 | ||
return 0 | ||
except error: | ||
print(f"Error: Database '{filename}' not found or cannot be opened", | ||
file=sys.stderr) | ||
return 1 | ||
|
||
|
||
def main(): | ||
furkanonder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
parser = argparse.ArgumentParser( | ||
prog="python -m dbm", description="DBM toolkit" | ||
) | ||
group = parser.add_mutually_exclusive_group(required=True) | ||
group.add_argument( | ||
"--whichdb", | ||
Comment on lines
+54
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of options, why not use subparsers for subcommands? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I chose not to include it to maintain clarity and simplicity, but I’m happy to add it if needed. |
||
nargs="+", | ||
metavar="file", | ||
help="Identify database type for one or more files", | ||
) | ||
group.add_argument( | ||
"--dump", metavar="file", help="Display database contents" | ||
) | ||
group.add_argument( | ||
"--reorganize", | ||
metavar="file", | ||
help="Reorganize the database", | ||
) | ||
options = parser.parse_args() | ||
|
||
try: | ||
if options.whichdb: | ||
return _whichdb_command(options.whichdb) | ||
elif options.dump: | ||
return _dump_command(options.dump) | ||
elif options.reorganize: | ||
return _reorganize_command(options.reorganize) | ||
except KeyboardInterrupt: | ||
return 1 | ||
|
||
|
||
if __name__ == "__main__": | ||
sys.exit(main()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.. program::
directive should be placed closer to options