atk_toolbox 0.0.39 → 0.0.40

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: 7615bfcaffd4148abbaa23fea5e6dc9b367b5db9b9b4a415b150f6202e88546c
4
- data.tar.gz: 51abc23807c65959dde210760d4657c2a4f342cf0a279696839904c5afb36f75
3
+ metadata.gz: f6eb32f46498701aef8e52e4e8e5ec4e8ae3a9d324d8bd2e56a83862b4d00280
4
+ data.tar.gz: dab81fc0056d18ad4e623999d5aed82dc3b5db27bc03a92ec0effd05a930a40e
5
5
  SHA512:
6
- metadata.gz: abb86efc828b1c189f4d0087b9f6464aa156782e52ba78142458e0d8e374104a122f49bf37cdb5e00a649f037d5ce0365fa342d62d0d1c1b2ad393a787698327
7
- data.tar.gz: 9910cce97aa11febd1dccee9420be1f2d16b848c1ccbf510042e81047d024ab30e9eb0499d4d3a0b5f5a9ffe604ad9103450f0ec1d9960d19576a360dea181e5
6
+ metadata.gz: 0c8f3af5288138909110c4ba4ddf16491578692c89bae75dcf97f54677d7d08374be6efe87a52f79cd2d4572f5cb74cfd1e62610cd150bed28fc0bf862b4cc84
7
+ data.tar.gz: 80d565ce037a4b369c4c25191a15132f88ad2beed8155389cbd1a59d09e2a8e094dc722518e039f47e42ba22442d2299587562410a8eef57cd133178f5f9d793
@@ -0,0 +1,64 @@
1
+ require_relative './extra_file_utils'
2
+ require_relative './os'
3
+ require 'yaml'
4
+
5
+ class AtkPaths
6
+ def self.[](name)
7
+ # convert symbols to strings
8
+ name = name.to_s
9
+ case name
10
+ when 'atk'
11
+ return HOME/"atk"
12
+ when 'temp'
13
+ return self['atk']/"temp"
14
+ when 'info'
15
+ return HOME/"info.yaml"
16
+ when 'ruby'
17
+ atk_path_settings = ATK.info["paths"]
18
+ if atk_path_settings.is_a?(Hash) and atk_path_settings["ruby"].is_a?(String)
19
+ ruby_path = atk_path_settings["ruby"]
20
+ elsif OS.is?(:unix)
21
+ ruby_path = "/usr/bin/ruby"
22
+ else
23
+ # TODO: fix this case
24
+ ruby_path = ""
25
+ end
26
+ return ruby_path
27
+ end
28
+ end
29
+ end
30
+
31
+ module ATK
32
+ def self.paths
33
+ return AtkPaths
34
+ end
35
+
36
+ def self.temp_path(filepath)
37
+ new_path = ATK.paths[:temp]/filepath
38
+ # make sure the path is empty
39
+ begin
40
+ File.delete(new_path)
41
+ end
42
+ return new_path
43
+ end
44
+
45
+ def self.info
46
+ settings_path = ATK.paths[:info]
47
+ atk_settings_key = "atk_settings"
48
+ # if it doesn't exist then create it
49
+ if not File.exist?(settings_path)
50
+ IO.write(settings_path, "#{atk_settings_key}: {}")
51
+ return {}
52
+ else
53
+ data = YAML.load_file(settings_path)
54
+ if data.is_a?(Hash)
55
+ if data[atk_settings_key].is_a?(Hash)
56
+ puts "data[atk_settings_key] is: #{data[atk_settings_key]} "
57
+ return data[atk_settings_key]
58
+ end
59
+ else
60
+ return {}
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,155 @@
1
+ require_relative './os'
2
+ require_relative './remove_indent'
3
+ require_relative './version'
4
+ require 'json'
5
+ require 'yaml'
6
+
7
+ # how the new parser should work
8
+ # turn every meaninful peice of the yaml file into data that is represented in ruby
9
+ # reference objects instead of actually replacing the reference with the value
10
+ # reference insertion keys instead of actually performing the <<:
11
+ # save the style data (like 0x00 vs 0) to every peice of data
12
+ # save whether or not the data must be single-line
13
+ # tokenize the yaml recursively with the base case of scalar values
14
+ # HARD: detect when values must be single-line or multiline
15
+ # non-complex keys are single-line
16
+ # anything inside of a JSON-like list or array must be single line
17
+ # everything else I know of can be multiline
18
+ # have both an inline and multiline conversion function for every data type
19
+ # when changing data, convert the new data into a single-line or multiline form based on the parent token
20
+ # if multiline, add the correct amount of indentation
21
+
22
+ #
23
+ #
24
+ # JSON method
25
+ #
26
+ #
27
+ # example:
28
+ # doc = <<-HEREDOC
29
+ # (dependencies):
30
+ # python: 3.6.7
31
+ # gcc: 8.0.0
32
+ # HEREDOC
33
+ # document = YamlEdit.new(string:doc)
34
+ # document["(dependencies)"]["python"].replace_value_with(value: "200")
35
+ # puts document.string
36
+ # # creates:
37
+ # doc = <<-HEREDOC
38
+ # (dependencies):
39
+ # python: "200"
40
+ # gcc: 8.0.0
41
+ # HEREDOC
42
+
43
+ def inject_string(string, middle_part, start_line, start_column, end_line, end_column)
44
+ lines = string.split("\n")
45
+ untouched_begining_lines = lines[0...start_line]
46
+ untouched_ending_lines = lines[end_line+1..-1]
47
+ middle_lines = []
48
+
49
+ before_part = lines[start_line][0...start_column]
50
+ after_part = lines[end_line][end_column..-1]
51
+
52
+ return (untouched_begining_lines + [ before_part + middle_part + after_part ] + untouched_ending_lines).join("\n")
53
+ end
54
+
55
+ class YamlEdit
56
+ attr_accessor :string
57
+
58
+ def initialize(string:nil, file:nil)
59
+ self.init(string:string, file: file)
60
+ end
61
+
62
+ def init(string:nil, file:nil)
63
+ if string == nil
64
+ string = IO.read(file)
65
+ end
66
+ @root_node = YAML.parse(string).children[0]
67
+ @string = string
68
+ self.attach_to_all_children(@root_node, self)
69
+ end
70
+
71
+ def attach_to_all_children(node, original_document)
72
+ if node.respond_to?(:children)
73
+ if node.children.is_a?(Array)
74
+ for each in node.children
75
+ self.attach_to_all_children(each, original_document)
76
+ end
77
+ end
78
+ end
79
+ node.document = original_document
80
+ end
81
+
82
+ def [](key)
83
+ return @root_node[key]
84
+ end
85
+
86
+ def save_to(file)
87
+ IO.write(file, @string)
88
+ end
89
+ end
90
+
91
+ class Psych::Nodes::Node
92
+ attr_accessor :document
93
+ def anchor_and_tag(anchor:nil, tag:nil)
94
+ anchor = @anchor if anchor == nil
95
+ tag = @tag if tag == nil
96
+
97
+ string = ""
98
+ if anchor
99
+ string += "&#{anchor} "
100
+ end
101
+
102
+ if tag
103
+ string += "!#{tag} "
104
+ end
105
+ return string
106
+ end
107
+
108
+ # saving this for later once the JSON method can be replaced
109
+ def indent_level
110
+ if self.respond_to?(:children)
111
+ if self.children.size > 0
112
+ return self.children[0].start_column
113
+ end
114
+ end
115
+ return 4 + self.start_column
116
+ end
117
+
118
+ def [](key)
119
+ previous = nil
120
+ # for seq
121
+ if key.is_a?(Integer)
122
+ return self.children[key]
123
+ end
124
+ # for maps
125
+ for each in self.children.reverse
126
+ if each.respond_to?(:value) && each.value == key
127
+ return previous
128
+ end
129
+ previous = each
130
+ end
131
+ end
132
+
133
+ def replace_value_with(value:nil, literal:nil, anchor: nil, tag:nil)
134
+ # check version
135
+ if VERSION_OF_RUBY < "2.5.0"
136
+ raise "\n\nSomewhere, replace_value_with() is being called, which is related to editing yaml\nthe problem is this function needs ruby >= 2.5.0\nbut the code is being run with ruby #{RUBY_VERSION}"
137
+ end
138
+
139
+ if literal == nil
140
+ new_value = value.to_json
141
+ else
142
+ new_value = literal
143
+ end
144
+ middle_part = self.anchor_and_tag(anchor:anchor, tag:tag) + new_value
145
+ new_string = inject_string(@document.string, middle_part, @start_line, @start_column, @end_line, @end_column)
146
+ @document.init( string: new_string )
147
+ end
148
+ end
149
+
150
+
151
+ class String
152
+ def yaml
153
+ return YAML.load(self)
154
+ end
155
+ end
@@ -1,18 +1,20 @@
1
- require 'method_source'
2
1
  require_relative './extra_file_utils'
