def test_optlist_no_vals(): d = optlist_to_dict('key1,key2=val2,key1=val1,key3', kv_sep=None) assert sorted(d.keys()) == sorted(['key1', 'key1=val1', 'key2=val2', 'key3']) assert d['key1'] is True assert d['key1=val1'] is True assert d['key2=val2'] is True assert d['key3'] is True
def _parse_mounts(self, content): self.rows = [] self.mounts = {} for line in get_active_lines(content): mount = {} mount['mount_clause'] = line # Handle the '\040' in `mount_point`, e.g. "VMware\040Tools" line_sp = line.encode().decode("unicode-escape") line_sp = _customized_split(raw=line, l=line_sp) mount['filesystem'] = mount['mounted_device'] = line_sp[0] line_sp = _customized_split(raw=line, l=line_sp[1], num=3, reverse=True) mount['mount_label'] = line_sp[-2:] line_sp = _customized_split(raw=line, l=line_sp[0], reverse=True) mount['mount_options'] = MountOpts(optlist_to_dict(line_sp[1])) line_sp = _customized_split(raw=line, l=line_sp[0], reverse=True) mount['mount_type'] = mount['filesystem_type'] = line_sp[1] mount['mount_point'] = line_sp[0] entry = MountEntry(mount) self.rows.append(entry) self.mounts[mount['mount_point']] = entry
def _parse_mounts(self, content): self.rows = [] self.mounts = {} for line in get_active_lines(content): mount = {} mount['mount_clause'] = line # Get the mounted filesystem by checking the ' on ' line_sp = _customized_split(line, line, sep=' on ') mount['filesystem'] = line_sp[0] # Get the mounted point by checking the last ' type ' before the last '(' mnt_pt_sp = _customized_split(raw=line, l=line_sp[1], sep=' (', reverse=True) line_sp = _customized_split(raw=line, l=mnt_pt_sp[0], sep=' type ', reverse=True) mount['mount_point'] = line_sp[0] mount['mount_type'] = line_sp[1].split()[0] line_sp = _customized_split(raw=line, l=mnt_pt_sp[1], sep=None, check=False) mount['mount_options'] = MountOpts( optlist_to_dict(line_sp[0].strip('()'))) if len(line_sp) == 2: mount['mount_label'] = line_sp[1] entry = MountEntry(mount) self.rows.append(entry) self.mounts[mount['mount_point']] = entry
def parse_content(self, content): """ Parse each line in the file ``/etc/fstab``. """ fstab_output = parse_delimited_table([FS_HEADINGS] + get_active_lines(content)) self.data = [] for line in fstab_output: line['fs_spec'] = line.get('fs_spec', '') line['fs_vfstype'] = line.get('fs_vfstype', '') # Decode fs_file to transfer the '\040' to ' '. # Encode first and then decode works for both Python2 and Python3. line['fs_file'] = line.get('fs_file', '').encode().decode("unicode-escape") line['fs_freq'] = int(line.get('fs_freq', '0')) line['fs_passno'] = int(line.get('fs_passno', '0')) # if there is no mntops, it is defaults. # (/dev/foo /foo somefs defaults 0 0) and (/dev/foo /foo somefs) are same line['raw_fs_mntops'] = line.get('raw_fs_mntops', 'defaults') # optlist_to_dict converts 'key=value' to key: value and 'key' to key: True line['fs_mntops'] = MountOpts( optlist_to_dict(line.get('raw_fs_mntops'))) # add `raw` here for displaying convenience on front-end line['raw'] = [ l for l in content if l.strip().startswith(line['fs_spec']) ][0] self.data.append(FSTabEntry(line)) # assert: all mount points of valid entries are unique by definition self.mounted_on = dict((row.fs_file, row) for row in self.data)
def test_optlist_strip_quotes(): d = optlist_to_dict( '''key1="foo",key2='bar',key3="mismatched quotes',key4="inner'quotes"''', strip_quotes=True) assert sorted(d.keys()) == sorted(['key1', 'key2', 'key3', 'key4']) assert d['key1'] == 'foo' assert d['key2'] == 'bar' assert d['key3'] == '"mismatched quotes\'' assert d['key4'] == "inner'quotes"
def parse_content(self, content): """ Details of all the bridges are extracted and stored in a list as dictionary elements. Each dictionary element contains the information of a specific bridge. """ # No content found or file is empty if not content: raise SkipException("Empty file") self.data = [] bridge_details = {} for line in get_active_lines(content): key, value = [i.strip() for i in line.split(":", 1)] parsed_value = value.strip('"') if value.startswith("{") and value.endswith("}"): parsed_value = {} value = value.strip("{}") if value: parsed_value = optlist_to_dict(value, opt_sep=", ", strip_quotes=True) elif value.startswith("[") and value.endswith("]"): parsed_value = [] value = value.strip("[]") if value: parsed_value = [ i.strip(' \t\"\'') for i in value.split(",") ] if key not in bridge_details: bridge_details[key] = parsed_value else: # A new bridge comes self.data.append(bridge_details) bridge_details = {key: parsed_value} # Add the last bridge self.data.append(bridge_details)
def test_optlist_with_spaces(): d = optlist_to_dict('''key1=foo, key2=bar''') assert 'key1' in d assert 'key2' in d
def test_optlist_standard(): d = optlist_to_dict('key1,key2=val2,key1=val1,key3') assert sorted(d.keys()) == sorted(['key1', 'key2', 'key3']) assert d['key1'] == 'val1' assert d['key2'] == 'val2' assert d['key3'] is True