-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
executable file
·151 lines (127 loc) · 5.16 KB
/
cli.py
File metadata and controls
executable file
·151 lines (127 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# cli.py
"""shane null command line workflows"""
import click
import os
import logging
import inspect
import sys
import shutil
import datetime
import sys
import sys
plugin_folder = os.path.join(os.path.dirname(__file__), "plugins")
homedir = os.path.expanduser("~")
TODAY = datetime.date.today().strftime("%Y-%m-%d")
ISODATE = datetime.date.today().strftime("%Y-%m-%d")
ISOFILE = ISODATE + ".md"
BUJO_FOLDER = os.path.join(os.path.dirname(__file__), "docs/bujo")
MONTH = datetime.date.today().strftime("%m_%B")
WEEK = datetime.date.today().strftime("%U")
DAYFILE = BUJO_FOLDER + "/" + TODAY + ".md"
WEEKFILE = BUJO_FOLDER + "/" + WEEK + ".md"
MONTHFILE = BUJO_FOLDER + "/" + MONTH + ".md"
YEAR = datetime.date.today().strftime("%Y")
# Set up the logger
# from plugins.mods.log_tools import setup_logger
# logger = setup_logger('cli_logger', logging.DEBUG)
# logger = setup_logger(logging.DEBUG)
# logger = setup_logger('cli_logger', logging.DEBUG)
# # Example usage of logging
# def main():
# logger.debug('This is a debug message from the CLI.')
# logger.info('This is an info message from the CLI.')
# logger.warning('This is a warning message from the CLI.')
# logger.error('This is an error message from the CLI.')
# logger.critical('This is a critical message from the CLI.')
# if __name__ == "__main__":
# main()
# Define the log file path in the plugins/ directory
# log_directory = "plugins/logs"
# os.makedirs(log_directory, exist_ok=True) # Create the 'logs' directory if it doesn't exist
# log_file = os.path.join(log_directory, "cli_commands.log")
# # Set up the logger to log to a file
# logging.basicConfig(
# level=logging.INFO, # Adjust level as needed (DEBUG, INFO, WARNING, etc.)
# format="%(asctime)s - %(levelname)s - %(message)s", # Customize log format
# handlers=[
# logging.FileHandler(log_file, mode='a'), # Append to log file instead of overwriting
# logging.StreamHandler() # Also print logs to the console
# ]
# )
# logger = logging.getLogger(__name__)
class PluginCLI(click.MultiCommand):
def list_commands(self, ctx):
"""Dynamically get the list of commands."""
rv = []
for filename in os.listdir(plugin_folder):
if filename.endswith(".py") and not filename.startswith("__init__"):
rv.append(filename[:-3])
rv.sort()
return rv
def get_command(self, ctx, name):
"""Dynamically get the command."""
ns = {}
fn = os.path.join(plugin_folder, name + ".py")
try:
with open(fn) as f:
code = compile(f.read(), fn, "exec")
eval(code, ns, ns)
# logger.info(f"cli.py {name} loaded successfully.") # Log successful command loading
# click.echo(f"cli.py {name} loaded successfully.") # Log successful command loading
pass
except Exception as e:
# logger.error(f"Error loading cli.py {name}: {e}")
# click.echo(f"Error loading cli.py {name}: {e}")
pass
# Return the dynamically loaded command
command = ns.get("cli")
if command:
if len(sys.argv) > 1:
pass
# logger.info(f"cli.py {sys.argv[1]}")
# click.echo(f"cli.py {sys.argv[1]}")
if len(sys.argv) > 2:
pass
# click.echo(f"cli.py {sys.argv[1]} {sys.argv[1]}")
# click.echo(f"Loading command: {name}") # Log the command being loaded
# Access before_invoke directly
# logger.info(f"cli.py '{ctx.command.name}' executed with arguments: {ctx.args}")
return command
# return command
# fix me this does not log the 2nd argument
# 2024-12-09 18:13:46,469 - INFO - Starting the CLI application...
# 2024-12-09 18:13:46,470 - INFO - Loading command: bujo
# 2024-12-09 18:13:46,475 - INFO - Command bujo loaded successfully.
# 2024-12-09 18:13:46,475 - INFO - Command 'cli' executed with arguments: []
import pdb
def before_invoke(self, ctx):
if len(sys.argv) > 1:
logger.info(f"cli.py {sys.argv[1]}")
if len(sys.argv) > 2:
logger.info(f"cli.py {sys.argv[1]} {sys.argv[1]}")
logger.info(f"Loading command: {name}") # Log the command being loaded
click.echo(f"Arguments: {ctx.args}")
# def before_invoke(self, ctx):
# print(f"Arguments: {ctx.args}")
# logger.info(f"Command '{ctx.command.name}' executed with arguments: {ctx.args}")
# def before_invoke(self, ctx):
# pdb.set_trace() # Set a breakpoint here
# args = ctx.args
# logger.info(f"Command '{ctx.command.name}' executed with arguments: {args}")
# def before_invoke(self, ctx):
# args = ctx.params.values()
# logger.info(f"Command '{ctx.command.name}' executed with arguments: {args}")
# def before_invoke(self, ctx):
# args = ctx.args
# if len(args) >= 2:
# command = args[0]
# argument = args[1]
# logger.info(f"Executing command '{command}' with argument '{argument}'")
@click.command(cls=PluginCLI)
def cli():
"""command line toolbox"""
pass
if __name__ == "__main__":
cli()