krpc 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ class Module
3
3
  def class_name
4
4
  name.rpartition("::").last
5
5
  end
6
-
6
+
7
7
  def const_get_or_create(module_name, value = nil, &block)
8
8
  return const_get(module_name) if const_defined?(module_name, false)
9
9
  value = block.call if block_given?
@@ -26,9 +26,9 @@ class String
26
26
  end
27
27
 
28
28
  def integer?
29
- Integer(self) != nil rescue false
29
+ Integer(self) != nil rescue false
30
30
  end
31
-
31
+
32
32
  def numeric?
33
33
  Float(self) != nil rescue false
34
34
  end
@@ -6,14 +6,14 @@ module KRPC
6
6
  OK_LENGTH = 2
7
7
  OK_MESSAGE = "\x4F\x4B"
8
8
  GUID_LENGTH = 16
9
-
9
+
10
10
  class << self
11
-
11
+
12
12
  # Given a type object, and serialized data, decode the ruby value/object
13
13
  def decode(data, type, client)
14
14
  if type.is_a?(Types::MessageType) then decode_message(data, type)
15
15
  elsif type.is_a?(Types::ValueType) then decode_value(data, type)
16
- elsif type.is_a?(Types::EnumType)
16
+ elsif type.is_a?(Types::EnumType)
17
17
  v = decode_value(data, TypeStore["int32"])
18
18
  type.ruby_type.key(v)
19
19
  elsif type.is_a?(Types::ClassType)
@@ -41,11 +41,11 @@ module KRPC
41
41
  def decode_value(data, type)
42
42
  ProtobufUtils::Decoder.decode(data, type.protobuf_type)
43
43
  end
44
-
44
+
45
45
  def decode_message(data, type)
46
46
  type.ruby_type.decode(data.to_s)
47
47
  end
48
-
48
+
49
49
  end
50
50
  end
51
51
  end
@@ -7,7 +7,7 @@ module KRPC
7
7
  @docstr_infos = {}
8
8
  @procedure_docstr_infos = {}
9
9
  class << self
10
-
10
+
11
11
  def docstring_for_method(method_owner, method_name, is_print_xmldoc_summary = true)
12
12
  is_static, class_cls = method_owner.class == Class ? [true, method_owner] : [false, method_owner.class]
13
13
  service_module_name, class_name = ruby_class_to_pb_module_class_pair(class_cls)
@@ -16,10 +16,10 @@ module KRPC
16
16
  construct_docstring(*@docstr_infos[key], true, is_static, is_print_xmldoc_summary)
17
17
  else
18
18
  "No docstring for #{class_cls.name}#{calc_separator(is_static)}#{method_name.to_s} method" +
19
- (method_owner.respond_to?(method_name) ? "" : "\nThere is no such method -- maybe a typo")
19
+ (method_owner.respond_to?(method_name) ? "" : "\nThere is no such method -- maybe a typo?")
20
20
  end
21
21
  end
22
-
22
+
23
23
  def docstring_for_procedure(service_name, procedure_name, is_print_xmldoc_summary = true)
24
24
  key = [service_name, procedure_name].hash
25
25
  if @procedure_docstr_infos.has_key? key
@@ -40,30 +40,30 @@ module KRPC
40
40
  @docstr_infos[key2] = val
41
41
  end
42
42
  end
43
-
43
+
44
44
  def add_special_docstring_info(key, value)
45
45
  @docstr_infos[key] = value
46
46
  end
47
-
47
+
48
48
  private #----------------------------------
49
-
49
+
50
50
  def ruby_class_to_pb_module_class_pair(ruby_class)
51
51
  return ["", Client.class_name] if ruby_class == Client
52
52
  rest, _, pb_class_name = ruby_class.name.rpartition("::")
53
53
  _, _, pb_service_name = rest.rpartition("::")
54
54
  [pb_service_name, pb_class_name]
55
55
  end
56
-
56
+
57
57
  def calc_separator(is_static)
58
58
  is_static ? '.' : '#'
59
59
  end
60
-
60
+
61
61
  def construct_docstring(namespace, separator, name, param_names, param_types, param_default, return_type, xmldoc, is_hide_this_param, is_prepend_client_param, is_print_xmldoc_summary)
