def get_answer(query, scope=None, **kwargs): suricate.debuglog('searching %r using scope %r...', query, scope) try: html = '' if scope and scope.lower() != query.lower(): scoped_kwargs = dict(kwargs) scoped_kwargs['hide_headers'] = True scoped_kwargs['hide_signature'] = True scoped_kwargs['max_number_of_results'] = 1 scoped_results = duckduckgo2html.search( scope + ' ' + query, DDG_USERAGENT) scoped_html = duckduckgo2html.results2html( scoped_results, **scoped_kwargs) if scoped_html and not kwargs.get('hide_headers', False): html += '<h1>Scope {0}</h1>'.format(scope) if scoped_html: html += scoped_html if 'max_number_of_results' in kwargs and kwargs['max_number_of_results']: kwargs['max_number_of_results'] -= 1 if kwargs['max_number_of_results'] < 1: return html results = duckduckgo2html.search(query, DDG_USERAGENT) html += duckduckgo2html.results2html(results, **kwargs) return html if html else 'Sorry, no results found' except Exception: message = 'Sorry, there was an error retrieving the answer.' logging.exception(message) return message
def _do(cmd, caption, path, out=None, ask=None, **kwargs): working_dir, base_name = os.path.split( path) if os.path.isfile(path) else (path, '.') cmd = suricate.expand_variables(cmd, {'path': base_name}) suricate.debuglog('vcs do: %s', ' '.join(cmd)) if ask and not sublime.ok_cancel_dialog( suricate.expand_variables( ask, { 'path': base_name}), caption): return if out == 'gui': process.spawn(cmd, working_dir) elif out == 'buffer': text, err = process.popen(cmd, working_dir) name = ' '.join(cmd[:2]) sublime_wrapper.flush_to_buffer( text, name=name, scratch=True, syntax='Diff') elif out is not None and out.endswith('_list'): cout, err = process.popen(cmd, working_dir) vcsname = suricate.flags.to_string(suricate.flags.get_vcs(path)) lst = vcs_parser.parse(cout, out.replace('_list', ''), vcsname) window = sublime.active_window() getpath = lambda picked: os.path.abspath(os.path.join(path, picked[0])) on_done = lambda picked: window.open_file(getpath(picked)) sublime_wrapper.show_quick_panel(lst, on_done, window) else: sublime_wrapper.execute(cmd=cmd, working_dir=working_dir)
def import_module(module_name): was_present = module_name in sys.modules suricate.debuglog('import module %r', module_name) module = importlib.import_module(module_name) # @todo do NOT reload the suricate module! # return suricate.reload_module(module) if was_present else module return module
def _read_time(time_string, time_formats): """Try to convert tstring to a format in tformats, return first match. If non of them matches, show an error message.""" for time_format in time_formats: try: return datetime.strptime(str(time_string), time_format), time_format except ValueError: pass message = 'unknown time format: %r, see %r in settings file.' suricate.debuglog(message, time_string, 'time_formats') return None, None
def call(cmd, active_flags, view): settings = sublime.load_settings(SourceControlFileBaseName) scslist = settings.get('user_source_control') scslist += settings.get('source_control') for item in scslist: cmdi = item['commands'].get(cmd) if cmdi is not None and suricate.flags.special_check( active_flags, suricate.flags.from_string( item['flags'])) and all( osutil.which(exe) is not None for exe in item['exes']): suricate.debuglog('%s: %s', item['name'], cmdi['caption']) cmdi['path'] = view.file_name() return _do(**cmdi) suricate.log('ERROR: Source control command \'%s\' is not available.', cmd)
def _find_best_css(view, folder, default=None): regex_remove = re.compile(r'[\(\)\{\}\[\]\\-]') get_basename = lambda x: os.path.splitext(os.path.basename(x))[0] clean = lambda x: regex_remove.sub(' ', get_basename(x).lower()) css_files = [ x for x in sublime.find_resources('*.css') if x.startswith(folder)] color_scheme = clean(view.settings().get('color_scheme')) scored_files = [(score(clean(x), color_scheme), x) for x in css_files] suricate.debuglog('color scheme: %r, css files: %s', color_scheme, scored_files) scored_files = [x for x in scored_files if x[0] > 0] if not scored_files: return default if len(scored_files) > 1: scored_files = sorted(scored_files, key=lambda x: x[0], reverse=True) return scored_files[0][1]