Skip to content

Commit

Permalink
--verbose flag added to CLI (#234)
Browse files Browse the repository at this point in the history
* fix : `--verbose` flag added to CLI.

* edit : `init_argparse` function separated from `main`.

* apply : Sepand's comments applied.

* fix : `main` function became simpler.

---------

Co-authored-by: sepandhaghighi <sepand.haghighi@yahoo.com>
  • Loading branch information
sadrasabouri and sepandhaghighi authored Oct 28, 2024
1 parent fab5b96 commit b5b811d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 24 deletions.
81 changes: 57 additions & 24 deletions samila/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@

from art import tprint
from .params import SAMILA_VERSION, GenerateMode, Projection, Marker
from .params import LOG_GI_CREATED, LOG_GI_GENERATED, LOG_GI_PLOTTED
from .params import LOG_IMG_SAVED, LOG_DATA_SAVED, LOG_CONFIG_SAVED
from .params import ERR_IMG_SAVE_FAILED, ERR_DATA_SAVE_FAILED, ERR_CONFIG_SAVE_FAILED
from .functions import samila_help
from .genimage import GenerativeImage


def main():
def init_argparse():
"""
CLI main function.
Initialize argparse.
:return: None
:return: parser
"""
parser = argparse.ArgumentParser()
parser.add_argument('--version', help='version', action='store_true', default=False)
parser.add_argument('--info', help='info', action='store_true', default=False)
parser.add_argument('--verbose', help='verbose', action='store_true', default=False)

parser.add_argument('--load-config', help='load config', type=str)
parser.add_argument('--load-data', help='load data', type=str)
Expand Down Expand Up @@ -60,6 +64,38 @@ def main():
parser.add_argument('--save-data', help='save data', type=str)
parser.add_argument('--save-config', help='save config', type=str)
parser.add_argument('--no-display', help='no display', action='store_true', default=False)

return parser


def log_results(is_verbose, result, log_success, log_fail):
"""
Save result function.
:param is_verbose: is verbose
:type is_verbose: bool
:param result: result dictionary
:type result: dict
:param log_success: log for successful result
:type log_success: str
:param log_fail: log for failed result
:type log_fail: str
:return: None
"""
if is_verbose:
if result['status']:
print(log_success.format(result['message']))
else:
print(log_fail.format(result['message']))


def main():
"""
CLI main function.
:return: None
"""
parser = init_argparse()
args = parser.parse_args()

if args.version:
Expand All @@ -70,40 +106,37 @@ def main():
samila_help()
else:
gi = GenerativeImage(
function1=args.function1,
function2=args.function2,
func_seed=args.function_seed,
data=args.load_data,
config=args.load_config,
function1=args.function1, function2=args.function2,
func_seed=args.function_seed, data=args.load_data, config=args.load_config,
)
if args.verbose:
print(LOG_GI_CREATED)
gi.generate(
seed=args.seed,
start=args.start,
step=args.step,
stop=args.stop,
start=args.start, step=args.step, stop=args.stop,
mode=args.mode,
)
if args.verbose:
print(LOG_GI_GENERATED)
gi.plot(
color=args.color,
bgcolor=args.bgcolor,
cmap=args.cmap,
spot_size=args.spot_size,
size=args.size,
projection=args.projection,
marker=args.marker,
alpha=args.alpha,
linewidth=args.linewidth,
rotation=args.rotation,
color=args.color, bgcolor=args.bgcolor, cmap=args.cmap, spot_size=args.spot_size,
size=args.size, projection=args.projection, marker=args.marker, alpha=args.alpha,
linewidth=args.linewidth, rotation=args.rotation,
)
if args.verbose:
print(LOG_GI_PLOTTED)
if not args.no_display:
plt.show()

if args.save_image:
gi.save_image(args.save_image, args.depth)
result = gi.save_image(args.save_image, args.depth)
log_results(args.verbose, result, LOG_IMG_SAVED, ERR_IMG_SAVE_FAILED)
if args.save_data:
gi.save_data(args.save_data)
result = gi.save_data(args.save_data)
log_results(args.verbose, result, LOG_DATA_SAVED, ERR_DATA_SAVE_FAILED)
if args.save_config:
gi.save_config(args.save_config)
result= gi.save_config(args.save_config)
log_results(args.verbose, result, LOG_CONFIG_SAVED, ERR_CONFIG_SAVE_FAILED)


if __name__ == "__main__":
Expand Down
10 changes: 10 additions & 0 deletions samila/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@
COLOR_NOT_FOUND_WARNING = "color '{0}' not found. Replacing it with '{1}'"
DEPRECATION_WARNING = "`{}` is deprecated and may be removed in future releases."

LOG_GI_CREATED = "[LOG] GenerativeImage object created."
LOG_GI_GENERATED = "[LOG] GenerativeImage generated."
LOG_GI_PLOTTED = "[LOG] GenerativeImage plotted."
LOG_IMG_SAVED = "[LOG] Image saved. File address: {0}"
LOG_DATA_SAVED = "[LOG] Data saved. File address: {0}"
LOG_CONFIG_SAVED = "[LOG] Config saved. File address: {0}"
ERR_IMG_SAVE_FAILED = "[ERROR] Image save failed. Message: {0}"
ERR_DATA_SAVE_FAILED = "[ERROR] Data save failed. Message: {0}"
ERR_CONFIG_SAVE_FAILED = "[ERROR] Config save failed. Message: {0}"


class GenerateMode(Enum):
"""
Expand Down

0 comments on commit b5b811d

Please sign in to comment.