62
62
  xmldoc = Nokogiri::XML(xmldoc)
63
63
  xmldoc_summary = xmlElements2str(xmldoc.xpath("doc/summary").children, :light_blue, :light_green, :light_red)
64
64
 
65
65
  xmldoc_returns = xmlElements2str(xmldoc.xpath("doc/returns").children, :blue, :green, :light_red)
66
- xmldoc_returns = "- ".blue + xmldoc_returns unless xmldoc_returns.empty?
66
+ xmldoc_returns = "- ".blue + xmldoc_returns unless xmldoc_returns.empty?
67
67
 
68
68
  xmldoc_params = {}
69
69
  xmldoc.xpath("doc/param").each do |e|
@@ -76,10 +76,10 @@ module KRPC
76
76
  end
77
77
  end
78
78
  end
79
-
79
+
80
80
  param_infos = param_names.zip(param_types.map{|x| type2str(x)}, param_default)
81
81
  param_infos.shift if is_hide_this_param && param_names[0] == "this"
82
- param_infos.unshift ["client", "Client", :no_default_value] if is_prepend_client_param
82
+ param_infos.unshift ["client", "Client", :no_default_value] if is_prepend_client_param
83
83
  if param_infos.empty?
84
84
  params = ""
85
85
  else
@@ -92,14 +92,14 @@ module KRPC
92
92
  "#{namespace.cyan}#{separator.cyan}#{name.bold}(#{params}) :#{type2str(return_type).light_red} #{xmldoc_returns}" \
93
93
  + (is_print_xmldoc_summary ? "\n\n#{xmldoc_summary}" : "")
94
94
  end
95
-
95
+
96
96
  def type2str(type)
97
97
  return "nil" if type.nil?
98
98
  return type.class_name if type.class == Class
99
99
  rt = type.ruby_type
100
100
  if type.is_a?(Types::EnumType) then "Enum" + rt.keys.to_s
101
101
  elsif type.is_a?(Types::ListType) ||
102
- type.is_a?(Types::SetType)
102
+ type.is_a?(Types::SetType)
103
103
  "#{rt.class_name}[#{type2str(type.value_type)}]"
104
104
  elsif type.is_a?(Types::DictionaryType)
