从sublime text外部在sublime text中运行python
sublime_harness的Python项目详细描述
升华线束
从Sublime Text之外的Sublime Text中运行Python
sublime-harness的构建允许在Sublime Text的上下文中执行任意python。它也是Sublime plugin tests框架的一部分。
Currently, only Linux is supported but OSX and Windows support are planned.
折旧通知
我们决定不赞成sublime-harness,而赞成randy3k/UnitTesting。
它有更大的平台支持,并且为本地开发设计的脆弱性更小。
https://github.com/randy3k/UnitTesting
开始
使用pip install sublime_harness
安装模块importos,timefromsublime_harnessimportHarnessharness=Harness()script=""" import sublime # Harness will run the `run` function def run(): with open('/tmp/hi', 'w') as f: f.write('Hello World!') sublime.run_command('exit')"""harness.run(script)# Wait for our file to exist (Sublime Text is forked and not synchronous)output_file='/tmp/hi'while(notos.path.exists(output_file)oros.stat(output_file).st_size==0):time.sleep(0.1)# Read our datawithopen(output_file)asf:printf.read()# Hello World!
文件
sublime_harness为您的所有引导需要提供Harness类。
Sublime Text将通过sublime-info解析,这允许通过环境变量重写。
线束。
Harness()"""Generate a new Harness for Sublime Text
When initialized, `Harness` allocates a directory (currently,
same for all harnesses) for your script.
"""
harness.目录
harness.directory"""Directory where `run` will be execute
If you would like to load relative modules, they should be copied to this directory."""
线束。运行
harness.run(script)"""Python to execute within the context of Sublime Text
**YOU MUST CLEAN UP AFTER RUNNING THIS METHOD VIA `close`**
You can only run one harness at a time due to the lack of namespacing.
:param script: Python to execute within Sublime Text
:type script: str
"""
线束。关闭
harness.close()"""Cleans up harness files"""
示例
如Harness.dictionary中所述,可以相对于脚本加载外部文件。这是一个如何设置和使用它们的示例。
# Set up a new harnessimportos,timefromsublime_harnessimportHarnessharness=Harness()# Copy over a local file to the directorydest_hello_path=harness.directory+'/hello.py'withopen(dest_hello_path,'w')asf:f.write('hello = "World!"')# Generate and run our temporary taskscript=""" import os import sublime try : from hello import hello # ST 2 compatible except: from .hello import hello # ST 3 compatible def run(): with open('/tmp/hi-directory', 'w') as f: f.write(hello) sublime.run_command('exit') """harness.run(script)output_file='/tmp/hi-directory'while(notos.path.exists(output_file)oros.stat(output_file).st_size==0):time.sleep(0.1)# Grab the file outputwithopen(output_file)asf:printf.read()# 'World!'# Remove the plugin and our fileharness.close()os.unlink(dest_hello_path)
贡献
代替正式的形式指南,注意保持现有的编码风格。为任何新的或更改的功能添加单元测试。通过nosetests进行测试。