showdown-team-import 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6512fd34fe6efb4e5641b41a81dd8a44a7f979d1
4
- data.tar.gz: 2502fdeda1458ef3405e99e115dd03dac9349330
3
+ metadata.gz: ecf800f0fcfa71cc66cd47a1c8a6684b6c86b670
4
+ data.tar.gz: 29fc3dd7fc9ec1a2633c5f19177175024e3bfeef
5
5
  SHA512:
6
- metadata.gz: 82fa19fc50d558f68fc35fc237be8f816e8c5f8dcd7e7a600aab93ce9b85deaa2ee9e1a3afa8acc0ab6eb3381b044e6e3a907de9b1443fac0b895f0ec3ce9932
7
- data.tar.gz: ede3cd6f74bd34131a2464c1ef60e3c6c79ce407c7255147eff2de3b7feb630465d9b2977ee71546c7811c2cf5c87f72573125ff79aa6d38fd6fc5f9741710be
6
+ metadata.gz: 45ec90b9edd14f12e6148a13f9e1b3eaa5be0df126d5eee49d97228aef0034fb9862a966db519f0bc4c061099d9743a83a74dcb275f6ac398c83ec6fa61af9e6
7
+ data.tar.gz: a06b294d98bbc122b2e305600459d141a49472e04270e49e612c81d4a8c8416ca5c3c98ddf3fa5e74737ffa4f1da7790dc0b2d2cd2e7119450aef31c827656c2
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
@@ -2,19 +2,27 @@
2
2
 
3
3
  require 'fileutils'
4
4
  require 'showdown-team-import'
5
+ require 'optparse'
5
6
 
6
- (puts "Usage: #{File.basename(__FILE__)} <file containing teams> [optional directory]"; exit) if ARGV.empty?
7
+ @options = {:silent => false,
8
+ :path => nil,
9
+ :no_dirs => false}
7
10
 
8
- raise ArgumentError, "No file provided" if ARGV[0].nil?
9
- raise ArgumentError, "File doesn't exist" unless File.exists?(ARGV[0])
11
+ OptionParser.new do |opts|
12
+ opts.banner = "Usage:" + " #{File.basename(__FILE__)}" + " [options] <File containing team backup>"
13
+ opts.on('-s', '--silent', 'Run silently') {@options[:silent] = true}
14
+ opts.on('-p', '--path [path]', 'Path to save to') {|v| @options[:path] = v}
15
+ opts.on( '-h', '--help', 'Display this screen' ) {puts opts; exit}
16
+ opts.on( '-n', '--no-dirs', 'Do not save in tier-specific directories (off by default)') {@options[:no_dirs] = true}
17
+ end.parse!
18
+
19
+ (puts "Input file needed. Run \"#{File.basename(__FILE__)} -h\" for more information."; exit) if ARGV.empty?
20
+ (puts "File doesn't exist. Run \"#{File.basename(__FILE__)} -h\" for more information."; exit) unless File.exists?(ARGV[0])
10
21
 
11
22
  lines = Array.new
12
- dir = ARGV[1] || false
23
+ dir = @options[:path] || nil
13
24
 
14
- unless dir == false
15
- raise ArgumentError, "Directory is actually file" if File.file?(dir)
16
- FileUtils::mkdir_p dir
17
- end
25
+ ((puts "Directory is actually a file. Run \"#{File.basename(__FILE__)} -h\" for more information."; exit) if File.file?(dir)) if dir
18
26
 
19
27
  File.foreach(ARGV[0]) do |line| # Necessary for skipping last empty line
20
28
  lines << line
@@ -25,8 +33,8 @@ importer = TeamImporter.new
25
33
  begin
26
34
  importer.import(lines)
27
35
  rescue ParserError => e
28
- puts e.message
29
- puts e.line
36
+ puts "An error has occurred: " + e.message
37
+ puts "The error occurred at the following line: " + e.line
30
38
  end
31
39
 
32
40
  teams = importer.teams
@@ -34,13 +42,20 @@ teams = importer.teams
34
42
  teams.each do |team| # actual file writing
35
43
  team_content = team.content.gsub(/\n{3,}/, "\n") # replace 3 or more newlines with a single one
36
44
  filename = team.name.delete("/").strip + ".team" # ruby mistakes / for path separator
