yay 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/data/yay/dna.yay ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/yay
2
+
3
+ # Latin characters used DNA and RNA sequences have poor legibility. This script
4
+ # helps with that by giving each character a distinct colour, vaguely similar
5
+ # to the florescent dyes used during sequencing.
6
+ #
7
+ # The colours I have chosen were taken from [1] and work best on a black screen.
8
+ # This will have to do until AmbiScript [2] characters have been given Unicode
9
+ # codepoints!
10
+ #
11
+ # 1. http://biology.kenyon.edu/courses/biol114/Chap08/Chapter_08a.html
12
+ # 2. http://www.biotechniques.com/multimedia/archive/00003/BTN_A_000112727_O_3654a.pdf
13
+
14
+ "a" is @adenine
15
+ "g" is @guanine
16
+ "c" is @cytosine
17
+ "t" is @thymine
18
+ "u" is @uracil
19
+
20
+ @adenine is green
21
+ @cytosine is blue
22
+ @guanine is yellow
23
+ @thymine is red
24
+ @uracil is @thymine
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/yay
2
+
3
+ # Matches common filenames and extensions so that you can quickly discover
4
+ # information about a directory structure using ls or tree
5
+
6
+ /\b\w*\.(png|gif|jpg|jpeg|svg)\b/i is @image
7
+ /\b\w*\.(txt|html|md|css)\b/i is @doc
8
+ /\b\w*\.(xml|json|properties|manifest)\b/i is @config
9
+
10
+ /\b\w*\.(sh|bash|yay)\b/i is @shell
11
+ /\b\w*\.(php|py|rb|pl|r|pm|ssi|shtml)\b/i is @script
12
+ /\b\w*\.(java|scala)\b/i is @jvm
13
+ /\b\w*\.(h|c|cc|cpp|cxx)\b/i is @c
14
+
15
+ @image is @resource
16
+ @config is @resource
17
+ @doc is @resource
18
+ @resource is green
19
+
20
+ @shell is dim
21
+ @jvm is red
22
+ @c is blue
23
+ @script is magenta
@@ -50,7 +50,8 @@ class Yay
50
50
  begin
51
51
 
52
52
  @parser = Yay::Parser.new
53
- @parser.allow_all = true
53
+ # allow restricted commands such as installation of new scripts
54
+ @parser.allow_restricted = true
54
55
  @parser.parse_array(@args)
55
56
  @rules = @parser.get_rules
56
57
 
@@ -0,0 +1,31 @@
1
+ require 'yay/rule_set'
2
+
3
+ class Yay
4
+ class Autoformatter
5
+
6
+ # the colour wheel can be retrieved for unit testing, etc
7
+ attr_reader :wheel
8
+
9
+ def initialize
10
+ @wheel = [
11
+ [Yay::ColourWheel::FG[:red], Yay::ColourWheel::MISC[:bold]],
12
+ [Yay::ColourWheel::FG[:green], Yay::ColourWheel::MISC[:bold]],
13
+ [Yay::ColourWheel::FG[:blue], Yay::ColourWheel::MISC[:bold]],
14
+ ]
15
+ @index = 0
16
+ end
17
+
18
+ def get_rules strings
19
+ raise ArgumentError, "Cannot be an empty array" unless
20
+ strings.class == Array and strings.length > 0
21
+ ruleset = Yay::RuleSet.new
22
+ strings.each { |string|
23
+ colour = @wheel[@index % @wheel.length]
24
+ ruleset.add_match [string], colour, false
25
+ # iterate the index
26
+ @index += 1
27
+ }
28
+ return ruleset.get_rules
29
+ end
30
+ end
31
+ end
@@ -17,6 +17,7 @@ class Yay
17
17
  :invert => 7, #alias
18
18
  :inverted => 7, #alias
19
19
  :underscored => 4, #alias
20
+ :bold => 1, #alias
20
21
  }
21
22
 
22
23
  # foreground colours
@@ -10,6 +10,12 @@ class Yay
10
10
  colourize_rules rules
11
11
  @input = input
12
12
  @output = output
13
+
14
+ # this is the colour we'll use when we haven't already applied a colour
15
+ # we remember the previous choice as we can colourize words within a line
16
+ # that's already been coloured
17
+ @default_end_colour = ColourWheel::end_colour
18
+ @end_colour = @default_end_colour
13
19
  end
14
20
 
15
21
  # get the rules that are applied to a whole line if it contains matching text
@@ -46,39 +52,45 @@ class Yay
46
52
  end
47
53
  }
48
54
  end
