~ubuntu-branches/debian/stretch/zim/stretch

« back to all changes in this revision

Viewing changes to zim/parsing.py

  • Committer: Package Import Robot
  • Author(s): Raphaël Hertzog
  • Date: 2013-05-02 17:48:47 UTC
  • mfrom: (1.2.23)
  • Revision ID: package-import@ubuntu.com-20130502174847-6q5sw0zarzcb08t5
Tags: 0.60-1
* New upstream release.
* Update Standards-Version to 3.9.4.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
import re
8
8
 
9
9
 
10
 
def split_quoted_strings(string, unescape=True):
 
10
def split_quoted_strings(string, unescape=True, strict=True):
11
11
        '''Split a word list respecting quotes
12
12
 
13
13
        This function always expect full words to be quoted, even if quotes
24
24
        @param string: string to split in words
25
25
        @param unescape: if C{True} quotes are removed, else they are
26
26
        left in place
 
27
        @param strict: if C{True} unmatched quotes will cause a C{ValueError}
 
28
        to be raised, if C{False} unmatched quotes are ignored.
27
29
        @returns: list of strings
28
30
        '''
29
31
        word_re = Re(r'''
38
40
                words.append(word_re[0])
39
41
                i = word_re.m.end()
40
42
                string = string[i:].lstrip()
41
 
        assert not string
 
43
 
 
44
        if string and strict:
 
45
                raise ValueError, 'Unmatched quote'
 
46
        elif string:
 
47
                words += string.split()
 
48
 
42
49
        if unescape:
43
50
                words = [unescape_quoted_string(w) for w in words]
44
51
        return words
51
58
        escape_re = re.compile(r'(\\(\\)|\\([\'\"]))')
52
59
        def replace(m):
53
60
                return m.group(2) or m.group(3)
54
 
        if string.startswith('"') or string.startswith("'"):
 
61
        if (string.startswith('"') or string.startswith("'")) \
 
62
        and string[-1] == string[0]:
55
63
                string = string[1:-1]
56
64
                string = escape_re.sub(replace, string)
57
65
        return string