def __init__(self, filename, mode = '<STR_LIT:r>'):
if mode not in ['<STR_LIT:r>', '<STR_LIT:w>', '<STR_LIT:a>']:<EOL><INDENT>raise IOError(<NUM_LIT:0>, '<STR_LIT>' + repr(mode))<EOL><DEDENT>if string.find(filename, '<STR_LIT>') > <NUM_LIT:1>: <EOL><INDENT>if mode == '<STR_LIT:w>':<EOL><INDENT>raise IOError("<STR_LIT>")<EOL><DEDENT>import urllib.request, urllib.parse, urllib.error<EOL>self.file = urllib.request.urlopen(filename)<EOL><DEDENT>else:<EOL><INDENT>filename = os.path.expanduser(filename)<EOL>if mode == '<STR_LIT:r>' or mode == '<STR_LIT:a>':<EOL><INDENT>if not os.path.exists(filename):<EOL><INDENT>raise IOError(<NUM_LIT:2>, '<STR_LIT>' + filename)<EOL><DEDENT><DEDENT>filen, file_extension = os.path.splitext(filename)<EOL>command_dict = {<EOL>('<STR_LIT>', '<STR_LIT:r>'):<EOL>"<STR_LIT>",<EOL>('<STR_LIT>', '<STR_LIT:r>'):<EOL>"<STR_LIT>",<EOL>('<STR_LIT>', '<STR_LIT:r>'):<EOL>"<STR_LIT>",<EOL>('<STR_LIT>', '<STR_LIT:w>'):<EOL>"<STR_LIT>",<EOL>('<STR_LIT>', '<STR_LIT:w>'):<EOL>"<STR_LIT>",<EOL>('<STR_LIT>', '<STR_LIT:w>'):<EOL>"<STR_LIT>",<EOL>('<STR_LIT>', '<STR_LIT:a>'):<EOL>"<STR_LIT>",<EOL>('<STR_LIT>', '<STR_LIT:a>'):<EOL>"<STR_LIT>",<EOL>('<STR_LIT>', '<STR_LIT:a>'):<EOL>"<STR_LIT>",<EOL>}<EOL>exec(command_dict.get((file_extension, mode),<EOL>'<STR_LIT>'))<EOL><DEDENT>self.grammar = None<EOL>definition_file_one = filen + "<STR_LIT>" + file_extension<EOL>definition_file_two = os.path.dirname(filen) + os.sep + "<STR_LIT>"<EOL>if os.path.exists(definition_file_one):<EOL><INDENT>self.parsedef = definition_file_one<EOL><DEDENT>elif os.path.exists(definition_file_two):<EOL><INDENT>self.parsedef = definition_file_two<EOL><DEDENT>else:<EOL><INDENT>self.parsedef = None<EOL>return None<EOL><DEDENT>decimal_sep = "<STR_LIT:.>"<EOL>sign = oneOf("<STR_LIT>")<EOL>special_chars = string.replace('<STR_LIT>',<EOL>decimal_sep, "<STR_LIT>")<EOL>integer = ToInteger(<EOL>Combine(Optional(sign) +<EOL>Word(nums))).setName("<STR_LIT>")<EOL>positive_integer = ToInteger(<EOL>Combine(Optional("<STR_LIT:+>") +<EOL>Word(nums))).setName("<STR_LIT>")<EOL>negative_integer = ToInteger(<EOL>Combine("<STR_LIT:->" +<EOL>Word(nums))).setName("<STR_LIT>")<EOL>real = ToFloat(<EOL>Combine(Optional(sign) +<EOL>Word(nums) +<EOL>decimal_sep +<EOL>Optional(Word(nums)) +<EOL>Optional(oneOf("<STR_LIT>") +<EOL>Word(nums)))).setName("<STR_LIT>")<EOL>positive_real = ToFloat(<EOL>Combine(Optional("<STR_LIT:+>") +<EOL>Word(nums) +<EOL>decimal_sep +<EOL>Optional(Word(nums)) +<EOL>Optional(oneOf("<STR_LIT>") +<EOL>Word(nums)))).setName("<STR_LIT>")<EOL>negative_real = ToFloat(<EOL>Combine("<STR_LIT:->" +<EOL>Word(nums) +<EOL>decimal_sep +<EOL>Optional(Word(nums)) +<EOL>Optional(oneOf("<STR_LIT>") +<EOL>Word(nums)))).setName("<STR_LIT>")<EOL>qString = ( sglQuotedString | dblQuotedString ).setName("<STR_LIT>")<EOL>integer_junk = Optional(<EOL>Suppress(<EOL>Word(alphas +<EOL>special_chars +<EOL>decimal_sep))).setName("<STR_LIT>")<EOL>real_junk = Optional(<EOL>Suppress(<EOL>Word(alphas +<EOL>special_chars))).setName("<STR_LIT>")<EOL>qString_junk = SkipTo(qString).setName("<STR_LIT>")<EOL>exec(compile(open(self.parsedef).read(), self.parsedef, '<STR_LIT>'))<EOL>grammar = []<EOL>for nam, expr in parse:<EOL><INDENT>grammar.append( eval(expr.name + "<STR_LIT>"))<EOL>grammar.append( expr.setResultsName(nam) )<EOL><DEDENT>self.grammar = And( grammar[<NUM_LIT:1>:] + [restOfLine] )<EOL>
Opens input file, and if available the definition file.  If the
        definition file is available __init__ will then create some pyparsing
        helper variables.