maprename 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b113974592630108fa3d0063ab010d20857e85fd80781fbf6c00de7ef7fb1d76
4
- data.tar.gz: 83f7fc257838d469896ed9d3fa568ffae88a087635840705c207e97cab5b86bc
3
+ metadata.gz: 5413ed3a8f4a8882a5fcfe94b180039542076848e1f5b5fd4783900b535d21c6
4
+ data.tar.gz: 1dddd6568a6e81bcd18fe3f05351d6adc6f0d47c1fb56825d3c442c2ff45a272
5
5
  SHA512:
6
- metadata.gz: '08c13702e2e2d94a766ad23c08fe1eb9e416fca04cb89709c92942380d9ff37f7a565c6a27acf59d874ced1e0b90680ba9684be184ef695f8ba40bade24aa712'
7
- data.tar.gz: 18d43c7b2f84951f384a16346633da9185807be18697edc7634a00152c00dd47051f6353549df40db916459fb482ecb784c493a266add774831e6333a6f2146a
6
+ metadata.gz: 8186efbee034f623b47946737c0b10d6fe5220e34e29ef4e499cc79c3a1c5f26d9c57b3041400527d03b4f76afdad56045edf44afe63af64ac116fd22e129f38
7
+ data.tar.gz: 5f91247b950189b99f8692593ea9ab4a748f214aac6eba647aa75c015473469934bfa4f6a64e64cc92b1ea4ee9b5e09d5fc35a368da4164ba98b5d3486f330e0
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- maprename (0.1.0)
4
+ maprename (0.1.1)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -17,7 +17,7 @@ GEM
17
17
  diff-lcs (1.3)
18
18
  i18n (1.6.0)
19
19
  concurrent-ruby (~> 1.0)
20
- minitest (5.12.2)
20
+ minitest (5.13.0)
21
21
  rake (12.3.3)
22
22
  rspec (3.8.0)
23
23
  rspec-core (~> 3.8.0)
data/README.md CHANGED
@@ -111,12 +111,14 @@ YAML 是一种常用的配置文件格式,它以不同层次的缩进来表示
111
111
 
112
112
  See also: Official YAML specification: https://yaml.org/spec/1.2/spec.html
113
113
 
114
- ## Involve with Regular Expression
114
+ ## Getting involved in Regular Expression
115
115
 
116
116
  正则表达式30分钟入门教程: https://deerchao.cn/tutorials/regex/regex.htm
117
117
 
118
118
  文中,在学习过程中使用一个 Windows 上的正则表达式测试器来帮助理解,Mac 上可以使用这个 Web 版的正则表达式测试器: https://deerchao.cn/tools/wegester/
119
119
 
120
+ Learn Regex the Easy Way: https://github.com/ziishaned/learn-regex/blob/master/translations/README-cn.md
121
+
120
122
  ## License
121
123
 
122
124
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -8,15 +8,18 @@ module Maprename
8
8
  @config = YAML.load(IO.read(config_file)).with_indifferent_access
9
9
  end
10
10
 
11
- def run!(dry = false)
11
+ def run!(opts)
12
12
  input_files.each do |file|
13
13
  file = File.join(@config[:input][:directory], file)
14
- Maprename::Renamer.new(file, @config).rename!(dry)
14
+ Maprename::Renamer.new(file, @config).rename!(opts)
15
15
  end
16
16
  end
17
17
 
18
18
  def input_files
19
- Dir.children(@config[:input][:directory]).grep(Regexp.new(@config[:input][:pattern]))
19
+ files = Dir.children(@config[:input][:directory]).grep(Regexp.new(@config[:input][:pattern]))
20
+ debug 'Matched input files:'
21
+ debug files.map { |f| ' %s' % f}
22
+ files
20
23
  end
21
24
  end
22
25
  end
@@ -1,6 +1,12 @@
1
1
  require 'optparse'
2
2
  require 'maprename/app'
3
3
 
4
+ module Kernel
5
+ def debug(msg)
6
+ puts msg if $debug
7
+ end
8
+ end
9
+
4
10
  module Maprename
5
11
  class Cli
6
12
  def initialize
@@ -20,6 +26,11 @@ module Maprename
20
26
  @options[:dry] = true
21
27
  end
22
28
 
29
+ opts.on("-D", "--debug", "debug") do
30
+ @options[:debug] = true
31
+ $debug = true
32
+ end
33
+
23
34
  opts.on("-h", "--help", "Prints this help") do
24
35
  puts opts
25
36
  exit
@@ -39,7 +50,8 @@ module Maprename
39
50
  puts @raw_options
40
51
  exit 1
41
52
  end
42
- Maprename::App.new(config_file).run!(@options[:dry])
53
+ debug "CLI Options: #{@options}"
54
+ Maprename::App.new(config_file).run!(@options)
43
55
  end
44
56
  end
45
57
  end
@@ -7,11 +7,15 @@ module Maprename
7
7
  @config = config
8
8
  @file = file
9
9
  @content = read_file_content(file)
10
+
11
+ debug " ContentParser: #@config"
10
12
  end
11
13
 
12
14
  def parse!(context)
13
15
  config[:fields].each do |field_definition|
16
+ debug " ContentParser field definition: #{field_definition}"
14
17
  fields = @content.match(Regexp.new(field_definition[:pattern])).to_a[1..-1]