49
-
55
+
56
+ # apply all rules that span the entire line
57
+ def apply_line_rules line
58
+ # track the line_rules end colour so we can return to this after each
59
+ # match
60
+ @line_rules.each { |rule|
61
+ if line.match(rule[0])
62
+ line = "#{rule[1]}#{line.rstrip}#{@default_end_colour}"
63
+ @end_colour = rule[1]
64
+ # leave loop; only allow one line match per line
65
+ break
66
+ end
67
+ }
68
+ return line
69
+ end
70
+
71
+ # apply all partial rules
72
+ def apply_word_rules line
73
+ @part_rules.each { |rule|
74
+ line = line.gsub(rule[0], "#{rule[1]}\\0#{@end_colour}")
75
+ }
76
+ return line
77
+ end
78
+
50
79
  # create a pipe between the input and output streams, applying colour rules
51
80
  # to every line that appears. only an interrupt, end of file or exception
52
81
  # will end this process
53
82
  def colourize_pipe
54
- # this is the colour we'll use when we haven't already applied a colour
55
- # we remember the previous colour as we can colourize words within a line
56
- # that's already been coloured
57
- default_end_colour = ColourWheel::end_colour
58
-
59
- @input.each_line { |line|
60
-
61
- # track the line_rules end colour so we can return to this after each
62
- # match
63
- end_colour = default_end_colour
64
-
65
- # apply all line rules
66
- @line_rules.each { |rule|
67
- if line.match(rule[0])
68
- line = "#{rule[1]}#{line.rstrip}#{default_end_colour}"
69
- end_colour = rule[1]
70
- # leave loop; only allow one line match per line
71
- break
72
- end
83
+ @end_colour = @default_end_colour
84
+ begin
85
+ @input.each_line { |line|
86
+ line = apply_line_rules(line)
87
+ line = apply_word_rules(line)
88
+ @output.puts line
73
89
  }
74
-
75
- # apply all partial rules
76
- @part_rules.each { |rule|
77
- line.gsub!(rule[0], "#{rule[1]}\\0#{end_colour}")
78
- }
79
-
80
- @output.puts line
81
- }
90
+ rescue Errno::EPIPE
91
+ # ignore the Broken Pipe error that is caused by tools like head
92
+ end
82
93
  end
94
+
83
95
  end
84
96
  end
data/lib/yay/loader.rb CHANGED
@@ -18,7 +18,7 @@ class Yay
18
18
  def parse_string string, context_name
19
19
  # invoke a new parser and return the rules it finds
20
20
  parser = Yay::Parser.new context_name
21
- parser.allow_include = true
21
+ parser.allow_include = true
22
22
  return parser.parse string
23
23
  end
24
24
 
data/lib/yay/parser.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'yay/parser_gen'
2
2
  require 'yay/lexer'
3
3
  require 'yay/colour_wheel'
4
+ require 'yay/autoformatter'
4
5
  require 'yay/rule_set'
5
6
  require 'yay/loader'
6
7
  require 'yay/installer'
@@ -15,20 +16,12 @@ class Yay
15
16
  # ruby code
16
17
  class Parser < Yay::ParserGen
17
18
 
18
- # allow this parser to load the default rule file if the rule body is empty
19
- attr_accessor :allow_default
20
-
21
- # allow this parser to install new rule files from remote sources. beware
22
- # the consequences. this should be allowed from the command line only!
23
- attr_accessor :allow_install
24
-
25
- # allow this parser to include rule files that are installed. including
26
- # ones in the gem folders, globally and locally
27
- attr_accessor :allow_include
28
-
29
- # allow this parser to search for and print out all the rules that are
30
- # installed
31
- attr_accessor :allow_list
19
+ # restricted mode is on by default. this prevents people from doing things
20
+ # like installing new files from within files
21
+ attr_accessor :allow_restricted
22
+
23
+ # a subset of restricted mode which allows include files
24
+ attr_accessor :allow_include
32
25
 
33
26
  # set to true to signal to the application or parent parser that it's time
34
27
  # to shut down
@@ -39,6 +32,23 @@ class Yay
39
32
  @lexer.context_name = context_name
40
33
  end
41
34
 
35
+ # tries to include a file if it exists; otherwise autoformats
36
+ def include_or_autoformat strings
37
+ begin
38
+ include_file strings[0].source
39
+ rescue Yay::CouldntFindFileError
40
+ # autoformat if we're on the commandline otherwise this doesn't make
41
+ # sense
42
+ autoformat strings if @allow_restricted
43
+ end
44
+ end
45
+
46
+ # takes strings without colours and automatically assigns one to each
47
+ def autoformat strings
48
+ autoformatter = Yay::Autoformatter.new
49
+ @ruleset.merge autoformatter.get_rules strings
50
+ end
51
+
42
52
  # load a file from a url
43
53
  def include_file filename
44
54
  raise NotAllowedError.new "include #{filename}", current_position unless @allow_include
@@ -49,7 +59,7 @@ class Yay
49
59
 
50
60
  # install a file from a url
51
61
  def install_file file_name, url
52
- raise NotAllowedError.new "install #{url}", current_position unless @allow_install
62
+ raise NotAllowedError.new "install #{url}", current_position unless @allow_restricted
53
63
  installer = Yay::Installer.new file_name, url
54
64
  installer.install
55
65
  @shutdown = true
@@ -57,21 +67,26 @@ class Yay
57
67
 
58
68
  # print the full list of yay files
59
69
  def list_installed
60
- raise NotAllowedError.new "list installed yay files", current_position unless @allow_list
70
+ raise NotAllowedError.new "list installed yay files", current_position unless @allow_restricted
61
71
  lister = Yay::Lister.new
62
72
  lister.print
63
73
  @shutdown = true
64
74
  end
65
75
 
66
76
  # allow all parser actions
67
- def allow_all= value
68
- @allow_default = @allow_install = @allow_include = @allow_list = value
77
+ def allow_restricted= value
78
+ @allow_include = @allow_restricted = value
69
79
  end
70
80
 
81
+ # allow the including of files
82
+ def allow_include= value
83
+ @allow_include = value
84
+ end
85
+
71
86
  # load the default file. used when the commandline is empty
72
87
  def use_default_file
73
88
  # don't throw an error in this case. it's legitimate for a file to be empty
74
- return unless @allow_default
89
+ return unless @allow_restricted
75
90
  loader = Yay::Loader.default_file_loader
76
91
  loader.load
77
92
  @ruleset.merge loader.get_rules
@@ -108,6 +123,8 @@ class Yay
108
123
  # for lack of a better function, this will take a string like "/abc/" and
109
124
  # transform it in to a regex object
110
125
  def string_to_regex string, escape=true
126
+ # extract the constituent part of the regexp. in ruby 1.8 we can't just
127
+ # create a regex from a perl-style regex literal like /abc/i
111
128
  matches = /\/([^\/\\\r\n]*(?:\\.[^\/\\\r\n]*)*)\/([a-z]\b)*/.match string
112
129
  return nil if matches[1].nil?
113
130
  content = matches[1]
@@ -15,76 +15,76 @@ module_eval(<<'...end grammar.y/module_eval...', 'grammar.y', 54)
15
15
  ##### State transition tables begin ###
16
16
 
17
17
  racc_action_table = [
18
- -7, -25, 33, 17, 25, 17, -25, 12, 22, 22,
19
- 33, 20, 30, 9, 10, 12, 2, 4, 8, 9,
20
- 10, 12, 28, 36, 31, 29, 29, 19, -2, 17,
21
- 38, 29, 38 ]
18
+ -4, -7, -25, 4, 27, 26, -25, 12, 20, 25,
19
+ 20, 21, 2, 4, 8, 9, 11, 12, 4, 21,
20
+ 9, 11, 12, 31, 34, 21, 32, 32, 18, 17,
21
+ 38, 32, 38, 21 ]
22
22
 
