Skip to content

Commit

Permalink
Added various methods to access nameless fields
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiNewt committed Feb 28, 2015
1 parent 15b9f3c commit c1efec9
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions src/pyshark/packet/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ def get_default_value(self):
val = self.showname
return val

@property
def showname_value(self):
"""
For fields which do not contain a normal value, we attempt to take their value from the showname.
"""
if self.showname and ': ' in self.showname:
return self.showname.split(': ')[1]

@property
def showname_key(self):
if self.showname and ': ' in self.showname:
return self.showname.split(': ')[0]

def __getstate__(self):
return {slot: getattr(self, slot) for slot in self.__slots__}

Expand Down Expand Up @@ -216,11 +229,16 @@ def pretty_print(self):
tw.write(field_name + ':', green=True, bold=True)
tw.write(field_line, bold=True)

def _get_all_fields_with_alternates(self):
all_fields = self._all_fields.values()
all_fields += sum([field.alternate_fields for field in all_fields], [])
return all_fields

def _get_all_field_lines(self):
"""
Returns all lines that represent the fields of the layer (both their names and values).
"""
for field in self._all_fields.values():
for field in self._get_all_fields_with_alternates():
if field.hide:
continue
if field.showname:
Expand All @@ -229,4 +247,17 @@ def _get_all_field_lines(self):
field_repr = field.show
else:
continue
yield '\t' + field_repr + os.linesep
yield '\t' + field_repr + os.linesep

def get_field_by_showname(self, showname):
"""
Gets a field by its "showname"
(the name that appears in Wireshark's detailed display i.e. in 'User-Agent: Mozilla...', 'User-Agent' is the
showname)
Returns None if not found.
"""
for field in self._get_all_fields_with_alternates():
if field.showname_key == showname:
# Return it if "XXX: whatever == XXX"
return field

0 comments on commit c1efec9

Please sign in to comment.