bruhl 0.2.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7e77ea8d7c75e094491eac965051c38783aa37aca41d2ffd9eaf0af0109ba741
4
+ data.tar.gz: 6a558fd0ef0a729595687e3545d02a3701da33734725fd247d6458ef4a748f9a
5
+ SHA512:
6
+ metadata.gz: e0e77c7b687b36334373176856775c179919757654352655dc0b27b9ab10888b6943abe1ba1a2a6b784751815c9d9f879b095dbaee207acdac3f38773664eb1a
7
+ data.tar.gz: 70aafb0abe7a623352628cb9f7674576bd910523a932e01cbe4ea53d44dbf7e7f03e7761e7577b2d1c8913af48ff04e7fab6347a2f1ac0c3d135c2e79b3085d5
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /vendor/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at wtchappell@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in bruhl.gemspec
6
+ gemspec
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bruhl (0.2.0)
5
+ citrus (~> 3.0)
6
+ hirb-unicode
7
+ tty-table
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ awesome_print (1.8.0)
13
+ citrus (3.0.2)
14
+ coderay (1.1.2)
15
+ diff-lcs (1.3)
16
+ equatable (0.5.0)
17
+ hirb (0.7.3)
18
+ hirb-unicode (0.0.5)
19
+ hirb (~> 0.5)
20
+ unicode-display_width (~> 0.1.1)
21
+ method_source (0.9.0)
22
+ necromancer (0.3.0)
23
+ pastel (0.6.1)
24
+ equatable (~> 0.5.0)
25
+ tty-color (~> 0.3.0)
26
+ pry (0.11.3)
27
+ coderay (~> 1.1.0)
28
+ method_source (~> 0.9.0)
29
+ rake (10.5.0)
30
+ rspec (3.7.0)
31
+ rspec-core (~> 3.7.0)
32
+ rspec-expectations (~> 3.7.0)
33
+ rspec-mocks (~> 3.7.0)
34
+ rspec-core (3.7.1)
35
+ rspec-support (~> 3.7.0)
36
+ rspec-expectations (3.7.0)
37
+ diff-lcs (>= 1.2.0, < 2.0)
38
+ rspec-support (~> 3.7.0)
39
+ rspec-mocks (3.7.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.7.0)
42
+ rspec-support (3.7.1)
43
+ tty-color (0.3.0)
44
+ tty-screen (0.5.1)
45
+ tty-table (0.5.0)
46
+ equatable (~> 0.5.0)
47
+ necromancer (~> 0.3.0)
48
+ pastel (~> 0.6.0)
49
+ tty-screen (~> 0.5.0)
50
+ unicode_utils (~> 1.4.0)
51
+ verse (~> 0.4.0)
52
+ unicode-display_width (0.1.1)
53
+ unicode_utils (1.4.0)
54
+ verse (0.4.0)
55
+ unicode_utils (~> 1.4.0)
56
+
57
+ PLATFORMS
58
+ ruby
59
+
60
+ DEPENDENCIES
61
+ awesome_print
62
+ bruhl!
63
+ bundler (~> 2.1.4)
64
+ pry
65
+ rake (~> 10.0)
66
+ rspec (~> 3.0)
67
+
68
+ BUNDLED WITH
69
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Will Chappell
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Bruhl
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/bruhl`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bruhl'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bruhl
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/bruhl. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Bruhl project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/bruhl/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ require 'bruhl'
3
+ ARGV.each do |filename|
4
+ next if File.extname(filename) == '.ini'
5
+ next if File.extname(filename) == '.yml'
6
+ next if File.extname(filename) == '.csv'
7
+ next if File.extname(filename) == '.wav'
8
+ next if File.extname(filename) == '.dds'
9
+ next if File.extname(filename) == '.lua'
10
+ next if File.directory?(filename)
11
+ begin
12
+ next if File.read(filename).empty?
13
+ parsed = Bruhl.parse_file(filename)
14
+ generated = Bruhl.generate(parsed)
15
+ generated = Bruhl.bom_present?(filename) ? "#{Bruhl::BOM.force_encoding('utf-8')}#{generated}" : generated
16
+ File.open(filename, 'w') { |f| f.write(generated) }
17
+ rescue => e
18
+ puts "FAILED WHILE PARSING #{filename}!"
19
+ raise(e)
20
+ end
21
+ end
22
+ # vim: set ft=ruby ts=2 sw=2 tw=79 :
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'bruhl'
5
+ require 'bruhl/awesome_print'
6
+ require 'yaml'
7
+
8
+ require 'pry'
9
+ Pry.start
10
+ # vim: set ft=ruby ts=2 sw=2 tw=79 :
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bruhl/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bruhl"
8
+ spec.version = Bruhl::VERSION
9
+ spec.authors = ["Will Chappell"]
10
+ spec.email = ["wtchappell@gmail.com"]
11
+
12
+ spec.summary = "Tools for Clausewitz game engine data"
13
+ spec.description = "bruhl provides an API to parse, generate, and " +
14
+ "manipulate the data files used by video games using the Clausewitz " +
15
+ "strategy game engine by Paradox."
16
+ spec.homepage = "http://github.com/wtchappell/bruhl"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "bin"
23
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "awesome_print"
27
+ spec.add_development_dependency "bundler", "~> 2.1.4"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+
32
+ spec.add_dependency "citrus", "~> 3.0"
33
+ spec.add_dependency "hirb-unicode"
34
+ spec.add_dependency "tty-table"
35
+ end
@@ -0,0 +1,230 @@
1
+ require 'citrus'
2
+ require 'bruhl/version'
3
+
4
+ module Bruhl
5
+ def self.generate(root, indent = 0)
6
+ case root
7
+ when Bruhl::Object
8
+ generate_object(root, indent)
9
+ when Bruhl::Relation
10
+ generate_relation(root, indent)
11
+ when Bruhl::RawString
12
+ root.to_s
13
+ when Bruhl::QuotedString
14
+ '"' + root.to_s + '"'
15
+ when Integer, Float
16
+ root.to_s
17
+ when Enumerable
18
+ root.map { |elem| Bruhl.generate(elem) }
19
+ end
20
+ end
21
+
22
+ def self.generate_object(root, indent = 0)
23
+ str = ''
24
+ str += "#{root.tag}: " if root.tag
25
+ if root.contents.empty?
26
+ str += '{}'
27
+ elsif root.contents.none? { |elem| elem.is_a?(Bruhl::Object) || elem.is_a?(Bruhl::Relation) }
28
+ str += '{ '
29
+ str += root.contents.map { |elem| Bruhl.generate(elem) }.join(' ')
30
+ str += ' }'
31
+ else
32
+ str += '{'
33
+ str += "\n"
34
+ root.contents.each do |elem|
35
+ str += ((' ' * (indent + 2)) + Bruhl.generate(elem, indent + 2)) + "\n"
36
+ end
37
+ str += ' ' * indent
38
+ str += '}'
39
+ end
40
+ str
41
+ end
42
+
43
+ def self.generate_relation(root, indent = 0)
44
+ [
45
+ root.left.to_s,
46
+ root.operator.to_s,
47
+ Bruhl.generate(root.right, indent)
48
+ ].join(' ')
49
+ end
50
+
51
+ def self.parse(str, opts = {})
52
+ self.parser.parse(str, opts).value
53
+ end
54
+
55
+ BOM = "\xEF\xBB\xBF"
56
+ def self.bom_present?(filename)
57
+ bom_range = File.open(filename, 'rb').read(BOM.bytesize)
58
+ bom_range == BOM.force_encoding(bom_range.encoding)
59
+ end
60
+
61
+ def self.parse_file(filename, opts = {})
62
+ file_body =
63
+ if bom_present?(filename)
64
+ File.open(filename, 'r:bom|utf-8') { |f| f.read }
65
+ else
66
+ File.open(filename, 'r:utf-8') { |f| f.read }
67
+ end
68
+ self.parse(file_body, opts)
69
+ end
70
+
71
+ def self.parser
72
+ @@parser ||=
73
+ begin
74
+ Citrus.load(File.join(__dir__, 'bruhl', 'grammar'))
75
+ @@parser = BruhlGrammar
76
+ end
77
+ end
78
+
79
+ class QuotedString
80
+ include Comparable
81
+ attr_reader :str
82
+ def initialize(str)
83
+ @str = str
84
+ end
85
+
86
+ def <=>(other)
87
+ other.respond_to?(:to_str) && @str <=> other.to_str
88
+ end
89
+
90
+ def eql?(other)
91
+ self == other
92
+ end
93
+
94
+ def hash
95
+ @str.hash
96
+ end
97
+
98
+ def inspect
99
+ to_s
100
+ end
101
+
102
+ def to_s
103
+ @str
104
+ end
105
+
106
+ def to_str
107
+ @str
108
+ end
109
+ end
110
+
111
+ class RawString
112
+ include Comparable
113
+ attr_reader :str
114
+ def initialize(str)
115
+ @str = str
116
+ end
117
+
118
+ def <=>(other)
119
+ other.respond_to?(:to_str) && @str <=> other.to_str
120
+ end
121
+
122
+ def eql?(other)
123
+ self == other
124
+ end
125
+
126
+ def hash
127
+ @str.hash
128
+ end
129
+
130
+ def inspect
131
+ to_s
132
+ end
133
+
134
+ def to_s
135
+ @str
136
+ end
137
+
138
+ def to_str
139
+ @str
140
+ end
141
+ end
142
+
143
+ class Object
144
+ attr_reader :contents, :tag
145
+ def initialize(contents, tag = nil)
146
+ @contents = contents
147
+ @tag = tag ? tag.strip : nil
148
+ end
149
+
150
+ def empty?
151
+ @contents.empty?
152
+ end
153
+
154
+ def size
155
+ @contents.size
156
+ end
157
+
158
+ def fetch_all(key)
159
+ @contents.select do |elem|
160
+ next unless elem.is_a?(Relation)
161
+ elem.left.to_s == key
162
+ end.map(&:right)
163
+ end
164
+
165
+ def fetch(key, default = nil)
166
+ results = fetch_all(key)
167
+ if results.size > 1
168
+ fail("Found too many #{key} sections!")
169
+ elsif results.empty?
170
+ default
171
+ else
172
+ results.first
173
+ end
174
+ end
175
+
176
+ def only(key)
177
+ results = fetch_all(key)
178
+ if results.size > 1
179
+ fail("Found too many #{key} sections!")
180
+ elsif results.empty?
181
+ fail("Found zero #{key} sections!")
182
+ else
183
+ results.first
184
+ end
185
+ end
186
+
187
+ def ==(other)
188
+ other.is_a?(self.class) &&
189
+ @contents == other.contents &&
190
+ @tag == other.tag
191
+ end
192
+
193
+ def inspect
194
+ to_s
195
+ end
196
+
197
+ def to_s
198
+ str = contents.map(&:to_s).join(', ')
199
+ '{' + (@tag ? "#{@tag}: #{str}" : str) + '}'
200
+ end
201
+ end
202
+ EMPTY_OBJECT = Bruhl::Object.new([]).freeze
203
+
204
+ class Relation
205
+ attr_reader :left, :operator, :right
206
+ def initialize(left, operator, right)
207
+ @left = left
208
+ @operator = operator
209
+ @right = right
210
+ end
211
+
212
+ def ==(other)
213
+ puts @left, @left.class
214
+ puts other.left, other.left.class
215
+ other.is_a?(Relation) &&
216
+ @left == other.left &&
217
+ @operator == other.operator &&
218
+ @right == other.right
219
+ end
220
+
221
+ def inspect
222
+ to_s
223
+ end
224
+
225
+ def to_s
226
+ "#{@left} #{@operator} #{@right}"
227
+ end
228
+ end
229
+ end
230
+ # vim: set ft=ruby ts=2 sw=2 tw=79 :
@@ -0,0 +1,46 @@
1
+ require 'awesome_print'
2
+
3
+ module AwesomePrint
4
+ module Bruhl
5
+ def self.included(base)
6
+ base.send :alias_method, :cast_without_bruhl, :cast
7
+ base.send :alias_method, :cast, :cast_with_bruhl
8
+ end
9
+
10
+ def cast_with_bruhl(object, type)
11
+ cast = cast_without_bruhl(object, type)
12
+ if defined?(::Bruhl::Object) && object.is_a?(::Bruhl::Object)
13
+ cast = :bruhl_object
14
+ elsif defined?(::Bruhl::Relation) && object.is_a?(::Bruhl::Relation)
15
+ cast = :bruhl_relation
16
+ end
17
+ cast
18
+ end
19
+
20
+ def awesome_bruhl_object(object)
21
+ str = awesome_array(object.contents)
22
+ str = "#{colorize(object.tag, :class)}: #{str}" if object.tag
23
+ str
24
+ end
25
+
26
+ def awesome_bruhl_relation(relation)
27
+ right = case relation.right
28
+ when ::Bruhl::Object
29
+ awesome_bruhl_object(relation.right)
30
+ when ::Bruhl::Relation
31
+ awesome_bruhl_relation(relation.right)
32
+ when ::Bruhl::RawString, ::Bruhl::QuotedString
33
+ colorize(relation.right.to_s, :string)
34
+ when ::Float, ::Integer
35
+ relation.right.ai
36
+ end
37
+ [
38
+ colorize(relation.left.to_s, :method),
39
+ colorize(relation.operator, :symbol),
40
+ right
41
+ ].join(' ')
42
+ end
43
+ end
44
+ end
45
+
46
+ AwesomePrint::Formatter.send(:include, AwesomePrint::Bruhl)
@@ -0,0 +1,103 @@
1
+ grammar BruhlGrammar
2
+ rule root
3
+ (ws* object_elem* ws*) {
4
+ Bruhl::Object.new(
5
+ captures(:object_elem).map(&:value)
6
+ )
7
+ }
8
+ end
9
+
10
+ rule object
11
+ (ws* string? '{' ws* (object_elem ws*)* '}' ws*) {
12
+ tag = capture(:string) ? capture(:string).to_s : nil
13
+ Bruhl::Object.new(
14
+ captures(:object_elem).map(&:value),
15
+ tag
16
+ )
17
+ }
18
+ end
19
+
20
+ rule object_elem
21
+ relation | rvalue
22
+ end
23
+
24
+ rule relation
25
+ (ws* key:(string | integer) ws* operator ws* rvalue ws*) {
26
+ Bruhl::Relation.new(
27
+ capture(:key ).value,
28
+ capture(:operator).value,
29
+ capture(:rvalue ).value
30
+ )
31
+ }
32
+ end
33
+
34
+ rule number
35
+ float | integer
36
+ end
37
+
38
+ rule integer
39
+ (ws* num:('-'? [\d]+) ws*) {
40
+ capture(:num).to_s.to_i
41
+ }
42
+ end
43
+
44
+ rule string
45
+ (ws* inside:(quoted_string | raw_string) ws*) {
46
+ capture(:inside).value
47
+ }
48
+ end
49
+
50
+ rule escaped_quote
51
+ ('\"') {
52
+ '"'
53
+ }
54
+ end
55
+
56
+ rule quote_string_part_raw
57
+ (!('\"' | '"') .) {
58
+ to_s
59
+ }
60
+ end
61
+
62
+ rule quote_string_part
63
+ escaped_quote | quote_string_part_raw
64
+ end
65
+
66
+ rule quoted_string
67
+ ('"' quote_string_part* '"') {
68
+ Bruhl::QuotedString.new(captures(:quote_string_part).map(&:value).join)
69
+ }
70
+ end
71
+
72
+ rule raw_string
73
+ ([-_'’@:\./%\p{alpha}][-\d_'’@:\./%\p{alpha}]*) {
74
+ Bruhl::RawString.new(to_s)
75
+ }
76
+ end
77
+
78
+ rule float
79
+ (ws* num:('-'? ([\d]+ '.' [\d]+)) ws*) {
80
+ capture(:num).to_s.to_f
81
+ }
82
+ end
83
+
84
+ rule rvalue
85
+ (ws* inside:(object | number | string) ws*) {
86
+ capture(:inside).value
87
+ }
88
+ end
89
+
90
+ rule operator
91
+ ('<=' | '>=' | '=' | '<' | '>') {
92
+ to_s
93
+ }
94
+ end
95
+
96
+ rule ws
97
+ [ \t\n§;~] | "\r\n" | comment
98
+ end
99
+
100
+ rule comment
101
+ '#' (!"\n" .)*
102
+ end
103
+ end
@@ -0,0 +1,3 @@
1
+ module Bruhl
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ap'
3
+ require 'hirb-unicode'
4
+ require 'tty-table'
5
+ require 'pathname'
6
+ require 'bruhl'
7
+
8
+ HOI4_ROOT = Pathname.new(ARGV[0] || ENV['HOI4_ROOT'])
9
+ NF_ROOT = HOI4_ROOT.join('common', 'national_focus')
10
+
11
+ summary = {}
12
+ NF_ROOT.children.each do |nf_file|
13
+ begin
14
+ begin
15
+ root = Bruhl.parse_file(nf_file)
16
+ rescue NoMethodError
17
+ puts "FUCK #{nf_file.to_s}"
18
+ next
19
+ end
20
+ nf_summ = {}
21
+ nf = root.fetch('focus_tree', [])
22
+ foci = (nf.size > 1 && nf.fetch_all('focus')) || []
23
+ foci.concat(root.fetch_all('shared_focus'))
24
+ nf_summ['focus_count'] = foci.size
25
+ nf_summ['total_cost'] = foci.reduce(0) do |sum, focus|
26
+ puts focus.only('id')
27
+ sum += (focus.only('cost') * 7).round(0)
28
+ end
29
+ nf_summ['average_cost'] = nf_summ['total_cost'] / nf_summ['focus_count']
30
+ grouped_foci = foci.group_by { |focus| (focus.only('cost').to_f * 7).round(0) }
31
+ grouped_foci = grouped_foci.to_a.sort_by! { |cost, group| cost }
32
+ nf_summ['cost_breakdown'] = grouped_foci.map do |cost, group|
33
+ count = group.size
34
+ "#{count}x#{cost} day foci"
35
+ end.join("\n")
36
+ #nf_summ['grouped_foci'] = grouped_foci
37
+ summary[nf_file.basename.sub_ext('').to_s] = nf_summ
38
+ rescue => e
39
+ puts e
40
+ warn "#{nf_file.to_s} could not be parsed!"
41
+ raise(e)
42
+ next
43
+ end
44
+ end
45
+
46
+ headers = %w[focus_tree focus_count total_cost average_cost cost_breakdown]
47
+ table = TTY::Table.new(
48
+ header: headers,
49
+ rows: summary.sort_by { |ft, val| val['focus_count'] }.map do |focus_tree, val|
50
+ name = { value: focus_tree, alignment: :left }
51
+ vals = val.map do |_, val|
52
+ { value: val, alignment: :right }
53
+ end
54
+ [name].concat(vals)
55
+ end
56
+ )
57
+ pastel = Pastel.new
58
+
59
+ #filter = proc do |val, row_index, col_index|
60
+ # colors = []
61
+ # colors << :on_black if row_index.odd?
62
+ # if headers[col_index] == 'delta' && row_index > 0
63
+ # val = val.to_i
64
+ # if val > 0
65
+ # colors.unshift(:green)
66
+ # elsif val < 0
67
+ # colors.unshift(:red)
68
+ # else
69
+ # end
70
+ # colors.unshift(:bold) unless val.to_i == 0
71
+ # end
72
+ # pastel.decorate(val.to_s, *colors)
73
+ #end
74
+
75
+ multi_renderer = TTY::Table::Renderer::Unicode.new(table, multiline: true)
76
+ multi_renderer.border.separator = :each_row
77
+ puts multi_renderer.render
78
+ #puts table.render
79
+ # vim: set ft=ruby ts=2 sw=2 tw=79 :
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ap'
3
+ require 'hirb-unicode'
4
+ require 'tty-table'
5
+ require 'pathname'
6
+ require 'bruhl'
7
+
8
+ ARGV.each do |file|
9
+ begin
10
+ root = Bruhl.parse_file(file)
11
+ puts(root.to_h)
12
+ rescue => e
13
+ puts("Could not parse #{file}: #{e}")
14
+ next
15
+ end
16
+ end
17
+ # vim: set ft=ruby ts=2 sw=2 tw=79 :
@@ -0,0 +1,110 @@
1
+ #!/usr/bin/env ruby
2
+ require 'ap'
3
+ require 'hirb-unicode'
4
+ require 'tty-table'
5
+ require 'pathname'
6
+ require 'bruhl'
7
+
8
+ class CountryManpower
9
+ attr_reader :tag, :name
10
+ attr_accessor :owned_manpower, :core_manpower
11
+ def initialize(tag, name)
12
+ @tag = tag
13
+ @name = name
14
+
15
+ @owned_manpower = 0
16
+ @core_manpower = 0
17
+ end
18
+
19
+ def self.headers
20
+ %w[tag name owned_manpower core_manpower]
21
+ end
22
+
23
+ def to_row
24
+ [@tag, @name, {value: @owned_manpower, alignment: :right}, {value: @core_manpower, alignment: :right}]
25
+ end
26
+ end
27
+
28
+ HOI4_ROOT = Pathname.new(ARGV[0] || ENV['HOI4_ROOT'])
29
+ STATE_ROOT = HOI4_ROOT.join('history', 'states')
30
+ COUNTRY_ROOT = HOI4_ROOT.join('history', 'countries')
31
+
32
+ country_tags = { '???' => 'Unknown' }
33
+ COUNTRY_ROOT.children.each do |country_file|
34
+ next if country_file.zero? || country_file.read.strip.size == 1
35
+ tag, country = country_file.basename.to_s.split(' - ')
36
+ country.gsub!('.txt', '')
37
+ country_tags[tag] = country
38
+ end
39
+
40
+ summary = {}
41
+ STATE_ROOT.children.each do |state_file|
42
+ begin
43
+ begin
44
+ root = Bruhl.parse_file(state_file)
45
+ rescue NoMethodError
46
+ puts "FUCK #{state_file.to_s}"
47
+ next
48
+ end
49
+ state = root.only('state')
50
+ manpower = state.fetch('manpower', 0)
51
+ history = state.fetch('history', Bruhl::EMPTY_OBJECT)
52
+ tag = history.fetch('owner', '???')
53
+ owner = country_tags[tag]
54
+
55
+ core_tags = history.fetch_all('add_core_of')
56
+
57
+ summary[tag] ||= CountryManpower.new(tag, owner)
58
+
59
+ core_tags.each do |core_tag|
60
+ core = country_tags[core_tag]
61
+ summary[core_tag] ||= CountryManpower.new(core_tag, core)
62
+ summary[core_tag].core_manpower += manpower
63
+ end
64
+ summary[tag].owned_manpower += manpower
65
+ rescue => e
66
+ puts e
67
+ warn "#{state_file.to_s} could not be parsed!"
68
+ raise(e)
69
+ next
70
+ end
71
+ end
72
+
73
+ missing = country_tags.keys - summary.keys.map(&:to_s)
74
+ missing.each do |elem|
75
+ summary[elem] ||= CountryManpower.new(elem, country_tags[elem])
76
+ end
77
+
78
+ values = summary.values.sort_by { |val| val.tag }.reverse
79
+ values = values.sort_by do |val|
80
+ [
81
+ val.owned_manpower,
82
+ val.core_manpower
83
+ ]
84
+ end.reverse
85
+ headers = CountryManpower.headers
86
+ headers << 'delta'
87
+ table = TTY::Table.new(
88
+ header: headers,
89
+ rows: values.map { |val| val.to_row << { value: (val.core_manpower - val.owned_manpower), alignment: :right } }
90
+ )
91
+ pastel = Pastel.new
92
+
93
+ filter = proc do |val, row_index, col_index|
94
+ colors = []
95
+ colors << :on_black if row_index.odd?
96
+ if headers[col_index] == 'delta' && row_index > 0
97
+ val = val.to_i
98
+ if val > 0
99
+ colors.unshift(:green)
100
+ elsif val < 0
101
+ colors.unshift(:red)
102
+ else
103
+ end
104
+ colors.unshift(:bold) unless val.to_i == 0
105
+ end
106
+ pastel.decorate(val.to_s, *colors)
107
+ end
108
+
109
+ puts table.render(filter: filter)
110
+ # vim: set ft=ruby ts=2 sw=2 tw=79 :
metadata ADDED
@@ -0,0 +1,179 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bruhl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Will Chappell
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-06-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: awesome_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 2.1.4
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 2.1.4
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: citrus
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: hirb-unicode
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: tty-table
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: bruhl provides an API to parse, generate, and manipulate the data files
126
+ used by video games using the Clausewitz strategy game engine by Paradox.
127
+ email:
128
+ - wtchappell@gmail.com
129
+ executables:
130
+ - bruhl-prettify
131
+ - console
132
+ - setup
133
+ extensions: []
134
+ extra_rdoc_files: []
135
+ files:
136
+ - ".gitignore"
137
+ - ".rspec"
138
+ - ".travis.yml"
139
+ - CODE_OF_CONDUCT.md
140
+ - Gemfile
141
+ - Gemfile.lock
142
+ - LICENSE.txt
143
+ - README.md
144
+ - Rakefile
145
+ - bin/bruhl-prettify
146
+ - bin/console
147
+ - bin/setup
148
+ - bruhl.gemspec
149
+ - lib/bruhl.rb
150
+ - lib/bruhl/awesome_print.rb
151
+ - lib/bruhl/grammar.citrus
152
+ - lib/bruhl/version.rb
153
+ - scripts/hoi4/focus-lengths
154
+ - scripts/hoi4/json-dump
155
+ - scripts/hoi4/population-summary
156
+ homepage: http://github.com/wtchappell/bruhl
157
+ licenses:
158
+ - MIT
159
+ metadata: {}
160
+ post_install_message:
161
+ rdoc_options: []
162
+ require_paths:
163
+ - lib
164
+ required_ruby_version: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
167
+ - !ruby/object:Gem::Version
168
+ version: '0'
169
+ required_rubygems_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ requirements: []
175
+ rubygems_version: 3.1.3
176
+ signing_key:
177
+ specification_version: 4
178
+ summary: Tools for Clausewitz game engine data
179
+ test_files: []