Merge lp:~javier.collado/checkbox-editor/bug984724 into lp:checkbox-editor

Proposed by Javier Collado
Status: Merged
Merged at revision: 145
Proposed branch: lp:~javier.collado/checkbox-editor/bug984724
Merge into: lp:checkbox-editor
Diff against target: 111 lines (+45/-55)
1 file modified
checkbox_editor/preferences.py (+45/-55)
To merge this branch: bzr merge lp:~javier.collado/checkbox-editor/bug984724
Reviewer Review Type Date Requested Status
Sylvain Pineau (community) Approve
Review via email: mp+102673@code.launchpad.net

Description of the change

This merge request fixes bug#984724

Instead of trying to parse each line, it goes directly to the interesting place using `re.search`.

Besides this,
- Here documents (<<) are ignored
- Only one pattern is expected after `cat` command
The initial design supported suites that included multiple files, but this has never been used, so this is a good opportunity to remove part of that code to simplify the implementation.

To post a comment you must log in.
Revision history for this message
Sylvain Pineau (sylvain-pineau) wrote :

Thanks for the here documents filter, it works perfectly.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'checkbox_editor/preferences.py'
2--- checkbox_editor/preferences.py 2011-11-10 08:33:28 +0000
3+++ checkbox_editor/preferences.py 2012-04-19 11:23:26 +0000
4@@ -208,62 +208,52 @@
5 force: Used to return a filename for a pattern
6 even when the file doesn't exist
7 """
8- logging.debug('Parsing command: {0}'.format(command))
9- try:
10- fragments = shlex.split(command)
11- except ValueError:
12- # Return nothing is shlex.split failed
13- problem = ('Command parsing error', command)
14- if problem not in self.problems:
15- self.problems.append(problem)
16- return []
17-
18- if not fragments[0] == 'cat':
19- logging.warning("'cat' command not found: {0!r}".format(command))
20- problem = ('Command parsing error', command)
21- if problem not in self.problems:
22- self.problems.append(problem)
23- return []
24- elif len(fragments) > 2:
25- logging.warning("Unexpected command: {0!r}".format(command))
26- problem = ('Command parsing error', command)
27- if problem not in self.problems:
28- self.problems.append(problem)
29- return []
30-
31-
32- # Iterate through all environment variables found in the path
33+ match = re.search('cat\s+(?P<pattern>\S+)', command)
34+ if not match:
35+ return []
36+
37+ filename_pattern = match.group('pattern')
38+
39+ # Skip here documents
40+ # (test suites embedded in the same file)
41+ if filename_pattern.startswith('<<'):
42+ return []
43+
44+ filename_pattern = self.expand(filename_pattern)
45+
46+ # patch to support for extglob patterns
47+ filename_pattern = filename_pattern.replace('?(.in)', '*')
48+
49 all_filenames = []
50- for filename_pattern in self.expand(command).split()[1:]:
51- filenames = [filename
52- for filename in glob(filename_pattern)
53- if not filename.endswith('~')]
54-
55- if not filenames and force:
56- directory = os.path.dirname(filename_pattern)
57- rootname, extension_pattern = os.path.splitext(os.path.basename(filename_pattern))
58-
59- if not self.ENV_VAR_REGEX.match(directory):
60- directory = os.path.realpath(directory)
61-
62- if extension_pattern == '.txt*':
63- if any(glob(os.path.join(directory, '*.txt.in'))):
64- extension = '.txt.in'
65- else:
66- extension = '.txt'
67-
68- all_filenames.append(os.path.join(directory,
69- '{0}{1}'.format(rootname, extension)))
70-
71- for filename in filenames:
72- if not self.ENV_VAR_REGEX.match(filename):
73- filename = os.path.realpath(filename)
74-
75- if not os.path.isfile(filename):
76- logging.error('File not found: (filename: {0}, command: {1}) '
77- .format(filename, command))
78- continue
79- all_filenames.append(filename)
80+ filenames = [filename
81+ for filename in glob(filename_pattern)
82+ if not filename.endswith('~')]
83+
84+ if not filenames and force:
85+ directory = os.path.dirname(filename_pattern)
86+ rootname, extension_pattern = os.path.splitext(os.path.basename(filename_pattern))
87+
88+ if not self.ENV_VAR_REGEX.match(directory):
89+ directory = os.path.realpath(directory)
90+
91+ if extension_pattern == '.txt*':
92+ if any(glob(os.path.join(directory, '*.txt.in'))):
93+ extension = '.txt.in'
94+ else:
95+ extension = '.txt'
96+
97+ all_filenames.append(os.path.join(directory,
98+ '{0}{1}'.format(rootname, extension)))
99+
100+ for filename in filenames:
101+ if not self.ENV_VAR_REGEX.match(filename):
102+ filename = os.path.realpath(filename)
103+
104+ if not os.path.isfile(filename):
105+ logging.error('File not found: (filename: {0}, command: {1}) '
106+ .format(filename, command))
107+ continue
108+ all_filenames.append(filename)
109
110 logging.debug('Filenames found in command: {0}'.format(all_filenames))
111 return all_filenames

Subscribers

People subscribed via source and target branches

to all changes: