ul-wukong 4.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +60 -0
  3. data/.gitmodules +6 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +19 -0
  6. data/.yardopts +6 -0
  7. data/CHANGELOG.md +7 -0
  8. data/Gemfile +17 -0
  9. data/Guardfile +12 -0
  10. data/LICENSE.md +95 -0
  11. data/NOTES-travis.md +31 -0
  12. data/README-old.md +422 -0
  13. data/README.md +1308 -0
  14. data/Rakefile +28 -0
  15. data/TODO.md +99 -0
  16. data/bin/cutc +30 -0
  17. data/bin/cuttab +5 -0
  18. data/bin/greptrue +6 -0
  19. data/bin/md5sort +20 -0
  20. data/bin/setcat +11 -0
  21. data/bin/tabchar +5 -0
  22. data/bin/uniq-ord +59 -0
  23. data/bin/uniqc +3 -0
  24. data/bin/wu +34 -0
  25. data/bin/wu-clean-encoding +31 -0
  26. data/bin/wu-date +13 -0
  27. data/bin/wu-datetime +13 -0
  28. data/bin/wu-hist +3 -0
  29. data/bin/wu-lign +186 -0
  30. data/bin/wu-local +4 -0
  31. data/bin/wu-plus +9 -0
  32. data/bin/wu-source +5 -0
  33. data/bin/wu-sum +31 -0
  34. data/diagrams/wu_local.dot +39 -0
  35. data/diagrams/wu_local.dot.png +0 -0
  36. data/examples/Gemfile +38 -0
  37. data/examples/README.md +9 -0
  38. data/examples/basic/string_reverser.rb +23 -0
  39. data/examples/basic/tiny_count.rb +8 -0
  40. data/examples/basic/word_count/accumulator.rb +26 -0
  41. data/examples/basic/word_count/tokenizer.rb +13 -0
  42. data/examples/basic/word_count/word_count.rb +6 -0
  43. data/examples/dataflow/scraper_macro_flow.rb +28 -0
  44. data/examples/deploy_pack/Gemfile +6 -0
  45. data/examples/deploy_pack/README.md +6 -0
  46. data/examples/deploy_pack/a/b/c/.gitkeep +0 -0
  47. data/examples/deploy_pack/app/processors/string_reverser.rb +5 -0
  48. data/examples/deploy_pack/config/environment.rb +1 -0
  49. data/examples/dsl/dataflow/fibonacci_series.rb +101 -0
  50. data/examples/dsl/dataflow/scraper_macro_flow.rb +28 -0
  51. data/examples/dsl/dataflow/simple.rb +12 -0
  52. data/examples/dsl/dataflow/telegram.rb +45 -0
  53. data/examples/dsl/workflow/cherry_pie.dot +97 -0
  54. data/examples/dsl/workflow/cherry_pie.md +104 -0
  55. data/examples/dsl/workflow/cherry_pie.png +0 -0
  56. data/examples/dsl/workflow/cherry_pie.rb +101 -0
  57. data/examples/empty/.gitkeep +0 -0
  58. data/examples/examples_helper.rb +9 -0
  59. data/examples/geo.rb +4 -0
  60. data/examples/geo/geo_grids.numbers +0 -0
  61. data/examples/geo/geolocated.rb +331 -0
  62. data/examples/geo/quadtile.rb +69 -0
  63. data/examples/geo/spec/geolocated_spec.rb +247 -0
  64. data/examples/geo/tile_fetcher.rb +77 -0
  65. data/examples/graph/implied_geolocation/README.md +63 -0
  66. data/examples/graph/minimum_spanning_tree/airfares_graphviz.rb +73 -0
  67. data/examples/improver/tweet_summary.rb +73 -0
  68. data/examples/loadable.rb +2 -0
  69. data/examples/munging/airline_flights/airline_flights.rake +83 -0
  70. data/examples/munging/airline_flights/airplane.rb +0 -0
  71. data/examples/munging/airline_flights/airport_id_unification.rb +129 -0
  72. data/examples/munging/airline_flights/airport_ok_chars.rb +4 -0
  73. data/examples/munging/airline_flights/indexable.rb +75 -0
  74. data/examples/munging/airline_flights/indexable_spec.rb +90 -0
  75. data/examples/munging/airline_flights/reconcile_airports.rb +142 -0
  76. data/examples/munging/airline_flights/tasks.rake +83 -0
  77. data/examples/munging/airline_flights/topcities.rb +167 -0
  78. data/examples/munging/geo/geo_json.rb +54 -0
  79. data/examples/munging/geo/geo_models.rb +69 -0
  80. data/examples/munging/geo/geonames_models.rb +107 -0
  81. data/examples/munging/geo/iso_codes.rb +172 -0
  82. data/examples/munging/geo/reconcile_countries.rb +124 -0
  83. data/examples/munging/geo/tasks.rake +71 -0
  84. data/examples/munging/wikipedia/articles/extract_articles-parsed.rb +79 -0
  85. data/examples/munging/wikipedia/articles/extract_articles-templated.rb +136 -0
  86. data/examples/munging/wikipedia/articles/textualize_articles.rb +54 -0
  87. data/examples/munging/wikipedia/articles/verify_structure.rb +43 -0
  88. data/examples/munging/wikipedia/articles/wp2txt-LICENSE.txt +22 -0
  89. data/examples/munging/wikipedia/articles/wp2txt_article.rb +259 -0
  90. data/examples/munging/wikipedia/articles/wp2txt_utils.rb +452 -0
  91. data/examples/munging/wikipedia/dbpedia/dbpedia_common.rb +5 -0
  92. data/examples/munging/wikipedia/dbpedia/dbpedia_extract_geocoordinates.rb +78 -0
  93. data/examples/munging/wikipedia/dbpedia/extract_links-cruft.rb +66 -0
  94. data/examples/munging/wikipedia/dbpedia/extract_links.rb +260 -0
  95. data/examples/munging/wikipedia/dbpedia/sameas_extractor.rb +20 -0
  96. data/examples/rake_helper.rb +97 -0
  97. data/examples/ruby_project/Gemfile +6 -0
  98. data/examples/ruby_project/README.md +6 -0
  99. data/examples/ruby_project/a/b/c/.gitkeep +0 -0
  100. data/examples/server_logs/geo_ip_mapping/munge_geolite.rb +82 -0
  101. data/examples/server_logs/logline.rb +95 -0
  102. data/examples/server_logs/models.rb +66 -0
  103. data/examples/server_logs/page_counts.pig +48 -0
  104. data/examples/server_logs/server_logs-01-parse-script.rb +13 -0
  105. data/examples/server_logs/server_logs-02-histograms-full.rb +33 -0
  106. data/examples/server_logs/server_logs-02-histograms-mapper.rb +14 -0
  107. data/examples/server_logs/server_logs-03-breadcrumbs-full.rb +71 -0
  108. data/examples/server_logs/server_logs-04-page_page_edges-full.rb +40 -0
  109. data/examples/serverlogs/geo_ip_mapping/munge_geolite.rb +82 -0
  110. data/examples/serverlogs/models/logline.rb +102 -0
  111. data/examples/serverlogs/parser/apache_parser_widget.rb +46 -0
  112. data/examples/serverlogs/visit_paths/common.rb +4 -0
  113. data/examples/serverlogs/visit_paths/page_counts.pig +48 -0
  114. data/examples/serverlogs/visit_paths/serverlogs-01-parse-script.rb +11 -0
  115. data/examples/serverlogs/visit_paths/serverlogs-02-histograms-full.rb +31 -0
  116. data/examples/serverlogs/visit_paths/serverlogs-02-histograms-mapper.rb +12 -0
  117. data/examples/serverlogs/visit_paths/serverlogs-03-breadcrumbs-full.rb +67 -0
  118. data/examples/serverlogs/visit_paths/serverlogs-04-page_page_edges-full.rb +38 -0
  119. data/examples/splitter.rb +94 -0
  120. data/examples/string_reverser.rb +7 -0
  121. data/examples/text/pig_latin/pig_latinizer.rb +35 -0
  122. data/examples/text/pig_latin/pig_latinizer_widget.rb +16 -0
  123. data/examples/text/regional_flavor/README.md +14 -0
  124. data/examples/text/regional_flavor/article_wordbags.pig +39 -0
  125. data/examples/text/regional_flavor/j01-article_wordbags.rb +4 -0
  126. data/examples/text/regional_flavor/simple_pig_script.pig +27 -0
  127. data/examples/twitter.rb +5 -0
  128. data/lib/hanuman.rb +36 -0
  129. data/lib/hanuman/graph.rb +97 -0
  130. data/lib/hanuman/graphvizzer.rb +206 -0
  131. data/lib/hanuman/graphvizzer/gv_models.rb +161 -0
  132. data/lib/hanuman/graphvizzer/gv_presenter.rb +97 -0
  133. data/lib/hanuman/link.rb +35 -0
  134. data/lib/hanuman/registry.rb +46 -0
  135. data/lib/hanuman/stage.rb +128 -0
  136. data/lib/hanuman/tree.rb +67 -0
  137. data/lib/wu/geo.rb +4 -0
  138. data/lib/wu/geo/geo_grids.numbers +0 -0
  139. data/lib/wu/geo/geolocated.rb +331 -0
  140. data/lib/wu/geo/quadtile.rb +69 -0
  141. data/lib/wu/graph/union_find.rb +62 -0
  142. data/lib/wu/model/reconcilable.rb +63 -0
  143. data/lib/wu/munging.rb +71 -0
  144. data/lib/wu/social/models/twitter.rb +31 -0
  145. data/lib/wu/wikipedia/models.rb +20 -0
  146. data/lib/wukong.rb +54 -0
  147. data/lib/wukong/dataflow.rb +43 -0
  148. data/lib/wukong/doc_helpers.rb +14 -0
  149. data/lib/wukong/doc_helpers/dataflow_handler.rb +29 -0
  150. data/lib/wukong/doc_helpers/field_handler.rb +91 -0
  151. data/lib/wukong/doc_helpers/processor_handler.rb +29 -0
  152. data/lib/wukong/driver.rb +214 -0
  153. data/lib/wukong/driver/event_machine_driver.rb +15 -0
  154. data/lib/wukong/driver/wiring.rb +68 -0
  155. data/lib/wukong/local.rb +42 -0
  156. data/lib/wukong/local/runner.rb +96 -0
  157. data/lib/wukong/local/stdio_driver.rb +104 -0
  158. data/lib/wukong/logger.rb +102 -0
  159. data/lib/wukong/model/faker.rb +136 -0
  160. data/lib/wukong/model/flatpack_parser/flat.rb +60 -0
  161. data/lib/wukong/model/flatpack_parser/flatpack.rb +4 -0
  162. data/lib/wukong/model/flatpack_parser/lang.rb +46 -0
  163. data/lib/wukong/model/flatpack_parser/parser.rb +55 -0
  164. data/lib/wukong/model/flatpack_parser/tokens.rb +130 -0
  165. data/lib/wukong/plugin.rb +48 -0
  166. data/lib/wukong/processor.rb +110 -0
  167. data/lib/wukong/rake_helper.rb +6 -0
  168. data/lib/wukong/runner.rb +169 -0
  169. data/lib/wukong/runner/boot_sequence.rb +123 -0
  170. data/lib/wukong/runner/code_loader.rb +52 -0
  171. data/lib/wukong/runner/command_runner.rb +44 -0
  172. data/lib/wukong/runner/deploy_pack_loader.rb +75 -0
  173. data/lib/wukong/runner/help_message.rb +42 -0
  174. data/lib/wukong/source.rb +33 -0
  175. data/lib/wukong/source/source_driver.rb +74 -0
  176. data/lib/wukong/source/source_runner.rb +38 -0
  177. data/lib/wukong/spec_helpers.rb +74 -0
  178. data/lib/wukong/spec_helpers/integration_tests.rb +150 -0
  179. data/lib/wukong/spec_helpers/integration_tests/integration_test_matchers.rb +207 -0
  180. data/lib/wukong/spec_helpers/integration_tests/integration_test_runner.rb +97 -0
  181. data/lib/wukong/spec_helpers/shared_examples.rb +22 -0
  182. data/lib/wukong/spec_helpers/unit_tests.rb +135 -0
  183. data/lib/wukong/spec_helpers/unit_tests/unit_test_driver.rb +132 -0
  184. data/lib/wukong/spec_helpers/unit_tests/unit_test_matchers.rb +169 -0
  185. data/lib/wukong/spec_helpers/unit_tests/unit_test_runner.rb +60 -0
  186. data/lib/wukong/version.rb +3 -0
  187. data/lib/wukong/widget/echo.rb +55 -0
  188. data/lib/wukong/widget/extract.rb +122 -0
  189. data/lib/wukong/widget/filters.rb +452 -0
  190. data/lib/wukong/widget/logger.rb +56 -0
  191. data/lib/wukong/widget/operators.rb +82 -0
  192. data/lib/wukong/widget/reducers.rb +10 -0
  193. data/lib/wukong/widget/reducers/accumulator.rb +73 -0
  194. data/lib/wukong/widget/reducers/bin.rb +368 -0
  195. data/lib/wukong/widget/reducers/count.rb +73 -0
  196. data/lib/wukong/widget/reducers/group.rb +128 -0
  197. data/lib/wukong/widget/reducers/group_concat.rb +98 -0
  198. data/lib/wukong/widget/reducers/improver.rb +71 -0
  199. data/lib/wukong/widget/reducers/join_xml.rb +37 -0
  200. data/lib/wukong/widget/reducers/moments.rb +72 -0
  201. data/lib/wukong/widget/reducers/sort.rb +180 -0
  202. data/lib/wukong/widget/reducers/uniq.rb +91 -0
  203. data/lib/wukong/widget/serializers.rb +317 -0
  204. data/lib/wukong/widget/utils.rb +46 -0
  205. data/lib/wukong/widgets.rb +7 -0
  206. data/spec/examples/dataflow/fibonacci_series_spec.rb +18 -0
  207. data/spec/examples/dataflow/parse_apache_logs_spec.rb +8 -0
  208. data/spec/examples/dataflow/parsing_spec.rb +14 -0
  209. data/spec/examples/dataflow/simple_spec.rb +34 -0
  210. data/spec/examples/dataflow/telegram_spec.rb +43 -0
  211. data/spec/examples/graph/minimum_spanning_tree_spec.rb +34 -0
  212. data/spec/examples/munging/airline_flights/identifiers_spec.rb +16 -0
  213. data/spec/examples/munging/airline_flights_spec.rb +202 -0
  214. data/spec/examples/text/pig_latin_spec.rb +18 -0
  215. data/spec/examples/workflow/cherry_pie_spec.rb +36 -0
  216. data/spec/hanuman/graph_spec.rb +119 -0
  217. data/spec/hanuman/hanuman_spec.rb +10 -0
  218. data/spec/hanuman/registry_spec.rb +123 -0
  219. data/spec/hanuman/stage_spec.rb +81 -0
  220. data/spec/hanuman/tree_spec.rb +119 -0
  221. data/spec/spec.opts +1 -0
  222. data/spec/spec_helper.rb +43 -0
  223. data/spec/support/example_test_helpers.rb +95 -0
  224. data/spec/support/hanuman_test_helpers.rb +92 -0
  225. data/spec/support/integration_helper.rb +38 -0
  226. data/spec/support/model_test_helpers.rb +115 -0
  227. data/spec/support/shared_context_for_graphs.rb +57 -0
  228. data/spec/support/shared_context_for_reducers.rb +37 -0
  229. data/spec/support/shared_examples_for_builders.rb +94 -0
  230. data/spec/support/shared_examples_for_shortcuts.rb +57 -0
  231. data/spec/wu/model/reconcilable_spec.rb +152 -0
  232. data/spec/wukong/dataflow_spec.rb +87 -0
  233. data/spec/wukong/driver_spec.rb +154 -0
  234. data/spec/wukong/local/runner_spec.rb +29 -0
  235. data/spec/wukong/local/stdio_driver_spec.rb +73 -0
  236. data/spec/wukong/local_spec.rb +6 -0
  237. data/spec/wukong/logger_spec.rb +49 -0
  238. data/spec/wukong/model/faker_spec.rb +132 -0
  239. data/spec/wukong/processor_spec.rb +21 -0
  240. data/spec/wukong/runner_spec.rb +132 -0
  241. data/spec/wukong/source_spec.rb +6 -0
  242. data/spec/wukong/widget/extract_spec.rb +101 -0
  243. data/spec/wukong/widget/filters_spec.rb +79 -0
  244. data/spec/wukong/widget/logger_spec.rb +23 -0
  245. data/spec/wukong/widget/operators_spec.rb +25 -0
  246. data/spec/wukong/widget/reducers/bin_spec.rb +92 -0
  247. data/spec/wukong/widget/reducers/count_spec.rb +11 -0
  248. data/spec/wukong/widget/reducers/group_spec.rb +21 -0
  249. data/spec/wukong/widget/reducers/join_xml_spec.rb +25 -0
  250. data/spec/wukong/widget/reducers/moments_spec.rb +36 -0
  251. data/spec/wukong/widget/reducers/sort_spec.rb +26 -0
  252. data/spec/wukong/widget/reducers/uniq_spec.rb +14 -0
  253. data/spec/wukong/widget/serializers_spec.rb +114 -0
  254. data/spec/wukong/widget/sink_spec.rb +19 -0
  255. data/spec/wukong/widget/source_spec.rb +65 -0
  256. data/spec/wukong/wu-local_spec.rb +109 -0
  257. data/spec/wukong/wu-source_spec.rb +32 -0
  258. data/spec/wukong/wu_spec.rb +14 -0
  259. data/spec/wukong/wukong_spec.rb +10 -0
  260. data/wukong.gemspec +35 -0
  261. metadata +465 -0
