Skip to content

Commit 321a064

Browse files
rubikizikeros
andauthored
feat: add PyCharm terminal friendly logging with paths in front (#56)
* feat: add --link option (#50) * fix: rename links option * fix: rename links option in xenon.core * fix: tests --------- Co-authored-by: Krystian Safjan <[email protected]>
1 parent 2b5c97e commit 321a064

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

test_xenon.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
from xenon import core, api, main
2222

2323

24-
Args = collections.namedtuple('Args', 'absolute average modules averagenum')
24+
Args = collections.namedtuple(
25+
'Args', 'absolute average modules averagenum paths_in_front'
26+
)
2527

2628

2729
class CatchAll(object):
@@ -44,6 +46,7 @@ class Arguments(object):
4446
absolute = None
4547
modules = None
4648
averagenum = None
49+
paths_in_front = False
4750

4851

4952
@parametrized(
@@ -87,42 +90,42 @@ def testCheck(self):
8790
# infractions
8891
(
8992
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
90-
('C', 'B', 'B', None),
93+
('C', 'B', 'B', None, False),
9194
0
9295
),
9396
(
9497
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
95-
('B', 'B', 'B', None),
98+
('B', 'B', 'B', None, False),
9699
1
97100
),
98101
(
99102
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
100-
('C', 'A', 'B', None),
103+
('C', 'A', 'B', None, True),
101104
1
102105
),
103106
(
104107
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
105-
('C', 'B', 'A', None),
108+
('C', 'B', 'A', None, False),
106109
1
107110
),
108111
(
109112
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
110-
(None, 'B', 'B', None),
113+
(None, 'B', 'B', None, True),
111114
0
112115
),
113116
(
114117
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
115-
('C', None, 'B', None),
118+
('C', None, 'B', None, False),
116119
0
117120
),
118121
(
119122
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
120-
('C', 'B', None, None),
123+
('C', 'B', None, None, True),
121124
0
122125
),
123126
(
124127
{'mod.py': [4, 12, 8, 9], 'mod2.py': [3, 3, 2, 10]},
125-
(None, None, None, 0),
128+
(None, None, None, 0, True),
126129
1
127130
),
128131
)

xenon/__init__.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ def get_parser():
154154
parser.add_argument('-c', '--config-file', metavar='<path>', dest='config',
155155
default='.xenon.yml', help='Xenon config file '
156156
'(default: %(default)s)')
157+
parser.add_argument('--paths-in-front', dest='paths_in_front', action='store_true',
158+
help='Print block and module complexity with log line starting with their path')
157159

158160
return parser
159161

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

233235
args = args or parse_args("pyproject.toml")
234-
logging.basicConfig(level=logging.INFO)
236+
if args.paths_in_front:
237+
# Skip the level and module name to have log line starting with message
238+
# When using xenon in PyCharm terminal, one can benefit from
239+
# PyCharm changing path to the violation location into the active link
240+
logging.basicConfig(level=logging.INFO, format='%(message)s')
241+
else:
242+
logging.basicConfig(level=logging.INFO)
235243
logger = logging.getLogger('xenon')
236244
if args.url and len(args.path) > 1:
237245
logger.error(

xenon/core.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,12 @@ def find_infractions(args, logger, results):
6565
module_cc += block['complexity']
6666
r = cc_rank(block['complexity'])
6767
if check(r, args.absolute):
68-
logger.error('block "%s:%s %s" has a rank of %s', module,
69-
block['lineno'], block['name'], r)
68+
if args.paths_in_front:
69+
logger.error('%s:%s "%s" block has a rank of %s', module,
70+
block['lineno'], block['name'], r)
71+
else:
72+
logger.error('block "%s:%s %s" has a rank of %s', module,
73+
block['lineno'], block['name'], r)
7074
infractions += 1
7175
module_averages.append((module, av(module_cc, len(blocks))))
7276
total_cc += module_cc
@@ -85,6 +89,9 @@ def find_infractions(args, logger, results):
8589
for module, ma in module_averages:
8690
mar = cc_rank(ma)
8791
if check(mar, args.modules):
88-
logger.error('module %r has a rank of %s', module, mar)
92+
if args.paths_in_front:
93+
logger.error('%r module has a rank of %s', module, mar)
94+
else:
95+
logger.error('module %r has a rank of %s', module, mar)
8996
infractions += 1
9097
return infractions

0 commit comments

Comments
 (0)