Skip to main content

sublime-harness

Build Status

Run Python in Sublime Text from outside of Sublime Text

sublime-harness was built to allow for execution of arbitrary Python within the context of Sublime Text. It is also part of the Sublime plugin tests framework.

Currently, only Linux is supported but OSX and Windows support are planned.

Deprecation notice

We have decided to deprecate sublime-harness in favor of randy3k/UnitTesting.

It has greater platform support and a less brittle design for local development.

https://github.com/randy3k/UnitTesting

Getting Started

Install the module with: pip install sublime_harness

import os, time
from sublime_harness import Harness

harness = 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 (not os.path.exists(output_file) or
       os.stat(output_file).st_size == 0):
    time.sleep(0.1)

# Read our data
with open(output_file) as f:
  print f.read()  # Hello World!

Documentation

sublime_harness provides the Harness class for all your bootstrapping needs.

Sublime Text will be resolved via sublime-info, which allows for overriding via environment variables.

Harness.__init__

Harness()
"""Generate a new Harness for Sublime Text

When initialized, `Harness` allocates a directory (currently,
same for all harnesses) for your script.
"""

Harness.directory

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

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

harness.close()
"""Cleans up harness files"""

Examples

As mentioned within Harness.dictionary, external files can be loaded relatively to the script. This is an example of how to set up and use them.

# Set up a new harness
import os, time
from sublime_harness import Harness
harness = Harness()

# Copy over a local file to the directory
dest_hello_path = harness.directory + '/hello.py'
with open(dest_hello_path, 'w') as f:
  f.write('hello = "World!"')

# Generate and run our temporary task
script = """
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 (not os.path.exists(output_file) or
       os.stat(output_file).st_size == 0):
    time.sleep(0.1)

# Grab the file output
with open(output_file) as f:
    print f.read() # 'World!'

# Remove the plugin and our file
harness.close()
os.unlink(dest_hello_path)

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Test via nosetests.

Donating

Support this project and others by twolfson via gittip.

Support via Gittip

Unlicense

As of Oct 16 2013, Todd Wolfson has released this repository and its contents to the public domain.

It has been released under the UNLICENSE.

Metadata

Release files for sublime_harness 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distributions (sdists)

Source distribution for sublime_harness 1.0.0
File Size Uploaded
sublime_harness-1.0.0.zip 17.8 kB Details
sublime_harness-1.0.0.tar.gz 8.9 kB Details

Release files / sublime_harness-1.0.0.zip

Download URL sublime_harness-1.0.0.zip
Size 17.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9c6ae394755e8677f7062cfe7e19b137a7e6560f017d26f1918e4d74451e9807
BLAKE2b-256 checksum
How to use checksums
9998d811c5f6acc66c7135a538784c527729cb2970403882ff4d6d268466003f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / sublime_harness-1.0.0.tar.gz

Download URL sublime_harness-1.0.0.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
11c9a8e7607269d6e4c5048d775ed64f2d1f6903206f8dc1b7b3e378b4f654e9
BLAKE2b-256 checksum
How to use checksums
5c1d8ce861fe5d3558e51667082df69f5f789b02186c7493a7fb6142c33af479
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page