Class: Yast::AsciiFileClass
- Inherits:
-
Module
- Object
- Module
- Yast::AsciiFileClass
- Defined in:
- ../../library/general/src/modules/AsciiFile.rb
Overview
Assume this /etc/fstab file
# main filesystem
UUID=001c0d61-e99f-4ab7-ba4b-bda6f54a052d / btrfs defaults 0 0
then with this set-up
file = {}
file_ref = arg_ref(file) # an artefact of YCP API
AsciiFile.SetComment(file_ref, "^[ \t]*#")
AsciiFile.SetDelimiter(file_ref, " \t")
AsciiFile.SetListWidth(file_ref, [20, 20, 10, 21, 1, 1])
this main call
AsciiFile.ReadFile(file_ref, "/etc/fstab")
# note that the result is `file["l"]`
# as the rest of `file` are the parsing parameters
result = file["l"]
will produce
result.size # => 2
# an integer keyed hash starting at ONE!
result.keys # => [1, 2]
result[1]["comment"] # => true
result[1]["line"] # => "# main filesystem"
result[2]["fields"].size # => 6
result[2]["fields"][1] # => "/"
Instance Method Summary (collapse)
-
- (Object) AppendLine(file, entry)
Appends a new line at the bottom.
-
- (Object) AssertLineValid(file, line)
Private function.
-
- (Object) ChangeLineField(file, line, field, entry)
Changes the record in the file defined by row and column.
-
- (Array<Fixnum>) FindLineField(file, field, content)
Returns the list of rows where matches searched string in the defined column.
-
- (Hash) GetLine(file, line)
Returns map of wanted line.
-
- (Hash<Fixnum, Hash>) GetLines(file, lines)
Returns map of wanted lines.
- - (Object) main
-
- (Fixnum) NumLines(file)
Returns count of lines in file.
-
- (Object) ReadFile(file, pathname)
Reads the file from the disk.
-
- (Object) RemoveLines(file, lines)
Removes lines.
-
- (Object) ReplaceLine(file, line, entry)
Changes a complete line.
-
- (Object) RewriteFile(file, fpath)
Writes a content into the file.
-
- (Object) SetComment(file, comment)
Sets the string how the comment starts.
-
- (Object) SetDelimiter(file, delim)
Sets the delimiter between the records on one line.
-
- (Object) SetListWidth(file, widths)
Sets the widths of records on one line.
Instance Method Details
- (Object) AppendLine(file, entry)
Appends a new line at the bottom
342 343 344 345 346 347 348 349 350 351 352 |
# File '../../library/general/src/modules/AsciiFile.rb', line 342 def AppendLine(file, entry) entry = deep_copy(entry) line = Ops.add(Builtins.size(Ops.get_map(file.value, "l", {})), 1) Builtins.y2debug("new line %1 entry %2", line, entry) Ops.set(file.value, ["l", line], {}) Ops.set(file.value, ["l", line, "fields"], entry) Ops.set(file.value, ["l", line, "changed"], true) Ops.set(file.value, ["l", line, "buildline"], true) nil end |
- (Object) AssertLineValid(file, line)
Private function
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File '../../library/general/src/modules/AsciiFile.rb', line 104 def AssertLineValid(file, line) if Builtins.haskey(Ops.get_map(file.value, "l", {}), line) && Ops.get_boolean(file.value, ["l", line, "buildline"], false) delim = Builtins.substring( Ops.get_string(file.value, "delim", " "), 0, 1 ) lstr = "" num = 0 Builtins.foreach(Ops.get_list(file.value, ["l", line, "fields"], [])) do |text| lstr = Ops.add(lstr, delim) if Ops.greater_than(num, 0) lstr = Ops.add(lstr, text) if Ops.less_than( Builtins.size(text), Ops.get_integer(file.value, ["widths", num], 0) ) lstr = Ops.add( lstr, Builtins.substring( @blanks, 0, Ops.subtract( Ops.get_integer(file.value, ["widths", num], 0), Builtins.size(text) ) ) ) end num = Ops.add(num, 1) end Ops.set(file.value, ["l", line, "line"], lstr) Ops.set(file.value, ["l", line, "buildline"], false) return lstr end Ops.get_string(file.value, ["l", line, "line"], "") end |
- (Object) ChangeLineField(file, line, field, entry)
Changes the record in the file defined by row and column
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File '../../library/general/src/modules/AsciiFile.rb', line 284 def ChangeLineField(file, line, field, entry) Builtins.y2debug("line %1 field %2 entry %3", line, field, entry) changed = false if !Builtins.haskey(Ops.get_map(file.value, "l", {}), line) Ops.set(file.value, ["l", line], {}) Ops.set(file.value, ["l", line, "fields"], []) end if Ops.less_than( Builtins.size(Ops.get_list(file.value, ["l", line, "fields"], [])), field ) changed = true i = 0 while Ops.less_than(i, field) if Builtins.size( Ops.get_string(file.value, ["l", line, "fields", i], "") ) == 0 Ops.set(file.value, ["l", line, "fields", i], "") end i = Ops.add(i, 1) end end if Ops.get_string(file.value, ["l", line, "fields", field], "") != entry Ops.set(file.value, ["l", line, "fields", field], entry) changed = true end if changed Ops.set(file.value, ["l", line, "changed"], true) Ops.set(file.value, ["l", line, "buildline"], true) end nil end |
- (Array<Fixnum>) FindLineField(file, field, content)
Returns the list of rows where matches searched string in the defined column
218 219 220 221 222 223 224 225 226 227 228 229 |
# File '../../library/general/src/modules/AsciiFile.rb', line 218 def FindLineField(file, field, content) file = deep_copy(file) ret = [] Builtins.foreach(Ops.get_map(file, "l", {})) do |num, line| if !Ops.get_boolean(line, "comment", false) && Ops.get_string(line, ["fields", field], "") == content ret = Builtins.add(ret, num) end end Builtins.y2milestone("field %1 content %2 ret %3", field, content, ret) deep_copy(ret) end |
- (Hash) GetLine(file, line)
Returns map of wanted line
256 257 258 259 260 261 262 263 264 265 266 |
# File '../../library/general/src/modules/AsciiFile.rb', line 256 def GetLine(file, line) ret = {} if Builtins.haskey(Ops.get_map(file.value, "l", {}), line) file_ref = arg_ref(file.value) AssertLineValid(file_ref, line) file.value = file_ref.value ret = Ops.get_map(file.value, ["l", line], {}) end Builtins.y2milestone("line %1 ret %2", line, ret) deep_copy(ret) end |
- (Hash<Fixnum, Hash>) GetLines(file, lines)
Returns map of wanted lines
236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File '../../library/general/src/modules/AsciiFile.rb', line 236 def GetLines(file, lines) lines = deep_copy(lines) ret = {} Builtins.foreach(lines) do |num| if Builtins.haskey(Ops.get_map(file.value, "l", {}), num) file_ref = arg_ref(file.value) AssertLineValid(file_ref, num) file.value = file_ref.value Ops.set(ret, num, Ops.get_map(file.value, ["l", num], {})) end end Builtins.y2milestone("lines %1 ret %2", lines, ret) deep_copy(ret) end |
- (Object) main
65 66 67 68 69 70 |
# File '../../library/general/src/modules/AsciiFile.rb', line 65 def main textdomain "base" @blanks = " " end |
- (Fixnum) NumLines(file)
Returns count of lines in file
273 274 275 276 |
# File '../../library/general/src/modules/AsciiFile.rb', line 273 def NumLines(file) file = deep_copy(file) Builtins.size(Ops.get_map(file, "l", {})) end |
- (Object) ReadFile(file, pathname)
Reads the file from the disk
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File '../../library/general/src/modules/AsciiFile.rb', line 147 def ReadFile(file, pathname) Builtins.y2milestone("path=%1", pathname) lines = [] if Ops.greater_than(SCR.Read(path(".target.size"), pathname), 0) value = Convert.to_string(SCR.Read(path(".target.string"), pathname)) lines = Builtins.splitstring(value, "\n") end lineno = 1 lmap = {} Builtins.foreach(lines) do |line| l = {} Ops.set(l, "line", line) if Ops.greater_than( Builtins.size(Ops.get_string(file.value, "comment", "")), 0 ) && Builtins.regexpmatch( line, Ops.get_string(file.value, "comment", "") ) Ops.set(l, "comment", true) end if !Ops.get_boolean(l, "comment", false) && Ops.greater_than( Builtins.size(Ops.get_string(file.value, "delim", "")), 0 ) pos = 0 fields = [] while Ops.greater_than(Builtins.size(line), 0) pos = Builtins.findfirstnotof( line, Ops.get_string(file.value, "delim", "") ) if pos != nil && Ops.greater_than(pos, 0) line = Builtins.substring(line, pos) end pos = Builtins.findfirstof( line, Ops.get_string(file.value, "delim", "") ) if pos != nil && Ops.greater_than(pos, 0) fields = Builtins.add(fields, Builtins.substring(line, 0, pos)) line = Builtins.substring(line, pos) else fields = Builtins.add(fields, line) line = "" end end Ops.set(l, "fields", fields) end Ops.set(lmap, lineno, l) lineno = Ops.add(lineno, 1) end if Ops.greater_than(Builtins.size(lmap), 0) && Builtins.size( Ops.get_string(lmap, [Ops.subtract(lineno, 1), "line"], "") ) == 0 lmap = Builtins.remove(lmap, Ops.subtract(lineno, 1)) end Ops.set(file.value, "l", lmap) nil end |
- (Object) RemoveLines(file, lines)
Removes lines
358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File '../../library/general/src/modules/AsciiFile.rb', line 358 def RemoveLines(file, lines) lines = deep_copy(lines) Builtins.y2debug("lines %1", lines) Builtins.foreach(lines) do |num| if Builtins.haskey(Ops.get_map(file.value, "l", {}), num) Ops.set( file.value, "l", Builtins.remove(Ops.get_map(file.value, "l", {}), num) ) end end nil end |
- (Object) ReplaceLine(file, line, entry)
Changes a complete line
324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File '../../library/general/src/modules/AsciiFile.rb', line 324 def ReplaceLine(file, line, entry) entry = deep_copy(entry) Builtins.y2debug("line %1 entry %2", line, entry) changed = false if !Builtins.haskey(Ops.get_map(file.value, "l", {}), line) Ops.set(file.value, ["l", line], {}) end Ops.set(file.value, ["l", line, "fields"], entry) Ops.set(file.value, ["l", line, "changed"], true) Ops.set(file.value, ["l", line, "buildline"], true) nil end |
- (Object) RewriteFile(file, fpath)
Writes a content into the file
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File '../../library/general/src/modules/AsciiFile.rb', line 378 def RewriteFile(file, fpath) Builtins.y2milestone("path %1", fpath) Builtins.y2debug("out: %1", file.value) out = "" Builtins.foreach(Ops.get_map(file.value, "l", {})) do |num, entry| out = Ops.add( Ops.add( out, ( file_ref = arg_ref(file.value); _AssertLineValid_result = AssertLineValid(file_ref, num); file.value = file_ref.value; _AssertLineValid_result ) ), "\n" ) end Builtins.y2debug("Out text: %1", out) if Builtins.size(out) == 0 if Ops.greater_or_equal(SCR.Read(path(".target.size"), fpath), 0) SCR.Execute(path(".target.remove"), fpath) end else SCR.Write(path(".target.string"), fpath, out) end nil end |
- (Object) SetComment(file, comment)
Sets the string how the comment starts
76 77 78 79 80 |
# File '../../library/general/src/modules/AsciiFile.rb', line 76 def SetComment(file, comment) Ops.set(file.value, "comment", Ops.add(comment, ".*")) nil end |
- (Object) SetDelimiter(file, delim)
Sets the delimiter between the records on one line
97 98 99 100 101 |
# File '../../library/general/src/modules/AsciiFile.rb', line 97 def SetDelimiter(file, delim) Ops.set(file.value, "delim", delim) nil end |
- (Object) SetListWidth(file, widths)
Sets the widths of records on one line
86 87 88 89 90 91 |
# File '../../library/general/src/modules/AsciiFile.rb', line 86 def SetListWidth(file, widths) widths = deep_copy(widths) Ops.set(file.value, "widths", widths) nil end |