3
2
  require_relative './os'
3
+ require_relative './atk_info'
4
4
 
5
5
  def set_command(name, code)
6
6
  if OS.is?("unix")
7
7
  exec_path = "/usr/local/bin/#{name}"
8
- local_place = HOME/"atk"/"temp"/name
8
+ local_place = ATK.temp_path(name)
9
9
  # create temp if it doesn't exist
10
10
  FileUtils.makedirs(File.dirname(local_place))
11
+ # add the hash bang
12
+ hash_bang = "#!#{ATK.paths[:ruby]}\n"
11
13
  # create the file
12
- IO.write(local_place, "#!/usr/bin/ruby\n"+code)
14
+ IO.write(local_place, hash_bang+code)
13
15
  # copy to command folder
14
16
  system("sudo", "cp", local_place, exec_path)
15
- system("sudo", "chmod", "o+x", exec_path)
17
+ system("sudo", "chmod", "ugo+x", exec_path)
16
18
  elsif OS.is?("windows")
17
19
  username = `powershell -command "echo $env:UserName"`.chomp
18
20
  exec_path = "C:\\Users\\#{username}\\AppData\\local\\Microsoft\\WindowsApps\\#{name}"
@@ -23,3 +25,5 @@ def set_command(name, code)
23
25
  IO.write(exec_path+".bat", "@echo off\nruby \"#{exec_path}.rb\" %*")