18
+ debug " ContentParser fields: #{fields}"
15
19
  eval_var_definition(fields, field_definition, context)
16
20
  end
17
21
  end
@@ -19,6 +23,8 @@ module Maprename
19
23
  def eval_var_definition(fields, field_definition, context)
20
24
  value = field_definition[:value].gsub(/\$(\d+)/, fields[$1.to_i - 1])
21
25
  context.instance_eval "self.#{field_definition[:name]} = #{value.inspect}"
26
+ debug " ContentParser value: #{value}"
27
+ debug " ContentParser context: #{context}"
22
28
  end
23
29
 
24
30
  def read_file_content(file)
@@ -2,24 +2,33 @@ module Maprename
2
2
  class FileNameParser
3
3
  def initialize(config)
4
4
  @config = config
5
+ debug " FileNameParser: #@config"
5
6
  end
6
7
 
7
8
  def parse!(subject, context)
9
+ debug " FileNameParser parse subject: #{subject}"
10
+ debug " FileNameParser parse context: #{context}"
8
11
  fields = if split_method?
9
12
  subject.split(Regexp.new(@config[:pattern]))
10
13
  else
11
14
  subject.match(Regexp.new(@config[:pattern])).to_a[1..-1]
12
15
  end
13
16
 
17
+ debug " FileNameParser fields: #{fields}"
14
18
  @config[:fields].each do |field_definition|
15
19
  eval_var_definition(fields, field_definition, context)
16
20
  end
17
21
  end
18
22
 
19
23
  def eval_var_definition(fields, field_definition, context)
24
+ debug " FileNameParser field definition: #{field_definition}"
25
+ debug " FileNameParser context: #{context}"
26
+
20
27
  value = field_definition[:value].gsub(/\$(\d+)/) { fields[$1.to_i - 1] }
21
28
  context.instance_eval "self.#{field_definition[:name]} = #{value.inspect}"
22
29
 
30
+ debug " FileNameParser value: #{value}"
31
+ debug " FileNameParser context: #{context}"
23
32
  if parse_config = field_definition[:name_parse]
24
33
  Maprename::FileNameParser.new(parse_config).parse!(context.instance_eval(field_definition[:name]), context)
25
34
  end
@@ -4,15 +4,20 @@ module Maprename
4
4
  attr_accessor :config
5
5
 
6
6
  def initialize(config)
7
+ debug " FileNameParser: #@config"
7
8
  @config = config
8
9
  @content = build_content
9
10
  end
10
11
 
11
12
  def parse!(context)
12
13
  @config[:select].each do |field_definition|
14
+ debug " MappingParser field definition: #{field_definition}"
13
15
  keyword_value = context.instance_eval(field_definition[:keyword_value])
16
+ debug " MappingParser keyword value: #{keyword_value}"
14
17
  row = @content.find { |row| row[field_definition[:keyword_column]] == keyword_value }
18
+ debug " MappingParser selected row: #{row}"
15
19
  context.instance_eval("self.%s = %s" % [field_definition[:name], row[field_definition[:select_column]].inspect])
20
+ debug " MappingParser context: #{context}"
16
21
  end
17
22
  end
18
23
 
@@ -20,6 +25,7 @@ module Maprename
20
25
  encoding = config[:encoding] || 'UTF-8'
21
26
  separator = config[:column_separator] || "\t"
22
27
  content = IO.readlines(config[:file], encoding: encoding).map(&:chomp)
28
+ debug " MappingParser total mapping lines: #{content.size}"
23
29
  if config[:first_line_as_column_defination]
24
30
  config[:columns] = content.first.split(separator).each_with_index.map do |(c, i)|
25
31
  {
@@ -29,6 +35,8 @@ module Maprename
29
35
  end
30
36
  end
31
37
 
38
+ debug " MappingParser columns: #{config[:columns]}"
39
+
32
40
  content.map do |raw_row|
33
41
  fields = raw_row.split(separator)
34
42
  config[:columns].each_with_object({}) do |column, result|
@@ -10,7 +10,7 @@ module Maprename
10
10
  @context = OpenStruct.new
11
11
  @file = file
12
12
  @config = config
13
-
13
+ debug "Process file: #@file"
14
14
  parse_config!
15
15
  end
16
16
 
@@ -29,10 +29,10 @@ module Maprename
29
29
  end
30
30
 
31
31
  def rename!(dry)
32
- if dry
33
- puts "mkdir -p %s" % File.dirname(destination)
34
- puts "cp %s %s" % [source, destination]
35
- else
32
+ puts "mkdir -p %s" % File.dirname(destination)
33
+ puts "cp %s %s" % [source, destination]
34
+
35
+ unless dry
36
36
  FileUtils.mkdir_p(File.dirname(destination))
37
37
  FileUtils.copy_file(source, destination)
38
38
  end
@@ -1,3 +1,3 @@
1
1
  module Maprename
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: maprename
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Liu Xiang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-26 00:00:00.000000000 Z
11
+ date: 2020-03-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -73,8 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  - !ruby/object:Gem::Version
74
74
  version: '0'
75
75
  requirements: []
76
- rubyforge_project:
77
- rubygems_version: 2.7.8
76
+ rubygems_version: 3.1.2
78
77
  signing_key:
79
78
  specification_version: 4
80
79
  summary: Simple file rename utils