Skip to content

Commit

Permalink
if code directory is not writable, store parser generated files in th…
Browse files Browse the repository at this point in the history
…e default python tempdir
  • Loading branch information
rlskoeser committed Jan 28, 2013
1 parent 68730c9 commit c02ed76
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions eulxml/xpath/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# file eulxml/xpath/core.py
#
#
# Copyright 2010,2011 Emory University Libraries
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -27,16 +27,17 @@
import os
import re
from ply import lex, yacc
import tempfile

from eulxml.xpath import lexrules
from eulxml.xpath import parserules
from eulxml.xpath.ast import serialize

__all__ = [ 'lexer', 'parser', 'parse', 'serialize' ]
__all__ = ['lexer', 'parser', 'parse', 'serialize']

# build the lexer. This will generate a lextab.py in the eulxml.xpath
# directory. Unfortunately, xpath requires some wonky lexing.
# Per http://www.w3.org/TR/xpath/#exprlex :
# Per http://www.w3.org/TR/xpath/#exprlex :
# 1 If there is a preceding token and the preceding token is not one of @,
# ::, (, [, , or an Operator, then a * must be recognized as a
# MultiplyOperator and an NCName must be recognized as an OperatorName.
Expand Down Expand Up @@ -74,7 +75,10 @@
# call it part of the qname)
'COLON',
])

NODE_TYPES = set(['comment', 'text', 'processing-instruction', 'node'])


class LexerWrapper(lex.Lexer):
def token(self):
tok = lex.Lexer.token(self)
Expand Down Expand Up @@ -116,7 +120,7 @@ def peek(self):
lexdir = os.path.dirname(lexrules.__file__)
lexer = None
try:
lexer = lex.lex(module=lexrules, optimize=1, outputdir=lexdir,
lexer = lex.lex(module=lexrules, optimize=1, outputdir=lexdir,
reflags=re.UNICODE)
except IOError, e:
import errno
Expand All @@ -134,15 +138,21 @@ def peek(self):
# (contrast lex's explosion). Other than that, it's much less exciting
# than the lexer wackiness.
parsedir = os.path.dirname(parserules.__file__)
# By default, store generated parse files with the code
# If we don't have write permission, put them in the configured tempdir
if (not os.access(parsedir, os.W_OK)):
parsedir = tempfile.gettempdir()
parser = yacc.yacc(module=parserules, outputdir=parsedir, debug=0)


def parse(xpath):
'''Parse an xpath.'''
# Expose the parse method of the constructed parser,
# but explicitly specify the lexer created here,
# since otherwise parse will use the most-recently created lexer.
# since otherwise parse will use the most-recently created lexer.
return parser.parse(xpath, lexer=lexer)


def ptokens(s):
'''Lex a string as XPath tokens, and print each token as it is lexed.
This is used primarily for debugging. You probably don't want this
Expand Down

0 comments on commit c02ed76

Please sign in to comment.