lang 0.1.0.pre → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/README.rdoc +1 -1
  2. data/bin/lang +2 -145
  3. data/lib/lang/cli.rb +147 -0
  4. data/lib/lang/version.rb +1 -1
  5. metadata +30 -18
@@ -18,7 +18,7 @@ Language tags implementation.
18
18
  == INSTALLATION:
19
19
 
20
20
  $ gem in lang
21
- $ lang update
21
+ $ lang index -u
22
22
 
23
23
  == LICENSE:
24
24
 
data/bin/lang CHANGED
@@ -2,149 +2,6 @@
2
2
 
3
3
  dir = File.expand_path(File.dirname(__FILE__) + '/../lib')
4
4
  $:.unshift(dir) unless $:.include?(dir)
5
- require 'lang/subtags'
6
5
 
7
- require 'net/http'
8
- require 'tempfile'
9
-
10
- module Lang
11
- module Subtags
12
- class Registry
13
-
14
- NAME_REGEX = /^(?:#{SUBTAG}|#{TAG}):\s*([\w-]+)\s*$/io.freeze
15
- TYPE_REGEX = /^#{TYPE}:\s*(\w+)\s*$/io.freeze
16
-
17
- def initialize(path)
18
- @path = File.expand_path(path)
19
- end
20
-
21
- def exists?
22
- File.exists?("#{@path}.registry")
23
- end
24
-
25
- def download(uri)
26
- FileUtils.mkdir_p(File.dirname(@path)) unless exists?
27
- write("registry") { |temp| http(uri) { |chunk| temp << chunk }}
28
- end
29
-
30
- def build_indices
31
- return false unless exists?
32
-
33
- STDOUT << "Building indices\n"
34
- calculate_indices
35
- calculate_boundaries
36
-
37
- write("indices") do |temp|
38
- @boundaries.each do |boundary|
39
- template = "%-#{boundary[-2]}s%#{boundary[-1] - boundary[-2] - 1}d\n"
40
- @indices[boundary.first].to_a.sort.each { |k,v| temp << template % [k,v] }
41
- end
42
- end
43
-
44
- write("boundaries") do |temp|
45
- @boundaries.each do |boundary|
46
- temp << "#{boundary.join(":")}\n"
47
- end
48
- end
49
-
50
- STDOUT << "Done\n"
51
- true
52
- end
53
-
54
- private
55
-
56
- def write(dest, &block)
57
-
58
- path = "#{@path}.#{dest}"
59
- temp = Tempfile.new(dest)
60
- temp.binmode
61
- yield(temp) if block_given?
62
- temp.close
63
-
64
- # somewhat stolen from ActiveSupport
65
-
66
- begin
67
- old = File.stat(path)
68
- rescue Errno::ENOENT
69
- check = File.join(File.dirname(path), ".permissions_check.#{Thread.current.object_id}.#{Process.pid}.#{rand(1000000)}")
70
- File.open(check, File::WRONLY | File::CREAT) { }
71
- old = File.stat(check)
72
- File.unlink(check)
73
- end
74
-
75
- FileUtils.mv(temp.path, "#{@path}.#{dest}")
76
-
77
- File.chown(old.uid, old.gid, path)
78
- File.chmod(old.mode, path)
79
- nil
80
- end
81
-
82
- def http(uri)
83
- STDOUT << "Downloading #{uri}\n"
84
- Net::HTTP.get_response(URI(uri)) do |response|
85
- total, size = response['Content-Length'].to_i, 0
86
- response.read_body do |chunk|
87
- size += chunk.size
88
- yield(chunk) if block_given?
89
- STDOUT << "\r%d%% done (%d of %d)" % [size*100/total, size, total]
90
- STDOUT.flush
91
- end
92
- end
93
- STDOUT << "\n"
94
- nil
95
- end
96
-
97
- def calculate_boundaries
98
- calculate_indices unless @indices
99
- offset = 0
100
- @boundaries = @indices.keys.sort{ |a,b| a.to_s <=> b.to_s }.map do |kind|
101
- segment = @indices[kind]
102
- boundary = []
103
- boundary << kind
104
- boundary << offset
105
- boundary << segment.size - 1
106
- boundary << segment.keys.map{ |s| s.size }.max
107
- boundary << segment.values.max.to_s.size + boundary.last + 1
108
- offset += segment.size * boundary.last
109
- boundary
110
- end
111
- true
112
- end
113
-
114
- def calculate_indices
115
- count = 0
116
- kind, name = nil, nil
117
- @indices = {}
118
- File.open("#{@path}.registry", File::RDONLY) do |f|
119
- f.each_line do |l|
120
- if TYPE_REGEX === l
121
- kind = $1.to_sym
122
- @indices[kind] ||= {}
123
- elsif kind && NAME_REGEX === l
124
- name = $1.downcase
125
- @indices[kind][name] = count
126
- elsif l == SEPARATOR
127
- kind, name = nil, nil
128
- end
129
- count += l.size
130
- end
131
- end
132
- #STDOUT << "#{count}\n"
133
- true
134
- end
135
-
136
- end
137
- end
138
- end
139
-
140
- command = ARGV.shift
141
- unless %w(reindex update).include?(command)
142
- STDERR << "unknown command: #{command.inspect}\n"
143
- exit 1
144
- end
145
-
146
- registry = Lang::Subtags::Registry.new(ARGV.shift || Lang::Subtags.registry_path)
147
- registry.download("http://www.iana.org/assignments/language-subtag-registry") if command == 'update' || !registry.exists?
148
- registry.build_indices
149
-
150
- # EOF
6
+ require 'lang/cli'
7
+ Lang::Cli.start(ARGV)
@@ -0,0 +1,147 @@
1
+ require 'thor' unless defined?(Thor)
2
+ require 'lang/subtags'
3
+ require 'net/http'
4
+ require 'tempfile'
5
+
6
+ module Lang #:nodoc:
7
+ class Cli < Thor
8
+
9
+ class_option :quiet, :type => :boolean, :aliases => '-q', :desc => 'Run command queitly.'
10
+
11
+ desc 'index [options]', 'Prepare lang to use a language-subtag-registry.'
12
+ method_option :update, :type => :boolean, :aliases => '-u', :desc => 'Update registry.'
13
+
14
+ def index(path = Lang::Subtags.registry_path)
15
+ @path = File.expand_path(path)
16
+ if !exists? || options.update?
17
+ download 'http://www.iana.org/assignments/language-subtag-registry'
18
+ end
19
+ build_indices
20
+ end
21
+
22
+ NAME_REGEX = /^(?:#{Subtags::SUBTAG}|#{Subtags::TAG}):\s*([\w-]+)\s*$/io.freeze
23
+ TYPE_REGEX = /^#{Subtags::TYPE}:\s*(\w+)\s*$/io.freeze
24
+
25
+ private
26
+
27
+ def exists?
28
+ File.exists?("#{@path}.registry")
29
+ end
30
+
31
+ def download(uri)
32
+ FileUtils.mkdir_p(File.dirname(@path)) unless exists?
33
+ write("registry") { |temp| http(uri) { |chunk| temp << chunk }}
34
+ end
35
+
36
+ def build_indices
37
+ return false unless exists?
38
+
39
+ say "Building indices"
40
+ calculate_indices
41
+ calculate_boundaries
42
+
43
+ write("indices") do |temp|
44
+ @boundaries.each do |boundary|
45
+ template = "%-#{boundary[-2]}s%#{boundary[-1] - boundary[-2] - 1}d\n"
46
+ @indices[boundary.first].to_a.sort.each { |k,v| temp << template % [k,v] }
47
+ end
48
+ end
49
+
50
+ write("boundaries") do |temp|
51
+ @boundaries.each do |boundary|
52
+ temp << "#{boundary.join(":")}\n"
53
+ end
54
+ end
55
+
56
+ say "Done"
57
+ true
58
+ end
59
+
60
+ private
61
+
62
+ def say(*args)
63
+ super unless options.quiet?
64
+ end
65
+
66
+ def write(dest, &block)
67
+
68
+ path = "#{@path}.#{dest}"
69
+ temp = Tempfile.new(dest)
70
+ temp.binmode
71
+ yield(temp) if block_given?
72
+ temp.close
73
+
74
+ # somewhat stolen from ActiveSupport
75
+
76
+ begin
77
+ old = File.stat(path)
78
+ rescue Errno::ENOENT
79
+ check = File.join(File.dirname(path), ".permissions_check.#{Thread.current.object_id}.#{Process.pid}.#{rand(1000000)}")
80
+ File.open(check, File::WRONLY | File::CREAT) { }
81
+ old = File.stat(check)
82
+ File.unlink(check)
83
+ end
84
+
85
+ FileUtils.mv(temp.path, "#{@path}.#{dest}")
86
+
87
+ File.chown(old.uid, old.gid, path)
88
+ File.chmod(old.mode, path)
89
+ nil
90
+ end
91
+
92
+ def http(uri)
93
+ say "Downloading #{uri}"
94
+ Net::HTTP.get_response(URI(uri)) do |response|
95
+ total, size = response['Content-Length'].to_i, 0
96
+ response.read_body do |chunk|
97
+ size += chunk.size
98
+ yield(chunk) if block_given?
99
+ say "\r%d%% done (%d of %d)" % [size*100/total, size, total], nil, false
100
+ STDOUT.flush unless options.quiet?
101
+ end
102
+ end
103
+ say "\n"
104
+ nil
105
+ end
106
+
107
+ def calculate_boundaries
108
+ calculate_indices unless @indices
109
+ offset = 0
110
+ @boundaries = @indices.keys.sort{ |a,b| a.to_s <=> b.to_s }.map do |kind|
111
+ segment = @indices[kind]
112
+ boundary = []
113
+ boundary << kind
114
+ boundary << offset
115
+ boundary << segment.size - 1
116
+ boundary << segment.keys.map{ |s| s.size }.max
117
+ boundary << segment.values.max.to_s.size + boundary.last + 1
118
+ offset += segment.size * boundary.last
119
+ boundary
120
+ end
121
+ true
122
+ end
123
+
124
+ def calculate_indices
125
+ count = 0
126
+ kind, name = nil, nil
127
+ @indices = {}
128
+ File.open("#{@path}.registry", File::RDONLY) do |f|
129
+ f.each_line do |l|
130
+ if TYPE_REGEX === l
131
+ kind = $1.to_sym
132
+ @indices[kind] ||= {}
133
+ elsif kind && NAME_REGEX === l
134
+ name = $1.downcase
135
+ @indices[kind][name] = count
136
+ elsif l == Subtags::SEPARATOR
137
+ kind, name = nil, nil
138
+ end
139
+ count += l.size
140
+ end
141
+ end
142
+ #STDOUT << "#{count}\n"
143
+ true
144
+ end
145
+
146
+ end
147
+ end
@@ -1,5 +1,5 @@
1
1
  module Lang #:nodoc:
2
- VERSION = "0.1.0.pre".freeze
2
+ VERSION = "0.1.0".freeze
3
3
  end
4
4
 
5
5
  # EOF
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lang
3
3
  version: !ruby/object:Gem::Version
4
- hash: 961915980
5
- prerelease: true
4
+ hash: 27
5
+ prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
9
  - 0
10
- - pre
11
- version: 0.1.0.pre
10
+ version: 0.1.0
12
11
  platform: ruby
13
12
  authors:
14
13
  - SSDany
@@ -16,25 +15,39 @@ autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2010-09-06 00:00:00 +04:00
18
+ date: 2010-11-05 00:00:00 +03:00
20
19
  default_executable: lang
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
23
- name: rspec
24
22
  prerelease: false
25
- requirement: &id001 !ruby/object:Gem::Requirement
23
+ type: :runtime
24
+ name: thor
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
26
26
  none: false
27
27
  requirements:
28
28
  - - ">="
29
29
  - !ruby/object:Gem::Version
30
- hash: 31
30
+ hash: 3
31
31
  segments:
32
- - 1
33
- - 2
34
32
  - 0
35
- version: 1.2.0
33
+ version: "0"
34
+ requirement: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ prerelease: false
36
37
  type: :development
37
- version_requirements: *id001
38
+ name: rspec
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 15
45
+ segments:
46
+ - 2
47
+ - 0
48
+ - 0
49
+ version: 2.0.0
50
+ requirement: *id002
38
51
  description: |
39
52
  Language tags implementation.
40
53
  Features: basic and extended filtering (RFC 4647), canonicalization (using IANA language subtag registry).
@@ -48,6 +61,7 @@ extra_rdoc_files:
48
61
  - README.rdoc
49
62
  files:
50
63
  - README.rdoc
64
+ - lib/lang/cli.rb
51
65
  - lib/lang/subtags/entry.rb
52
66
  - lib/lang/subtags/extlang.rb
53
67
  - lib/lang/subtags/grandfathered.rb
@@ -89,14 +103,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
89
103
  required_rubygems_version: !ruby/object:Gem::Requirement
90
104
  none: false
91
105
  requirements:
92
- - - ">"
106
+ - - ">="
93
107
  - !ruby/object:Gem::Version
94
- hash: 25
108
+ hash: 3
95
109
  segments:
96
- - 1
97
- - 3
98
- - 1
99
- version: 1.3.1
110
+ - 0
111
+ version: "0"
100
112
  requirements: []
101
113
 
102
114
  rubyforge_project: lang