thisisbad 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.pyc
2
+ python_module.py
3
+ *.swp
4
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+ gem "rspec"
3
+ gem "escape"
@@ -0,0 +1,20 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.3)
5
+ escape (0.0.4)
6
+ rspec (2.8.0)
7
+ rspec-core (~> 2.8.0)
8
+ rspec-expectations (~> 2.8.0)
9
+ rspec-mocks (~> 2.8.0)
10
+ rspec-core (2.8.0)
11
+ rspec-expectations (2.8.0)
12
+ diff-lcs (~> 1.1.2)
13
+ rspec-mocks (2.8.0)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ escape
20
+ rspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Sumeet Agarwal
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,8 @@
1
+ require "json"
2
+
3
+ class JSONWithScalars
4
+ def self.parse(json)
5
+ JSON.parse(%Q{[#{json}]}).first
6
+ end
7
+ end
8
+
@@ -0,0 +1,48 @@
1
+ require "shell"
2
+
3
+ require "json_with_scalars"
4
+
5
+ Shell.def_system_command(:pygmentize, "pygmentize -l pytb")
6
+
7
+ module Python
8
+ class Module
9
+ def initialize(name)
10
+ @name = name
11
+ end
12
+
13
+ def method_missing(python_function_name, *args)
14
+ output = call_python(python_function_name, args.to_json)
15
+ if contains_python_stack_trace?(output)
16
+ puts colorize_stack_trace(output)
17
+ raise PythonError
18
+ end
19
+ convert_to_ruby(output)
20
+ end
21
+
22
+ private
23
+ def call_python(python_function_name, args)
24
+ %x[python -c 'import json ; from #{@name} import * ; \
25
+ args = json.loads("""#{args}""") ; \
26
+ print json.dumps(#{python_function_name}(*args))' \
27
+ 2>&1].strip
28
+ end
29
+
30
+ def contains_python_stack_trace?(output)
31
+ output.include? "Traceback (most recent call last)"
32
+ end
33
+
34
+ def colorize_stack_trace(output)
35
+ Shell.new.transact { echo(output) | pygmentize }.to_s
36
+ end
37
+
38
+ def convert_to_ruby(output)
39
+ JSONWithScalars.parse(output)
40
+ end
41
+ end
42
+
43
+ def self.method_missing(name, *)
44
+ Module.new(name)
45
+ end
46
+
47
+ class PythonError < StandardError ; end
48
+ end
@@ -0,0 +1,15 @@
1
+ require "json_with_scalars"
2
+
3
+ describe JSONWithScalars do
4
+ it "parses regular JSON stuff" do
5
+ JSONWithScalars.parse({"blah" => 123}.to_json).should == {"blah" => 123}
6
+ end
7
+
8
+ it "parses scalar ints" do
9
+ JSONWithScalars.parse("123").should == 123
10
+ end
11
+
12
+ it "parses scalar strings" do
13
+ JSONWithScalars.parse('"abc"').should == "abc"
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ require "thisisbad"
2
+
3
+ describe Python do
4
+ before(:each) do
5
+ File.open("python_module.py", "w") do |f|
6
+ f.write("def add_one(x): return x + 1")
7
+ end
8
+ end
9
+
10
+ after(:each) { File.delete("python_module.py") }
11
+
12
+ it "raises an error when there's an exception" do
13
+ output = ["Traceback (most recent call last):\e[39;49;00m\n File ",
14
+ "\e[39;49;00m\e[36m\"<string>\"\e[39;49;00m, line ",
15
+ "\e[39;49;00m\e[34m1\e[39;49;00m, in ",
16
+ "\e[39;49;00m<module>\e[39;49;00m\n\e[31;01mNameError",
17
+ "\e[39;49;00m: \e[39;49;00mname 'idx' is not ",
18
+ "defined\e[39;49;00m\n"].join("")
19
+ STDOUT.should_receive(:puts).with(output)
20
+ expect do
21
+ Python::python_module.idx(123)
22
+ end.to raise_exception(Python::PythonError)
23
+ end
24
+
25
+ it "receives output from python" do
26
+ Python::python_module.add_one(123).should == 124
27
+ end
28
+ end
29
+
@@ -0,0 +1,19 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "thisisbad"
5
+ s.version = "0.0.2"
6
+ s.platform = Gem::Platform::RUBY
7
+ s.authors = ["Sumeet Agarwal"]
8
+ s.email = ["sumeet.a@gmail.com"]
9
+ s.homepage = "http://github.com/sumeet/thisisbad"
10
+ s.summary = %q{if you ever want to pretend python could have rspec}
11
+ s.description = %q{if you ever want to pretend python could have rspec}
12
+
13
+ s.rubyforge_project = "thisisbad"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: thisisbad
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sumeet Agarwal
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-27 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: if you ever want to pretend python could have rspec
15
+ email:
16
+ - sumeet.a@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE
25
+ - lib/json_with_scalars.rb
26
+ - lib/thisisbad.rb
27
+ - spec/json_with_scalars_spec.rb
28
+ - spec/thisisbad_spec.rb
29
+ - thisisbad.gemspec
30
+ homepage: http://github.com/sumeet/thisisbad
31
+ licenses: []
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project: thisisbad
50
+ rubygems_version: 1.8.17
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: if you ever want to pretend python could have rspec
54
+ test_files:
55
+ - spec/json_with_scalars_spec.rb
56
+ - spec/thisisbad_spec.rb