/usr/lib/gedit/plugins/gdp/find.py is in gedit-developer-plugins 0.5.15-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | #!/usr/bin/python
# Copyright (C) 2009-2012 - Curtis Hovey <sinzui.is at verizon.net>
# This software is licensed under the GNU General Public License version 2
# (see the file COPYING).
"""Find in files and replace strings in many files."""
__all__ = [
'extract_match',
'find_files',
'find_matches',
'Finder',
]
from collections import namedtuple
from gettext import gettext as _
import mimetypes
import os
import re
import sre_constants
import sys
import threading
from optparse import OptionParser
from gi.repository import (
GObject,
Gdk,
Gtk,
)
from gdp import (
config,
ControllerMixin,
setup_file_lines_view,
)
GObject.threads_init()
find_params = namedtuple(
'FindParams', ['path', 'pattern', 'is_re', 'is_case', 'file_pattern'])
def find_matches(root_dir, file_pattern, match_pattern, substitution=None):
"""Iterate a summary of matching lines in a file."""
match_re = re.compile(match_pattern)
for candidate in find_files(root_dir, file_pattern=file_pattern):
file_path, mime_type = candidate
summary = extract_match(
file_path, match_re, substitution=substitution)
if summary:
summary['mime_type'] = mime_type
yield summary
def find_files(root_dir, skip_dir_pattern='^[.]', file_pattern='.*'):
"""Iterate the matching files below a directory."""
skip_dir_re = re.compile(r'^.*%s' % skip_dir_pattern)
file_re = re.compile(r'^.*%s' % file_pattern)
for path, subdirs, files in os.walk(root_dir):
subdirs[:] = [dir_ for dir_ in subdirs
if skip_dir_re.match(dir_) is None]
for file_ in files:
file_path = os.path.join(path, file_)
if os.path.islink(file_path):
continue
mime_type, encoding_ = mimetypes.guess_type(file_)
if ControllerMixin.is_editable(mime_type):
if file_re.match(file_path) is not None:
yield file_path, mime_type
def extract_match(file_path, match_re, substitution=None):
"""Return a summary of matches in a file."""
lines = []
content = []
match = None
file_ = open(file_path, 'r')
try:
for lineno, line in enumerate(file_):
match = match_re.search(line)
if match:
lines.append(
{'lineno': lineno + 1, 'text': line.strip(),
'match': match})
if substitution is not None:
line = match_re.sub(substitution, line)
if substitution is not None:
content.append(line)
finally:
file_.close()
if lines:
if substitution is not None:
file_ = open(file_path, 'w')
try:
file_.write(''.join(content))
finally:
file_.close()
return {'file_path': file_path, 'lines': lines}
return None
class FinderWorker(threading.Thread):
def __init__(self, treestore, find_params, callback, substitution=None):
super(FinderWorker, self).__init__()
self.treestore = treestore
self.find_params = find_params
self.callback = callback
self.substitution = substitution
self.theme = Gtk.IconTheme.get_default()
@property
def pattern(self):
pattern = self.find_params.pattern
if not self.find_params.is_re:
pattern = re.escape(pattern)
if not self.find_params.is_case:
pattern = '(?i)%s' % pattern
return pattern
def append_match(self, piter, file_path, icon, lineno, text, path):
self.treestore.append(piter, (file_path, icon, lineno, text, path))
return False
def idle_append_match(self, piter, file_path, icon, lineno, text, path):
GObject.idle_add(
self.append_match, piter, file_path, icon, lineno, text, path)
def start(self):
super(FinderWorker, self).start()
return False
def run(self):
try:
for summary in find_matches(
self.find_params.path, self.find_params.file_pattern,
self.pattern, substitution=self.substitution):
file_path = summary['file_path']
mime_type = summary['mime_type'] or 'text'
mime_type = 'gnome-mime-%s' % mime_type.replace('/', '-')
if not self.theme.has_icon(mime_type):
mime_type = 'gnome-mime-text'
piter = self.treestore.append(
None,
(file_path, mime_type, 0, None, self.find_params.path))
if self.substitution is None:
icon = 'edit-find' # Gtk.STOCK_FIND
else:
icon = 'edit-find-replace' # Gtk.STOCK_FIND_AND_REPLACE
for line in summary['lines']:
self.idle_append_match(
piter, file_path, icon, line['lineno'], line['text'],
self.find_params.path)
if self.treestore.get_iter_first() is None:
message = 'No matches found'
self.idle_append_match(
None, message, 'stock_dialog-info', 0, None, None)
except sre_constants.error, e:
message = 'Find could not be run: %s' % str(e)
self.idle_append_match(
None, message, 'stock_dialog-info', 0, None, None)
self.callback()
class Finder(ControllerMixin):
"""Find and replace content in files."""
WORKING_DIRECTORY = '<Working Directory>'
CURRENT_FILE = '<Current File>'
ANY_FILE = '<Any Text File>'
def __init__(self, window):
self.window = window
self.signal_ids = {}
self.last_find = None
self.widgets = Gtk.Builder()
self.widgets.add_from_file(
'%s/find.ui' % os.path.dirname(__file__))
self.setup_widgets()
self.find_panel = self.widgets.get_object('find_side_panel')
panel = window.get_side_panel()
icon = Gtk.Image.new_from_stock(Gtk.STOCK_FIND, Gtk.IconSize.MENU)
panel.add_item(self.find_panel, 'gdpfind', 'Find in files', icon)
def setup_widgets(self):
"""Setup the widgets with default data."""
self.widgets.connect_signals(self.ui_callbacks)
self.pattern_comboentry = self.widgets.get_object(
'pattern_comboentry')
self.pattern_comboentry.get_child().set_width_chars(24)
self.setup_comboentry(self.pattern_comboentry, config_key='matches')
self.path_comboentry = self.widgets.get_object('path_comboentry')
self.setup_comboentry(
self.path_comboentry, self.CURRENT_FILE, 'paths')
self.update_comboentry(self.path_comboentry, os.getcwd())
self.file_comboentry = self.widgets.get_object('file_comboentry')
self.setup_comboentry(
self.file_comboentry, self.ANY_FILE, 'files')
self.substitution_comboentry = self.widgets.get_object(
'substitution_comboentry')
self.setup_comboentry(
self.substitution_comboentry, config_key='substitutions')
self.file_lines_view = self.widgets.get_object('file_lines_view')
setup_file_lines_view(self.file_lines_view, self, 'Matches')
def deactivate(self):
"""Clean up resources before deactivation."""
panel = self.window.get_side_panel()
panel.remove_item(self.find_panel)
def valid_state(self, config_key, value):
if (config_key == 'paths'
and value not in (self.WORKING_DIRECTORY, self.CURRENT_FILE)):
return os.path.exists(value)
return True
def setup_comboentry(self, comboentry, default=None, config_key=None):
liststore = Gtk.ListStore.new([GObject.TYPE_STRING])
liststore.set_sort_column_id(0, Gtk.SortType.ASCENDING)
comboentry.set_model(liststore)
comboentry.set_entry_text_column(0)
if config_key is not None:
for value in config.getlist('finder', config_key):
# This might need to know that paths contains dirs.
if value and self.valid_state(config_key, value):
self.update_comboentry(comboentry, value, False)
if default is not None:
self.update_comboentry(comboentry, default)
def update_comboentry(self, comboentry, text, set_active=True):
"""Update the match text combobox."""
found_index = None
for index, row in enumerate(iter(comboentry.get_model())):
if row[0] == text:
# The text is already in the list, does it need to be active?
found_index = index
break
if found_index is not None and set_active:
comboentry.set_active(found_index)
elif found_index is None:
comboentry.append_text(text)
if set_active:
self.update_comboentry(comboentry, text)
@property
def ui_callbacks(self):
"""The dict of callbacks for the ui widgets."""
return {
'on_choose_directory_icon_press':
self.on_choose_directory_icon_press,
'on_find_in_files': self.on_find_in_files,
'on_find_in_files_icon_press': self.on_find_in_files_icon_press,
'on_replace_in_files': self.on_replace_in_files,
'on_replace_in_files_icon_press':
self.on_replace_in_files_icon_press,
'on_save_results': self.on_save_results,
}
def show(self, action):
"""Show the finder pane."""
panel = self.window.get_side_panel()
panel.activate_item(self.find_panel)
panel.props.visible = True
def show_replace(self, action):
"""Show the finder pane and expand replace."""
self.show(None)
self.widgets.get_object('actions').activate()
@property
def path(self):
"""The base directory to traverse set by the user."""
path_ = self.path_comboentry.get_active_text()
self.update_comboentry(self.path_comboentry, path_)
if path_ in (self.WORKING_DIRECTORY, '', None):
path_ = '.'
elif path_ == self.CURRENT_FILE:
document = self.active_document
path_ = document.get_uri_for_display().replace('file://', '')
path_ = os.path.dirname(path_)
return path_
@property
def file_pattern(self):
"""The pattern to match the file name with."""
pattern = self.file_comboentry.get_active_text()
self.update_comboentry(self.file_comboentry, pattern)
if pattern in (self.ANY_FILE, '', None):
pattern = '.'
if self.path_comboentry.get_active_text() == self.CURRENT_FILE:
document = self.active_document
pattern = os.path.basename(document.get_uri_for_display())
return pattern
@property
def match_pattern(self):
pattern = self.pattern_comboentry.get_active_text()
self.update_comboentry(self.pattern_comboentry, pattern)
return pattern
def on_file_path_added(self, window, new_path):
self.update_comboentry(
self.path_comboentry, new_path, set_active=False)
def save_find_data(self):
data = [
('paths', self.path),
('matches', self.match_pattern),
('files', self.file_comboentry.get_active_text()),
('substitutions', self.substitution_comboentry.get_active_text()),
]
for key, value in data:
history = config.getlist('finder', key)
if value in history:
history.remove(value)
history.insert(0, value)
if len(history) > 10:
history = history[0:10]
config.setlist('finder', key, history)
config.dump()
def get_find_params(self):
"""Return the find parameters as a tuple."""
return find_params(
os.path.abspath(self.path),
self.match_pattern,
self.widgets.get_object('re_checkbox').get_active(),
self.widgets.get_object('match_case_checkbox').get_active(),
self.file_pattern)
def on_choose_directory_icon_press(self, widget, position, event):
"""Choose a directory using a dialog."""
dialog = Gtk.FileChooserDialog(
title="Choose a directory to find in", parent=self.window,
action=Gtk.FileChooserAction.SELECT_FOLDER,
buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.ACCEPT))
dialog.set_current_folder(os.getcwd())
if dialog.run() == Gtk.ResponseType.ACCEPT:
path = dialog.get_filename()
if path:
self.path_comboentry.get_child().set_text(path)
dialog.destroy()
def on_find_in_files(self, widget=None, substitution=None):
"""Find and present the matches."""
treestore = self.file_lines_view.get_model()
treestore.clear()
find_params = self.get_find_params()
if (find_params.path != self.CURRENT_FILE
and not os.path.exists(find_params.path)):
message = 'The directory path does not exist!'
self.file_lines_view.get_model().append(
None, (message, 'stock_dialog-info', 0, None, None))
return
self.last_find = find_params
pattern = find_params.pattern
self.save_find_data()
self.file_lines_view.get_column(0).props.title = (
'Matches for [%s]' % pattern)
find_worker = FinderWorker(
treestore, find_params, self.on_find_complete, substitution)
# Queue the find worker after the events emited at the top
# of this method.
GObject.idle_add(find_worker.start)
def on_find_in_files_icon_press(self, widget, position, event):
"""Handle find pattern entry icon-press event."""
self.on_find_in_files()
def on_find_complete(self):
if self.path_comboentry.get_active_text() == self.CURRENT_FILE:
self.file_lines_view.expand_all()
def _get_untested_replacement_dialog(self, find_params):
dialog_flags = Gtk.DialogFlags
dialog = Gtk.Dialog(
title="Untested replacement", parent=self.window,
flags=dialog_flags.MODAL | dialog_flags.DESTROY_WITH_PARENT,
buttons=(Gtk.STOCK_FIND, Gtk.ResponseType.REJECT,
Gtk.STOCK_FIND_AND_REPLACE, Gtk.ResponseType.ACCEPT))
question = Gtk.Label(
_("Do want to test this replacement using Find first?"))
question.set_alignment(0, 0)
question.props.xpad = 6
dialog.vbox.pack_start(question, True, False, 6)
question.show()
params_summary = Gtk.Label()
params_summary.set_markup(_(
"<b>Look in:</b> %s\n"
"<b>Search for:</b> %s\n"
"<b>Regular expression:</b> %s\n"
"<b>Match case:</b> %s\n"
"<b>File name pattern:</b> %s")
% find_params)
params_summary.props.selectable = True
params_summary.props.xpad = 3
params_summary.props.ypad = 3
box = Gtk.EventBox()
box.set_border_width(6)
white = Gdk.color_parse('#fff')
box.modify_bg(Gtk.StateType.NORMAL, white)
box.add(params_summary)
dialog.vbox.pack_start(box, True, False, 0)
box.show()
params_summary.show()
# Uncomment the next line to preview the layout.
#dialog.run()
return dialog
def on_replace_in_files(self, widget=None):
"""Find, replace, and present the matches."""
substitution = self.substitution_comboentry.get_active_text() or ''
self.update_comboentry(self.substitution_comboentry, substitution)
response = Gtk.ResponseType.ACCEPT
find_params = self.get_find_params()
if self.last_find != find_params:
dialog = self._get_untested_replacement_dialog(find_params)
response = dialog.run()
dialog.destroy()
if response == Gtk.ResponseType.REJECT:
self.on_find_in_files()
elif response == Gtk.ResponseType.ACCEPT:
self.on_find_in_files(substitution=substitution)
def on_replace_in_files_icon_press(self, widget, position, event):
"""Handle replace pattern entry icon-press event."""
self.on_replace_in_files()
def on_save_results(self, widget=None):
"""Save the search results to a file."""
dialog = Gtk.FileChooserDialog(
title="Save find results", parent=self.window,
action=Gtk.FileChooserAction.SAVE,
buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_SAVE, Gtk.ResponseType.ACCEPT))
dialog.set_do_overwrite_confirmation(True)
dialog.set_current_name('_find.log')
if (dialog.run() == Gtk.ResponseType.ACCEPT):
file_name = dialog.get_filename()
log_text = self.get_results_as_log()
with open(file_name, 'w') as log_file:
log_file.write(log_text)
dialog.destroy()
def get_results_as_log(self):
"""Return the results in the file_lines_view as a log."""
lines = []
treestore = self.file_lines_view.get_model()
for file_match in treestore:
lines.append(file_match[0])
for line_match in file_match.iterchildren():
line = ' %4s: %s' % (line_match[2], line_match[3])
lines.append(line)
return '\n'.join(lines)
def get_option_parser():
"""Return the option parser for this program."""
usage = "usage: %prog [options] root_dir file_pattern match"
parser = OptionParser(usage=usage)
parser.add_option(
"-s", "--substitution", dest="substitution",
help="The substitution string (may contain \\[0-9] match groups).")
parser.set_defaults(substitution=None)
return parser
def main(argv=None):
"""Run the command line operations."""
if argv is None:
argv = sys.argv
parser = get_option_parser()
(options, args) = parser.parse_args(args=argv[1:])
root_dir = args[0]
file_pattern = args[1]
match_pattern = args[2]
substitution = options.substitution
print "Looking for [%s] in files like %s under %s:" % (
match_pattern, file_pattern, root_dir)
for summary in find_matches(
root_dir, file_pattern, match_pattern, substitution=substitution):
print "\n%(file_path)s" % summary
for line in summary['lines']:
print " %(lineno)4s: %(text)s" % line
if __name__ == '__main__':
sys.exit(main())
|