<commit_before># Copyright 2011 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import db_proxy
import json
import logging
import message_loop
import re
import os
import sys
import time

from trace_event import *

TICK_RATE = 0.025

class OpenDialogBase(object):
  @tracedmethod
  def __init__(self, settings, options, db, initial_filter = None):
    settings.register("filter_text", str, "")
    settings.register("query_log", str, "") 
    if initial_filter:
      settings.filter_text = initial_filter
    else:
      had_position = False
    self._filter_text = settings.filter_text
    self._settings = settings
    self._db = db
    self._can_process_queries = False
    self._last_search_query = None
    self._pending_search = None
    self._options = options
    if initial_filter:
      self.should_position_cursor_for_replace = False
    else:
      self.should_position_cursor_for_replace = True

    message_loop.post_delayed_task(self.on_tick, TICK_RATE)
    
  def set_can_process_queries(self, can_process):
    could_process = self._can_process_queries
    self._can_process_queries = can_process

    self.set_results_enabled(can_process)

  @tracedmethod
  def set_filter_text(self, text):
    self._filter_text = text
    if self._settings.query_log != "":
      try:
        f = open(os.path.expanduser(self._settings.query_log), 'a')
        f.write(json.dumps({"ts": time.time(), "query": text}))
        f.write("\n");
        f.close()
      except IOError:
        import traceback; traceback.print_exc()
        pass

  def on_reindex_clicked(self):
    self._db.begin_reindex()

  @tracedmethod
  def on_tick(self,*args):
    @trace
    def begin_search():
      self.set_status("DB Status: %s" % "searching")
      self._last_search_query = self._filter_text
      self._pending_search = self._db.search_async(self._last_search_query)

    @trace
    def on_ready():
      try:
        res = self._pending_search.result
      except db_proxy.AsyncSearchError:
        res = None
      self._pending_search = None
      trace_begin("update_results_list")
      if res:
        self.update_results_list(res.hits,res.ranks)
      else:
        self.update_results_list([],[])
      trace_end("update_results_list")
      self._pending_search = None

    @trace
    def check_status():
      try:
        stat = self._db.status()
        status = stat.status
        enabled = stat.has_index
      except Exception, ex:
        status = "quickopend not running"
        enabled = False
      self.set_status("DB Status: %s" % status)
      self.set_can_process_queries(enabled)

    if self._pending_search:
      self.set_status("DB Status: %s" % "searching")
      if self._pending_search.ready:
        on_ready()

    # re-check the self._pending_search since we might have cleared it
    if not self._pending_search:
      # kick off a query
      need_to_begin_search = self._filter_text != self._last_search_query
      if need_to_begin_search and self._can_process_queries:
        begin_search()
      else:
        # poll status
        check_status()

    # renew the tick
    message_loop.post_delayed_task(self.on_tick, TICK_RATE)

  @trace
  def on_done(self, canceled):
    self._settings.filter_text = self._filter_text.encode('utf8')
    if canceled:
      res = []
    else:
      res = self.get_selected_items()
    if self._options.ok and not canceled:
      print "OK"<commit_msg>Reduce polling rate when database is indexing to maximize indexing speed.<commit_after># Copyright 2011 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import db_proxy
import json
import logging
import message_loop
import re
import os
import sys
import time

from trace_event import *

TICK_RATE_WHEN_UP_TO_DATE = 0.025
TICK_RATE_WHEN_NOT_UP_TO_DATE = 0.2

class OpenDialogBase(object):
  @tracedmethod
  def __init__(self, settings, options, db, initial_filter = None):
    settings.register("filter_text", str, "")
    settings.register("query_log", str, "") 
    if initial_filter:
      settings.filter_text = initial_filter
    else:
      had_position = False
    self._filter_text = settings.filter_text
    self._settings = settings
    self._db = db
    self._can_process_queries = False
    self._db_is_up_to_date = True
    self._last_search_query = None
    self._pending_search = None
    self._options = options
    if initial_filter:
      self.should_position_cursor_for_replace = False
    else:
      self.should_position_cursor_for_replace = True

    message_loop.post_delayed_task(self.on_tick, TICK_RATE_WHEN_UP_TO_DATE)
    
  def set_can_process_queries(self, can_process):
    could_process = self._can_process_queries
    self._can_process_queries = can_process

    self.set_results_enabled(can_process)

  @tracedmethod
  def set_filter_text(self, text):
    self._filter_text = text
    if self._settings.query_log != "":
      try:
        f = open(os.path.expanduser(self._settings.query_log), 'a')
        f.write(json.dumps({"ts": time.time(), "query": text}))
        f.write("\n");
        f.close()
      except IOError:
        import traceback; traceback.print_exc()
        pass

  def on_reindex_clicked(self):
    self._db.begin_reindex()

  @tracedmethod
  def on_tick(self,*args):
    @trace
    def begin_search():
      self.set_status("DB Status: %s" % "searching")
      self._last_search_query = self._filter_text
      self._pending_search = self._db.search_async(self._last_search_query)

    @trace
    def on_ready():
      try:
        res = self._pending_search.result
      except db_proxy.AsyncSearchError:
        res = None
      self._pending_search = None
      trace_begin("update_results_list")
      if res:
        self.update_results_list(res.hits,res.ranks)
      else:
        self.update_results_list([],[])
      trace_end("update_results_list")
      self._pending_search = None

    @trace
    def check_status():
      try:
        stat = self._db.status()
        status = stat.status
        enabled = stat.has_index
        self._db_is_up_to_date = stat.is_up_to_date
      except Exception, ex:
        status = "quickopend not running"
        enabled = False
        self._db_is_up_to_date = False
      self.set_status("DB Status: %s" % status)
      self.set_can_process_queries(enabled)

    if self._pending_search:
      self.set_status("DB Status: %s" % "searching")
      if self._pending_search.ready:
        on_ready()

    # re-check the self._pending_search since we might have cleared it
    if not self._pending_search:
      # kick off a query
      need_to_begin_search = self._filter_text != self._last_search_query
      if need_to_begin_search and self._can_process_queries:
        begin_search()
      else:
        # poll status
        check_status()

    # renew the tick
    if self._db_is_up_to_date:
      message_loop.post_delayed_task(self.on_tick, TICK_RATE_WHEN_UP_TO_DATE)
    else:
      message_loop.post_delayed_task(self.on_tick, TICK_RATE_WHEN_NOT_UP_TO_DATE)

  @trace
  def on_done(self, canceled):
    self._settings.filter_text = self._filter_text.encode('utf8')
    if canceled:
      res = []
    else:
      res = self.get_selected_items()
    if self._options.ok and not canceled:
      print "OK"
{'fraction_non_alphanumeric': 0.08756652152878568, 'fraction_numerical': 0.003386550556361877, 'mean_word_length': 3.1241895261845385, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2313050', 'n_tokens_mistral': 2591, 'n_tokens_neox': 2539, 'n_words': 628}