24
26
  end
25
27
  end
28
+
29
+ puts ATK.paths[:ruby]
@@ -0,0 +1,39 @@
1
+ # create a variable for the current ruby version
2
+
3
+ class Version
4
+ attr_accessor :major, :minor, :patch
5
+
6
+ def initialize(version_as_string)
7
+ # TODO: make this more safe/better error handling
8
+ @major, @minor, @patch = version_as_string.split('.').map{|each| each.to_i}
9
+ @as_string = version_as_string
10
+ end
11
+
12
+ def <=>(other_version)
13
+ if not other_version.is_a?(Version)
14
+ raise "When doing version comparision, both sides must be a version object"
15
+ end
16
+ major_size = (@major <=> other_version.major * 100)
17
+ minor_size = (@minor <=> other_version.minor * 10 )
18
+ patch_size = (@patch <=> other_version.patch * 1 )
19
+ return (major_size + minor_size + patch_size) <=> 0
20
+ end
21
+
22
+ def >(other_version)
23
+ return (self <=> other_version) == 1
24
+ end
25
+
26
+ def <(other_version)
27
+ return (self <=> other_version) == -1
28
+ end
29
+
30
+ def ==(other_version)
31
+ return (self <=> other_version) == 0
32
+ end
33
+
34
+ def to_s
35
+ @as_string
36
+ end
37
+ end
38
+
39
+ VERSION_OF_RUBY = Version.new(RUBY_VERSION)
@@ -3,7 +3,7 @@ require "yaml"
3
3
  require_relative './cmd'
