~aaronp/software-center/tests

« back to all changes in this revision

Viewing changes to softwarecenter/utils.py

  • Committer: Aaron Peachey
  • Date: 2011-06-20 09:50:42 UTC
  • mfrom: (1805.10.18 software-center)
  • Revision ID: alpeachey@gmail.com-20110620095042-a5s30o4vtx9l9fgr
mergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
# this program; if not, write to the Free Software Foundation, Inc.,
17
17
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
18
 
19
 
import apt_pkg
20
19
import dbus
21
20
import gmenu
22
21
import gettext
26
25
import logging
27
26
import os
28
27
import re
 
28
import string
29
29
import tempfile
30
30
import traceback
31
31
import time
87
87
        return False
88
88
    return wrapper
89
89
 
 
90
def normalize_package_description(desc):
 
91
    """ this takes a package description and normalizes it
 
92
        so that all uneeded \n are stripped away and all
 
93
        enumerations are at the start of the line and start with a "*"
 
94
        E.g.:
 
95
        Some potentially very long paragrah that is in a single line.
 
96
        A new paragrpah.
 
97
        A list:
 
98
        * item1
 
99
        * item2 that may again be very very long
 
100
    """
 
101
    BULLETS = ('- ', '* ', 'o ')
 
102
    norm_description = ""
 
103
    in_blist = False
 
104
    # process it
 
105
    old_indent_level = 0
 
106
    for i, part in enumerate(desc.split("\n")):
 
107
        part = part.strip()
 
108
        # explicit newline
 
109
        if not part:
 
110
            norm_description += "\n"
 
111
            continue
 
112
        # get indent level
 
113
        for j, c in enumerate(part):
 
114
            if not c in string.whitespace+"".join([s.strip() for s in BULLETS]):
 
115
                indent_level = j
 
116
                break
 
117
        # check if in a enumeration
 
118
        if part[:2] in BULLETS:
 
119
            in_blist = True
 
120
            norm_description += "\n* " + part[2:]
 
121
        elif in_blist and old_indent_level == indent_level:
 
122
            norm_description += " " + part
 
123
        else:
 
124
            in_blist = False
 
125
            if not norm_description.endswith("\n"):
 
126
                norm_description += " "
 
127
            norm_description += part
 
128
        old_indent_level = indent_level
 
129
    return norm_description.strip()
90
130
 
91
 
def htmlize_package_desc(desc):
92
 
    def _is_bullet(line):
93
 
        return re.match("^(\s*[-*])", line)
94
 
    inside_p = False
 
131
def htmlize_package_description(desc):
 
132
    html = ""
95
133
    inside_li = False
96
 
    indent_len = None
97
 
    for line in desc.splitlines():
98
 
        stripped_line = line.strip()
99
 
        if (not inside_p and 
100
 
            not inside_li and 
101
 
            not _is_bullet(line) and
102
 
            stripped_line):
103
 
            yield '<p tabindex="0">'
104
 
            inside_p = True
105
 
        if stripped_line:
106
 
            match = re.match("^(\s*[-*])", line)
107
 
            if match:
108
 
                if inside_li:
109
 
                    yield "</li>"
110
 
                yield "<li>"
 
134
    for part in normalize_package_description(desc).split("\n"):
 
135
        if part.startswith("* "):
 
136
            if not inside_li:
 
137
                html += "<ul>"
111
138
                inside_li = True
112
 
                indent_len = len(match.group(1))
113
 
                stripped_line = line[indent_len:].strip()
114
 
                yield stripped_line
115
 
            elif inside_li:
116
 
                if not line.startswith(" " * indent_len):
117
 
                    yield "</li>"
118
 
                    inside_li = False
119
 
                yield stripped_line
120
 
            else:
121
 
                yield stripped_line
 
139
            html += '<li>%s</li>' % part[2:]
122
140
        else:
123
141
            if inside_li:
124
 
                yield "</li>"
125
 
                inside_li = False
126
 
            if inside_p:
127
 
                yield "</p>"
128
 
                inside_p = False
 
142
                html += "</ul>"
 
143
            html += '<p tabindex="0">%s</p>' % part
 
144
            inside_li = False
129
145
    if inside_li:
130
 
        yield "</li>"
131
 
    if inside_p:
132
 
        yield "</p>"
 
146
        html += "</ul>"
 
147
    return html
133
148
 
134
149
def get_parent_xid(widget):
135
150
    while widget.get_parent():
203
218
        and converts it to
204
219
        'Search...'
205
220
    """
206
 
    import re
207
221
    p = re.compile("\&\#x(\d\d\d\d);")
208
222
    return p.sub(r"\u\1", s).decode("unicode-escape")
209
223
    
213
227
    """
214
228
    return xml.sax.saxutils.unescape(text, ESCAPE_ENTITIES)
215
229
 
216
 
#def get_current_arch():
217
 
#    return apt_pkg.config.find("Apt::Architecture")
218
 
 
219
230
def uri_to_filename(uri):
 
231
    import apt_pkg
220
232
    return apt_pkg.uri_to_filename(uri)
221
233
 
222
234
def human_readable_name_from_ppa_uri(ppa_uri):
513
525
upstream_version = get_pkg_info().upstream_version
514
526
version_compare = get_pkg_info().version_compare
515
527
 
 
528
# only when needed
 
529
try:
 
530
    import apt_pkg
 
531
    size_to_str = apt_pkg.size_to_str
 
532
except ImportError:
 
533
    def size_to_str(size):
 
534
        return str(size)
516
535
        
517
536
if __name__ == "__main__":
518
537
    s = decode_xml_char_reference('Search&#x2026;')