Sublime Forum

Call plugin class command from EventListener

#1

So here’s the relevant code:

class MPzeroCVSCommand(sublime_plugin.EventListener): # On Save Event Handler def on_post_save(self, view): global project, cvsMsg if project != None or project != "None": if cvsMsg == None: view.run_command(on_mpz_load) else: DoCVSCommand(sublime_plugin.WindowCommand)

In this event handler, I make calls to two other classes defined in my plugin, (they look different, e.g. run_command vs. DCVSCommand out of experimentation). I have to keep each of these a separate class (in my mind) because the first (on_mpz_load) is called via a new menu I added, and the second has to be a WindowCommand, not an EventListener.

The problem I have is how to run these classes (with instances of TextCommand, and WindowCommand respectively) from a separate class?

For those interested, here’s the code: (and feel free to critic! I’m a newb python/Sublime guy, plus I haven’t finished debugging!)

#
# MPzero CVS Auto-Commit Plugin for Sublime Text 2
# a private plugin for MPzero Studios
# Copyright 2014
# by Gabe Blodgett
#


import sublime, sublime_plugin, subprocess, urllib2

#globals
global settings
settings = None
global project
project = None
global cvsDir
cvsDir = None
global cvsPath
cvsPath = None
global cgiPath
cgiPath = None
global docPath
docPath = None
global cvsMsg
cvsMsg = None
global cvsProjDir
cvsProjDir = None

class OnMpzLoadCommand(sublime_plugin.TextCommand):
	def run(self, edit):
		print('here')
		self.view.window().show_input_panel("Please say what you're working on", 'e.g. reset password flow', self.on_done, None, None)
	def on_done(self, user_input):
		global cvsMsg
		cvsMsg = user_input

class SetCvsProjectCommand(sublime_plugin.TextCommand):
	def run(self, edit, proj=]):
		print('setting proj')
		global settings, project, cvsDir, cgiPath, cvsProjDir
		settings = sublime.load_settings('MPzeroCVS.sublime-settings')
		if proj == None or proj == 'None' :
			sublime.status_message('No project set, cvs and site will not be updated.')
		else:
			project = settings.get(proj)
			cvsDir = project.get('cvsDir')
			cgiPath = project.get('cgiPath')
			cvsProjDir = project.get('cvsProjDir')
			

class MPzeroCVSCommand(sublime_plugin.EventListener):
	# On Save Event Handler
	def on_post_save(self, view):
		global project, cvsMsg
		if project != None or project != "None":
			if cvsMsg == None:
				view.run_command(on_mpz_load)
			else:
				DoCVSCommand(sublime_plugin.WindowCommand)

class DoCVSCommand(sublime_plugin.WindowCommand):
	def run(self, view):
		global docPath, cvsMsg, project, cgiPath
		# do cvs commit
		docPath = self.window.active_view().file_name()
		cvsCDPath = docPath.split(cvsProjDir,1)[1] 
		cvsCDPath = cvsCDPath[2:]
		print(cvsCDPath)
		cvsFileName = docPath.split('MPzeroCVS',1)[1] 
		cvsFileName = cvsFileName[2:]
		print(cvsFileName)
		sublime.status_message('committing your file to the '+project+' repository...')
		cvsPipe = subprocess.Popen('C:\cygwin\bin\bash --login', stderr.STDOUT, stdin=PIPE, stdout=PIPE)
		cvsPipe.communicate(cvsDir, 500)
		cvsPipe.communicate('cd ' + cvsCDPath, 500)
		cvsOutput = cvsPipe.communicate('cvs commit -m "' + cvsMsg + '" ' + docPath)	
		sublime.status_message(cvsOutput.stdout)

		# run cgi site update script
		sublime.status_message('running the cgi update script...')
		try:
			request = urllib2.Request(cgiPath,
				headers={"User-Agent": "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"})
			http_file = urllib2.urlopen(request, timeout=1000)
			self.result = http_file.read()
			return

		except (urllib2.HTTPError) as (e):
			err = '%s: HTTP error %s contacting API' % (__name__, str(e.code))
		except (urllib2.URLError) as (e):
			err = '%s: URL error %s contacting API' % (__name__, str(e.reason))

		sublime.error_message(err)
		self.result = False
		
		sublime.status_message('cvs commit and site update complete.')
	
OnMpzLoadCommand(sublime_plugin.TextCommand)
0 Likes

#2

as DoCVSCommand is a WindowCommand you can run it usings window.run_command()

sublime.active_window().run_command("do_c_v_s")

or use a @staticmethod worker function

[code]class mystuff(sublime_plugin.WindowCommand):
def run(self, view):
mystuff.dowork(42)

@staticmethod
def dowork(number):
	pass

[/code]

0 Likes