summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Hellmann <doug@doughellmann.com>2014-12-22 20:04:22 +0000
committerDoug Hellmann <doug@doughellmann.com>2014-12-22 20:04:22 +0000
commitf8b9b8cda0e5e395d5a8d52ba015e050c3a90dbf (patch)
tree88f0dfd44281759253011750a7308d28c6d83b7a
parent85dc323561be9b707da034069e1e8342673c988f (diff)
abstract the project filter logic a bit
-rw-r--r--aeromancer/cli/grep.py12
-rw-r--r--aeromancer/project.py8
-rw-r--r--aeromancer/project_filter.py36
3 files changed, 43 insertions, 13 deletions
diff --git a/aeromancer/cli/grep.py b/aeromancer/cli/grep.py
index c0acfd2..d7cb92d 100644
--- a/aeromancer/cli/grep.py
+++ b/aeromancer/cli/grep.py
@@ -4,6 +4,7 @@ import logging
4import os 4import os
5 5
6from aeromancer import project 6from aeromancer import project
7from aeromancer import project_filter
7 8
8from cliff.command import Command 9from cliff.command import Command
9 10
@@ -15,13 +16,7 @@ class Grep(Command):
15 16
16 def get_parser(self, prog_name): 17 def get_parser(self, prog_name):
17 parser = super(Grep, self).get_parser(prog_name) 18 parser = super(Grep, self).get_parser(prog_name)
18 parser.add_argument( 19 project_filter.ProjectFilter.add_arguments(parser)
19 '--project',
20 action='append',
21 default=[],
22 dest='projects',
23 help='projects to limit search',
24 )
25 parser.add_argument('pattern', 20 parser.add_argument('pattern',
26 action='store', 21 action='store',
27 help='regular expression', 22 help='regular expression',
@@ -31,7 +26,8 @@ class Grep(Command):
31 def take_action(self, parsed_args): 26 def take_action(self, parsed_args):
32 session = self.app.get_db_session() 27 session = self.app.get_db_session()
33 pm = project.ProjectManager(session) 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 line_num, content, filename, project_name = r 31 line_num, content, filename, project_name = r
36 print('%s/%s:%s:%s' % 32 print('%s/%s:%s:%s' %
37 (project_name, filename, line_num, content.rstrip()) 33 (project_name, filename, line_num, content.rstrip())
diff --git a/aeromancer/project.py b/aeromancer/project.py
index 5ee91a0..f9d8d4f 100644
--- a/aeromancer/project.py
+++ b/aeromancer/project.py
@@ -194,7 +194,7 @@ class ProjectManager(object):
194 self._remove_file_data(obj, reason='file no longer exists') 194 self._remove_file_data(obj, reason='file no longer exists')
195 self.session.flush() 195 self.session.flush()
196 196
197 def grep(self, pattern, projects=[]): 197 def grep(self, pattern, prj_filter):
198 """Given a pattern, search for lines in files in all projects that match it. 198 """Given a pattern, search for lines in files in all projects that match it.
199 199
200 Returns results of the query, including the four columns line 200 Returns results of the query, including the four columns line
@@ -210,9 +210,7 @@ class ProjectManager(object):
210 ).join(File, Project).filter( 210 ).join(File, Project).filter(
211 Line.content.op('regexp')(pattern) 211 Line.content.op('regexp')(pattern)
212 ) 212 )
213 if projects: 213 if prj_filter:
214 query = query.filter( 214 query = prj_filter.update_query(query)
215 Project.name.in_(projects)
216 )
217 query = query.order_by(Project.name, File.name, Line.number) 215 query = query.order_by(Project.name, File.name, Line.number)
218 return query.yield_per(20).all() 216 return query.yield_per(20).all()
diff --git a/aeromancer/project_filter.py b/aeromancer/project_filter.py
new file mode 100644
index 0000000..f05122c
--- /dev/null
+++ b/aeromancer/project_filter.py
@@ -0,0 +1,36 @@
1import argparse
2
3
4from aeromancer.db.models import Project
5
6
7class 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