Python files updated for creation bindings
Gitea Actions For Tree-Structurer / Explore-Gitea-Actions (push) Successful in 19s
Details
Gitea Actions For Tree-Structurer / Explore-Gitea-Actions (push) Successful in 19s
Details
This commit is contained in:
parent
1eb338789c
commit
055fa5bcc7
|
@ -1 +1,13 @@
|
||||||
from prodir._tree_structurer import create_tree_structurer, get_structure
|
from prodir._tree_structurer import (
|
||||||
|
create_tree_structurer,
|
||||||
|
get_structure,
|
||||||
|
create_structure_from_file,
|
||||||
|
create_structure_from_string
|
||||||
|
)
|
||||||
|
|
||||||
|
__all__ = [
|
||||||
|
'create_tree_structurer',
|
||||||
|
'get_structure',
|
||||||
|
'create_structure_from_file',
|
||||||
|
'create_structure_from_string'
|
||||||
|
]
|
|
@ -1,36 +1,120 @@
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
from prodir import create_tree_structurer, get_structure
|
from prodir import (
|
||||||
|
create_tree_structurer,
|
||||||
|
get_structure,
|
||||||
|
create_structure_from_file,
|
||||||
|
create_structure_from_string
|
||||||
|
)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description='Display directory structure in a tree-like format')
|
parser = argparse.ArgumentParser(
|
||||||
parser.add_argument('path', nargs='?', default=os.getcwd(),
|
description='Directory structure tool: Display and create directory structures'
|
||||||
help='Path to analyze (default: current directory)')
|
)
|
||||||
parser.add_argument('-v', '--verbose', action='store_true',
|
|
||||||
help='Show more detailed output')
|
# Create subparsers for different commands
|
||||||
|
subparsers = parser.add_subparsers(dest='command', help='Available commands')
|
||||||
|
|
||||||
|
# Display command
|
||||||
|
display_parser = subparsers.add_parser('display', help='Display directory structure')
|
||||||
|
display_parser.add_argument(
|
||||||
|
'path',
|
||||||
|
nargs='?',
|
||||||
|
default=os.getcwd(),
|
||||||
|
help='Path to analyze (default: current directory)'
|
||||||
|
)
|
||||||
|
display_parser.add_argument(
|
||||||
|
'-v', '--verbose',
|
||||||
|
action='store_true',
|
||||||
|
help='Show more detailed output'
|
||||||
|
)
|
||||||
|
|
||||||
|
# Create command
|
||||||
|
create_parser = subparsers.add_parser('create', help='Create directory structure')
|
||||||
|
create_parser.add_argument(
|
||||||
|
'-f', '--file',
|
||||||
|
help='File containing the directory structure'
|
||||||
|
)
|
||||||
|
create_parser.add_argument(
|
||||||
|
'-s', '--string',
|
||||||
|
help='String containing the directory structure'
|
||||||
|
)
|
||||||
|
create_parser.add_argument(
|
||||||
|
'-o', '--output',
|
||||||
|
default=os.getcwd(),
|
||||||
|
help='Output directory (default: current directory)'
|
||||||
|
)
|
||||||
|
create_parser.add_argument(
|
||||||
|
'-v', '--verbose',
|
||||||
|
action='store_true',
|
||||||
|
help='Show more detailed output'
|
||||||
|
)
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
# If no command is specified, default to display
|
||||||
|
if not args.command:
|
||||||
|
args.command = 'display'
|
||||||
|
args.path = os.getcwd()
|
||||||
|
args.verbose = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if args.verbose:
|
|
||||||
print(f"Analyzing directory: {args.path}")
|
|
||||||
|
|
||||||
create_tree_structurer()
|
create_tree_structurer()
|
||||||
structure = get_structure(args.path)
|
|
||||||
|
if args.command == 'display':
|
||||||
for line in structure:
|
if args.verbose:
|
||||||
print(line)
|
print(f"Analyzing directory: {args.path}")
|
||||||
|
|
||||||
|
structure = get_structure(args.path)
|
||||||
|
for line in structure:
|
||||||
|
print(line)
|
||||||
|
|
||||||
|
elif args.command == 'create':
|
||||||
|
if not args.file and not args.string:
|
||||||
|
parser.error("Either --file or --string must be specified")
|
||||||
|
|
||||||
|
if args.file and args.string:
|
||||||
|
parser.error("Cannot specify both --file and --string")
|
||||||
|
|
||||||
|
if args.verbose:
|
||||||
|
print(f"Creating directory structure in: {args.output}")
|
||||||
|
|
||||||
|
if args.file:
|
||||||
|
if args.verbose:
|
||||||
|
print(f"Using structure from file: {args.file}")
|
||||||
|
create_structure_from_file(args.file, args.output)
|
||||||
|
|
||||||
|
elif args.string:
|
||||||
|
if args.verbose:
|
||||||
|
print("Creating structure from provided string")
|
||||||
|
create_structure_from_string(args.string, args.output)
|
||||||
|
|
||||||
|
if args.verbose:
|
||||||
|
print("Structure created successfully")
|
||||||
|
print("\nResulting structure:")
|
||||||
|
structure = get_structure(args.output)
|
||||||
|
for line in structure:
|
||||||
|
print(line)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
error_msg = str(e)
|
error_msg = str(e)
|
||||||
if "Directory does not exist" in error_msg:
|
if "Directory does not exist" in error_msg:
|
||||||
print(f"Error: The specified directory does not exist: {args.path}", file=sys.stderr)
|
print(f"Error: The specified directory does not exist: {args.path if args.command == 'display' else args.output}",
|
||||||
|
file=sys.stderr)
|
||||||
elif "Directory is empty" in error_msg:
|
elif "Directory is empty" in error_msg:
|
||||||
print(f"Error: The specified directory is empty: {args.path}", file=sys.stderr)
|
print(f"Error: The specified directory is empty: {args.path if args.command == 'display' else args.output}",
|
||||||
|
file=sys.stderr)
|
||||||
elif "Path is not a directory" in error_msg:
|
elif "Path is not a directory" in error_msg:
|
||||||
print(f"Error: The specified path is not a directory: {args.path}", file=sys.stderr)
|
print(f"Error: The specified path is not a directory: {args.path if args.command == 'display' else args.output}",
|
||||||
|
file=sys.stderr)
|
||||||
|
elif "Unable to open structure file" in error_msg:
|
||||||
|
print(f"Error: Unable to open structure file: {args.file}", file=sys.stderr)
|
||||||
|
elif "Invalid structure format" in error_msg:
|
||||||
|
print(f"Error: Invalid structure format in {'file' if args.file else 'string'}", file=sys.stderr)
|
||||||
else:
|
else:
|
||||||
print(f"Error: {error_msg}", file=sys.stderr)
|
print(f"Error: {error_msg}", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue