MCPcopy Create free account
hub / github.com/ActiveState/code / unquote

Function unquote

recipes/Python/281056_Listparse/recipe-281056.py:124–185  ·  view source on GitHub ↗

Given a line - if it's correctly quoted - it reurns the 'unquoted' value. If not quoted at all, it returns -1. If badly quoted, it returns None. line is stripped before starting. Any instances of '&mjf-quot;' found (from elem_quot) are turned back into '"' Any instances of

(inline, fullquote = 1, retain = 0, **keywargs)

Source from the content-addressed store, hash-verified

122 return thiselement + char
123
124def unquote(inline, fullquote = 1, retain = 0, **keywargs):
125 """Given a line - if it's correctly quoted - it reurns the 'unquoted' value.
126 If not quoted at all, it returns -1.
127 If badly quoted, it returns None.
128
129 line is stripped before starting.
130
131 Any instances of '&mjf-quot;' found (from elem_quot) are turned back into '"'
132 Any instances of '&mjf-lf;' found (from elem_quot) are turned back into '\n'
133
134 Quotes can be escaped with a '\'.
135 '\' (or any other character) can also be escaped with a '\'.
136 No triple quotes though :-)
137 (Escaping can be switched off by passing in the keyword argument 'escapechar' set to None
138 If you want to use literal '\' without escaping them then you must turn escaping off).
139
140 If fullquote is set to 0 (default is 1)
141 then unquote will return the first correctly quoted part of the line *and* the rest of the line.
142 If retain is set to 1 (default is 0)
143 then unquote will retain the quote characters in the returned value."""
144 if keywargs.has_key('escapechar'):
145 escapechar = keywargs['escapechar']
146 else:
147 escapechar = True
148 outline = ''
149 quotes = ["'",'"']
150 escape = 0
151 index = 0
152 quotechar = None
153 inline = inline.strip()
154 while index < len(inline):
155 thischar = inline[index]
156 index += 1
157 if not quotechar and thischar not in quotes:
158 return -1
159 elif not quotechar:
160 quotechar = thischar
161 if retain:
162 outline += thischar
163 continue
164 if escape:
165 outline += thischar
166 escape = 0
167 continue
168 if thischar in quotes:
169 if thischar == quotechar:
170 if retain:
171 outline += thischar
172 if not fullquote:
173 return outline.replace('&mjf-quot;','\"').replace('&mjf-lf;','\n'), inline[index:]
174 elif index == len(inline):
175 return outline.replace('&mjf-quot;','\"').replace('&mjf-lf;','\n')
176 else:
177 return None
178 else:
179 outline += thischar
180 continue
181 if thischar == '\\' and escapechar: # a continue here to *not* retain the escape character

Callers 5

processResultMethod · 0.85
requestMethod · 0.85
normFunction · 0.85
listparseFunction · 0.85
recipe-281056.pyFile · 0.85

Calls 3

has_keyMethod · 0.45
stripMethod · 0.45
replaceMethod · 0.45

Tested by

no test coverage detected