Ejemplo n.º 1
0
    def test_paragraphjoiner(self):
        maxdistance = fixer.paragraphjoiner.maxdistance

        paragraphs = [store.paragraph(i, chr(ord('a') + i), None, False) for i in range(26)]
        options = [
                store.option(paragraphs[0], [], [], False),
                store.option(paragraphs[1], [], [], False),
                store.option(paragraphs[5], [], [], False),
                store.option(paragraphs[5+maxdistance-1], [], [], False),
                store.option(paragraphs[15], [], [], False),
                store.option(paragraphs[17], [], [], False),
                store.option(paragraphs[-1], [], [], False)]

        f = fixer.paragraphjoiner(None)
        merged = f._join(paragraphs, options)

        #self.assertEquals(merged, 7)
        #self.assertEquals(len(paragraphs), 19)
        self.assertEquals(options[0].text, 'a')
        self.assertEquals(options[1].text.replace('\n', ''), 'bcde')
        self.assertEquals(options[2].text.replace('\n', ''), 'fghi')
        self.assertEquals(options[3].text, 'j')
        self.assertEquals(options[4].text.replace('\n', ''), 'pq')
        self.assertEquals(options[5].text, 'r')
        self.assertEquals(options[6].text, 'z')

        # join again to make sure nothing is changed
        oldparagraphs = copy.deepcopy(paragraphs)
        oldoptions = copy.deepcopy(options)
        f._join(paragraphs, options)
        self.assertEquals(oldparagraphs, paragraphs)
        self.assertEquals(oldoptions, options)
Ejemplo n.º 2
0
    def test_paragraphjoiner(self):
        maxdistance = fixer.paragraphjoiner.maxdistance

        paragraphs = [store.paragraph(i, chr(ord('a') + i), None, False) for i in range(26)]
        options = [
                store.option(paragraphs[0], [], [], False),
                store.option(paragraphs[1], [], [], False),
                store.option(paragraphs[5], [], [], False),
                store.option(paragraphs[5+maxdistance-1], [], [], False),
                store.option(paragraphs[15], [], [], False),
                store.option(paragraphs[17], [], [], False),
                store.option(paragraphs[-1], [], [], False)]

        f = fixer.paragraphjoiner(None)
        merged = f._join(paragraphs, options)

        #self.assertEquals(merged, 7)
        #self.assertEquals(len(paragraphs), 19)
        self.assertEqual(options[0].text, 'a')
        self.assertEqual(options[1].text.replace('\n', ''), 'bcde')
        self.assertEqual(options[2].text.replace('\n', ''), 'fghi')
        self.assertEqual(options[3].text, 'j')
        self.assertEqual(options[4].text.replace('\n', ''), 'pq')
        self.assertEqual(options[5].text, 'r')
        self.assertEqual(options[6].text, 'z')

        # join again to make sure nothing is changed
        oldparagraphs = copy.deepcopy(paragraphs)
        oldoptions = copy.deepcopy(options)
        f._join(paragraphs, options)
        self.assertEqual(oldparagraphs, paragraphs)
        self.assertEqual(oldoptions, options)
Ejemplo n.º 3
0
def extract(manpage):
    '''extract options from all paragraphs that have been classified as containing
    options'''
    for i, p in enumerate(manpage.paragraphs):
        if p.is_option:
            s, l = extract_option(p.cleantext())
            if s or l:
                expectsarg = any(x.expectsarg for x in s + l)
                s = [x.flag for x in s]
                l = [x.flag for x in l]
                manpage.paragraphs[i] = store.option(p, s, l, expectsarg)
            else:
                logger.error("no options could be extracted from paragraph %r", p)
Ejemplo n.º 4
0
def extract(manpage):
    '''extract options from all paragraphs that have been classified as containing
    options'''
    for i, p in enumerate(manpage.paragraphs):
        if p.is_option:
            s, l = extract_option(p.cleantext())
            if s or l:
                expectsarg = any(x.expectsarg for x in s + l)
                s = [x.flag for x in s]
                l = [x.flag for x in l]
                manpage.paragraphs[i] = store.option(p, s, l, expectsarg)
            else:
                logger.error("no options could be extracted from paragraph %r", p)
Ejemplo n.º 5
0
def tag(source):
    mngr = manager.manager(config.MONGO_URI, 'explainshell', [], False, False)
    s = mngr.store
    m = s.findmanpage(source)[0]
    assert m

    if 'paragraphs' in request.form:
        paragraphs = json.loads(request.form['paragraphs'])
        mparagraphs = []
        for d in paragraphs:
            idx = d['idx']
            text = d['text']
            section = d['section']
            short = [s.strip() for s in d['short']]
            long = [s.strip() for s in d['long']]
            expectsarg = _convertvalue(d['expectsarg'])
            nestedcommand = _convertvalue(d['nestedcommand'])
            if isinstance(nestedcommand, str):
                nestedcommand = [nestedcommand]
            elif nestedcommand is True:
                logger.error('nestedcommand %r must be a string or list',
                             nestedcommand)
                abort(503)
            argument = d['argument']
            if not argument:
                argument = None
            p = store.paragraph(idx, text, section, d['is_option'])
            if d['is_option'] and (short or int or argument):
                p = store.option(p, short, int, expectsarg, argument,
                                 nestedcommand)
            mparagraphs.append(p)

        if request.form.get('nestedcommand', '').lower() == 'true':
            m.nestedcommand = True
        else:
            m.nestedcommand = False
        m = mngr.edit(m, mparagraphs)
        if m:
            return redirect(url_for('explain', cmd=m.name))
        else:
            abort(503)
    else:
        helpers.convertparagraphs(m)
        for p in m.paragraphs:
            if isinstance(p, store.option):
                if isinstance(p.expectsarg, list):
                    p.expectsarg = ', '.join(p.expectsarg)
                if isinstance(p.nestedcommand, list):
                    p.nestedcommand = ', '.join(p.nestedcommand)

        return render_template('tagger.html', m=m)
Ejemplo n.º 6
0
def tag(source):
    mngr = manager.manager(config.MONGO_URI, 'explainshell', [], False, False)
    s = mngr.store
    m = s.findmanpage(source)[0]
    assert m

    if 'paragraphs' in request.form:
        paragraphs = json.loads(request.form['paragraphs'])
        mparagraphs = []
        for d in paragraphs:
            idx = d['idx']
            text = d['text']
            section = d['section']
            short = [s.strip() for s in d['short']]
            long = [s.strip() for s in d['long']]
            expectsarg = _convertvalue(d['expectsarg'])
            nestedcommand = _convertvalue(d['nestedcommand'])
            if isinstance(nestedcommand, str):
                nestedcommand = [nestedcommand]
            elif nestedcommand is True:
                logger.error('nestedcommand %r must be a string or list', nestedcommand)
                abort(503)
            argument = d['argument']
            if not argument:
                argument = None
            p = store.paragraph(idx, text, section, d['is_option'])
            if d['is_option'] and (short or long or argument):
                p = store.option(p, short, long, expectsarg, argument, nestedcommand)
            mparagraphs.append(p)

        if request.form.get('nestedcommand', '').lower() == 'true':
            m.nestedcommand = True
        else:
            m.nestedcommand = False
        m = mngr.edit(m, mparagraphs)
        if m:
            return redirect(url_for('explain', cmd=m.name))
        else:
            abort(503)
    else:
        helpers.convertparagraphs(m)
        for p in m.paragraphs:
            if isinstance(p, store.option):
                if isinstance(p.expectsarg, list):
                    p.expectsarg = ', '.join(p.expectsarg)
                if isinstance(p.nestedcommand, list):
                    p.nestedcommand = ', '.join(p.nestedcommand)

        return render_template('tagger.html', m=m)
Ejemplo n.º 7
0
def tag(source):
    mngr = manager.manager(config.MONGO_URI, 'explainshell', [], False, False)
    s = mngr.store
    m = s.findmanpage(source)[0]
    assert m

    if 'paragraphs' in request.form:
        paragraphs = json.loads(request.form['paragraphs'])
        mparagraphs = []
        for d in paragraphs:
            idx = d['idx']
            text = d['text']
            section = d['section']
            short = [s.strip() for s in d['short']]
            long = [s.strip() for s in d['long']]
            if isinstance(d['expectsarg'], list):
                expectsarg = [s.strip() for s in d['expectsarg']]
            elif d['expectsarg'].lower() == 'true':
                expectsarg = True
            elif d['expectsarg']:
                expectsarg = d['expectsarg']
            else:
                expectsarg = False
            argument = d['argument']
            if not argument:
                argument = None
            p = store.paragraph(idx, text, section, d['is_option'])
            if d['is_option'] and (short or long or argument):
                p = store.option(p, short, long, expectsarg, argument)
            mparagraphs.append(p)

        m = mngr.edit(m, mparagraphs)
        if m:
            return redirect(url_for('explain', program=m.name))
        else:
            abort(503)
    else:
        helpers.convertparagraphs(m)
        for p in m.paragraphs:
            if isinstance(p, store.option) and isinstance(p.expectsarg, list):
                p.expectsarg = ', '.join(p.expectsarg)

        return render_template('tagger.html', m=m)
Ejemplo n.º 8
0
def tag(source):
    mngr = manager.manager(config.MONGO_URI, 'explainshell', [], False, False)
    s = mngr.store
    m = s.findmanpage(source)[0]
    assert m

    if 'paragraphs' in request.form:
        paragraphs = json.loads(request.form['paragraphs'])
        mparagraphs = []
        for d in paragraphs:
            idx = d['idx']
            text = d['text']
            section = d['section']
            short = [s.strip() for s in d['short']]
            long = [s.strip() for s in d['long']]
            if isinstance(d['expectsarg'], list):
                expectsarg = [s.strip() for s in d['expectsarg']]
            elif d['expectsarg'].lower() == 'true':
                expectsarg = True
            elif d['expectsarg']:
                expectsarg = d['expectsarg']
            else:
                expectsarg = False
            argument = d['argument']
            if not argument:
                argument = None
            p = store.paragraph(idx, text, section, d['is_option'])
            if d['is_option'] and (short or long or argument):
                p = store.option(p, short, long, expectsarg, argument)
            mparagraphs.append(p)

        m = mngr.edit(m, mparagraphs)
        if m:
            return redirect(url_for('explain', program=m.name))
        else:
            abort(503)
    else:
        helpers.convertparagraphs(m)
        for p in m.paragraphs:
            if isinstance(p, store.option) and isinstance(p.expectsarg, list):
                p.expectsarg = ', '.join(p.expectsarg)

        return render_template('tagger.html', m=m)