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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d964a5f967172531ef2a23d40319702f0f797d68aa98e8edc74388822c427e1
4
- data.tar.gz: 8833017e32e64592a6631507cc28d5f7a94837030ebb5c78c3434f882fd375aa
3
+ metadata.gz: 3434dd3967d04310b2feefa38cc06a50afc4acd90557c3c62ac2f152037f9aa2
4
+ data.tar.gz: b61ada0d41d7385173f9ff0a88bab694058c7edec8fec47b681f2b0c0f70deb9
5
5
  SHA512:
6
- metadata.gz: 208563db733b06e4509b198567eee63666e54b002bb858e91a4257e22e147810c11f664dd8c62359d281b6df281811ed7d27326182621bbea803c3a89f444dfc
7
- data.tar.gz: 43b86e89ebf073c2046aba914667e5b76ae52c250e530b05ffa6cbfcce24234af8ab6fa8442a9120bad6c45bd33e818b8cdbf5c052fa6530e35d0b25d316be62
6
+ metadata.gz: 96bcc7c68e260048ae2570d3186f0c65996ab522ace3d1bb17cdd6b24235e67ff5d8dc76db49d6f1390b76a07e6adf47ece42cbf7bdf5966df9e6e565c02820b
7
+ data.tar.gz: b74ba108502a965b2e1353dc9a11a81fb12bed9659d27b761d572ac89e0c701d667e1d2effee01d587f7673b7958fcb5ad46aa81c46abe5e214ed3c000e92c91
data/lib/atk/os.rb CHANGED
@@ -66,7 +66,7 @@ module OS
66
66
  when 'linux'
67
67
  return (not OS.is?(:windows)) && (not OS.is?(:mac))
68
68
  when 'unix'
69
- return OS.is?(:windows) || OS.is?(:mac)
69
+ return not( OS.is?(:windows))
70
70
  end
71
71
  end
72
72
  end
@@ -0,0 +1,10 @@
1
+ class String
2
+ # example usage
3
+ # puts <<-HEREDOC.remove_indent
4
+ # This is a string
5
+ # this part is extra indented
6
+ # HEREDOC
7
+ def remove_indent
8
+ gsub(/^[ \t]{#{self.match(/^[ \t]*/)[0].length}}/, '')
9
+ end
10
+ end
@@ -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
- RubyCode.register_as('!language/ruby')
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
- ConsoleCode.register_as('!language/console')
63
+ register_tag('!language/console', ConsoleCode)
65
64
  #
66
65
  # project info (specific to operating sytem)
67
66
  #
@@ -0,0 +1,3 @@
1
+ module AtkToolbox
2
+ VERSION = '0.0.5'
3
+ end
data/lib/atk_toolbox.rb CHANGED
@@ -1,6 +1,5 @@
1
- require_relative './atk/cmd'
2
- require_relative './atk/download'
3
- require_relative './atk/os'
4
- require_relative './atk/output'
5
- require_relative './atk/package_managers'
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
@@ -1,4 +1,4 @@
1
- require 'atk_toolbox'
1
+ require_relative '../lib/atk_toolbox'
2
2
 
3
3
  Dir.chdir __dir__
4
4
 
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
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-06 00:00:00.000000000 Z
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