37
-
38
- unless dir == false
45
+ filename = File.join(team.tier, filename) unless @options[:no_dirs] # add tier-specific path
46
+
47
+ if dir
48
+ unless @options[:no_dirs]
49
+ FileUtils::mkdir_p File.join(dir, team.tier)
50
+ else
51
+ FileUtils::mkdir_p dir
52
+ end
39
53
  location = File.join(dir, filename) # saving in specified directory
40
54
  else
55
+ FileUtils::mkdir_p team.tier unless @options[:no_dirs]
41
56
  location = filename # saving in current directory
42
57
  end
43
58
 
44
- puts "Writing \"#{location}\""
59
+ puts "Writing \"#{location}\"" unless @options[:silent]
45
60
  File.write(location, team_content)
46
61
  end
Binary file
@@ -1,6 +1,8 @@
1
1
  # Classes related to team parsing are defined here.
2
2
 
3
- Team = Struct.new(:name, :content)
3
+ require 'securerandom'
4
+
5
+ Team = Struct.new(:name, :tier, :content)
4
6
 
5
7
  class ParserError < StandardError
6
8
  attr_reader :line
@@ -17,20 +19,20 @@ class TeamImporter
17
19
  end
18
20
 
19
21
  def import team_import
20
- team = Team.new(String.new, String.new)
22
+ team = Team.new(String.new, String.new, String.new)
21
23
  line_count = 0
22
24
  in_team = false
23
25
 
24
26
  team_import.each do |line, count|
25
27
  next if line.equal? team_import.last # I haven't found any way to make this not an error except adding more shitty checks
26
28
  if in_team
27
- team.content += line unless team.content.nil? && line.strip.empty? # Beginning of file
29
+ team.content += line unless team.content.empty? && line.strip.empty? # Beginning of file
28
30
 
29
31
  if line.strip == ''
30
32
  line_count += 1
31
33
  if line_count == 2 # 2 empty lines = end of current team
32
34
  @teams.push(team) # add team to array and reset everything
33
- team = Team.new(String.new, String.new)
35
+ team = Team.new(String.new, String.new, String.new)
34
36
  in_team = false
35
37
  line_count = 0
36
38
  end
@@ -39,9 +41,23 @@ class TeamImporter
39
41
  end
40
42
  elsif line[0..2] == "===" && line.chomp.reverse[0..2] == "===" # beginning of team
41
43
  in_team = true
42
- team.name = line[4..-5] # remove identifiers
44
+ name_and_tier = line[4..-5]
45
+ bracket_index = name_and_tier.index(']') # 10/10 code I know
46
+ format = name_and_tier[1..bracket_index-1] if !bracket_index.nil? && bracket_index > 2
47
+ format = "others" unless format
48
+
49
+ if !bracket_index.nil? && !name_and_tier[bracket_index + 1..-1].strip.empty?
50
+ team_name = name_and_tier[bracket_index + 1..-1]
51
+ elsif !name_and_tier.strip.empty? && bracket_index.nil?
52
+ team_name = name_and_tier
53
+ else
54
+ team_name = "Untitled [#{SecureRandom.hex[0..5]}]"
55
+ end
56
+
57
+ team.name = team_name
58
+ team.tier = format
43
59
  else
44
- raise ParserError(line), "Error parsing backup file"
60
+ raise ParserError.new(line), "Error parsing backup file"
45
61
  end
46
62
  end
47
63
 
@@ -2,11 +2,11 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: showdown-team-import 0.2.2 ruby lib
5
+ # stub: showdown-team-import 0.3.0 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "showdown-team-import"
9
- s.version = "0.2.2"
9
+ s.version = "0.3.0"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
@@ -28,6 +28,7 @@ Gem::Specification.new do |s|
28
28
  "Rakefile",
29
29
  "VERSION",
30
30
  "bin/showdown-team-import",
31
+ "demo.webm",
31
32
  "lib/showdown-team-import.rb",
32
33
  "showdown-team-import.gemspec"
33
34
  ]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: showdown-team-import
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - apt-get
@@ -71,6 +71,7 @@ files:
71
71
  - Rakefile
72
72
  - VERSION
73
73
  - bin/showdown-team-import
74
+ - demo.webm
74
75
  - lib/showdown-team-import.rb
75
76
  - showdown-team-import.gemspec
76
77
  homepage: http://github.com/lc-guy/showdown-team-import