如何在sublime中导入和引用其他类?

时间:2014-11-10 14:35:58

标签: python sublimetext2 sublimetext sublime-text-plugin

我想构建一个使用默认git-sublime类和方法的插件。

在Git-plugin中,使用如下方法:

   def run(self):
        self.run_command(['git', 'branch', '--no-color'] + self.extra_flags, self.branch_done)

它是这样导入的:

from git import GitTextCommand, GitWindowCommand, git_root_exist

run_command在名为git.py的文件中的另一个类中定义,如下所示:

class GitCommand(object):
    may_change_files = False

    def run_command(self, command, callback=None, show_status=True,
            filter_empty_args=True, no_save=False, **kwargs):
        if filter_empty_args:
            command = [arg for arg in command if arg]
        if 'working_dir' not in kwargs:
            kwargs['working_dir'] = self.get_working_dir()
        if 'fallback_encoding' not in kwargs and self.active_view() and self.active_view().settings().get('fallback_encoding'):
            kwargs['fallback_encoding'] = self.active_view().settings().get('fallback_encoding').rpartition('(')[2].rpartition(')')[0]

        s = sublime.load_settings("Git.sublime-settings")
        if s.get('save_first') and self.active_view() and self.active_view().is_dirty() and not no_save:
            self.active_view().run_command('save')
        if command[0] == 'git' and s.get('git_command'):
            command[0] = s.get('git_command')
        if command[0] == 'git-flow' and s.get('git_flow_command'):
            command[0] = s.get('git_flow_command')
        if not callback:
            callback = self.generic_done

        thread = CommandThread(command, callback, **kwargs)
        thread.start()

        if show_status:
            message = kwargs.get('status_message', False) or ' '.join(command)
            sublime.status_message(message)
...

从git引用文件git.py吗?

导入是否与类gitCommand相关,在这种情况下是如何进行的?

我试图在我自己的回购中导入run_command,如下所示:

import sublime, sublime_plugin, subprocess, os, time, sys, re
from git import GitTextCommand, GitWindowCommand, git_root_exist

class PanagoraBuildCommand(sublime_plugin.TextCommand, sublime_plugin.WindowCommand):
    def run(self, edit):

        self.run_command(['git', 'branch' '--no-color'], self.branch_done)

但它说AttributeError: 'PanagoraBuildCommand' object has no attribute 'run_command',为什么?

1 个答案:

答案 0 :(得分:1)

我猜问题出现在:

class PanagoraBuildCommand(sublime_plugin.TextCommand, sublime_plugin.WindowCommand):

你应该在这里继承GitWindowCommand

class PanagoraBuildCommand(GitWindowCommand):