<s> import os <EOL> import sys <EOL> import re <EOL> import json <EOL> import urllib <EOL> import sublime <EOL> import tempfile <EOL> import subprocess <EOL> import sublime_plugin <EOL> import webbrowser <EOL> PLUGIN_VERSION = "<STR_LIT>" <EOL> class ViewInBrowserCommand ( sublime_plugin . TextCommand ) : <EOL> _pythonVersion = sys . version_info [ <NUM_LIT:0> ] <EOL> def expandWindowsUserShellFolder ( self , command ) : <EOL> browserCommand = "<STR_LIT>" <EOL> windowsFolders = self . getWindowsUserShellFolders ( ) <EOL> specialFolder = re . sub ( r"<STR_LIT>" , "<STR_LIT>" , command ) <EOL> if specialFolder != command : <EOL> expandedFolder = windowsFolders [ specialFolder ] . replace ( "<STR_LIT:\\>" , "<STR_LIT>" ) <EOL> browserCommand = re . sub ( r"<STR_LIT>" , "<STR_LIT>" % expandedFolder , command ) <EOL> else : <EOL> browserCommand = command <EOL> return browserCommand . encode ( "<STR_LIT:ascii>" , "<STR_LIT:ignore>" ) if self . _pythonVersion < <NUM_LIT:3> else browserCommand <EOL> def getBaseCommand ( self , command , osName ) : <EOL> baseCommand = command <EOL> if osName == "<STR_LIT>" : <EOL> baseCommand = self . expandWindowsUserShellFolder ( baseCommand ) <EOL> return baseCommand <EOL> def getOsName ( self ) : <EOL> return os . name <EOL> def getPlatform ( self ) : <EOL> return sys . platform <EOL> def getWindowsUserShellFolders ( self ) : <EOL> if self . _pythonVersion < <NUM_LIT:3> : <EOL> import _winreg <EOL> else : <EOL> import winreg as _winreg <EOL> return_dict = { } <EOL> try : <EOL> Hive = _winreg . ConnectRegistry ( None , _winreg . HKEY_CURRENT_USER ) <EOL> except WindowsError : <EOL> print ( "<STR_LIT>" ) <EOL> return return_dict <EOL> try : <EOL> Key = _winreg . OpenKey ( Hive , "<STR_LIT>" ) <EOL> except WindowsError : <EOL> print ( "<STR_LIT>" ) <EOL> _winreg . CloseKey ( Hive ) <EOL> return return_dict <EOL> try : <EOL> for i in range ( <NUM_LIT:0> , _winreg . QueryInfoKey ( Key ) [ <NUM_LIT:1> ] ) : <EOL> name , value , val_type = _winreg . EnumValue ( Key , i ) <EOL> return_dict [ name ] = value . encode ( "<STR_LIT:ascii>" ) <EOL> i += <NUM_LIT:1> <EOL> _winreg . CloseKey ( Key ) <EOL> _winreg . CloseKey ( Hive ) <EOL> return return_dict <EOL> except WindowsError : <EOL> _winreg . CloseKey ( Key ) <EOL> _winreg . CloseKey ( Hive ) <EOL> return { } <EOL> def giveFileAProjectPath ( self , fileToOpen , basePath , baseUrl ) : <EOL> return re . sub ( r"<STR_LIT:\\>" , "<STR_LIT:/>" , fileToOpen . replace ( basePath , baseUrl ) ) . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) . replace ( "<STR_LIT:(>" , "<STR_LIT>" ) . replace ( "<STR_LIT:)>" , "<STR_LIT>" ) <EOL> def loadPluginSettings ( self , defaultBrowser ) : <EOL> result = { <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" <EOL> } <EOL> settings = sublime . load_settings ( "<STR_LIT>" ) <EOL> if not defaultBrowser : <EOL> result [ "<STR_LIT>" ] = settings . get ( "<STR_LIT>" ) <EOL> else : <EOL> result [ "<STR_LIT>" ] = defaultBrowser <EOL> osName = self . getOsName ( ) <EOL> platform = self . getPlatform ( ) <EOL> selectedOs = settings . get ( osName ) <EOL> result [ "<STR_LIT>" ] = self . getBaseCommand ( selectedOs [ platform ] [ result [ "<STR_LIT>" ] ] , osName ) <EOL> return result <EOL> def loadProjectSettings ( self , view ) : <EOL> return view . settings ( ) . get ( "<STR_LIT>" ) <EOL> def normalizePath ( self , fileToOpen ) : <EOL> fileToOpen = fileToOpen . replace ( "<STR_LIT:\\>" , "<STR_LIT:/>" ) <EOL> fileToOpen = "<STR_LIT>" % fileToOpen . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) . replace ( "<STR_LIT:(>" , "<STR_LIT>" ) . replace ( "<STR_LIT:)>" , "<STR_LIT>" ) <EOL> return fileToOpen <EOL> def openBrowser ( self , command , osName ) : <EOL> useShell = False if osName != "<STR_LIT>" else True <EOL> subprocess . Popen ( command , shell = useShell ) <EOL> def run ( self , edit , browser = None ) : <EOL> print ( "<STR_LIT>" . format ( PLUGIN_VERSION , self . _pythonVersion ) ) <EOL> pluginSettings = self . loadPluginSettings ( browser ) <EOL> projectSettings = self . loadProjectSettings ( self . view ) <EOL> fileToOpen = self . view . file_name ( ) <EOL> if projectSettings : <EOL> fileToOpen = self . giveFileAProjectPath ( fileToOpen , projectSettings [ "<STR_LIT>" ] , projectSettings [ "<STR_LIT>" ] ) <EOL> if fileToOpen == None : <EOL> fileToOpen = self . normalizePath ( self . saveCurrentViewInTempFile ( self . view ) ) <EOL> else : <EOL> if self . view . is_dirty ( ) : <EOL> print ( "<STR_LIT>" % ( fileToOpen , ) ) <EOL> self . view . window ( ) . run_command ( "<STR_LIT>" ) <EOL> if not projectSettings : <EOL> fileToOpen = self . normalizePath ( fileToOpen ) <EOL> if pluginSettings [ "<STR_LIT>" ] : <EOL> command = "<STR_LIT>" % ( pluginSettings [ "<STR_LIT>" ] , fileToOpen . decode ( ) . encode ( sys . getfilesystemencoding ( ) ) if self . _pythonVersion < <NUM_LIT:3> else fileToOpen , ) <EOL> print ( command ) <EOL> self . openBrowser ( command , self . getOsName ( ) ) <EOL> else : <EOL> if self . _pythonVersion < <NUM_LIT:3> : <EOL> webbrowser . open_new_tab ( fileToOpen . encode ( sys . getfilesystemencoding ( ) ) ) <EOL> else : <EOL> webbrowser . open_new_tab ( fileToOpen ) <EOL> def saveCurrentViewInTempFile ( self , view ) : <EOL> tempFile = tempfile . NamedTemporaryFile ( suffix = "<STR_LIT>" , delete = False ) <EOL> region = sublime . Region ( <NUM_LIT:0> , view . size ( ) ) <EOL> text = view . substr ( region ) <EOL> tempFile . write ( text . encode ( "<STR_LIT:utf-8>" ) ) <EOL> tempFile . close ( ) <EOL> return tempFile . name </s>
