kennknowles / python-jsonpath-rw

A robust and significantly extended implementation of JSONPath for Python, with a clear AST for metaprogramming.
Apache License 2.0
598 stars 193 forks source link

[Jython][SyntaxError] import parser$py_jsonpath_parsetab #37

Open marekjagielski opened 8 years ago

marekjagielski commented 8 years ago

Hi, I started to use your module inside jython scrips in JMeter tests. I use jmeter-maven-plugin.

To be able to use your module, I download jsonpath code as well as other dependencies and put i inside Lib folder where all jython libs are:

    <plugins><plugin><artifactId>maven-antrun-plugin</artifactId>
            <executions>
                <execution><id>check-properties</id><phase>validate</phase><goals><goal>run</goal></goals>
                    <configuration>
                        <target>
                            <echo>[host]: ${host}</echo>
                            <echo>[port]: ${port}</echo>
                            <echo>[flowName]: ${flowName}</echo>
                        </target>
                    </configuration>
                </execution>
                <execution><id>init</id><phase>initialize</phase><goals><goal>run</goal></goals>
                    <configuration>
                        <target>
                            <unzip src="${basedir}/target/jmeter/lib/jython-standalone-${jython-version}.jar" dest="${basedir}/target/jmeter/lib/">
                                <patternset><include name="Lib/"/></patternset>
                            </unzip>
                            <exec executable="git" dir="${project.build.directory}">
                                <arg value="clone"/><arg value="-b"/><arg value="1.4.0"/><arg value="https://github.com/kennknowles/python-jsonpath-rw.git"/>
                            </exec>
                            <copy todir="${project.build.directory}/jmeter/lib/Lib/jsonpath_rw">
                                <fileset dir="${project.build.directory}/python-jsonpath-rw/jsonpath_rw"/>
                            </copy>
                            <get src="https://bitbucket.org/gutworth/six/raw/a9b120c9c49734c1bd7a95e7f371fd3bf308f107/six.py" dest="${project.build.directory}/jmeter/lib/Lib/six.py"/>
                            <exec executable="git" dir="${project.build.directory}">
                                <arg value="clone"/><arg value="https://github.com/dabeaz/ply.git"/>
                            </exec>
                            <copy todir="${project.build.directory}/jmeter/lib/Lib/ply">
                                <fileset dir="${project.build.directory}/ply/ply"/>
                            </copy>
                        </target>
                    </configuration>
                </execution>

It seems to work. However, I get an error in JMeter logs:

2016/03/31 18:29:33 WARN - jmeter.extractor.BSFPostProcessor: Problem in BSF script org.apache.bsf.BSFException: exception from Jython: Traceback (most recent call last): File "", line 15, in File "/home/marek/git/SUP-7419/JMeter_S7/target/jmeter/lib/Lib/jsonpath_rw/parser.py", line 14, in parse return JsonPathParser().parse(string) File "/home/marek/git/SUP-7419/JMeter_S7/target/jmeter/lib/Lib/jsonpath_rw/parser.py", line 32, in parse return self.parse_token_stream(lexer.tokenize(string)) File "/home/marek/git/SUP-7419/JMeter_S7/target/jmeter/lib/Lib/jsonpath_rw/parser.py", line 47, in parse_token_stream new_parser = ply.yacc.yacc(module=self, File "/home/marek/git/SUP-7419/JMeter_S7/target/jmeter/lib/Lib/ply/yacc.py", line 3264, in yacc read_signature = lr.read_table(tabmodule) File "/home/marek/git/SUP-7419/JMeter_S7/target/jmeter/lib/Lib/ply/yacc.py", line 3264, in yacc read_signature = lr.read_table(tabmodule) File "/home/marek/git/SUP-7419/JMeter_S7/target/jmeter/lib/Lib/ply/yacc.py", line 1964, in read_table exec('import %s' % module) File "", line 1 import parser$py_jsonpath_parsetab ^ SyntaxError: no viable alternative at character '$'

I see in parser.py code that: parsing_table_module = '_'.join([module_name, start_symbol, 'parsetab'])

and after in yacc.py code it is used: exec('import %s' % module)

Jsonpath I am using is: parse('*.Error.Validator').find(response)

What is wrong?

Thanks in advance for any help. Anyway the library seems great and it fits well in JMeter JSON tests.

marekjagielski commented 8 years ago

Ok, I see that with jython I have a java class generated for each python file. So next to parser.py there is parser$py.class. When I restart JMeter, problably jython interpreter start to use cached class. If I extract a substring in the module_name, it works:

    try:
        module_name = os.path.splitext(os.path.split(__file__)[1])[0]
    except:
        module_name = __name__

    module_name = module_name[0:module_name.find("$")]

    parsing_table_module = '_'.join([module_name, start_symbol, 'parsetab'])

Is it possible to be official solution to be able to use python-jsonpath-rw with Jython?