Home Download Buy Blog Forum Support

need simple plugin help

need simple plugin help

Postby vikaskbh on Mon Sep 20, 2010 3:12 pm

Hello,

I am new to this community and I have moderate knowledge of python programming.

I was quickly referring some functions at http://www.sublimetext.com/docs/api-reference but couldn't figure out how to grab entire file in variable. I need to create a plugin to convert xml file to text list.

I know how to read dom using python's xml.dom.minidom module.

[1] Only the problem is to get entire xml file into variable.
After then it's easy to get text nodes.

i.e.
Code: Select all
from xml.dom.minidom import parse, parseString
dom = parseString(xml_string)
for entries in dom.getElementsByTagName('url'):
    ... # node processing


[2] And once processing is done, replace entire xml content with new text list.

Please guide me for these (point 1,2) missing functions.
vikaskbh
 
Posts: 15
Joined: Fri Sep 17, 2010 6:49 am

Re: need simple plugin help

Postby gpfsmurf on Mon Sep 20, 2010 5:58 pm

Code: Select all
import sublime, sublimeplugin

class xml2listCommand(sublimeplugin.TextCommand):
   def run(self, view, args):
      region = sublime.Region(0L, view.size())
      xml_string = view.substr(region)
      # ...
      # process xml_string and put the result in list_output
      # ...
      view.replace(region, list_output)
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: need simple plugin help

Postby vikaskbh on Mon Sep 20, 2010 7:17 pm

wow done! Google XML sitemap => URL List.

Code: Select all
import sublime, sublimeplugin
from xml.dom.minidom import parse, parseString
class xml2listCommand(sublimeplugin.TextCommand):
    def run(self, view, args):
        list_output = []
        region = sublime.Region(0L, view.size())
        xml_string = view.substr(region)
        dom = parseString(xml_string)
        for entries in dom.getElementsByTagName('url'):
            list_output.append(entries.getElementsByTagName('loc').item(0).firstChild.nodeValue)
        list_output = "\n".join(list_output)
        view.replace(region, list_output)


Just a little more help needed.

Any idea, after last line how to show "Save As" dialog box :?:
vikaskbh
 
Posts: 15
Joined: Fri Sep 17, 2010 6:49 am

Re: need simple plugin help

Postby gpfsmurf on Mon Sep 20, 2010 9:26 pm

I think you can do something like this:
Code: Select all
view.runCommand("saveas")

See http://www.sublimetext.com/docs/commands

But instead of changing the current buffer and calling 'save as', you could put the output in a new buffer.
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: need simple plugin help

Postby vikaskbh on Tue Sep 21, 2010 3:34 am

I tried that but

Code: Select all
view.runCommand("saveAs")


seems not working.

But on console [~ + CTRL],

Code: Select all
window.runCommand("saveAs")


is working!

While adding that line in plugin throws following error

Code: Select all
NameError: global name 'window' is not defined


Would you guide, how to import window object inside plugin code?

I tried following... but not working.

Code: Select all
import window
from sublime import window
from sublimeplugin import window
vikaskbh
 
Posts: 15
Joined: Fri Sep 17, 2010 6:49 am

Re: need simple plugin help

Postby jps on Tue Sep 21, 2010 3:39 am

To get a Window from a TextCommand, call window() on the view passed to you
jps
Site Admin
 
Posts: 3058
Joined: Wed Mar 19, 2008 12:33 pm

Re: need simple plugin help

Postby vikaskbh on Tue Sep 21, 2010 3:41 am

Also tried this one.

Code: Select all
cmd = sublime.Window()
cmd.runCommand("saveAs")


Throwing Error

Code: Select all
RuntimeError: This class cannot be instantiated from Python
vikaskbh
 
Posts: 15
Joined: Fri Sep 17, 2010 6:49 am

Re: need simple plugin help

Postby vikaskbh on Tue Sep 21, 2010 3:54 am

Great tip admin. :idea:

cmd = view.window()
cmd.runCommand("saveAs")


It's working now. :)
vikaskbh
 
Posts: 15
Joined: Fri Sep 17, 2010 6:49 am


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 2 guests