Skip to content
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

Added main module detection and cli construction, now should show the… #361

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions plumbum/cli/application.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import functools
import inspect
import os
import re
import sys
from collections import defaultdict
from textwrap import TextWrapper

from plumbum import colors, local
from plumbum.cli.i18n import get_translation_for
from plumbum.lib import getdoc
from plumbum.lib import getdoc, get_main_module_frame

from .switches import (
CountOf,
Expand Down Expand Up @@ -77,6 +78,7 @@ def __repr__(self):
# CLI Application base class
# ===================================================================================================

main_module_ending_rx = re.compile("\.__main__$")

class Application:
"""The base class for CLI applications; your "entry point" class should derive from it,
Expand Down Expand Up @@ -182,7 +184,11 @@ def __init__(self, executable):
# Filter colors

if self.PROGNAME is None:
self.PROGNAME = os.path.basename(executable)
spec = get_main_module_frame().f_globals.get("__spec__", None)
if spec:
self.PROGNAME = " ".join(("python -m", main_module_ending_rx.sub("", spec.name)))
else:
self.PROGNAME = os.path.basename(executable)
elif isinstance(self.PROGNAME, colors._style):
self.PROGNAME = self.PROGNAME | os.path.basename(executable)
elif colors.filter(self.PROGNAME) == "":
Expand Down
9 changes: 9 additions & 0 deletions plumbum/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,12 @@ def read_fd_decode_safely(fd, size=4096):
data += os.read(fd.fileno(), 1)

return data, data.decode("utf-8")

def get_main_module_frame():
"""
Gets the frame of the __main__ module (the one which is called with command line) of an app.
"""
fr = sys._getframe(0)
while fr and fr.f_globals["__name__"] != "__main__":
fr = fr.f_back
return fr