<s> import re <EOL> import os <EOL> import json <EOL> from sphinx import addnodes <EOL> from sphinx . domains import Domain , ObjType <EOL> from sphinx . directives import ObjectDescription <EOL> from sphinx . roles import XRefRole <EOL> from sphinx . util . docfields import GroupedField <EOL> from docutils import nodes <EOL> from docutils . statemachine import ViewList <EOL> from sphinx . util . compat import Directive <EOL> from sphinx . util . nodes import nested_parse_with_titles <EOL> from sphinx . util . docstrings import prepare_docstring <EOL> def import_class ( name ) : <EOL> module_name , cls = name . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) <EOL> mod = __import__ ( module_name ) <EOL> mod = reduce ( getattr , module_name . split ( '<STR_LIT:.>' ) [ <NUM_LIT:1> : ] , mod ) <EOL> return getattr ( mod , cls ) <EOL> class SublimeObject ( ObjectDescription ) : <EOL> pass <EOL> class SublimeWindowCommand ( SublimeObject ) : <EOL> doc_field_types = [ <EOL> GroupedField ( '<STR_LIT>' , label = '<STR_LIT>' , names = ( '<STR_LIT>' , ) ) , <EOL> GroupedField ( '<STR_LIT>' , label = '<STR_LIT>' , names = ( '<STR_LIT>' , ) ) , <EOL> ] <EOL> def handle_signature ( self , sig , signode ) : <EOL> signode += addnodes . desc_name ( sig , sig ) <EOL> signode [ '<STR_LIT>' ] = sig <EOL> return sig <EOL> def needs_arglist ( self ) : <EOL> return False <EOL> def add_target_and_index ( self , name , sig , signode ) : <EOL> slug = re . sub ( r'<STR_LIT>' , '<STR_LIT:->' , name ) . lower ( ) <EOL> if slug not in self . state . document . ids : <EOL> signode [ '<STR_LIT>' ] . append ( slug ) <EOL> signode [ '<STR_LIT>' ] . append ( slug ) <EOL> signode [ '<STR_LIT>' ] = ( not self . names ) <EOL> self . state . document . note_explicit_target ( signode ) <EOL> inv = self . env . domaindata [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> inv [ name ] = ( self . env . docname , self . objtype ) <EOL> indextext = self . get_index_text ( name ) <EOL> if indextext : <EOL> self . indexnode [ '<STR_LIT>' ] . append ( ( '<STR_LIT>' , indextext , slug , '<STR_LIT>' ) ) <EOL> def get_index_text ( self , name ) : <EOL> name = name . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> return u"<STR_LIT>" % name <EOL> class SublimeTextCommand ( SublimeObject ) : <EOL> pass <EOL> class SublimeXRefRole ( XRefRole ) : <EOL> def process_link ( self , env , refnode , has_explicit_title , title , target ) : <EOL> return "<STR_LIT>" <EOL> class SublimeDomain ( Domain ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> label = "<STR_LIT>" <EOL> object_types = { <EOL> '<STR_LIT>' : ObjType ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> } <EOL> directives = { <EOL> '<STR_LIT>' : SublimeWindowCommand , <EOL> } <EOL> roles = { <EOL> '<STR_LIT>' : SublimeXRefRole ( ) <EOL> } <EOL> initial_data = { <EOL> '<STR_LIT>' : { } <EOL> } <EOL> class AutoWindowCommand ( Directive ) : <EOL> has_content = True <EOL> required_arguments = <NUM_LIT:1> <EOL> def cls_to_sublime_command ( self , clsname ) : <EOL> clsname = clsname [ : - <NUM_LIT:7> ] <EOL> clsname = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , clsname ) <EOL> return clsname . lower ( ) <EOL> def get_sublime_caption ( self , clsname ) : <EOL> path = os . path . realpath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) ) <EOL> command = self . cls_to_sublime_command ( clsname ) <EOL> with open ( path , '<STR_LIT:r>' ) as f : <EOL> data = json . load ( f ) <EOL> for cmd in data : <EOL> if cmd [ '<STR_LIT>' ] == command : <EOL> return cmd [ '<STR_LIT>' ] <EOL> def make_rst ( self ) : <EOL> classname = self . arguments [ <NUM_LIT:0> ] <EOL> cls = import_class ( classname ) <EOL> sublime_caption = self . get_sublime_caption ( cls . __name__ ) <EOL> doc = cls . __doc__ <EOL> yield '<STR_LIT>' <EOL> yield '<STR_LIT>' . format ( sublime_caption ) <EOL> yield '<STR_LIT>' <EOL> if doc : <EOL> for line in prepare_docstring ( doc ) : <EOL> yield '<STR_LIT:U+0020>' + line <EOL> yield '<STR_LIT>' <EOL> def run ( self ) : <EOL> node = nodes . section ( ) <EOL> node . document = self . state . document <EOL> result = ViewList ( ) <EOL> for line in self . make_rst ( ) : <EOL> result . append ( line , '<STR_LIT>' ) <EOL> nested_parse_with_titles ( self . state , result , node ) <EOL> return node . children <EOL> class AutoTextCommand ( Directive ) : <EOL> pass </s>
