Browse Source

abstract the project filter logic a bit

Doug Hellmann 4 years ago
parent
commit
f8b9b8cda0
3 changed files with 43 additions and 13 deletions
  1. 4
    8
      aeromancer/cli/grep.py
  2. 3
    5
      aeromancer/project.py
  3. 36
    0
      aeromancer/project_filter.py

+ 4
- 8
aeromancer/cli/grep.py View File

@@ -4,6 +4,7 @@ import logging
4 4
 import os
5 5
 
6 6
 from aeromancer import project
7
+from aeromancer import project_filter
7 8
 
8 9
 from cliff.command import Command
9 10
 
@@ -15,13 +16,7 @@ class Grep(Command):
15 16
 
16 17
     def get_parser(self, prog_name):
17 18
         parser = super(Grep, self).get_parser(prog_name)
18
-        parser.add_argument(
19
-            '--project',
20
-            action='append',
21
-            default=[],
22
-            dest='projects',
23
-            help='projects to limit search',
24
-        )
19
+        project_filter.ProjectFilter.add_arguments(parser)
25 20
         parser.add_argument('pattern',
26 21
                             action='store',
27 22
                             help='regular expression',
@@ -31,7 +26,8 @@ class Grep(Command):
31 26
     def take_action(self, parsed_args):
32 27
         session = self.app.get_db_session()
33 28
         pm = project.ProjectManager(session)
34
-        for r in pm.grep(parsed_args.pattern, parsed_args.projects):
29
+        prj_filt = project_filter.ProjectFilter.from_parsed_args(parsed_args)
30
+        for r in pm.grep(parsed_args.pattern, prj_filt):
35 31
             line_num, content, filename, project_name = r
36 32
             print('%s/%s:%s:%s' %
37 33
                   (project_name, filename, line_num, content.rstrip())

+ 3
- 5
aeromancer/project.py View File

@@ -194,7 +194,7 @@ class ProjectManager(object):
194 194
                 self._remove_file_data(obj, reason='file no longer exists')
195 195
                 self.session.flush()
196 196
 
197
-    def grep(self, pattern, projects=[]):
197
+    def grep(self, pattern, prj_filter):
198 198
         """Given a pattern, search for lines in files in all projects that match it.
199 199
 
200 200
         Returns results of the query, including the four columns line
@@ -210,9 +210,7 @@ class ProjectManager(object):
210 210
         ).join(File, Project).filter(
211 211
             Line.content.op('regexp')(pattern)
212 212
         )
213
-        if projects:
214
-            query = query.filter(
215
-                Project.name.in_(projects)
216
-            )
213
+        if prj_filter:
214
+            query = prj_filter.update_query(query)
217 215
         query = query.order_by(Project.name, File.name, Line.number)
218 216
         return query.yield_per(20).all()

+ 36
- 0
aeromancer/project_filter.py View File

@@ -0,0 +1,36 @@
1
+import argparse
2
+
3
+
4
+from aeromancer.db.models import Project
5
+
6
+
7
+class ProjectFilter(object):
8
+    """Manage the arguments for filtering queries by project.
9
+    """
10
+
11
+    @staticmethod
12
+    def add_arguments(parser):
13
+        """Given an argparse.ArgumentParser add arguments.
14
+        """
15
+        grp = parser.add_argument_group('Project Filter')
16
+        grp.add_argument(
17
+            '--project',
18
+            action='append',
19
+            default=[],
20
+            dest='projects',
21
+            help='projects to limit search, by exact name',
22
+        )
23
+
24
+    @classmethod
25
+    def from_parsed_args(cls, parsed_args):
26
+        return cls(projects=parsed_args.projects)
27
+
28
+    def __init__(self, projects):
29
+        self.projects = projects
30
+
31
+    def update_query(self, query):
32
+        if self.projects:
33
+            query = query.filter(
34
+                Project.name.in_(self.projects)
35
+            )
36
+        return query

Loading…
Cancel
Save