atk_toolbox 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/atk/os.rb +1 -1
- data/lib/atk/remove_indent.rb +10 -0
- data/lib/atk/yaml_info_parser.rb +6 -7
- data/lib/atk_toolbox/version.rb +3 -0
- data/lib/atk_toolbox.rb +5 -6
- data/lib/yaml_edit/remove_key.py +29 -0
- data/lib/yaml_edit/set_key.py +31 -0
- data/lib/yaml_edit/yaml_edit.rb +48 -0
- data/test/main.rb +1 -1
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3434dd3967d04310b2feefa38cc06a50afc4acd90557c3c62ac2f152037f9aa2
|
4
|
+
data.tar.gz: b61ada0d41d7385173f9ff0a88bab694058c7edec8fec47b681f2b0c0f70deb9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 96bcc7c68e260048ae2570d3186f0c65996ab522ace3d1bb17cdd6b24235e67ff5d8dc76db49d6f1390b76a07e6adf47ece42cbf7bdf5966df9e6e565c02820b
|
7
|
+
data.tar.gz: b74ba108502a965b2e1353dc9a11a81fb12bed9659d27b761d572ac89e0c701d667e1d2effee01d587f7673b7958fcb5ad46aa81c46abe5e214ed3c000e92c91
|
data/lib/atk/os.rb
CHANGED
data/lib/atk/yaml_info_parser.rb
CHANGED
@@ -7,17 +7,16 @@ require 'fileutils'
|
|
7
7
|
#
|
8
8
|
# Create loaders for ruby code literal and console code literal
|
9
9
|
#
|
10
|
+
def register_tag(tag_name, class_value)
|
11
|
+
YAML.add_tag(tag_name, class_value)
|
12
|
+
Code.tags[tag_name] = class_value
|
13
|
+
end
|
10
14
|
class Code
|
11
15
|
@@tags = {}
|
12
16
|
def self.tags
|
13
17
|
return @@tags
|
14
18
|
end
|
15
19
|
|
16
|
-
def self.register_as(yaml_tag_name)
|
17
|
-
YAML.add_tag(yaml_tag_name, self.class)
|
18
|
-
Code.tags[yaml_tag_name] = self.class
|
19
|
-
end
|
20
|
-
|
21
20
|
def init_with(coder)
|
22
21
|
@value = coder.scalar
|
23
22
|
end
|
@@ -40,7 +39,7 @@ require 'fileutils'
|
|
40
39
|
eval(@value)
|
41
40
|
end
|
42
41
|
end
|
43
|
-
|
42
|
+
register_tag('!language/ruby', RubyCode)
|
44
43
|
# add an evaluater for ruby code
|
45
44
|
ruby_evaluation_tag = 'evaluate/ruby'
|
46
45
|
Code.tags[ruby_evaluation_tag] = "evaluate"
|
@@ -61,7 +60,7 @@ require 'fileutils'
|
|
61
60
|
-"#{@value}"
|
62
61
|
end
|
63
62
|
end
|
64
|
-
|
63
|
+
register_tag('!language/console', ConsoleCode)
|
65
64
|
#
|
66
65
|
# project info (specific to operating sytem)
|
67
66
|
#
|
data/lib/atk_toolbox.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
require_relative
|
5
|
-
|
6
|
-
require_relative './atk/yaml_info_parser'
|
1
|
+
# require all the ruby files
|
2
|
+
files = Dir.glob(File.join(__dir__, "atk", "*.rb"))
|
3
|
+
for each in files
|
4
|
+
require_relative each
|
5
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
import ruamel.yaml
|
2
|
+
import json
|
3
|
+
import sys
|
4
|
+
|
5
|
+
yaml_string = sys.argv[1]
|
6
|
+
json_key_list = sys.argv[2]
|
7
|
+
|
8
|
+
# a tool from https://stackoverflow.com/questions/14692690/access-nested-dictionary-path-via-a-list-of-keys
|
9
|
+
from functools import reduce # forward compatibility for Python 3
|
10
|
+
import operator
|
11
|
+
def get_by_path(data, path):
|
12
|
+
"""Access a nested object in data by item sequence."""
|
13
|
+
return reduce(operator.getitem, path, data)
|
14
|
+
|
15
|
+
def set_by_path(data, path, value):
|
16
|
+
"""Set a value in a nested object in data by item sequence."""
|
17
|
+
get_by_path(data, path[:-1])[path[-1]] = value
|
18
|
+
|
19
|
+
|
20
|
+
# parse the yaml
|
21
|
+
data = ruamel.yaml.round_trip_load(yaml_string)
|
22
|
+
# parse the path
|
23
|
+
path_to_element = json.loads(json_key_list)
|
24
|
+
# get the parent element
|
25
|
+
path_element = get_by_path(data, path_to_element[:-1])
|
26
|
+
# remove the key
|
27
|
+
del path_element[path_to_element[-1]]
|
28
|
+
# output the data
|
29
|
+
print(ruamel.yaml.round_trip_dump(data))
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import ruamel.yaml
|
2
|
+
import json
|
3
|
+
import sys
|
4
|
+
|
5
|
+
yaml_string = sys.argv[1]
|
6
|
+
json_key_list = sys.argv[2]
|
7
|
+
new_key_value = sys.argv[3]
|
8
|
+
|
9
|
+
|
10
|
+
# a tool from https://stackoverflow.com/questions/14692690/access-nested-dictionary-path-via-a-list-of-keys
|
11
|
+
from functools import reduce # forward compatibility for Python 3
|
12
|
+
import operator
|
13
|
+
def get_by_path(data, path):
|
14
|
+
"""Access a nested object in data by item sequence."""
|
15
|
+
return reduce(operator.getitem, path, data)
|
16
|
+
|
17
|
+
def set_by_path(data, path, value):
|
18
|
+
"""Set a value in a nested object in data by item sequence."""
|
19
|
+
get_by_path(data, path[:-1])[path[-1]] = value
|
20
|
+
|
21
|
+
|
22
|
+
# parse the yaml
|
23
|
+
data = ruamel.yaml.round_trip_load(yaml_string)
|
24
|
+
# parse the path
|
25
|
+
path_to_element = json.loads(json_key_list)
|
26
|
+
# parse the new value
|
27
|
+
new_value = json.loads(new_key_value)
|
28
|
+
# set the value
|
29
|
+
set_by_path(data, path_to_element, new_value)
|
30
|
+
# output the data
|
31
|
+
print(ruamel.yaml.round_trip_dump(data))
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require_relative '../atk/os'
|
2
|
+
require_relative '../atk/remove_indent'
|
3
|
+
require 'open3'
|
4
|
+
require 'json'
|
5
|
+
|
6
|
+
def execute_with_local_python(python_file_path, *args)
|
7
|
+
# save the current directory
|
8
|
+
pwd = Dir.pwd
|
9
|
+
# change to where this file is
|
10
|
+
Dir.chdir __dir__
|
11
|
+
# run the python file with the virtual environment
|
12
|
+
stdout_str, stderr_str, status = Open3.capture3('python3', python_file_path, *args)
|
13
|
+
# change back to the original dir
|
14
|
+
Dir.chdir pwd
|
15
|
+
return [stdout_str, stderr_str, status]
|
16
|
+
end
|
17
|
+
|
18
|
+
def set_key(yaml_string, key_list, new_value)
|
19
|
+
# run the python file with the virtual environment
|
20
|
+
stdout_str, stderr_str, status = execute_with_local_python('set_key.py', yaml_string, key_list.to_json, new_value.to_json)
|
21
|
+
if not status.success?
|
22
|
+
raise "\n\nFailed to set key in yaml file:\n#{stderr_str}"
|
23
|
+
end
|
24
|
+
return stdout_str
|
25
|
+
end
|
26
|
+
|
27
|
+
def remove_key(yaml_string, key_list)
|
28
|
+
# run the python file with the virtual environment
|
29
|
+
stdout_str, stderr_str, status = execute_with_local_python('remove_key.py', yaml_string, key_list.to_json)
|
30
|
+
if not status.success?
|
31
|
+
raise "\n\nFailed to remove key in yaml file:\n#{stderr_str}"
|
32
|
+
end
|
33
|
+
return stdout_str
|
34
|
+
end
|
35
|
+
|
36
|
+
# yaml_test_string = <<-HEREDOC
|
37
|
+
# foo:
|
38
|
+
# a: 1
|
39
|
+
# b: 2
|
40
|
+
# list:
|
41
|
+
# - 1
|
42
|
+
# - 2
|
43
|
+
# - 3
|
44
|
+
# thing:
|
45
|
+
# a: 10
|
46
|
+
# HEREDOC
|
47
|
+
# puts remove_key(yaml_test_string, ["foo", "list", 2])
|
48
|
+
# puts set_key(yaml_test_string, ["foo", "c"], 3)
|
data/test/main.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: atk_toolbox
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeff Hykin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-07 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: ''
|
14
14
|
email: jeff.hykin@gmail.com
|
@@ -22,8 +22,13 @@ files:
|
|
22
22
|
- lib/atk/os.rb
|
23
23
|
- lib/atk/output.rb
|
24
24
|
- lib/atk/package_managers.rb
|
25
|
+
- lib/atk/remove_indent.rb
|
25
26
|
- lib/atk/yaml_info_parser.rb
|
26
27
|
- lib/atk_toolbox.rb
|
28
|
+
- lib/atk_toolbox/version.rb
|
29
|
+
- lib/yaml_edit/remove_key.py
|
30
|
+
- lib/yaml_edit/set_key.py
|
31
|
+
- lib/yaml_edit/yaml_edit.rb
|
27
32
|
- test/info.yaml
|
28
33
|
- test/main.rb
|
29
34
|
homepage: http://github.com//atk-toolbox
|