4
4
  require_relative './os'
5
5
  require_relative './extra_file_utils'
6
- require_relative '../yaml_edit/yaml_edit.rb'
6
+ require_relative './extra_yaml'
7
7
  require 'fileutils'
8
8
 
9
9
  #
@@ -262,14 +262,4 @@ class Info
262
262
  def self.source_path()
263
263
  return "."/"info.yaml"
264
264
  end
265
-
266
- def self.set_key(key_list, new_value)
267
- path = Info.source_path
268
- IO.write(path, YAML.set_key(IO.read(path), key_list, new_value))
269
- end
270
-
271
- def self.remove_key(key_list, new_value)
272
- path = Info.source_path
273
- IO.write(path, YAML.remove_key(IO.read(path), key_list))
274
- end
275
265
  end
@@ -1,3 +1,3 @@
1
1
  module AtkToolbox
2
- VERSION = '0.0.39'
2
+ VERSION = '0.0.40'
3
3
  end
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.39
4
+ version: 0.0.40
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-08-02 00:00:00.000000000 Z
11
+ date: 2019-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: zip
@@ -50,36 +50,18 @@ dependencies:
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
52
  version: 1.5.0
53
- - !ruby/object:Gem::Dependency
54
- name: method_source
55
- requirement: !ruby/object:Gem::Requirement
56
- requirements:
57
- - - "~>"
58
- - !ruby/object:Gem::Version
59
- version: 0.9.2
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: 0.9.2
63
- type: :runtime
64
- prerelease: false
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - "~>"
68
- - !ruby/object:Gem::Version
69
- version: 0.9.2
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- version: 0.9.2
73
53
  description: ''
74
54
  email: jeff.hykin@gmail.com
75
55
  executables: []
76
56
  extensions: []
77
57
  extra_rdoc_files: []
78
58
  files:
59
+ - lib/atk/atk_info.rb
79
60
  - lib/atk/cmd.rb
80
61
  - lib/atk/default_info.yaml
81
62
  - lib/atk/download.rb
82
63
  - lib/atk/extra_file_utils.rb
64
+ - lib/atk/extra_yaml.rb
83
65
  - lib/atk/git.rb
84
66
  - lib/atk/online.rb
85
67
  - lib/atk/os.rb
@@ -87,13 +69,11 @@ files:
87
69
  - lib/atk/package_managers.rb
88
70
  - lib/atk/remove_indent.rb
89
71
  - lib/atk/set_command.rb
72
+ - lib/atk/version.rb
90
73
  - lib/atk/yaml_info_parser.rb
91
74
  - lib/atk/zip.rb
92
75
  - lib/atk_toolbox.rb
93
76
  - lib/atk_toolbox/version.rb
94
- - lib/yaml_edit/remove_key.py
95
- - lib/yaml_edit/set_key.py
96
- - lib/yaml_edit/yaml_edit.rb
97
77
  - test/info.yaml
98
78
  - test/main.rb
99
79
  homepage: http://github.com//atk-toolbox
@@ -116,7 +96,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
96
  version: '0'
117
97
  requirements: []
118
98
  rubyforge_project:
119
- rubygems_version: 2.7.3
99
+ rubygems_version: 2.7.6.2
120
100
  signing_key:
121
101
  specification_version: 4
122
102
  summary: The Ruby gem for all the standard tools ATK uses internally