23
23
  racc_action_check = [
24
- 7, 6, 21, 17, 10, 9, 6, 21, 6, 7,
25
- 23, 4, 19, 23, 23, 23, 0, 0, 0, 0,
26
- 0, 0, 18, 24, 20, 18, 24, 3, 2, 1,
27
- 27, 29, 35 ]
24
+ 7, 10, 6, 19, 18, 17, 6, 19, 6, 11,
25
+ 10, 7, 0, 0, 0, 0, 0, 0, 24, 21,
26
+ 24, 24, 24, 22, 23, 9, 22, 23, 3, 2,
27
+ 30, 32, 33, 36 ]
28
28
 
29
29
  racc_action_pointer = [
30
- 14, 18, 28, 27, 9, nil, -1, 0, nil, -6,
31
- 2, nil, nil, nil, nil, nil, nil, -8, 17, 12,
32
- 22, 0, nil, 8, 18, nil, nil, 20, nil, 23,
33
- nil, nil, nil, nil, nil, 22, nil, nil, nil, nil,
30
+ 10, nil, 26, 28, nil, nil, -1, 0, nil, 14,
31
+ 1, 6, nil, nil, nil, nil, nil, 2, 4, 0,
32
+ nil, 8, 18, 19, 15, nil, nil, nil, nil, nil,
33
+ 20, nil, 23, 22, nil, nil, 22, nil, nil, nil,
34
34
  nil ]
35
35
 
36
36
  racc_action_default = [
37
- -5, -29, -20, -30, -30, -1, -19, -25, -4, -29,
38
- -30, -8, -21, -9, -10, -11, -12, -29, -30, -30,
39
- -30, -30, -24, -30, -30, -17, -28, -27, -14, -23,
40
- 41, -3, -18, -20, -6, -27, -16, -13, -26, -22,
37
+ -5, -12, -30, -30, -20, -1, -19, -29, -3, -29,
38
+ -25, -30, -21, -8, -9, -10, -11, -30, -30, -30,
39
+ -24, -29, -30, -30, -30, -17, -2, 41, -18, -28,
40
+ -27, -14, -23, -27, -16, -6, -29, -13, -26, -22,
41
41
  -15 ]
42
42
 
43
43
  racc_goto_table = [
44
- 5, 18, 27, 37, 21, 23, 32, 3, 35, 24,
45
- nil, 40, nil, 39, nil, nil, nil, 26, nil, nil,
46
- nil, nil, nil, 34 ]
44
+ 7, 5, 23, 30, 33, 19, 3, 37, nil, 24,
45
+ 40, nil, nil, 39, 29, nil, nil, nil, nil, 28,
46
+ nil, nil, nil, nil, 36, 35 ]
47
47
 
48
48
  racc_goto_check = [
49
- 2, 11, 12, 13, 4, 4, 10, 1, 12, 11,
50
- nil, 13, nil, 12, nil, nil, nil, 11, nil, nil,
51
- nil, nil, nil, 2 ]
49
+ 3, 2, 11, 12, 12, 5, 1, 13, nil, 5,
50
+ 13, nil, nil, 12, 11, nil, nil, nil, nil, 3,
51
+ nil, nil, nil, nil, 3, 2 ]
52
52
 
53
53
  racc_goto_pointer = [
54
- nil, 7, 0, nil, -2, nil, nil, nil, nil, nil,
55
- -15, 0, -16, -24, nil ]
54
+ nil, 6, 1, 0, nil, -1, nil, nil, nil, nil,
55
+ nil, -7, -19, -23, nil ]
56
56
 
57
57
  racc_goto_default = [
58
- nil, nil, nil, 7, nil, 11, 13, 14, 15, 16,
59
- 1, nil, nil, nil, 6 ]
58
+ nil, nil, nil, nil, 10, nil, 13, 14, 15, 16,
59
+ 1, 22, nil, nil, 6 ]
60
60
 
