【问题标题】:Assistance with expanding a Sublime Text Plugin协助扩展 Sublime Text 插件
【发布时间】:2015-08-04 07:50:45
【问题描述】:

我有一个 sublime text 插件,它监视以 lp_ 开头的文件的创建

创建带有 lp_ 前缀的文件时,插件会创建一个同名文件夹,其中包含一个图像文件夹。

我想查看我网站的不同区域,并在离所创建文件最近的 lp 文件夹中创建相关文件夹。

例如我有以下文件夹结构

根>桌面> 根>桌面> lp

根 > 手机 > 根>移动>lp

根 > 平板电脑 > 根>平板电脑> lp

当在任一“设备”文件夹中创建带有 lp_ 前缀的文件时,我希望在最近的 lp 文件夹中创建文件夹。

下面的插件是正确的,但我不确定如何设置针对特定文件夹的规则。

导入 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)

谁能指出我正确的方向?我对 Python 不是很有经验,因此不确定实现目标的最佳方法。

【问题讨论】:

  • 正如您在sublime API 中看到的,提取的变量还包含file_path (variables['file_path'])。如果我正确理解了您的问题,您只需要测试保存文件的路径即可决定使用哪个文件夹。
  • 很高兴你回来塞尔吉奥,我一直在尝试使用可用的文档来解决这个问题,但有点迷失了。如何检查保存文件的位置以确定在哪里创建文件夹?
  • 如果您要保存的文件在 /root/desktop 中,则将其保存到 /root/desktop/whatever/lp。也许您可以使用startswith来查看文件路径是否以/root/desktop开头
  • 所以只使用标准 if else 语句?
  • 为什么不呢?我不确定它是否会起作用,请尝试测试它。

标签: python sublimetext2 sublimetext3 sublimetext sublime-text-plugin


【解决方案1】:

非常感谢 SergioFC 提供的帮助!

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')

从这里扩展将是在保存 lp_ 文件时上传 lp 文件夹内容,然后为多用途设置用户选项

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 2013-03-19
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多