Skip to content

Commit

Permalink
feat: add PyCharm terminal friendly logging with paths in front (#56)
Browse files Browse the repository at this point in the history
* feat: add --link option (#50)

* fix: rename links option

* fix: rename links option in xenon.core

* fix: tests

---------

Co-authored-by: Krystian Safjan <ksafjan@gmail.com>
  • Loading branch information
rubik and izikeros authored Oct 20, 2024
1 parent 2b5c97e commit 321a064
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
21 changes: 12 additions & 9 deletions test_xenon.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
from xenon import core, api, main


Args = collections.namedtuple('Args', 'absolute average modules averagenum')
Args = collections.namedtuple(
'Args', 'absolute average modules averagenum paths_in_front'
)


class CatchAll(object):
Expand All @@ -44,6 +46,7 @@ class Arguments(object):
absolute = None
modules = None
averagenum = None
paths_in_front = False


@parametrized(
Expand Down Expand Up @@ -87,42 +90,42 @@ def testCheck(self):
# infractions
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'B', 'B', None),
('C', 'B', 'B', None, False),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('B', 'B', 'B', None),
('B', 'B', 'B', None, False),
1
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'A', 'B', None),
('C', 'A', 'B', None, True),
1
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'B', 'A', None),
('C', 'B', 'A', None, False),
1
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
(None, 'B', 'B', None),
(None, 'B', 'B', None, True),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', None, 'B', None),
('C', None, 'B', None, False),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
('C', 'B', None, None),
('C', 'B', None, None, True),
0
),
(
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
(None, None, None, 0),
(None, None, None, 0, True),
1
),
)
Expand Down
10 changes: 9 additions & 1 deletion xenon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ def get_parser():
parser.add_argument('-c', '--config-file', metavar='<path>', dest='config',
default='.xenon.yml', help='Xenon config file '
'(default: %(default)s)')
parser.add_argument('--paths-in-front', dest='paths_in_front', action='store_true',
help='Print block and module complexity with log line starting with their path')

return parser

Expand Down Expand Up @@ -231,7 +233,13 @@ def main(args=None):
from xenon.repository import gitrepo

args = args or parse_args("pyproject.toml")
logging.basicConfig(level=logging.INFO)
if args.paths_in_front:
# Skip the level and module name to have log line starting with message
# When using xenon in PyCharm terminal, one can benefit from
# PyCharm changing path to the violation location into the active link
logging.basicConfig(level=logging.INFO, format='%(message)s')
else:
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('xenon')
if args.url and len(args.path) > 1:
logger.error(
Expand Down
13 changes: 10 additions & 3 deletions xenon/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,12 @@ def find_infractions(args, logger, results):
module_cc += block['complexity']
r = cc_rank(block['complexity'])
if check(r, args.absolute):
logger.error('block "%s:%s %s" has a rank of %s', module,
block['lineno'], block['name'], r)
if args.paths_in_front:
logger.error('%s:%s "%s" block has a rank of %s', module,
block['lineno'], block['name'], r)
else:
logger.error('block "%s:%s %s" has a rank of %s', module,
block['lineno'], block['name'], r)
infractions += 1
module_averages.append((module, av(module_cc, len(blocks))))
total_cc += module_cc
Expand All @@ -85,6 +89,9 @@ def find_infractions(args, logger, results):
for module, ma in module_averages:
mar = cc_rank(ma)
if check(mar, args.modules):
logger.error('module %r has a rank of %s', module, mar)
if args.paths_in_front:
logger.error('%r module has a rank of %s', module, mar)
else:
logger.error('module %r has a rank of %s', module, mar)
infractions += 1
return infractions

0 comments on commit 321a064

Please sign in to comment.