61
61
  racc_reduce_table = [
62
62
  0, 0, :racc_error,
63
63
  1, 13, :_reduce_1,
64
- 1, 13, :_reduce_2,
65
- 3, 13, :_reduce_3,
64
+ 3, 13, :_reduce_2,
65
+ 1, 13, :_reduce_3,
66
66
  1, 13, :_reduce_4,
67
67
  0, 13, :_reduce_5,
68
68
  3, 14, :_reduce_6,
69
69
  1, 14, :_reduce_7,
70
- 1, 15, :_reduce_none,
71
- 1, 15, :_reduce_none,
72
- 1, 15, :_reduce_none,
73
- 1, 15, :_reduce_none,
74
- 1, 15, :_reduce_none,
75
- 4, 17, :_reduce_13,
76
- 3, 18, :_reduce_14,
77
- 4, 19, :_reduce_15,
78
- 3, 20, :_reduce_16,
79
- 2, 21, :_reduce_17,
80
- 3, 22, :_reduce_18,
81
- 1, 22, :_reduce_19,
70
+ 1, 16, :_reduce_none,
71
+ 1, 16, :_reduce_none,
72
+ 1, 16, :_reduce_none,
73
+ 1, 16, :_reduce_none,
74
+ 1, 16, :_reduce_none,
75
+ 4, 18, :_reduce_13,
76
+ 3, 19, :_reduce_14,
77
+ 4, 20, :_reduce_15,
78
+ 3, 21, :_reduce_16,
79
+ 2, 22, :_reduce_17,
80
+ 3, 15, :_reduce_18,
81
+ 1, 15, :_reduce_19,
82
82
  1, 26, :_reduce_20,
83
83
  1, 26, :_reduce_21,
84
84
  2, 24, :_reduce_22,
85
85
  1, 24, :_reduce_23,
86
- 1, 16, :_reduce_24,
87
- 0, 16, :_reduce_25,
86
+ 1, 17, :_reduce_24,
87
+ 0, 17, :_reduce_25,
88
88
  1, 25, :_reduce_26,
89
89
  0, 25, :_reduce_27,
90
90
  2, 23, :_reduce_28,
@@ -97,8 +97,8 @@ racc_shift_n = 41
97
97
  racc_token_table = {
98
98
  false => 0,
99
99
  :error => 1,
100
- :literal => 2,
101
- :install => 3,
100
+ :install => 2,
101
+ :literal => 3,
102
102
  :list_installed => 4,
103
103
  :variable => 5,
104
104
  :include => 6,
@@ -131,8 +131,8 @@ Racc_arg = [
131
131
  Racc_token_to_s_table = [
132
132
  "$end",
133
133
  "error",
134
- "literal",
135
134
  "install",
135
+ "literal",
136
136
  "list_installed",
137
137
  "variable",
138
138
  "include",
@@ -144,6 +144,7 @@ Racc_token_to_s_table = [
144
144
  "$start",
145
145
  "body",
146
146
  "command_list",
147
+ "string_list",
147
148
  "command",
148
149
  "and_opt",
149
150
  "match",
@@ -151,7 +152,6 @@ Racc_token_to_s_table = [
151
152
  "substitution",
152
153
  "equivalence",
153
154
  "include_file",
154
- "string_list",
155
155
  "verbs_opt",
156
156
  "colour_list",
157
157
  "line_opt",
@@ -172,21 +172,21 @@ module_eval(<<'.,.,', 'grammar.y', 5)
172
172
 
173
173
  module_eval(<<'.,.,', 'grammar.y', 6)
174
174
  def _reduce_2(val, _values, result)
175
- include_file val[0]
175
+ install_file val[1], val[2]
176
176
  result
177
177
  end
178
178
  .,.,
179
179
 
180
180
  module_eval(<<'.,.,', 'grammar.y', 7)
181
181
  def _reduce_3(val, _values, result)
182
- install_file val[1], val[2]
182
+ list_installed
183
183
  result
184
184
  end
185
185
  .,.,
186
186
 
187
187
  module_eval(<<'.,.,', 'grammar.y', 8)
188
188
  def _reduce_4(val, _values, result)
189
- list_installed
189
+ include_or_autoformat val[0]
190
190
  result
191
191
  end
192
192
  .,.,
data/lib/yay/paths.rb CHANGED
@@ -20,13 +20,17 @@ class Yay
20
20
 
21
21
  # get all the paths where we might be able to find .yay files
22
22
  def yay_paths
23
- result = [local_yay_path,global_yay_path]
23
+ result = [current_path,local_yay_path,global_yay_path]
24
24
  gempaths.each { |v|
25
25
  result.push gempath_to_yaypath(v)
26
26
  }
27
27
  return result
28
28
  end
29
29
 
30
+ def current_path
31
+ return '.'
32
+ end
33
+
30
34
  def global_yay_path
31
35
  return '/etc/yay'
32
36
  end
data/lib/yay/version.rb CHANGED
@@ -3,7 +3,7 @@ require 'open3'
3
3
  class Yay
4
4
 
5
5
  # the gem version. increment to make a new release!
6
- VERSION = "0.0.7"
6
+ VERSION = "0.0.8"
7
7
 
8
8
  # yoinked from chef. this gives us the git revision number of the current
9
9
  # build if possible. used for --version
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yay
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 15
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 7
10
- version: 0.0.7
9
+ - 8
10
+ version: 0.0.8
11
11
  platform: ruby
12
12
  authors:
13
13
  - jon davey
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-22 00:00:00 +00:00
18
+ date: 2012-01-26 00:00:00 +00:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -31,12 +31,15 @@ files:
31
31
  - LICENSE
32
32
  - data/yay/log4x.yay
33
33
  - data/yay/syslog.yay
34
+ - data/yay/dna.yay
35
+ - data/yay/types.yay
34
36
  - data/yay/default.yay
35
37
  - lib/yay/parser_gen.rb
36
38
  - lib/yay/rule_set.rb
37
39
  - lib/yay/version.rb
38
40
  - lib/yay/lister.rb
39
41
  - lib/yay/lexer.rb
42
+ - lib/yay/autoformatter.rb
40
43
  - lib/yay/loader.rb
41
44
  - lib/yay/application.rb
42
45
  - lib/yay/colour_wheel.rb