Merge lp:~dholbach/click-reviewers-tools/modules-module-plus-tests into lp:click-reviewers-tools

Proposed by Daniel Holbach
Status: Merged
Approved by: Martin Albisetti
Approved revision: 234
Merged at revision: 232
Proposed branch: lp:~dholbach/click-reviewers-tools/modules-module-plus-tests
Merge into: lp:click-reviewers-tools
Diff against target: 194 lines (+103/-49)
4 files modified
bin/click-review (+5/-48)
clickreviews/modules.py (+67/-0)
clickreviews/tests/test_modules.py (+29/-0)
debian/changelog (+2/-1)
To merge this branch: bzr merge lp:~dholbach/click-reviewers-tools/modules-module-plus-tests
Reviewer Review Type Date Requested Status
Martin Albisetti (community) Approve
Review via email: mp+232444@code.launchpad.net
To post a comment you must log in.
Revision history for this message
Martin Albisetti (beuno) wrote :

105 + lambda a: a.startswith('cr_') and \

I feel like we're so close to not having that logic spread around, on what to filter out, it feels like a bit of a shame to have that there instead of in a global variable as well.

I don't feel strongly enough about it to block it, so here's my +1, and decide what you feel is the best balance.

review: Approve
Revision history for this message
Daniel Holbach (dholbach) wrote :

Makes perfect sense.

233. By Daniel Holbach

move functionality to find relevant modules into its own simple function

234. By Daniel Holbach

make sure we actually have clickreviews modules to look at

Revision history for this message
Daniel Holbach (dholbach) wrote :

Hope it's better now.

Revision history for this message
Martin Albisetti (beuno) wrote :