@@ -0,0 +1,55 @@
1
+ module Wukong
2
+ module FlatPack
3
+ class Parser
4
+ attr_accessor :re
5
+ attr_accessor :lang
6
+
7
+ def initialize(lang)
8
+ @lang = lang
9
+ @re = re_from_language @lang
10
+ end
11
+
12
+ # returns true if the supplied string is in the parser's language
13
+ def string_in_lang? str
14
+ return (not (str =~ @re).nil?)
15
+ end
16
+
17
+ # Creates a regular expression from the
18
+ # supplied language
19
+ def re_from_language lang
20
+ regex = "^"
21
+ lang.each do |token|
22
+ regex += "(#{token.re})"
23
+ end
24
+ regex += "$"
25
+ return Regexp.new(regex)
26
+ end
27
+
28
+ def parse(str,trim=false)
29
+ return nil unless string_in_lang? str
30
+ result = []
31
+ str.match(@re)[1..-1].each_with_index do |val,index|
32
+ token = lang[index].translate(val)
33
+ if trim and token.is_a?(String)
34
+ token.strip!
35
+ end
36
+ result << token
37
+ end
38
+ return result - [:ignore]
39
+ end
40
+
41
+ def file_to_tsv(in_filename,out_filename,trim=true)
42
+ infile = File.open(in_filename,'r')
43
+ outfile = File.open(out_filename,'a')
44
+ infile.each_line do |line|
45
+ outfile.write(line_to_tsv(line,trim))
46
+ end
47
+ end
48
+
49
+ def line_to_tsv(line,trim=true)
50
+ fields = parse(line,trim)
51
+ return fields.join("\t") + "\n"
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,130 @@
1
+ module Wukong
2
+ module FlatPack
3
+ module Tokens
4
+ TOKEN_CLASSES = {}
5
+
6
+ def self.token_for_indicator(indicator)
7
+ return TOKEN_CLASSES[indicator].new
8
+ end
9
+
10
+ class Token
11
+ attr_accessor :position
12
+ attr_accessor :length
13
+ attr_accessor :indicator
14
+
15
+ def self.indicator= indicator
16
+ TOKEN_CLASSES[indicator] = self
17
+ @indicator = indicator
18
+ end
19
+ end
20
+
21
+ class FixedPointToken < Token
22
+ attr_accessor :power
23
+ attr_accessor :strict
24
+
25
+ self.indicator = 'D'
26
+
27
+ #TODO: Allow negative powers
28
+ def re
29
+ strict ? "(?:(?:\\+|-)\\d{#{@length-1}}|\\d{#{@length}})" : ".{#{@length}}"
30
+ end
31
+
32
+ def translate str
33
+ return nil if str.strip == ""
34
+ base = str.to_f
35
+ return base / (10**@power)
36
+ end
37
+ end
38
+
39
+ class BasicToken < Token
40
+ attr_accessor :modifier
41
+
42
+ def re token= '.'
43
+ if not @length.nil?
44
+ return "#{token}{#{@length}}"
45
+ elsif not @modifier.nil?
46
+ return "#{token}#{@modifier}"
47
+ else
48
+ return token
49
+ end
50
+ end
51
+
52
+ end
53
+
54
+ class IntToken < BasicToken
55
+ self.indicator = 'i'
56
+ RE = '(?:\+|-)?\\d'
57
+
58
+ def re
59
+ if not @length.nil?
60
+ return "(?:(?:\\+|-)\\d{#{@length-1}}|\\d{#{@length}})"
61
+ elsif not @modifier.nil?
62
+ return "#{RE}#{@modifier}"
63
+ else
64
+ return RE
65
+ end
66
+ end
67
+
68
+ def translate str
69
+ return Integer(str)
70
+ rescue ArgumentError => err
71
+ return str.to_i
72
+ end
73
+ end
74
+
75
+ class StringToken < BasicToken
76
+ self.indicator = 's'
77
+
78
+ def translate str
79
+ return str
80
+ end
81
+ end
82
+
83
+ class FloatToken < BasicToken
84
+ self.indicator = 'f'
85
+ #TODO: Implement floats
86
+
87
+ def get_re
88
+ #TODO: Implement
89
+ end
90
+
91
+ def translate
92
+ #TODO: Implement
93
+ end
94
+ end
95
+
96
+ class BoolToken < BasicToken
97
+ self.indicator = 'b'
98
+ TRUE_TOKENS = ['t','y','1']
99
+ FALSE_TOKENS = ['f','n','0']
100
+
101
+ #TODO: Add back multi-char options and think through allowing padding
102
+ #TODO: Allow users to override true and false
103
+
104
+ def re
105
+ return "(?:#{(TRUE_TOKENS + TRUE_TOKENS.map {|c| c.upcase} +
106
+ FALSE_TOKENS + FALSE_TOKENS.map{|c| c.upcase}).join('|')})"
107
+ end
108
+
109
+ def translate str
110
+ if TRUE_TOKENS.include?(str.downcase)
111
+ return true
112
+ elsif FALSE_TOKENS.include?(str.downcase)
113
+ return false
114
+ else
115
+ return nil
116
+ end
117
+ end
118
+ end
119
+
120
+ class IgnoreToken < BasicToken
121
+ self.indicator = '_'
122
+
123
+ # ignore symbols are removed from the final output
124
+ def translate str
125
+ return :ignore
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,48 @@
1
+ module Wukong
2
+
3
+ # An array of known plugins.
4
+ PLUGINS = []
5
+
6
+ # Asks each loaded plugin to configure the given +settings+ for the
7
+ # given +program_name+.
8
+ #
9
+ # @param [Configliere::Param] settings the settings to be configured by each plugin
10
+ # @param [String] program_name the name of the currently executing program
11
+ def self.configure_plugins(settings, program_name)
12
+ PLUGINS.each do |plugin|
13
+ plugin.configure(settings, program_name) if plugin.respond_to?(:configure)
14
+ end
15
+ end
16
+
17
+ # Asks each loaded plugin to boot itself from the given +settings+
18
+ # in the given +root+ directory.
19
+ #
20
+ # @param [Configliere::Param] settings the settings for each plugin to boot from
21
+ # @param [String] root the root directory the plugins are booting in
22
+ def self.boot_plugins(settings, root)
23
+ PLUGINS.each do |plugin|
24
+ plugin.boot(settings, root) if plugin.respond_to?(:boot)
25
+ end
26
+ end
27
+
28
+ # Include this module in your own class or module to have it
29
+ # register itself as a Wukong plugin.
30
+ #
31
+ # Your class or module must define the following methods:
32
+ #
33
+ # * `configure` called with a (pre-resolved) Configliere::Param argument and the basename of the running program
34
+ # * `boot` called with a (resolved) Configliere::Param argument and the current working directory of the running program, reacts to any settings as necessary
35
+ #
36
+ # Subclasses of Wukong::Runner will automatically load and boot each
37
+ # plugin.
38
+ module Plugin
39
+ # :nodoc:
40
+ def self.included mod
41
+ PLUGINS << mod unless PLUGINS.include?(mod)
42
+ end
43
+ end
44
+
45
+ end
46
+
47
+
48
+
@@ -0,0 +1,110 @@
1
+ module Wukong
2
+ class ProcessorBuilder < Hanuman::StageBuilder
3
+ def namespace(*args)
4
+ args.first.is_a?(Class) ? args.first : Wukong::Processor
5
+ end
6
+ end
7
+
8
+ # The Processor is the basic unit of computation in Wukong. A
9
+ # processor can be thought of as an arbitrary function that takes
10
+ # certain inputs and produces certain (or no) outputs.
11
+ #
12
+ # A Processor can be written and tested purely in Ruby and on your
13
+ # local machine. You can glue processors together
14
+ class Processor < Hanuman::Stage
15
+ include Logging
16
+
17
+ field :action, Whatever, :doc => false
18
+
19
+ class << self
20
+
21
+ def description desc=nil
22
+ @description = desc if desc
23
+ @description
24
+ end
25
+
26
+ def configure(settings)
27
+ settings.description = description if description
28
+ fields.each_pair do |name, field|
29
+ next if field.doc == false || field.doc.to_s == 'false'
30
+ next if [:log].include?(name)
31
+ field_props = {}.tap do |props|
32
+ props[:description] = field.doc unless field.doc == "#{name} field"
33
+ field_type = (field.type.respond_to?(:product) ? field.type.product : field.type)
34
+ configliere_type = case field_type
35
+ when String then nil
36
+ when TrueClass, FalseClass then :boolean
37
+ else field_type
38
+ end
39
+
40
+ props[:type] = configliere_type if configliere_type
41
+ props[:default] = field.default if field.default
42
+ end
43
+ existing_value = settings[name]
44
+ settings.define(name, field_props)
45
+ settings[name] = existing_value unless existing_value.nil?
46
+ end
47
+ end
48
+
49
+ end
50
+
51
+ # When instantiated with a block, the block will replace this
52
+ # method.
53
+ #
54
+ # @param [Array<Object>] args
55
+ # @yield record a record that might be yielded by the block
56
+ # @yieldparam [Object] record the yielded record
57
+ def perform_action(*args)
58
+ end
59
+
60
+ # :nodoc:
61
+ #
62
+ # The action attribute is turned into the perform action method.
63
+ #
64
+ # @param [Proc] action
65
+ def receive_action(action)
66
+ self.define_singleton_method(:perform_action, &action)
67
+ end
68
+
69
+ # This method is called after the processor class has been
70
+ # instantiated but before any records are given to it to process.
71
+ #
72
+ # Override this method in your subclass.
73
+ def setup
74
+ end
75
+
76
+ # This method is called once per record.
77
+ #
78
+ # Override this method in your subclass.
79
+ #
80
+ # @param [Object] record
81
+ # @yield record the record you want to yield
82
+ # @yieldparam [Object] record the yielded record
83
+ def process(record, &emit)
84
+ yield record
85
+ end
86
+
87
+ # This method is called to signal the last record has been
88
+ # received but that further processing may still be done, events
89
+ # still be yielded, &c.
90
+ #
91
+ # This can be used within an aggregating processor (like a reducer
92
+ # in a map/reduce job) to start processing the final aggregate of
93
+ # records since the "last record" has already been received.
94
+ #
95
+ # Override this method in your subclass
96
+ #
97
+ # @yield record the record you want to yield
98
+ # @yieldparam [Object] record the yielded record
99
+ def finalize
100
+ end
101
+
102
+ # This method is called after all records have been passed. It
103
+ # signals that processing should stop.
104
+ #
105
+ # Override this method in your subclass.
106
+ def stop
107
+ end
108
+
109
+ end
110
+ end
@@ -0,0 +1,6 @@
1
+ require 'rake'
2
+ require 'wukong'
3
+
4
+ task :environment => [] do
5
+ Wukong::Runner.run
6
+ end
@@ -0,0 +1,169 @@
1
+ require_relative("runner/code_loader")
2
+ require_relative("runner/deploy_pack_loader")
3
+ require_relative("runner/boot_sequence")
4
+ require_relative("runner/command_runner")
5
+
6
+ module Wukong
7
+
8
+ # A base class which handles
9
+ #
10
+ # * requiring any necessary code like deploy packs or code from command-line arguments
11
+ # * having all plugins configure settings as necessary
12
+ # * resolving settings
13
+ # * having all plugins boot from now resolved settings
14
+ # * parsing command-line arguments
15
+ # * instantiating and handing over control to a driver which runs the actual code
16
+ class Runner
17
+
18
+ include Logging
19
+ include CodeLoader
20
+ include DeployPackLoader
21
+ include BootSequence
22
+ include CommandRunner
23
+
24
+ # The settings object that will be configured and booted from.
25
+ # All plugins will configure this object.
26
+ attr_accessor :settings
27
+
28
+ # Create a new Runner with the given +settings+.
29
+ #
30
+ # Uses an empty Configliere::Param object if no +settings+ are
31
+ # given.
32
+ #
33
+ # @param [Configliere::Param] settings
34
+ def initialize settings=Configliere::Param.new
35
+ self.settings = settings
36
+ end
37
+
38
+ # Instantiates a new Runner and boot it up.
39
+ #
40
+ # Will rescue any Wukong::Error with a logged error message and
41
+ # exit.
42
+ def self.run(settings=Configliere::Param.new)
43
+ begin
44
+ new(settings).boot!
45
+ rescue Wukong::Error => e
46
+ die(e.message, 127)
47
+ end
48
+ end
49
+
50
+ # The parsed command-line arguments.
51
+ #
52
+ # Will raise an error if +boot+ hasn't been called yet.
53
+ #
54
+ # @return [Array<String>]
55
+ def args
56
+ settings.rest
57
+ end
58
+
59
+ # The root directory we should consider ourselves to be running
60
+ # in.
61
+ #
62
+ # Defaults to the root directory of a deploy pack if we're running
63
+ # inside one, else just returns `Dir.pwd`.
64
+ #
65
+ # @return [String]
66
+ def root
67
+ in_deploy_pack? ? deploy_pack_dir : Dir.pwd
68
+ end
69
+
70
+ # Convenience method for setting the usage message of a Runner.
71
+ #
72
+ # @param [String, nil] msg set the usage message
73
+ # @return [String] the usage message
74
+ def self.usage msg=nil
75
+ return @usage unless msg
76
+ @usage = msg
77
+ end
78
+
79
+ # Convenience method for setting the description message of a Runner.
80
+ #
81
+ # @param [String, nil] msg set the description message
82
+ # @return [String] the description message
83
+ def self.description msg=nil
84
+ return @description unless msg
85
+ @description = msg
86
+ end
87
+
88
+ # Kill this process with the given error `message` and exit
89
+ # `code`.
90
+ #
91
+ # @param [String] message
92
+ # @param [Integer] code.
93
+ def self.die(message=nil, code=127)
94
+ log.error(message) if message
95
+ exit(code)
96
+ end
97
+
98
+ # Return the name of the program this Runner is running.
99
+ #
100
+ # This is passed to plugins which can configure settings
101
+ # appropriately. Defaults to the name of the currently running
102
+ # process.
103
+ #
104
+ # @return [String]
105
+ def program_name
106
+ @program_name || File.basename($0)
107
+ end
108
+
109
+ # Explicitly set the name of the program this Runner is running.
110
+ #
111
+ # This is useful for unit tests in which the name of the currently
112
+ # running process may be different from the runner command being
113
+ # tested (`rspec` vs. `wu-local`).
114
+ #
115
+ # @param [String] name
116
+ def program_name= name
117
+ @program_name = name
118
+ end
119
+
120
+ # Return the usage message for this runner.
121
+ #
122
+ # @return [String] the usage message
123
+ def usage
124
+ ["usage: #{program_name} [ --param=val | --param | -p val | -p ]", self.class.usage].compact.join(' ')
125
+ end
126
+
127
+ # Return the description text for this runner.
128
+ #
129
+ # @return [String] the description text
130
+ def description
131
+ self.class.description
132
+ end
133
+
134
+ # Is there a processor or dataflow registered with the given
135
+ # `name`?
136
+ #
137
+ # @param [String] name
138
+ # @return [true, false]
139
+ def registered? name
140
+ name && Wukong.registry.registered?(name.to_sym)
141
+ end
142
+
143
+ # Retrieve the dataflow registered under a given `name`.
144
+ #
145
+ # @param [String,Symbol] name
146
+ # @return [Wukong::Processor, Wukong::Dataflow, nil]
147
+ def dataflow_class_for(name)
148
+ builder = (Wukong.registry.retrieve(name.to_sym) or return)
149
+ builder.for_class
150
+ end
151
+
152
+ # Is the given `name` a registered as a processor?
153
+ #
154
+ # @param [String,Symbol] name
155
+ # @return [true, false]
156
+ def processor?(name)
157
+ registered?(name) && dataflow_class_for(name).ancestors.include?(Wukong::Processor)
158
+ end
159
+
160
+ # Is the given `name` a registered as a dataflow?
161
+ #
162
+ # @param [String,Symbol] name
163
+ # @return [true, false]
164
+ def dataflow?(name)
165
+ registered?(name) && dataflow_class_for(name).ancestors.include?(Wukong::Dataflow)
166
+ end
167
+
168
+ end
169
+ end