Sublime Forum

[Solved][Question] Variable in another variable without quotes

#1

1. Briefly

I don’t find in Google, how I can remove "quotes", if I insert Python variable in another variable.

2. Expected behavior

I write small plugin for split current file:

# Swiss File Knife split utility:
# http://stahlworks.com/dev/index.php?tool=split


class grace_splitter_sfk_knife(sublime_plugin.TextCommand):

    def run(self, edit):
        FILE_PATH = self.view.file_name()
        if(FILE_PATH):
            command = ["sfk", "split", "100k", "-yes", "-text", FILE_PATH]
            print(command)
            subprocess.Popen(command, shell=True)

If I run command:

  • command successful work for me,
  • I get in Sublime Text console:
command: grace_splitter_sfk_knife
['sfk', 'split', '100k', '-yes', '-text', 'D:\\Test\\SashaLog.log']

3. Actual behavior

Now I want, that user may run custom command, which user set in user settings.

My GraceSplitter.sublime-settings file:

{
    "gracesplitter_cmd_options": "sfk split 100k -yes -text"
}

My class:

# Custom split command


class grace_splitter_custom_splitter(sublime_plugin.TextCommand):

    def run(self, edit):
        FILE_PATH = self.view.file_name()
        if(FILE_PATH):
                # Get Sublime Text settings —
                # http://stackoverflow.com/a/14186945/5951529
            settings = sublime.load_settings(
                'GraceSplitter.sublime-settings')
            cmd_options = settings.get(
                'gracesplitter_cmd_options')
            # Split settings — http://stackoverflow.com/a/743824/5951529
            cmd_options_split = cmd_options.split()
            # Method strip —
            # https://www.tutorialspoint.com/python/string_strip.htm
            strip_brackets = str(cmd_options_split).strip('[]')
            command = strip_brackets, FILE_PATH
            print(command)
            subprocess.call(command, shell=True)

If I run command:

  • command don’t work for me,
  • I get in Sublime Text console:
command: grace_splitter_custom_splitter
["'sfk', 'split', '100k', '-yes', '-text'", 'D:\\Test\\SashaLog.log']

How I can get expected behavior?

3. Did not help

  1. I try make other value for variable command:
command = '[' + strip_brackets + ',' + \
    ' \'' + FILE_PATH + '\'' + ']'

I get in Sublime Text console:

command: grace_splitter_custom_splitter
['sfk', 'split', '100k', '-yes', '-text', 'D:\Test\SashaLog.log']

Command don’t work for me again.

Thanks.

0 Likes

#2

@Sasha_Chernykh Try renaming your class from grace_splitter_custom_splitter to GraceSplitterCustomSplitter, which should work with sublime.run_command(' grace_splitter_custom_splitter').

0 Likes

#3

@rppn, I rename my class → I get same behavior.

Thanks.

0 Likes

#4

your strip_brackets = str(cmd_options_split).strip('[]') line is converting a list of strings to a string, and then stripping the brackets, so this is where the quote characters come from. Probably you want to do this instead:

strip_brackets = [option.strip('[]') for option in cmd_options_split]

i.e. strip the brackets from each option

0 Likes

#5

@kingkeith, brackets don’t remove for me in your example:

command: grace_splitter_custom_splitter
(['sfk', 'split', '100k', '-yes', '-text'], 'D:\\Test\\SashaLog.log')

Thanks.

0 Likes

#6

oh I see, I misunderstood what you were trying to do:

command = cmd_options.split() + [FILE_PATH]
subprocess.call(command, shell=True)
1 Like

#7

It works for me.

Thank you very much!

0 Likes

#8

A few improvements:

if you don’t use the command variable, you can simpy:

subprocess.call(cmd_options.split() + [FILE_PATH], shell=True)

For the if:

if (FILE_PATH):
     pass
# you can remove the parenthesis

if FILE_PATH:
    pass

And, since it’s always better to keep your code flat instead of nested, here’s what you could do:

def run(self, edit):
    if not FILE_NAME:
        return # stops the execution of the function
    # do your stuff here
1 Like

#9

@rppn, yes, I must to do it.

PEP 8 class names.

Thanks.

0 Likes

#10

Actually the PEP8 convention is NOT the reason for what rppn wrote.

The Sublime Text plugin classes which inherit from ApplicationCommand, WindowCommand, and TextCommand should be named using the following system.

Camel case should be used followed by the word ‘Command’. e.g. CamelCaseCommand. Sublime Text will then translate that to a snake case command and will remove the word ‘Command’ from the end.

e.g. A class called CamelCaseCommand is run with camel_case.

Sublime Text is very forgiving about this, certainly it is not enforced in the current version, but at some point this could change so it’s best to stick with the stated requirement.

See the documentation here.

HTH.

Since English is obviously not your first language maybe this will help.

import sublime, sublime_plugin

class TempTestCodeCommand(sublime_plugin.WindowCommand):
    def run(self):
        sublime.status_message("Running OK...")

Run the plugin with this command: temp_test_code. e.g.
In the console with: window.run_command("temp_test_code")
Or with key bindings: { "keys": ["ctrl+k", "ctrl+z"], "command": "temp_test_code" }

1 Like