blob: 629edd180db823173d3447b48eb729b6f80c945d [file] [log] [blame]
piotr437f5462014-02-04 17:57:25 +01001#
2# Copyright 2010 Free Software Foundation, Inc.
3#
4# This file is part of GNU Radio
5#
6# GNU Radio is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3, or (at your option)
9# any later version.
10#
11# GNU Radio is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with GNU Radio; see the file COPYING. If not, write to
18# the Free Software Foundation, Inc., 51 Franklin Street,
19# Boston, MA 02110-1301, USA.
20#
21"""
22Utilities for extracting text from generated classes.
23"""
24
25def is_string(txt):
26 if isinstance(txt, str):
27 return True
28 try:
29 if isinstance(txt, unicode):
30 return True
31 except NameError:
32 pass
33 return False
34
35def description(obj):
36 if obj is None:
37 return None
38 return description_bit(obj).strip()
39
40def description_bit(obj):
41 if hasattr(obj, 'content'):
42 contents = [description_bit(item) for item in obj.content]
43 result = ''.join(contents)
44 elif hasattr(obj, 'content_'):
45 contents = [description_bit(item) for item in obj.content_]
46 result = ''.join(contents)
47 elif hasattr(obj, 'value'):
48 result = description_bit(obj.value)
49 elif is_string(obj):
50 return obj
51 else:
52 raise StandardError('Expecting a string or something with content, content_ or value attribute')
53 # If this bit is a paragraph then add one some line breaks.
54 if hasattr(obj, 'name') and obj.name == 'para':
55 result += "\n\n"
56 return result