Thank you, looks great.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'bin/click-review'
2--- bin/click-review 2014-08-25 07:43:05 +0000
3+++ bin/click-review 2014-08-27 17:21:20 +0000
4@@ -1,54 +1,11 @@
5 #!/usr/bin/python3
6
7+from clickreviews import modules
8 import argparse
9-import clickreviews
10-import imp
11-import inspect
12 import json
13-import pkgutil
14 import sys
15
16
17-def get_modules():
18- '''
19- Here we have a look at all the modules in the
20- clickreviews package and filter out a few which
21- are not relevant.
22-
23- Basically we look at all the ones which are
24- derived from cr_common, where we can later on
25- instantiate a *Review* object and run the
26- necessary checks.
27- '''
28-
29- all_modules = [name for _, name, _ in
30- pkgutil.iter_modules(clickreviews.__path__)]
31- narrow_down_modules = \
32- lambda a: a.startswith('cr_') and \
33- a not in ['cr_common', 'cr_tests', 'cr_skeleton']
34- return list(filter(narrow_down_modules, all_modules))
35-
36-
37-def init_main_class(module_name, click_file):
38- '''
39- This function will find the Click*Review class in
40- the specified module and instantiate it with the
41- location of the .click file we want to inspect.
42- '''
43-
44- module = imp.load_source(module_name,
45- '%s/%s.py' % (clickreviews.__path__[0],
46- module_name))
47-
48- classes = inspect.getmembers(module, inspect.isclass)
49- find_main_class = lambda a: a[0].startswith('Click') and \
50- not a[0].endswith('Exception') and \
51- a[1].__module__ == module_name
52- main_class = list(filter(find_main_class, classes))
53- init_object = getattr(module, main_class[0][0])
54- return init_object(click_file)
55-
56-
57 def print_findings(which, kind):
58 '''
59 Print a summary of the issues found.
60@@ -116,12 +73,12 @@
61 # rc = review.do_report()
62 #
63 results = {}
64- modules = get_modules()
65- if not modules:
66+ cr_modules = modules.get_modules()
67+ if not cr_modules:
68 print("No 'clickreviews' modules found.")
69 sys.exit(1)
70- for module in modules:
71- review = init_main_class(module, click_file)
72+ for module in cr_modules:
73+ review = modules.init_main_class(module, click_file)
74 review.do_checks()
75 results[module.replace('cr_', '')] = review.click_report
76 rc = report(results, args)
77
78=== added file 'clickreviews/modules.py'
79--- clickreviews/modules.py 1970-01-01 00:00:00 +0000
80+++ clickreviews/modules.py 2014-08-27 17:21:20 +0000
81@@ -0,0 +1,67 @@
82+import clickreviews
83+import imp
84+import inspect
85+import os
86+import pkgutil
87+
88+IRRELEVANT_MODULES = ['cr_common', 'cr_tests', 'cr_skeleton']
89+
90+
91+def narrow_down_modules(modules):
92+ '''
93+ Get a list of file names or module names and filter out
94+ the ones we know are irrelevant.
95+ '''
96+ relevant_modules = []
97+ for module in modules:
98+ module_name = os.path.basename(module).replace('.py', '')
99+ if module_name not in IRRELEVANT_MODULES and \
100+ module_name.startswith('cr_'):
101+ relevant_modules += [module]
102+ return relevant_modules
103+
104+
105+def get_modules():
106+ '''
107+ Here we have a look at all the modules in the
108+ clickreviews package and filter out a few which
109+ are not relevant.
110+
111+ Basically we look at all the ones which are
112+ derived from cr_common, where we can later on
113+ instantiate a *Review* object and run the
114+ necessary checks.
115+ '''
116+
117+ all_modules = [name for _, name, _ in
118+ pkgutil.iter_modules(clickreviews.__path__)]
119+ return narrow_down_modules(all_modules)
120+
121+
122+def find_main_class(module_name):
123+ '''
124+ This function will find the Click*Review class in
125+ the specified module.
126+ '''
127+ module = imp.load_source(module_name,
128+ '%s/%s.py' % (clickreviews.__path__[0],
129+ module_name))
130+
131+ classes = inspect.getmembers(module, inspect.isclass)
132+ find_main_class = lambda a: a[0].startswith('Click') and \
133+ not a[0].endswith('Exception') and \
134+ a[1].__module__ == module_name
135+ main_class = list(filter(find_main_class, classes))
136+ init_object = getattr(module, main_class[0][0])
137+ return init_object
138+
139+
140+def init_main_class(module_name, click_file):
141+ '''
142+ This function will instantiate the main Click*Review
143+ class of a given module and instantiate it with the
144+ location of the .click file we want to inspect.
145+ '''
146+
147+ init_object = find_main_class(module_name)
148+ return init_object(click_file)
149
150=== added file 'clickreviews/tests/test_modules.py'
151--- clickreviews/tests/test_modules.py 1970-01-01 00:00:00 +0000
152+++ clickreviews/tests/test_modules.py 2014-08-27 17:21:20 +0000
153@@ -0,0 +1,29 @@
154+from clickreviews import modules, cr_tests
155+import clickreviews
156+import glob
157+
158+
159+class TestModules(cr_tests.TestClickReview):
160+ '''Tests for the modules module.'''
161+ def setUp(self):
162+ self.cr_modules = modules.get_modules()
163+ cr_tests.mock_patch()
164+ super()
165+
166+ def test_number_of_suitable_modules(self):
167+ path = clickreviews.__path__[0]
168+ module_files = glob.glob(path + '/*.py')
169+ relevant_module_files = modules.narrow_down_modules(module_files)
170+ self.assertEqual(len(relevant_module_files),
171+ len(self.cr_modules))
172+
173+ def test_number_of_suitable_modules_greater0(self):
174+ self.assertTrue(len(self.cr_modules) > 0)
175+
176+ def test_number_of_available_review_classes(self):
177+ count = 0
178+ for module_name in self.cr_modules:
179+ review = modules.find_main_class(module_name)
180+ if review:
181+ count += 1
182+ self.assertEqual(count, len(self.cr_modules))
183
184=== modified file 'debian/changelog'
185--- debian/changelog 2014-08-27 14:22:21 +0000
186+++ debian/changelog 2014-08-27 17:21:20 +0000
187@@ -1,6 +1,7 @@
188 click-reviewers-tools (0.10) UNRELEASED; urgency=medium
189
190- *
191+ * Split out code to find Click*Review classes in the clickreviews package
192+ into its own module, add tests for it.
193
194 -- Daniel Holbach <daniel.holbach@ubuntu.com> Wed, 27 Aug 2014 16:22:03 +0200
195

Subscribers

People subscribed via source and target branches