Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/parser/ini.rb
Views: 11780
# -*- coding: binary -*-1module Rex2module Parser34###5#6# This class parses the contents of an INI file.7#8###9class Ini < Hash1011##12#13# Factories14#15##1617#18# Creates a new class instance and reads in the contents of the supplied19# file path.20#21def self.from_file(path)22ini = Ini.new(path)23ini.from_file24return ini25end2627#28# Creates a new class instance from the supplied string.29#30def self.from_s(str)31ini = Ini.new32ini.from_s(str)33return ini34end3536#37# Initializes an ini instance and tries to read in the groups from the38# file if it exists.39#40def initialize(path = nil)41self.path = path4243# Try to synchronize ourself with the file if we44# have one45begin46self.from_file if (self.path)47rescue48end49end5051alias each_group each_key5253#54# Adds a group of the supplied name if it doesn't already exist.55#56def add_group(name = 'global', reset = true)57self[name] = {} if (reset == true)58self[name] = {} if (!self[name])5960return self[name]61end6263#64# Checks to see if name is a valid group.65#66def group?(name)67return (self[name] != nil)68end6970##71#72# Serializers73#74##7576#77# Reads in the groups from the supplied file path or the instance's file78# path.79#80def from_file(fpath = nil)81fpath = path if (!fpath)8283read_groups(fpath)84end8586#87# Reads in the groups from the supplied string.88#89def from_s(str)90read_groups_string(str.split("\n"))91end9293#94# Writes the group settings to a file.95#96def to_file(tpath = nil)97tpath = path if (!tpath)9899f = File.new(tpath, "w")100f.write(to_s)101f.close102end103104#105# Converts the groups to a string.106#107def to_s108str = ''109keys.sort.each { |k|110str << "[#{k}]\n"111112self[k].each_pair { |var, val|113str << "#{var}=#{val}\n"114}115116str << "\n";117}118119return str120end121122attr_reader :path123124protected125126#127# Reads in the groups and their attributes from the supplied file128# path or from the instance's file path if one was set.129#130def read_groups(fpath) # :nodoc:131if (!fpath)132raise ArgumentError, "No file path specified.",133caller134end135136# Read in the contents of the file137lines = ::IO.readlines(fpath)138139# Now read the contents from the supplied string140read_groups_string(lines)141end142143#144# Reads groups from the supplied string145#146def read_groups_string(str) # :nodoc:147# Reset the groups hash148self.clear149150# The active group151active_group = nil152153# Walk each line initializing the groups154str.each { |line|155next if (line.match(/^;/))156157# Eliminate cr/lf158line.gsub!(/(\n|\r)/, '')159160# Is it a group [bob]?161if (md = line.match(/^\[(.+?)\]/))162active_group = md[1]163self[md[1]] = {}164# Is it a VAR=VAL?165elsif (md = line.match(/^(.+?)=(.*)$/))166if (active_group)167var, val = md[1], md[2]168169# don't clobber datastore nils with ""170unless val.empty?171self[active_group][var] = val172end173end174end175}176end177178attr_writer :path # :nodoc:179180end181182end183end184185186