0

I am trying to build a plugin for the program Sublimetext2.

It uses plugins coded with Python. I have no Python knowledge at all but from looking at existing plugins and my PHP knowledge here is what I need help with...

this is the start of the Python file so far

import sublime, sublime_plugin
import webbrowser

settings = sublime.load_settings('openonserver.sublime-settings')
settings.get('file_path_prefix')
settings.get('server_url')

class OpenonServerCommand(sublime_plugin.TextCommand):
   def run(self,edit):
      file_path = self.view.file_name()

What I need to do though take the value of the settings

file_path will be the path to the file I am running this on so lets say...

E:\Server\htdocs\mytest_project_\some\folder_\test.php

The settings

file_path_prefix will be E:\Server\htdocs\ and

server_url will be http://localhost/

I need to see if file_path_prefix exist in file_path if it does,

I need to replace the E:\Server\htdocs\ with the http://localhost/ and replace all \ to / and then store this new path in a variable

so... E:\Server\htdocs\mytest_project_\some\folder_\test.php would become

http://localhost/mytest_project_/some/folder_/test.php

I then need to send this to the browser.

Any help is greatly appreciated

JasonDavis
  • 45,100
  • 92
  • 294
  • 508
  • `os.path` is your friend: http://docs.python.org/library/os.path.html `os.path.join` (instead of manually concatenating paths) and `os.getcwd` (current working dir) in particular. – Russell Dias Dec 15 '11 at 12:23

2 Answers2

1

Use

os.system("path_to_browser url")

To run any external program. I also recomend to take a look at this comment

Community
  • 1
  • 1
floodkoff
  • 98
  • 5
0

Ok after many hours (I hate Python now) my solution (i'm very not impressed) but it partially works

#Context.sublime-menu
[
    { "command": "openserver", "caption": "Open on Server" }
]

#Default (Windows).sublime-keymap
[
        { "keys": ["ctrl+shift+b"], "command": "openserver" }
]

#Main.sublime-menu
[
    {
        "caption": "Tools",
        "mnemonic": "T",
        "id": "tools",
        "children":
        [
            { "command": "openserver", "caption": "Open on Server" }
        ]
    }
]

#Openserver.sublime-commands
[
    {
        "caption": "Open file on Server in Browser",
        "command": "openserver"
    }
]


#Openserver.sublime-settings
{
    "file_path_prefix": "E:/Server/htdocs",
    "url_prefix": "http://localhost"
}

Main file

#openserver.py  

import sublime, sublime_plugin
import os
import webbrowser
import re
import os2emxpath
import logging
import sys

class OpenserverCommand(sublime_plugin.TextCommand):
   def run(self,edit):
    file_path = self.view.file_name()

    settings = sublime.load_settings('Openserver.sublime-settings')

    file = os2emxpath.normpath(file_path)

    url = re.sub(settings.get('file_path_prefix'), settings.get('url_prefix'), file)
    #logging.warning(url)

    #webbrowser.open_new(url)
    if sys.platform=='win32':
        os.startfile(url)
    elif sys.platform=='darwin':
        subprocess.Popen(['open', url])
    else:
        try:
            subprocess.Popen(['xdg-open', url])
        except OSError:
            logging.warning(url)

Now when I say it works but it partially doesn't, it does take the file name, replaces my path and server URL from a settings file and then does launch a browser with the proper URL

Except, in Sublimetext2 when you run this on a .py file or any file that you do not have set to be able to open in a web browser, then instead of opening the file in the web browser it will give the windows popup asking to set a default program to open the file, very annoying!

JasonDavis
  • 45,100
  • 92
  • 294
  • 508