Stanza 0.0.1
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 +7 -0
- data/lib/Stanza.rb +7 -0
- data/lib/Stanza/Stanza.rb +114 -0
- data/lib/Stanza/StanzaFile.rb +179 -0
- metadata +47 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: a218c99396e4c4069413b07ea33cd4cb7d9f5003
|
|
4
|
+
data.tar.gz: 80a9e154e07bfc0080964d2c06af37bb4051f449
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 4248ba3ffb5fd24425cc391f35971d8b361873878e2f6210e1fdedc30889af367266faf69233e3cb70ab1f080d76cc4b0ea2b13cabca87b9b04f7c578f5a3e15
|
|
7
|
+
data.tar.gz: 28f1414426cf861b82faba376171ddb0b3f240c5c5e4e6bd82167c28e2a3710618e47761a502ff8ba5b0bd72e751a1b26b01beffca11b0b7cf5466eddb5ad245
|
data/lib/Stanza.rb
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
#!/usr/bin/ruby
|
|
2
|
+
|
|
3
|
+
require 'erb'
|
|
4
|
+
require 'tempfile'
|
|
5
|
+
|
|
6
|
+
module Stanza
|
|
7
|
+
class Stanza
|
|
8
|
+
def initialize(name, attrs)
|
|
9
|
+
@name = name
|
|
10
|
+
@attrs = attrs
|
|
11
|
+
@comment = ''
|
|
12
|
+
@commentChar = '*'
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def copy(stanza)
|
|
16
|
+
if stanza.kind_of?(Stanza)
|
|
17
|
+
empty!
|
|
18
|
+
@attrs = stanza.attrs
|
|
19
|
+
@comment = stanza.comment
|
|
20
|
+
@commentChar = stanza.commentChar
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def addComment(comment)
|
|
25
|
+
@comment += comment + "\n"
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def delComment()
|
|
29
|
+
@comment = ''
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def addAttribute(name, value)
|
|
33
|
+
@attrs[name] = value
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def delAttribute(name)
|
|
37
|
+
@attrs.delete(name) if attrs.key?(name)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def getAttribute(name)
|
|
41
|
+
return @attrs[name] if attrs.key?(name)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def attributes
|
|
45
|
+
@attrs.keys
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def values
|
|
49
|
+
@attrs.values
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def hash
|
|
53
|
+
@attrs
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def empty!
|
|
57
|
+
@attrs.clear
|
|
58
|
+
@name = ''
|
|
59
|
+
@comment = ''
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def each
|
|
63
|
+
@attrs.each { |k, v| yield(k, v) }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def each_attribute
|
|
67
|
+
@attrs.each_key { |k| yield(k) }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def each_value
|
|
71
|
+
@attrs.each_value { |v| yield(v) }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def empty?
|
|
75
|
+
@attrs.empty?
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def has_attribute?(name)
|
|
79
|
+
@attrs.has_key(name)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def merge!(otherStanza)
|
|
83
|
+
if otherStanza.kind_of?(Stanza)
|
|
84
|
+
@attrs.merge!(otherStanza.attrs)
|
|
85
|
+
@comment += otherStanza.comment
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def to_s
|
|
90
|
+
s = ERB.new("<% @comment.lines do |line| %>
|
|
91
|
+
<%= @commentChar %> <%= line %>
|
|
92
|
+
<% end %>
|
|
93
|
+
<%= @name %>:
|
|
94
|
+
<% @attrs.each do |key, value| %>
|
|
95
|
+
<%= key %> = <%= value.chomp %>
|
|
96
|
+
<% end %>
|
|
97
|
+
", 0, '<>').result(binding)
|
|
98
|
+
return s
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
attr_accessor :name, :attrs, :comment, :commentChar
|
|
102
|
+
alias keys attributes
|
|
103
|
+
alias setAttribute addAttribute
|
|
104
|
+
alias attribute getAttribute
|
|
105
|
+
alias key getAttribute
|
|
106
|
+
alias each_attr each_attribute
|
|
107
|
+
alias each_key each_attribute
|
|
108
|
+
alias each_pair each
|
|
109
|
+
alias has_key? has_attribute?
|
|
110
|
+
alias key? has_attribute?
|
|
111
|
+
alias clear! empty!
|
|
112
|
+
alias inspect to_s
|
|
113
|
+
end
|
|
114
|
+
end
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
#!/usr/bin/ruby
|
|
2
|
+
|
|
3
|
+
require 'erb'
|
|
4
|
+
require 'tempfile'
|
|
5
|
+
|
|
6
|
+
module Stanza
|
|
7
|
+
class StanzaFile
|
|
8
|
+
def initialize(file)
|
|
9
|
+
@fileName = file
|
|
10
|
+
@commentChar = '*'
|
|
11
|
+
@comment = ''
|
|
12
|
+
@stanzas = []
|
|
13
|
+
if ::File.exist?(file)
|
|
14
|
+
readStanzaFile(file)
|
|
15
|
+
else
|
|
16
|
+
::File.new(file, File::CREAT|File::WRONLY, 0644)
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def read!
|
|
21
|
+
empty!
|
|
22
|
+
readStanzaFile(@fileName)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def write!
|
|
26
|
+
writeStanzaFile(@fileName)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def empty!
|
|
30
|
+
@comment = ''
|
|
31
|
+
@stanzas = []
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def readFromFile(file)
|
|
35
|
+
empty!
|
|
36
|
+
readStanzaFile(file)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def writeToFile(filename)
|
|
40
|
+
writeStanzaFile(filename)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def addComment(comment)
|
|
44
|
+
@comment += comment + "\n"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def addStanza(stanza)
|
|
48
|
+
if stanza.kind_of?(Stanza)
|
|
49
|
+
@stanzas << stanza
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def setStanza(name, stanza)
|
|
54
|
+
if stanza.kind_of?(Stanza)
|
|
55
|
+
@stanzas.each do |st|
|
|
56
|
+
st.copy(stanza) if st.name == name
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def deleteStanza(name)
|
|
62
|
+
@stanzas.each do |st|
|
|
63
|
+
@stanzas.delete(st) if st.name == name
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def getStanza(name)
|
|
68
|
+
@stanzas.each do |st|
|
|
69
|
+
return st if st.name == name
|
|
70
|
+
end
|
|
71
|
+
return nil
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def setStanzaAttr(stanzaName, attr, value)
|
|
75
|
+
@stanzas.each do |st|
|
|
76
|
+
if st.name == stanzaName
|
|
77
|
+
st.setAttribute(attr, value)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def getStanzaAttr(stanzaName, attr)
|
|
83
|
+
@stanzas.each do |st|
|
|
84
|
+
if st.name == stanzaName
|
|
85
|
+
return st.getAttribute(attr)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def deleteStanzaAttr(stanzaName, attr)
|
|
91
|
+
@stanzas.each do |st|
|
|
92
|
+
if st.name == stanzaName
|
|
93
|
+
st.delAttribute(attr)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def to_s
|
|
99
|
+
@stanzas.each do |st|
|
|
100
|
+
st.commentChar = @commentChar
|
|
101
|
+
end
|
|
102
|
+
s = ERB.new("<% @comment.lines do |line| %>
|
|
103
|
+
<%= @commentChar %> <%= line %>
|
|
104
|
+
<% end %>
|
|
105
|
+
|
|
106
|
+
<% @stanzas.each do |st| %>
|
|
107
|
+
<%= st %>
|
|
108
|
+
|
|
109
|
+
<% end %>
|
|
110
|
+
", 0, '<>').result(binding)
|
|
111
|
+
return s
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
private
|
|
115
|
+
def writeStanzaFile(file)
|
|
116
|
+
open(file, 'w') do |f|
|
|
117
|
+
f.write(to_s)
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def readStanzaFile(file)
|
|
122
|
+
open(file) do |f|
|
|
123
|
+
stanzaName = ''
|
|
124
|
+
stanzaLines = []
|
|
125
|
+
nextcomment = ''
|
|
126
|
+
f.each do |line|
|
|
127
|
+
next if line.nil?
|
|
128
|
+
line.rstrip!
|
|
129
|
+
if line.start_with?("#{@commentChar}")
|
|
130
|
+
if stanzaName == '-'
|
|
131
|
+
nextcomment += line.sub(/^./, "").strip + "\n"
|
|
132
|
+
else
|
|
133
|
+
@comment += line.sub(/^./, "").strip + "\n"
|
|
134
|
+
end
|
|
135
|
+
next
|
|
136
|
+
end
|
|
137
|
+
if line.end_with?(':')
|
|
138
|
+
stanzaName = line.gsub(/:$/, "")
|
|
139
|
+
next
|
|
140
|
+
end
|
|
141
|
+
if line.empty?
|
|
142
|
+
unless stanzaName == '' || stanzaName == '-'
|
|
143
|
+
s = readStanza(stanzaName, stanzaLines)
|
|
144
|
+
s.comment = nextcomment unless s.nil?
|
|
145
|
+
@stanzas << s unless s.nil?
|
|
146
|
+
stanzaName = '-'
|
|
147
|
+
stanzaLines = []
|
|
148
|
+
nextcomment = ''
|
|
149
|
+
end
|
|
150
|
+
next
|
|
151
|
+
end
|
|
152
|
+
stanzaLines << line unless stanzaName == '' || stanzaName == '-'
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def readStanza(name, lines)
|
|
158
|
+
return nil if name == '' || name == '-'
|
|
159
|
+
h = Hash.new
|
|
160
|
+
lines.each do |line|
|
|
161
|
+
line.strip!
|
|
162
|
+
sAttr, sValue = line.split('=')
|
|
163
|
+
next if sAttr.nil?
|
|
164
|
+
next if sValue.nil?
|
|
165
|
+
sAttr.strip!
|
|
166
|
+
sValue.strip!
|
|
167
|
+
h[sAttr] = sValue
|
|
168
|
+
end
|
|
169
|
+
s = Stanza.new(name, h)
|
|
170
|
+
return s
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
attr_accessor :commentChar, :stanzas
|
|
174
|
+
alias clear! empty!
|
|
175
|
+
alias inspect to_s
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
|
metadata
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: Stanza
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Andrey Klyachkin
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-05-23 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Ruby interface to text files in stanza format
|
|
14
|
+
email: andrey.klyachkin@enfence.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/Stanza.rb
|
|
20
|
+
- lib/Stanza/Stanza.rb
|
|
21
|
+
- lib/Stanza/StanzaFile.rb
|
|
22
|
+
homepage: https://github.com/enfence/rb-stanza
|
|
23
|
+
licenses:
|
|
24
|
+
- Apache-2.0
|
|
25
|
+
metadata: {}
|
|
26
|
+
post_install_message:
|
|
27
|
+
rdoc_options: []
|
|
28
|
+
require_paths:
|
|
29
|
+
- lib
|
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '0'
|
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
requirements: []
|
|
41
|
+
rubyforge_project:
|
|
42
|
+
rubygems_version: 2.5.2
|
|
43
|
+
signing_key:
|
|
44
|
+
specification_version: 4
|
|
45
|
+
summary: Ruby interface to text files in stanza format
|
|
46
|
+
test_files: []
|
|
47
|
+
has_rdoc:
|