105
105
  %Q{#{rt.class_name}[#{type2str(type.key_type)} => #{type2str(type.value_type)}]}
@@ -107,7 +107,7 @@ module KRPC
107
107
  %Q{#{rt.class_name}[#{type.value_types.map{|x| type2str(x)}.join(", ")}]}
108
108
  else rt.class_name end
109
109
  end
110
-
110
+
111
111
  def xmlElements2str(elements, main_color, paramref_color, value_color, error_color = :red)
112
112
  elements.map do |elem|
113
113
  if elem.is_a?(Nokogiri::XML::Text)
@@ -138,13 +138,13 @@ module KRPC
138
138
  elem.text.gsub("null","nil").colorize(value_color)
139
139
  else elem.to_s.colorize(error_color)
140
140
  end
141
- rescue RuntimeException => exc
141
+ rescue RuntimeException
142
142
  elem.to_s.colorize(error_color)
143
143
  end
144
144
  end
145
145
  end.join.gsub("\n"," ").gsub("!n!","\n").squeeze(' ').strip.gsub("!s!"," ")
146
146
  end
147
-
147
+
148
148
  def is_method_defined(path_array)
149
149
  method_name = path_array[-1].underscore
150
150
  begin
@@ -154,23 +154,18 @@ module KRPC
154
154
  false
155
155
  end
156
156
  end
157
-
157
+
158
158
  end
159
-
160
-
159
+
160
+
161
161
  module SuffixMethods
162
162
  DOCSTRING_SUFFIX = "_doc"
163
163
  DOCSTRING_SUFFIX_REGEX = /^(.+)(?:#{DOCSTRING_SUFFIX}(=)?)$/
164
-
164
+
165
165
  def self.included(base)
166
- base.extend self
167
- class << base
168
- def krpc_name
169
- class_name
170
- end
171
- end
166
+ base.extend self, ClassMethods
172
167
  end
173
-
168
+
174
169
  def method_missing(method, *args, &block)
175
170
  if DOCSTRING_SUFFIX_REGEX =~ method.to_s
176
171
  documented_method_name = $1 + $2.to_s
@@ -180,14 +175,20 @@ module KRPC
180
175
  end
181
176
  super
182
177
  end
183
-
178
+
184
179
  def respond_to_missing?(method, *)
185
180
  if DOCSTRING_SUFFIX_REGEX =~ method.to_s
186
181
  return true if respond_to? ($1 + $2.to_s).to_sym
187
182
  end
188
183
  super
189
184
  end
185
+
186
+ module ClassMethods
187
+ def krpc_name
188
+ class_name
189
+ end
190
+ end
190
191
  end
191
-
192
+
192
193
  end
193
194
  end
@@ -6,9 +6,9 @@ module KRPC
6
6
  RPC_HELLO_MESSAGE = "\x48\x45\x4C\x4C\x4F\x2D\x52\x50\x43\x00\x00\x00"
7
7
  STREAM_HELLO_MESSAGE = "\x48\x45\x4C\x4C\x4F\x2D\x53\x54\x52\x45\x41\x4D"
8
8
  NAME_LENGTH = 32
9
-
9
+
10
10
  class << self
11
-
11
+
12
12
  # Given a type object, and ruby object, encode the ruby object
13
13
  def encode(obj, type)
14
14
  if type.is_a?(Types::MessageType) then type.ruby_type.encode(obj)
@@ -51,17 +51,21 @@ module KRPC
51
51
  else raise(RuntimeError, "Cannot encode object #{obj} of type #{type}")
52
52
  end
53
53
  end
54
-
54
+
55
55
  def encode_value(value, type)
56
56
  ProtobufUtils::Encoder.encode(value, type.protobuf_type)
57
57
  end
58
-
58
+
59
59
  def encode_request(req)
60
60
  data = PB::Request.encode(req)
61
61
  length = ProtobufUtils::Encoder.encode_nonnegative_varint(data.length)
62
62
  length + data
63
63
  end
64
-
64
+
65
+ def hash_to_enumeration_values(hash)
66
+ hash.map {|k,v| PB::EnumerationValue.new(name: k.to_s, value: v) }
67
+ end
68
+
65
69
  end
66
70
  end
67
71
  end
@@ -31,7 +31,7 @@ module KRPC
31
31
  def with_arguments_count_incremented_by(args_count_increment)
32
32
  self.class.new(args_count + args_count_increment, (valid_params_count_range.min + args_count_increment)..(valid_params_count_range.max + args_count_increment), signature)
33
33
  end
34
-
34
+
35
35
  def with_signature(sig)
36
36
  self.class.new(args_count, valid_params_count_range, sig)
37
37
  end
@@ -6,10 +6,10 @@ require 'colorize'
6
6
  module KRPC
7
7
  module Gen
8
8
  class << self
9
- def service_gen_module(service_name)
9
+ def service_gen_module(service_name)
10
10
  const_get_or_create(service_name, Module.new)
11
11
  end
12
-
12
+
13
13
  def generate_class(service_name, class_name)
14
14
  mod = service_gen_module(service_name)
15
15
  mod.const_get_or_create(class_name) do
@@ -19,7 +19,7 @@ module KRPC
19
19
  end
20
20
  end
21
21
  end
22
-
22
+
23
23
  def generate_enum(service_name, enum_name, values)
24
24
  mod = service_gen_module(service_name)
25
25
  mod.const_get_or_create(enum_name) do
@@ -27,17 +27,17 @@ module KRPC
27
27
  end
28
28
  end
29
29
 
30
- def add_rpc_method(cls, method_name, service_name, proc, *options)
31
- is_static = options.include? :static
32
- prepend_self_to_args = options.include? :prepend_self_to_args
30
+ def add_rpc_method(cls, method_name, service_name, proc, *switches, **options)
31
+ is_static = switches.include? :static
32
+ prepend_self_to_args = switches.include? :prepend_self_to_args
33
33
  param_names, param_types, param_default, return_type = parse_procedure(proc)
34
34
  method_name = method_name.underscore
35
35
  args = [cls, method_name, param_default, param_names, param_types, prepend_self_to_args, proc, return_type, service_name]
36
36
 
37
37
  define_rpc_method(*args)
38
38
  define_static_rpc_method(*args) if is_static
39
- add_stream_constructing_proc(*args) unless options.include? :no_stream
40
- Doc.add_docstring_info(is_static, cls, method_name, service_name, proc.name, param_names, param_types, param_default, return_type: return_type, xmldoc: proc.documentation)
39
+ add_stream_constructing_proc(*args) unless switches.include? :no_stream
40
+ Doc.add_docstring_info(is_static, cls, method_name, options[:doc_service_name] || service_name, proc.name, param_names, param_types, param_default, return_type: return_type, xmldoc: proc.documentation)
41
41
  end
42
42
 
43
43
  def transform_exceptions(method_owner, method_name, prepend_self_to_args, &block)
@@ -52,15 +52,15 @@ module KRPC
52
52
  raise err.with_signature(Doc.docstring_for_method(method_owner, method_name, false))
53
53
  end
54
54
  end
55
-
55
+
56
56
  private #----------------------------------
57
-
57
+
58
58
  def define_static_rpc_method(cls, method_name, param_default, param_names, param_types, prepend_self_to_args, proc, return_type, service_name)
59
59
  cls.instance_eval do
60
60
  define_singleton_method method_name do |*args|
61
61
  Gen.transform_exceptions(cls, method_name, prepend_self_to_args) do
62
62
  raise ArgumentErrorSig.new("missing argument for parameter \"client\"") if args.count < 1
63
- raise ArgumentErrorSig.new("argument for parameter \"client\" must be a #{KRPC::Client.name} -- got #{args.first.inspect} of type #{args.first.class}") unless args.first.is_a?(KRPC::Client)
63
+ raise ArgumentErrorSig.new("argument for parameter \"client\" must be a #{::KRPC::Client.name} -- got #{args.first.inspect} of type #{args.first.class}") unless args.first.is_a?(::KRPC::Client)
64
64
  client = args.shift
65
65
  kwargs = args.extract_kwargs!
66
66
  client.execute_rpc(service_name, proc.name, args, kwargs, param_names, param_types, param_default, return_type: return_type)
@@ -90,7 +90,7 @@ module KRPC
90
90
  end
91
91
  end
92
92
  end
93
-
93
+
94
94
  def parse_procedure(proc)
95
95
  param_names = proc.parameters.map{|p| p.name.underscore}
96
96
  param_types = proc.parameters.map.with_index do |p,i|
@@ -108,29 +108,29 @@ module KRPC
108
108
  [param_names, param_types, param_default, return_type]
109
109
  end
110
110
  end
111
-
111
+
112
112
  module RPCMethodGenerator
113
- def include_rpc_method(method_name, service_name, procedure_name, params: [], return_type: nil, xmldoc: "", options: [])
114
- Gen.add_rpc_method(self.class, method_name, service_name, PB::Procedure.new(name: procedure_name, parameters: params, has_return_type: return_type != nil, return_type: return_type != nil ? return_type : "", documentation: xmldoc), options)
113
+ def include_rpc_method(method_name, service_name, procedure_name, params: [], return_type: nil, attributes: [], xmldoc: "", switches: [], options: {})
114
+ Gen.add_rpc_method(self.class, method_name, service_name, PB::Procedure.new(name: procedure_name, parameters: params, has_return_type: return_type != nil, return_type: return_type != nil ? return_type : "", attributes: attributes, documentation: xmldoc), *switches, **options)
115
115
  end
116
116
  end
117
-
117
+
118
118
  ##
119
119
  # Base class for service-defined class types.
120
120
  class ClassBase
121
121
  include Doc::SuffixMethods
122
122
  include Streaming::StreamConstructors
123
-
123
+
124
124
  attr_reader :client, :remote_oid
125
-
125
+
126
126
  def self.krpc_name
127
127
  name[11..-1]
128
128
  end
129
-
129
+
130
130
  def initialize(client, remote_oid)
131
131
  @client, @remote_oid = client, remote_oid
132
132
  end
133
-
133
+
134
134
  alias_method :eql?, :==
135
135
  def ==(other)
136
136
  other.class == self.class and other.remote_oid == remote_oid
@@ -138,15 +138,15 @@ module KRPC
138
138
  def hash
139
139
  remote_oid.hash
140
140
  end
141
-
141
+
142
142
  def to_s
143
143
  "#<#{self.class} @remote_oid=#{remote_oid}>"
144
144
  end
145
-
145
+
146
146
  def inspect
147
147
  "#<#{self.class} ".green + "@remote_oid" + "=".green + remote_oid.to_s.bold.blue + ">".green
148
148
  end
149
149
  end
150
-
151
- end
150
+
151
+ end
152
152
  end
@@ -8,7 +8,7 @@ module KRPC
8
8
  protobuf_module.constants.map do |name|
9
9
  [package + "." + name.to_s, protobuf_module.const_get(name,false)]
10
10
  end.to_h
11
- end
11
+ end
12
12
  end
13
13
 
14
14
  module Decoder
@@ -19,7 +19,7 @@ module KRPC
19
19
  send(meth_name, bytes)
20
20
  end
21
21
 
22
- # based on: https://developers.google.com/protocol-buffers/docs/encoding#varints & http://www.rubydoc.info/gems/ruby-protocol-buffers/1.0.1/ProtocolBuffers/Varint#decode-class_method & https://github.com/google/protobuf/blob/master/python/google/protobuf/internal/decoder.py#L136
22
+ # based on: https://developers.google.com/protocol-buffers/docs/encoding#varints & http://www.rubydoc.info/gems/ruby-protocol-buffers/1.0.1/ProtocolBuffers/Varint#decode-class_method & https://github.com/google/protobuf/blob/master/python/google/protobuf/internal/decoder.py#L136
23
23
  def decode_varint(bytes)
24
24
  decode_varint_pos(bytes)[0]
25
25
  end
@@ -37,16 +37,16 @@ module KRPC
37
37
  end
38
38
  end
39
39
  def decode_signed_varint(bytes)
40
- result = decode_varint(bytes)
40
+ result = decode_varint(bytes)
41
41
  result -= (1 << 64) if result > 0x7fffffffffffffff
42
42
  result
43
43
  end
44
-
44
+
45
45
  alias_method :decode_int32, :decode_signed_varint
46
46
  alias_method :decode_int64, :decode_signed_varint
47
47
  alias_method :decode_uint32, :decode_varint
48
48
  alias_method :decode_uint64, :decode_varint
49
-
49
+
50
50
  # based on: https://github.com/ruby-protobuf/protobuf/search?q=pack
51
51
  def decode_float(bytes)
52
52
  bytes.unpack('e').first
@@ -65,10 +65,10 @@ module KRPC
65
65
  size, pos = decode_varint_pos(bytes)
66
66
  bytes[pos..(pos+size)].bytes
67
67
  end
68
-
68
+
69
69
  end
70
70
  end
71
-
71
+
72
72
  module Encoder
73
73
  class << self
74
74
  def encode(value, type)
@@ -76,7 +76,7 @@ module KRPC
76
76
  raise(RuntimeError, "Unsupported type #{type}") unless respond_to?(meth_name)
77
77
  send(meth_name, value)
78
78
  end
79
-
79
+
80
80
  # based on: http://www.rubydoc.info/gems/ruby-protocol-buffers/1.0.1/ProtocolBuffers/Varint#decode-class_method & https://github.com/google/protobuf/blob/master/python/google/protobuf/internal/encoder.py#L390
81
81
  def encode_varint(value)
82
82
  return [value].pack('C') if value < 0b1000_0000
@@ -99,12 +99,12 @@ module KRPC
99
99
  raise(RangeError, "Value must be non-negative, got #{value}") if value < 0
100
100
  encode_varint(value)
101
101
  end
102
-
102
+
103
103
  alias_method :encode_int32, :encode_signed_varint
104
104
  alias_method :encode_int64, :encode_signed_varint
105
105
  alias_method :encode_uint32, :encode_nonnegative_varint
106
106
  alias_method :encode_uint64, :encode_nonnegative_varint
107
-
107
+
108
108
  def encode_float(value)
109
109
  [value].pack('e')
110
110
  end
@@ -122,9 +122,9 @@ module KRPC
122
122
  size = encode_varint(value.size)
123
123
  size + value.map(&:chr).join.b
124
124
  end
125
-
125
+
126
126
  end
127
127
  end
128
-
128
+
129
129
  end
130
130
  end