Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
bob.devtools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
bob
bob.devtools
Commits
35f13db1
Commit
35f13db1
authored
6 years ago
by
André Anjos
Browse files
Options
Downloads
Patches
Plain Diff
[package] Add color support
parent
178d4b6a
No related branches found
No related tags found
1 merge request
!17
Terminal colors
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
bob/devtools/log.py
+86
-1
86 additions, 1 deletion
bob/devtools/log.py
conda/meta.yaml
+1
-0
1 addition, 0 deletions
conda/meta.yaml
setup.py
+1
-0
1 addition, 0 deletions
setup.py
with
88 additions
and
1 deletion
bob/devtools/log.py
+
86
−
1
View file @
35f13db1
...
@@ -7,6 +7,10 @@
...
@@ -7,6 +7,10 @@
import
sys
import
sys
import
logging
import
logging
import
click
import
termcolor
# get the default root logger of Bob
# get the default root logger of Bob
_logger
=
logging
.
getLogger
(
'
bob
'
)
_logger
=
logging
.
getLogger
(
'
bob
'
)
...
@@ -28,6 +32,87 @@ _debug_info.addFilter(_InfoFilter())
...
@@ -28,6 +32,87 @@ _debug_info.addFilter(_InfoFilter())
_logger
.
addHandler
(
_debug_info
)
_logger
.
addHandler
(
_debug_info
)
COLORMAP
=
dict
(
debug
=
dict
(),
info
=
dict
(
attrs
=
[
'
bold
'
]),
warn
=
dict
(
color
=
'
yellow
'
,
attrs
=
[
'
bold
'
]),
warning
=
dict
(
color
=
'
yellow
'
,
attrs
=
[
'
bold
'
]),
error
=
dict
(
color
=
'
red
'
),
exception
=
dict
(
color
=
'
red
'
,
attrs
=
[
'
bold
'
]),
critical
=
dict
(
color
=
'
red
'
,
attrs
=
[
'
bold
'
]),
)
'''
Default color map for homogenized color display
'''
def
_supports_color
():
"""
Returns True if the running system
'
s terminal supports color, and False
otherwise.
"""
plat
=
sys
.
platform
supported_platform
=
plat
!=
'
Pocket PC
'
and
(
plat
!=
'
win32
'
or
'
ANSICON
'
in
os
.
environ
)
# isatty is not always implemented, #6223.
is_a_tty
=
hasattr
(
sys
.
stdout
,
'
isatty
'
)
and
sys
.
stdout
.
isatty
()
if
not
supported_platform
or
not
is_a_tty
:
return
False
return
True
class
ColorLog
(
object
):
'''
Colorizes logging colors
'''
def
__init__
(
self
,
logger
):
self
.
_log
=
logger
def
__getattr__
(
self
,
name
):
if
name
in
[
'
debug
'
,
'
info
'
,
'
warn
'
,
'
warning
'
,
'
error
'
,
'
exception
'
,
'
critical
'
]:
if
_supports_color
():
return
lambda
s
,
*
args
:
getattr
(
self
.
_log
,
name
)(
termcolor
.
colored
(
s
,
**
COLORMAP
[
name
]),
*
args
)
else
:
return
lambda
s
,
*
args
:
getattr
(
self
.
_log
,
name
)(
s
,
*
args
)
return
getattr
(
self
.
_log
,
name
)
def
get_logger
(
name
):
"""
Returns the default logger as setup by this module
"""
return
ColorLog
(
logging
.
getLogger
(
name
))
def
_echo
(
text
,
*
args
,
**
kwargs
):
"""
Provides a colorized version of :py:func:`click.echo` (for terminals)
The color is stripped off if outputting to a file or piping the results of
a command using this function.
Parameters:
text (str): The text to be printed
args (tuple): Tuple of attributes directly passed to
:py:func:`termcolor.colored`
kwargs (dict): Dictionary of attributes directly passed to
:py:func:`termcolor.colored`
"""
click
.
echo
(
termcolor
.
colored
(
text
,
*
args
,
**
kwargs
))
def
echo_normal
(
text
):
"""
Color preset for normal text output for :py:func:`click.echo`
"""
_echo
(
text
,
'
green
'
)
def
echo_warning
(
text
):
"""
Color preset for normal warning output for :py:func:`click.echo`
"""
_echo
(
text
,
**
COLORMAP
[
'
warn
'
])
# helper functions to instantiate and set-up logging
# helper functions to instantiate and set-up logging
def
setup
(
logger_name
,
def
setup
(
logger_name
,
format
=
"
%(levelname)s:%(name)s@%(asctime)s: %(message)s
"
):
format
=
"
%(levelname)s:%(name)s@%(asctime)s: %(message)s
"
):
...
@@ -68,7 +153,7 @@ def setup(logger_name,
...
@@ -68,7 +153,7 @@ def setup(logger_name,
for
handler
in
_logger
.
handlers
:
for
handler
in
_logger
.
handlers
:
handler
.
setFormatter
(
formatter
)
handler
.
setFormatter
(
formatter
)
return
logger
return
ColorLog
(
logger
)
def
set_verbosity_level
(
logger
,
level
):
def
set_verbosity_level
(
logger
,
level
):
...
...
This diff is collapsed.
Click to expand it.
conda/meta.yaml
+
1
−
0
View file @
35f13db1
...
@@ -47,6 +47,7 @@ requirements:
...
@@ -47,6 +47,7 @@ requirements:
-
twine
-
twine
-
lxml
-
lxml
-
jinja2
-
jinja2
-
termcolor
test
:
test
:
requires
:
requires
:
...
...
This diff is collapsed.
Click to expand it.
setup.py
+
1
−
0
View file @
35f13db1
...
@@ -21,6 +21,7 @@ requires = [
...
@@ -21,6 +21,7 @@ requires = [
'
twine
'
,
'
twine
'
,
'
lxml
'
,
'
lxml
'
,
'
jinja2
'
,
'
jinja2
'
,
'
termcolor
'
,
]
]
setup
(
setup
(
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment