Search code examples
pythonsublimetext2sublimetext3sublimetextsublime-text-plugin

Assistance with expanding a Sublime Text Plugin


I have a sublime text plugin that watches for the creation of files beginning with lp_

When a file with lp_ prefix is created the plugin creates a folder of the same name with an images folder within.

I would like to watch different areas of my site and create the relevant folder within the nearest lp folder to the created file.

For example I have the following folder strucure

Root > desktop > Root > desktop > lp

Root > Mobile > Root > Mobile > lp

Root > Tablet > Root > Tablet > lp

When a file with lp_ prefix is created in either 'device' folder I would like folder to be created within the nearest lp folder.

The plugin below is along the right lines but I am unsure as how to set rules for targeting specific folders.

import sublime, sublime_plugin, os

# We extend event listener
class ExampleCommand(sublime_plugin.EventListener):
    # This method is called every time a file is saved (not only the first time is saved)
    def on_post_save_async(self, view):
        variables = view.window().extract_variables()
        fileBaseName = variables['file_base_name'] # File name without extension
        path = '/Users/jameshusband/Dropbox/development/remote/superfreeslotgames.com/css/' + fileBaseName
        imagepath = path + '/images/'

        if fileBaseName.startswith('lp_') and not os.path.exists(path):
            os.mkdir(path)
            os.mkdir(imagepath)

Could anyone point me in the right direction for this? I am not very experienced with Python so am unsure of the best way to achieve my goal.


Solution

  • Many thanks to SergioFC for helping out with this!

    import sublime, sublime_plugin, os
    
    
    # We extend event listener
    class ExampleCommand(sublime_plugin.EventListener):
        # This method is called every time a file is saved (not only the first time is saved)
        def on_post_save_async(self, view):
            variables = view.window().extract_variables()
            fileBaseName = variables['file_base_name'] # File name without extension
            file_path = variables['file_path']
    
            if fileBaseName.startswith('lp_'):
    
                if file_path.endswith('/desktop'):
                    path = file_path + '/lp/' + fileBaseName
                    imagepath = path + '/images/'
                    os.mkdir(path)
                    os.mkdir(imagepath)
                    open(path + '/' + "style.css", 'w')
    
                if file_path.endswith('/tablet'):
                    path = file_path + '/lp/' + fileBaseName
                    imagepath = path + '/images/'
                    os.mkdir(path)
                    os.mkdir(imagepath)
                    open(path + '/' + "style.css", 'w')
    
                if file_path.endswith('/mobile'):
                    path = file_path + '/lp/' + fileBaseName
                    imagepath = path + '/images/'
                    os.mkdir(path)
                    os.mkdir(imagepath)
                    open(path + '/' + "style.css", 'w')
    

    Expanding from here would be to upload lp folder content on save of lp_ file, then set user options for multi purpose