apohllo-cyc-console 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,8 @@
1
+ 0.0.3
2
+ - fix: added missing library and changelog.txt to gemspec
3
+ 0.0.2
4
+ - versin bump for github
5
+ 0.0.1
6
+ - history support
7
+ - highlighting of unmatched parenthesis
8
+ - colorful prompt
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "cyc-console"
3
- s.version = "0.0.2"
3
+ s.version = "0.0.3"
4
4
  s.date = "2009-07-15"
5
5
  s.summary = "Ruby console for the Cyc ontology"
6
6
  s.email = "apohllo@o2.pl"
@@ -10,10 +10,13 @@ Gem::Specification.new do |s|
10
10
  # the lib path is added to the LOAD_PATH
11
11
  s.require_path = "lib"
12
12
  # include Rakefile, gemspec, README and all the source files
13
- s.files = ["Rakefile", "cyc-console.gemspec", "README.txt"] +
14
- Dir.glob("lib/**/*")
15
- # include tests and specs
16
- s.test_files = Dir.glob("{test,spect,features}/**/*")
13
+ s.files = [
14
+ 'lib/cyc/console.rb',
15
+ "Rakefile",
16
+ "cyc-console.gemspec",
17
+ "README.txt",
18
+ "changelog.txt"
19
+ ]
17
20
  # include README while generating rdoc
18
21
  s.rdoc_options = ["--main", "README.txt"]
19
22
  s.has_rdoc = true
@@ -0,0 +1,154 @@
1
+ #!/usr/bin/ruby
2
+
3
+ require 'readline'
4
+ require 'colors'
5
+ require 'net/telnet'
6
+
7
+ module Cyc
8
+ class ParenthesisNotMatched < RuntimeError; end
9
+ #
10
+ # Stolen (with permission for the author:) from Wirble history for IRB
11
+ # http://pablotron.org/software/wirble/
12
+ #
13
+ class History
14
+ DEFAULTS = {
15
+ :history_path => ENV['CYC_HISTORY_FILE'] || "~/.cyc_history",
16
+ :history_size => (ENV['CYC_HISTORY_SIZE'] || 1000).to_i,
17
+ :history_perms => File::WRONLY | File::CREAT | File::TRUNC,
18
+ }
19
+
20
+ private
21
+
22
+ def say(*args)
23
+ puts *args if @verbose
24
+ end
25
+
26
+ def cfg(key)
27
+ @opt["history_#{key}".intern]
28
+ end
29
+
30
+ def save_history
31
+ path, max_size, perms = %w{path size perms}.map { |v| cfg(v) }
32
+
33
+ # read lines from history, and truncate the list (if necessary)
34
+ lines = Readline::HISTORY.to_a.uniq
35
+ lines = lines[-max_size, -1] if lines.size > max_size
36
+
37
+ # write the history file
38
+ real_path = File.expand_path(path)
39
+ File.open(real_path, perms) { |fh| fh.puts lines }
40
+ say 'Saved %d lines to history file %s.' % [lines.size, path]
41
+ end
42
+
43
+ def load_history
44
+ # expand history file and make sure it exists
45
+ real_path = File.expand_path(cfg('path'))
46
+ unless File.exist?(real_path)
47
+ say "History file #{real_path} doesn't exist."
48
+ return
49
+ end
50
+
51
+ # read lines from file and add them to history
52
+ lines = File.readlines(real_path).map { |line| line.chomp }
53
+ Readline::HISTORY.push(*lines)
54
+
55
+ say 'Read %d lines from history file %s' % [lines.size, cfg('path')]
56
+ end
57
+
58
+ public
59
+
60
+ def initialize(opt = nil)
61
+ @opt = DEFAULTS.merge(opt || {})
62
+ return unless defined? Readline::HISTORY
63
+ load_history
64
+ Kernel.at_exit { save_history }
65
+ end
66
+ end
67
+
68
+ class Console
69
+ @@constants = []
70
+
71
+ API_QUIT = "(api-quit)"
72
+
73
+ # Initializes connection with Cyc runing on host :host: on port :port:
74
+ def initialize(host, port)
75
+ History.new({})
76
+ @host = host
77
+ @port = port
78
+ @conn = Net::Telnet.new("Port" => @port, "Telnetmode" => false,
79
+ "Host" => @host, "Timeout" => 600)
80
+ @line = ""
81
+ @count = 0
82
+ end
83
+
84
+ # Scans the :str: to find out if the parenthesis are matched
85
+ # raises ParenthesisNotMatched exception if there is not matched closing
86
+ # parenthesis. The message of the exception contains the string with the
87
+ # unmatched parenthesis highlighted.
88
+ def match_par(str)
89
+ position = 0
90
+ str.scan(/./) do |char|
91
+ position += 1
92
+ next if char !~ /\(|\)/
93
+ @count += (char == "(" ? 1 : -1)
94
+ if @count < 0
95
+ @count = 0
96
+ raise ParenthesisNotMatched.
97
+ new((position > 1 ? str[0..position-2] : "") +
98
+ ")".hl(:red) + str[position..-1])
99
+ end
100
+ end
101
+ @count == 0
102
+ end
103
+
104
+ def add_autocompletion(str)
105
+ @@constants |= str.split(/[() ]/).select{|e| e =~ /\#\$/}
106
+ end
107
+
108
+ # The main loop of the console
109
+ def main_loop
110
+ loop do
111
+ begin
112
+ line = Readline::readline(("cyc@" + "#{@host}:#{@port}" +
113
+ (@count > 0 ? ":#{"("*@count}" : "") + "> ").hl(:blue))
114
+ case line
115
+ when API_QUIT
116
+ @conn.puts(line)
117
+ break
118
+ when "exit"
119
+ @conn.puts(API_QUIT)
120
+ break
121
+ else
122
+ @line += "\n" unless @line.empty?
123
+ @line += line
124
+ letters = @line.split("")
125
+ Readline::HISTORY.push(@line) if line
126
+ if match_par(line)
127
+ @conn.puts(@line)
128
+ @line = ""
129
+ answer = @conn.waitfor(/\d\d\d/)
130
+ message = answer.sub(/(\d\d\d) (.*)\n/,"\\2") if answer
131
+ if($1.to_i == 200)
132
+ puts message
133
+ add_autocompletion(message)
134
+ else
135
+ puts "Error: " + answer.to_s
136
+ end
137
+ end
138
+ end
139
+ rescue ParenthesisNotMatched => exception
140
+ puts exception
141
+ @line = ""
142
+ # rescue Exception => exception
143
+ # puts exception
144
+ end
145
+ end
146
+ end
147
+
148
+ CompletionProc = proc {|input|
149
+ candidates = %w{denotation-mapper}
150
+ @@constants.grep(/^#{Regexp.quote(input)}/)
151
+ }
152
+ end
153
+ end
154
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apohllo-cyc-console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aleksander Pohl
@@ -31,9 +31,11 @@ extensions: []
31
31
  extra_rdoc_files:
32
32
  - README.txt
33
33
  files:
34
+ - lib/cyc/console.rb
34
35
  - Rakefile
35
36
  - cyc-console.gemspec
36
37
  - README.txt
38
+ - changelog.txt
37
39
  has_rdoc: true
38
40
  homepage: http://apohllo.pl
39
41
  post_install_message: