Skip to content

Commit

Permalink
start-at-the-top refactoring
Browse files Browse the repository at this point in the history
- reactor initialization refactored with proper initialization exception handling, much safer integration particularly for other plugins using twisted
- check to see if ThreadedSelectReactor is being used and warning logged if not
- better? logging support, i hope
- twisted updated to 13.1 and patches removed to go into subclasses later while official twisted patches move through their release process
  • Loading branch information
nlloyd committed Sep 11, 2013
1 parent db3535b commit 1dad30b
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 20 deletions.
65 changes: 45 additions & 20 deletions commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import sublime, sublime_plugin
import sys, os, platform
import os
import platform
import sys

# this assures we use the included libs/twisted and libs/zope libraries
# this is of particular importance on Mac OS X since an older version of twisted
Expand All @@ -40,34 +41,58 @@
if libs_path not in sys.path:
sys.path.insert(0, libs_path)

if not 'REACTORINSTALLED' in globals():
# --- configure logging system --- #
import logging

logging.basicConfig(
format="[SubliminalCollaborator|%(name)s(%(levelname)s): %(message)s]",
level=logging.DEBUG,
stream=sys.stdout
)
# --- ------------------------ --- #

logger = logging.getLogger("main")

import sublime

def callInSublimeLoop(funcToCall):
sublime.set_timeout(funcToCall, 0)

# --- install and start the twisted reactor, if it hasn't already be started --- #
from twisted.internet.error import ReactorAlreadyInstalledError, ReactorAlreadyRunning, ReactorNotRestartable

reactorAlreadyInstalled = False
try:
from twisted.internet import _threadedselect
_threadedselect.install()
globals()['REACTORINSTALLED'] = True
except ReactorAlreadyInstalledError:
reactorAlreadyInstalled = True

from twisted.internet import reactor

try:
reactor.interleave(callInSublimeLoop, installSignalHandlers=False)
except ReactorAlreadyRunning:
reactorAlreadyInstalled = True
except ReactorNotRestartable:
reactorAlreadyInstalled = True

if reactorAlreadyInstalled:
logger.debug('twisted reactor already installed')
if type(reactor) != _threadedselect.ThreadedSelectReactor:
logger.warn('unexpected reactor type installed: %s, it is best to use twisted.internet._threadedselect!' % type(reactor))
else:
logger.debug('twisted reactor installed and running')
# --- --------------------------------------------------------------------- --- #

import sublime_plugin
from sub_collab.negotiator import irc
from sub_collab.peer import interface as pi
from sub_collab import status_bar
from sub_collab.peer import base
from twisted.internet import reactor
import threading, logging, time, shutil, fileinput, re, functools

logger = logging.getLogger(__name__)
logger.propagate = False
# purge previous handlers set... for plugin reloading
del logger.handlers[:]
stdoutHandler = logging.StreamHandler(sys.stdout)
stdoutHandler.setFormatter(logging.Formatter(fmt='[SubliminalCollaborator(%(levelname)s): %(message)s]'))
logger.addHandler(stdoutHandler)
logger.setLevel(logging.INFO)

def callInSublimeLoop(funcToCall):
sublime.set_timeout(funcToCall, 0)

if not 'REACTORSTARTED' in globals():
reactor.interleave(callInSublimeLoop, installSignalHandlers=False)
globals()['REACTORSTARTED'] = True

negotiatorFactoryMap = {
'irc': irc.IRCNegotiator
}
Expand Down
31 changes: 31 additions & 0 deletions dccchat-refactor-patch.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Index: twisted/words/protocols/irc.py
===================================================================
--- twisted/words/protocols/irc.py (revision 39738)
+++ twisted/words/protocols/irc.py (working copy)
@@ -2285,7 +2285,7 @@
if len(data) < 3:
raise IRCBadMessage, "malformed DCC CHAT request: %r" % (data,)

- (filename, address, port) = data[:3]
+ (protocol, address, port) = data[:3]

address = dccParseAddress(address)
try:
@@ -2293,7 +2293,7 @@
except ValueError:
raise IRCBadMessage, "Indecipherable port %r" % (port,)

- self.dccDoChat(user, channel, address, port, data)
+ self.dccDoChat(user, channel, protocol, address, port, data)

### The dccDo methods are the slightly higher-level siblings of
### common dcc_ methods; the arguments have been parsed for them.
@@ -2320,7 +2320,7 @@
request made by us. By default it will do nothing."""
pass

- def dccDoChat(self, user, channel, address, port, data):
+ def dccDoChat(self, user, channel, protocol, address, port, data):
pass
#factory = DccChatFactory(self, queryData=(user, channel, data))
#reactor.connectTCP(address, port, factory)
Loading

0 comments on commit 1dad30b

Please sign in to comment.