tefil 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rdoc", "~> 3.12"
10
+ gem "bundler", "~> 1.1.3"
11
+ gem "jeweler", "~> 1.8.3"
12
+ gem "simplecov", ">= 0"
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 ippei94da
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = tefil
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to tefil
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 ippei94da. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,55 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "tefil"
18
+ gem.homepage = "http://github.com/ippei94da/tefil"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{Basic framework of text filter}
21
+ gem.description = %Q{This gem provides a framework of text filter.
22
+ Tefil eneable to make text filter commands which have overwrite option easily.
23
+ }
24
+ gem.email = "ippei94da@gmail.com"
25
+ gem.authors = ["ippei94da"]
26
+ # dependencies defined in Gemfile
27
+ end
28
+ Jeweler::RubygemsDotOrgTasks.new
29
+
30
+ require 'rake/testtask'
31
+ Rake::TestTask.new(:test) do |test|
32
+ test.libs << 'lib' << 'test'
33
+ test.pattern = 'test/**/test_*.rb'
34
+ test.verbose = true
35
+ end
36
+
37
+ #require 'rcov/rcovtask'
38
+ #Rcov::RcovTask.new do |test|
39
+ # test.libs << 'test'
40
+ # test.pattern = 'test/**/test_*.rb'
41
+ # test.verbose = true
42
+ # test.rcov_opts << '--exclude "gems/*"'
43
+ #end
44
+
45
+ task :default => :test
46
+
47
+ require 'rdoc/task'
48
+ Rake::RDocTask.new do |rdoc|
49
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
50
+
51
+ rdoc.rdoc_dir = 'rdoc'
52
+ rdoc.title = "tefil #{version}"
53
+ rdoc.rdoc_files.include('README*')
54
+ rdoc.rdoc_files.include('lib/**/*.rb')
55
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
data/bin/lineedit ADDED
@@ -0,0 +1,38 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+ #
4
+ # USAGE: textfilter [options] str0 str1 files ...
5
+
6
+ require "pp"
7
+ require "optparse"
8
+ require "rubygems"
9
+ gem "tefil"
10
+ require "tefil.rb"
11
+
12
+ ## option analysis
13
+ OPTIONS = {}
14
+ op = OptionParser.new
15
+ op.on("-s" , "--sort" , "Sort."){ OPTIONS[:sort] = true}
16
+ op.on("-r" , "--randomize" , "Randomize."){ OPTIONS[:random] = true}
17
+ op.on("-o" , "--overwrite" , "Overwrite."){ OPTIONS[:overwrite] = true}
18
+ op.parse!(ARGV)
19
+
20
+ module TextFilter
21
+ def self.process_stream(in_io, out_io)
22
+ in_io.each do |line|
23
+ if OPTIONS[:sort]
24
+ out_io.puts line.chomp.split(//).sort.join('')
25
+ end
26
+ if OPTIONS[:random]
27
+ out_io.puts line.chomp.split(//).sort_by{rand}.join('')
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ OPTIONS[:overwrite] ||= false
34
+
35
+
36
+ #pp OPTIONS[:overwrite]
37
+ #TextFilter.run(ARGV, OPTIONS[:overwrite])
38
+ TextFilter.run(ARGV, OPTIONS[:overwrite])
data/bin/textfilter ADDED
@@ -0,0 +1,34 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+ #
4
+ # USAGE: textfilter [options] str0 str1 files ...
5
+
6
+ require "pp"
7
+ require "optparse"
8
+ require "rubygems"
9
+ #gem "tefil"
10
+ require "tefil.rb"
11
+
12
+ ## option analysis
13
+ OPTIONS = {}
14
+ op = OptionParser.new
15
+ op.on("-o" , "--overwrite" , "Overwrite."){ OPTIONS[:overwrite] = true}
16
+ op.parse!(ARGV)
17
+
18
+ OLD_PATTERN = ARGV.shift
19
+ NEW_STR = ARGV.shift
20
+
21
+ module TextFilter
22
+ def self.process_stream(in_io, out_io)
23
+ in_io.each do |line|
24
+ out_io.puts line.sub(OLD_PATTERN, NEW_STR)
25
+ end
26
+ end
27
+ end
28
+
29
+ OPTIONS[:overwrite] ||= false
30
+
31
+
32
+ #pp OPTIONS[:overwrite]
33
+ #TextFilter.run(ARGV, OPTIONS[:overwrite])
34
+ TextFilter.run(ARGV, OPTIONS[:overwrite])
data/lib/tefil.rb ADDED
@@ -0,0 +1,82 @@
1
+ #! /usr/bin/env ruby
2
+ # coding: utf-8
3
+
4
+ require "tempfile"
5
+
6
+ # Module that provide a framework for text filters.
7
+ #
8
+ # Provide basic functions for a command like below:
9
+ # Input:
10
+ # - Without indicating filenames like "command.rb",
11
+ # input data is eaten from STDIN.
12
+ # - With indicating filenames like "command.rb *.txt",
13
+ # input data is eaten from indicated files in order.
14
+ # When the file which is in the filenames does not exist,
15
+ # output report to STDERR.
16
+ #
17
+ # Output:
18
+ # - Without indicating "--overwrite" option,
19
+ # output to STDOUT even from many files.
20
+ # - With indicating "--overwrite" option,
21
+ # - With indicating input files,
22
+ # overwrite each file.
23
+ # - Without indicating input files,
24
+ # output to STDOUT in one stream.
25
+ #
26
+ # If indicated file(s) not found,
27
+ # this program notify on stderr and does not throw an exception.
28
+ #
29
+ module TextFilter
30
+
31
+ class NotRedefinedMethodError < Exception; end
32
+ class TypeError < Exception; end
33
+
34
+ # 保持している入力ファイル対して、順に処理を実行する。
35
+ # filenames.size が 0 ならば STDIN からの入力を待つことになる。
36
+ #
37
+ # STDIN からの入力だった場合、出力先は必ず STDOUT になる。
38
+ # STDIN からの入力ではない、すなわちファイル入力であった場合、
39
+ # - overwrite_flag が false ならば STDOUT に出力する。
40
+ # - overwrite_flag が true ならば 個々のファイルに上書きする。
41
+ #
42
+ # Process of each file is defined in 'process_stream' method.
43
+ #
44
+ def self.run(filenames, overwrite_flag = false)
45
+ #p self; exit
46
+ if filenames.size == 0
47
+ self.process_stream( $stdin, $stdout )
48
+ else
49
+ #p filenames
50
+ filenames.each do |filename|
51
+ if overwrite_flag
52
+ tempfile = Tempfile.new("tefil", "/tmp")
53
+ File.open(filename, "r") do |input_file|
54
+ self.process_stream(input_file, tempfile)
55
+ end
56
+ tempfile.close
57
+ tempfile.open
58
+ File.open(filename, "w") do |output_file|
59
+ tempfile.each { |line| output_file.puts(line) }
60
+ end
61
+ else
62
+ File.open(filename, "r") do |input_file|
63
+ self.process_stream(input_file, $stdout)
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ private
71
+
72
+ # Process a file.
73
+ # An argument 'in_io' indicates an io (file handle) for input.
74
+ # Another argument 'out_io' indicates an io (file handle) for output.
75
+ # This method must be redefined in a subclass or be overridden.
76
+ # If not redefined, raise an exception TextFilter::NotRedefinedMethodError.
77
+ def self.process_stream(in_io, out_io)
78
+ raise NotRedefinedMethodError
79
+ end
80
+
81
+ end
82
+
data/test/helper.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'test/unit'
11
+
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'tefil'
15
+
16
+ class Test::Unit::TestCase
17
+ end
@@ -0,0 +1,147 @@
1
+ require 'helper'
2
+
3
+ require "test/unit"
4
+ require "tefil.rb"
5
+ require "stringio"
6
+ require "tempfile"
7
+ require "fileutils"
8
+
9
+ module TextFilter
10
+ def self.process_stream(in_file, out_file)
11
+ results = []
12
+ in_file.each do |line|
13
+ out_file.puts line.sub('a', 'A')
14
+ end
15
+ end
16
+ end
17
+
18
+ class TestTefil < Test::Unit::TestCase
19
+ TMP00 = "test/tmp00"
20
+ TMP01 = "test/tmp01"
21
+
22
+ def setup
23
+ FileUtils.rm TMP00 if File.exist? TMP00
24
+ FileUtils.rm TMP01 if File.exist? TMP01
25
+ File.open(TMP00, "w") do |io|
26
+ io.puts "abc"
27
+ io.puts "def"
28
+ end
29
+ File.open(TMP01, "w") do |io|
30
+ io.puts "abc"
31
+ io.puts "def"
32
+ io.puts "cab"
33
+ end
34
+ end
35
+
36
+ def teardown
37
+ FileUtils.rm TMP00 if File.exist? TMP00
38
+ FileUtils.rm TMP01 if File.exist? TMP01
39
+ end
40
+
41
+ def test_self_run
42
+ # Not found
43
+ assert_raise(Errno::ENOENT){ TextFilter.run([""]) }
44
+ assert_raise(Errno::ENOENT){ TextFilter.run([""], true) }
45
+
46
+ # ファイル指定なしで標準入力
47
+ $stdin = StringIO.new
48
+ $stdin.puts "abc"
49
+ $stdin.puts "def"
50
+ $stdin.rewind
51
+ # stdout
52
+ $stdout = StringIO.new
53
+ TextFilter.run([], false)
54
+ $stdout.rewind
55
+ t = $stdout.readlines
56
+ assert_equal([ "Abc\n", "def\n" ], t)
57
+ $stdout.close
58
+
59
+ $stdin = StringIO.new
60
+ $stdin.puts "abc"
61
+ $stdin.puts "def"
62
+ $stdin.rewind
63
+ $stdout = StringIO.new
64
+ TextFilter.run([], true)
65
+ $stdout.rewind
66
+ t = $stdout.readlines
67
+ assert_equal([ "Abc\n", "def\n" ], t)
68
+ $stdout.close
69
+
70
+ # 単数のファイルを指定。
71
+ # overwrite なし。
72
+ setup
73
+ $stdout = StringIO.new
74
+ TextFilter.run([TMP00])
75
+ $stdout.rewind
76
+ tmp = $stdout.readlines
77
+ assert_equal(["Abc\n", "def\n"], tmp)
78
+ $stdout.close
79
+ tmp = File.open(TMP00, "r").readlines
80
+ assert_equal(["abc\n", "def\n"], tmp)
81
+ tmp = File.open(TMP01, "r").readlines
82
+ assert_equal(["abc\n", "def\n", "cab\n"], tmp)
83
+
84
+ # overwrite あり
85
+ setup
86
+ $stdout = StringIO.new
87
+ TextFilter.run([TMP00], true)
88
+ $stdout.rewind
89
+ tmp = $stdout.readlines
90
+ assert_equal([], tmp)
91
+ $stdout.close
92
+ tmp = File.open(TMP00, "r").readlines
93
+ assert_equal(["Abc\n", "def\n"], tmp)
94
+ tmp = File.open(TMP01, "r").readlines
95
+ assert_equal(["abc\n", "def\n", "cab\n"], tmp)
96
+
97
+ # 複数のファイルを指定。
98
+ # overwrite なし。
99
+ setup
100
+ $stdout = StringIO.new
101
+ TextFilter.run([TMP00, TMP01])
102
+ $stdout.rewind
103
+ tmp = $stdout.readlines
104
+ assert_equal(["Abc\n", "def\n", "Abc\n", "def\n", "cAb\n"], tmp)
105
+ $stdout.close
106
+ tmp = File.open(TMP00, "r").readlines
107
+ assert_equal(["abc\n", "def\n"], tmp)
108
+ tmp = File.open(TMP01, "r").readlines
109
+ assert_equal(["abc\n", "def\n", "cab\n"], tmp)
110
+
111
+ # overwrite あり。
112
+ setup
113
+ $stdout = StringIO.new
114
+ TextFilter.run([TMP00, TMP01], true)
115
+ $stdout.rewind
116
+ stdout = $stdout.readlines
117
+ assert_equal([], stdout)
118
+ $stdout.close
119
+ tmp = File.open(TMP00, "r").readlines
120
+ assert_equal(["Abc\n", "def\n"], tmp)
121
+ tmp = File.open(TMP01, "r").readlines
122
+ assert_equal(["Abc\n", "def\n", "cAb\n"], tmp)
123
+
124
+ # グローバル変数の標準出力、標準入力を元に戻す。
125
+ $stdout = STDOUT
126
+ $stdin = STDIN
127
+ end
128
+
129
+ def test_textfilter_command
130
+ result = `echo "ab" | textfilter a A`
131
+ assert_equal("Ab\n", result)
132
+
133
+ setup
134
+ result = `textfilter a A #{TMP00}`
135
+ assert_equal("Abc\ndef\n", result)
136
+ str = File.open(TMP00, "r").read
137
+ assert_equal("abc\ndef\n", str)
138
+
139
+ setup
140
+ result = `textfilter -o a A #{TMP00}`
141
+ assert_equal("", result)
142
+ str = File.open(TMP00, "r").read
143
+ assert_equal("Abc\ndef\n", str)
144
+ end
145
+
146
+ end
147
+
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tefil
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ippei94da
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.12'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.12'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.1.3
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.1.3
46
+ - !ruby/object:Gem::Dependency
47
+ name: jeweler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.3
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.3
62
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: ! "This gem provides a framework of text filter.\n Tefil eneable to
79
+ make text filter commands which have overwrite option easily.\n "
80
+ email: ippei94da@gmail.com
81
+ executables:
82
+ - lineedit
83
+ - textfilter
84
+ extensions: []
85
+ extra_rdoc_files:
86
+ - LICENSE.txt
87
+ - README.rdoc
88
+ files:
89
+ - .document
90
+ - Gemfile
91
+ - LICENSE.txt
92
+ - README.rdoc
93
+ - Rakefile
94
+ - VERSION
95
+ - bin/lineedit
96
+ - bin/textfilter
97
+ - lib/tefil.rb
98
+ - test/helper.rb
99
+ - test/test_tefil.rb
100
+ homepage: http://github.com/ippei94da/tefil
101
+ licenses:
102
+ - MIT
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ segments:
114
+ - 0
115
+ hash: -1035133043
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 1.8.21
125
+ signing_key:
126
+ specification_version: 3
127
+ summary: Basic framework of text filter
128
+ test_files: []