Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
reconvert Namespace Reference

Functions

def convert
 
def quote
 
def main
 

Variables

int append = 1
 
list __all__ = ["convert","quote"]
 
dictionary mastertable
 

Function Documentation

def reconvert.convert (   s,
  syntax = None 
)
Convert a regex regular expression to re syntax.

The first argument is the regular expression, as a string object,
just like it would be passed to regex.compile().  (I.e., pass the
actual string object -- string quotes must already have been
removed and the standard escape processing has already been done,
e.g. by eval().)

The optional second argument is the regex syntax variant to be
used.  This is an integer mask as passed to regex.set_syntax();
the flag bits are defined in regex_syntax.  When not specified, or
when None is given, the current regex syntax mask (as retrieved by
regex.get_syntax()) is used -- which is 0 by default.

The return value is a regular expression, as a string object that
could be passed to re.compile().  (I.e., no string quotes have
been added -- use quote() below, or repr().)

The conversion is not always guaranteed to be correct.  More
syntactical analysis should be performed to detect borderline
cases and decide what to do with them.  For example, 'x*?' is not
translated correctly.

Definition at line 90 of file reconvert.py.

90 
91 def convert(s, syntax=None):
92  """Convert a regex regular expression to re syntax.
93 
94  The first argument is the regular expression, as a string object,
95  just like it would be passed to regex.compile(). (I.e., pass the
96  actual string object -- string quotes must already have been
97  removed and the standard escape processing has already been done,
98  e.g. by eval().)
99 
100  The optional second argument is the regex syntax variant to be
101  used. This is an integer mask as passed to regex.set_syntax();
102  the flag bits are defined in regex_syntax. When not specified, or
103  when None is given, the current regex syntax mask (as retrieved by
104  regex.get_syntax()) is used -- which is 0 by default.
105 
106  The return value is a regular expression, as a string object that
107  could be passed to re.compile(). (I.e., no string quotes have
108  been added -- use quote() below, or repr().)
109 
110  The conversion is not always guaranteed to be correct. More
111  syntactical analysis should be performed to detect borderline
112  cases and decide what to do with them. For example, 'x*?' is not
113  translated correctly.
114 
115  """
116  table = mastertable.copy()
117  if syntax is None:
118  syntax = regex.get_syntax()
119  if syntax & RE_NO_BK_PARENS:
120  del table[r'\('], table[r'\)']
121  del table['('], table[')']
122  if syntax & RE_NO_BK_VBAR:
123  del table[r'\|']
124  del table['|']
125  if syntax & RE_BK_PLUS_QM:
126  table['+'] = r'\+'
127  table['?'] = r'\?'
128  table[r'\+'] = '+'
129  table[r'\?'] = '?'
130  if syntax & RE_NEWLINE_OR:
131  table['\n'] = '|'
132  res = ""
133 
134  i = 0
135  end = len(s)
136  while i < end:
137  c = s[i]
138  i = i+1
139  if c == '\\':
140  c = s[i]
141  i = i+1
142  key = '\\' + c
143  key = table.get(key, key)
144  res = res + key
145  else:
146  c = table.get(c, c)
147  res = res + c
148  return res
149 
def reconvert.main ( )
Main program -- called when run as a script.

Definition at line 182 of file reconvert.py.

References convert(), and quote().

183 def main():
184  """Main program -- called when run as a script."""
185  import sys
186  s = eval(sys.stdin.read())
187  sys.stdout.write(quote(convert(s)))
188  if sys.stdout.isatty():
189  sys.stdout.write("\n")
190 
def reconvert.quote (   s,
  quote = None 
)
Convert a string object to a quoted string literal.

This is similar to repr() but will return a "raw" string (r'...'
or r"...") when the string contains backslashes, instead of
doubling all backslashes.  The resulting string does *not* always
evaluate to the same string as the original; however it will do
just the right thing when passed into re.compile().

The optional second argument forces the string quote; it must be
a single character which is a valid Python string quote.

Definition at line 150 of file reconvert.py.

151 def quote(s, quote=None):
152  """Convert a string object to a quoted string literal.
153 
154  This is similar to repr() but will return a "raw" string (r'...'
155  or r"...") when the string contains backslashes, instead of
156  doubling all backslashes. The resulting string does *not* always
157  evaluate to the same string as the original; however it will do
158  just the right thing when passed into re.compile().
159 
160  The optional second argument forces the string quote; it must be
161  a single character which is a valid Python string quote.
162 
163  """
164  if quote is None:
165  q = "'"
166  altq = "'"
167  if q in s and altq not in s:
168  q = altq
169  else:
170  assert quote in ('"', "'")
171  q = quote
172  res = q
173  for c in s:
174  if c == q: c = '\\' + c
175  elif c < ' ' or c > '~': c = "\\%03o" % ord(c)
176  res = res + c
177  res = res + q
178  if '\\' in res:
179  res = 'r' + res
180  return res
181 

Variable Documentation

list __all__ = ["convert","quote"]

Definition at line 70 of file reconvert.py.

int append = 1

Definition at line 65 of file reconvert.py.

dictionary mastertable
Initial value:
1 = {
2  r'<': r'\b',
3  r'>': r'\b',
4  r'\`': r'\A',
5  r'\'': r'\Z',
6  r'\(': '(',
7  r'\)': ')',
8  r'\|': '|',
9  '(': r'\(',
10  ')': r'\)',
11  '|': r'\|',
12  '\t': r'\t',
13  '\n': r'\n',
14  '\r': r'\r',
15 }

Definition at line 73 of file reconvert.py.