@@ -1,29 +0,0 @@
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))
@@ -1,45 +0,0 @@
1
- import ruamel.yaml
2
- import regex as re
3
- import json
4
- import sys
5
-
6
- yaml_string = sys.argv[1]
7
- json_key_list = sys.argv[2]
8
- new_key_value = sys.argv[3]
9
-
10
-
11
- # a tool from https://stackoverflow.com/questions/14692690/access-nested-dictionary-path-via-a-list-of-keys
12
- from functools import reduce # forward compatibility for Python 3
13
- import operator
14
- def get_by_path(data, path):
15
- """Access a nested object in data by item sequence."""
16
- return reduce(operator.getitem, path, data)
17
-
18
- def set_by_path(data, path, value):
19
- """Set a value in a nested object in data by item sequence."""
20
- get_by_path(data, path[:-1])[path[-1]] = value
21
-
22
- # detect the indent
23
- indent_match = re.search(r'^ +', yaml_string, flags=re.MULTILINE)
24
- if indent_match == None:
25
- indent = 4
26
- else:
27
- indent = len(indent_match[0])
28
-
29
-
30
-
31
- # parse the yaml
32
- data = ruamel.yaml.round_trip_load(yaml_string)
33
- # parse the path
34
- path_to_element = json.loads(json_key_list)
35
- # parse the new value
36
- new_value = json.loads(new_key_value)
37
- # set the value
38
- set_by_path(data, path_to_element, new_value)
39
- # output the data
40
- yaml = ruamel.yaml.YAML()
41
- yaml.indent(mapping=4, sequence=4, offset=2)
42
- yaml.width = float("Infinity")
43
- yaml.default_flow_style = False
44
- yaml.dump(data, sys.stdout)
45
- # print(ruamel.yaml.round_trip_dump(data, indent=indent, block_seq_indent=int(indent/2), width=, default_flow_style=False))
@@ -1,58 +0,0 @@
1
- require_relative '../atk/os'
2
- require_relative '../atk/remove_indent'
3
- require 'open3'
4
- require 'json'
5
- require 'yaml'
6
-
7
- def execute_with_local_python(python_file_path, *args)
8
- # save the current directory
9
- pwd = Dir.pwd
10
- # change to where this file is
11
- Dir.chdir __dir__
12
- # run the python file with the virtual environment
13
- stdout_str, stderr_str, status = Open3.capture3('python3', python_file_path, *args)
14
- # change back to the original dir
15
- Dir.chdir pwd
16
- return [stdout_str, stderr_str, status]
17
- end
18
-
19
- module YAML
20
- def set_key(yaml_string, key_list, new_value)
21
- if not key_list.is_a?(Array)
22
- raise "when using YAML.set_key, the second argument needs to be a list of keys"
23
- end
24
- # run the python file with the virtual environment
25
- stdout_str, stderr_str, status = execute_with_local_python('set_key.py', yaml_string, key_list.to_json, new_value.to_json)
26
- if not status.success?
27
- raise "\n\nFailed to set key in yaml file:\n#{stderr_str}"
28
- end
29
- return stdout_str
30
- end
31
-
32
- def remove_key(yaml_string, key_list)
33
- if not key_list.is_a?(Array)
34
- raise "when using YAML.remove_key, the second argument needs to be a list of keys"
35
- end
36
- # run the python file with the virtual environment
37
- stdout_str, stderr_str, status = execute_with_local_python('remove_key.py', yaml_string, key_list.to_json)
38
- if not status.success?
39
- raise "\n\nFailed to remove key in yaml file:\n#{stderr_str}"
40
- end
41
- return stdout_str
42
- end
43
- module_function :remove_key, :set_key
44
- end
45
-
46
- # yaml_test_string = <<-HEREDOC
47
- # foo:
48
- # a: 1
49
- # b: 2
50
- # list:
51
- # - 1
52
- # - 2
53
- # - 3
54
- # thing:
55
- # a: 10
56
- # HEREDOC
57
- # puts remove_key(yaml_test_string, ["foo", "list", 2])
58
- # puts set_key(yaml_test_string, ["foo", "c"], 3)