girishchandranc / autosarfactory

AutosarFactory is a python package which helps user to read/create/modify AUTOSAR standard arxml files.
MIT License
87 stars 28 forks source link

etree.parse Error #13

Closed qCyb3r closed 1 year ago

qCyb3r commented 1 year ago

Thank you foremost for providing the library. After installing and trying to execute some code from the examples I ran into the following problem:

Traceback (most recent call last):
  File "c:\Users\user123\Desktop\AutosarFactory2\autosarfactory\autosarFactoryTest.py", line 22, in <module>
    root, status = autosarfactory.read(input_files)
  File "c:\Users\user123\Desktop\AutosarFactory2\autosarfactory\autosarfactory\autosarfactory.py", line 39, in read    
    tree = etree.parse(file, etree.XMLParser(remove_blank_text=True))
  File "src\lxml\etree.pyx", line 3521, in lxml.etree.parse
  File "src\lxml\parser.pxi", line 1859, in lxml.etree._parseDocument
  File "src\lxml\parser.pxi", line 1885, in lxml.etree._parseDocumentFromURL
  File "src\lxml\parser.pxi", line 1789, in lxml.etree._parseDocFromFile
  File "src\lxml\parser.pxi", line 1177, in lxml.etree._BaseParser._parseDocFromFile
  File "src\lxml\parser.pxi", line 615, in lxml.etree._ParserContext._handleParseResultDoc
  File "src\lxml\parser.pxi", line 725, in lxml.etree._handleParseResult
  File "src\lxml\parser.pxi", line 652, in lxml.etree._raiseParseError
OSError: Error reading file 'c:\Users\user123\Desktop\AutosarFactory2\autosarfactory\resources\components.arxml': failed to load external entity "file:/c:/Users/user123/Desktop/AutosarFactory2/autosarfactory/resources/components.arxml"   
PS C:\Users\user123\Desktop\AutosarFactory2>
AD1D1 commented 1 year ago

Hi girishchandranc,

I am too facing the exact error, when I run this:

files = ['interface.arxml', 'datatypes.arxml'] root, status = autosarfactory.read(files)

I get the following error:


OSError Traceback (most recent call last) Cell In[10], line 1 ----> 1 root, status = autosarfactory.read(files)

File c:\Users\users12345\Desktop\autosarfactory\autosarfactory\autosarfactory.py:42, in read(files) 40 status &= __read_folder(file) 41 else: ---> 42 status &= __read_file(file) 43 if status is False: 44 break

File c:\Users\users12345\Desktop\autosarfactory\autosarfactory\autosarfactory.py:67, in __read_file(file) 65 global autosarNode 66 status = False ---> 67 tree = etree.parse(file, etree.XMLParser(remove_blank_text=True)) 69 if tree is not None and tree.getroot() is not None: 70 if etree.QName(tree.getroot()).localname == 'AUTOSAR':

File src\lxml\etree.pyx:3541, in lxml.etree.parse()

File src\lxml\parser.pxi:1879, in lxml.etree._parseDocument()

File src\lxml\parser.pxi:1905, in lxml.etree._parseDocumentFromURL() ... File src\lxml\parser.pxi:728, in lxml.etree._handleParseResult()

File src\lxml\parser.pxi:655, in lxml.etree._raiseParseError()

OSError: Error reading file 'interface.arxml': failed to load external entity "interface.arxml"

First I tried it out with components.arxml, thinking there was somefault with components.arxml, I tried it with interface.arxml it threw up the same thing then with interfance.arxml - same again:/

I'm just thinking this error is cause of a difference in schema maybe? do we need to define a schema in the code for the parsing to work?

Thanks :)

girishchandranc commented 1 year ago

Hi guys,

I am not sure if you are passing the file location properly? Maybe, the path is not properly escaped? @qCyb3r , Using single backlash in the file path may cause python to consider it as escape sequence. Could you check if using double backslash could help? Or better to use forward slashes in the path. If that doesn't help, could you please check if the path is accessible?

@AD1D1 , it has not to do with schema. Here also, I reckon, its due to the issue with non.existence of file. I also get to see this error when the file location is not properly passed. Could you please make sure that the arxml files exist in the path from where the python script is run? If you still, want to keep it in the same folder as script, then could you add relative path from the python file to the arxml file, like we did it in the test

Please let me know if it helps.

I think, the package needs to be more verbose. Should add a error message that the file doesn't exist. Thanks!

qCyb3r commented 1 year ago

Thank you for the fast reply. Unfortunately all 3 cases cause the same error.

import os, sys, shutil
import pytest, filecmp

mod_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, mod_path)

from autosarfactory import autosarfactory

resourcesDir = os.path.join(os.path.dirname(__file__), 'resources')

input_files = [os.path.join(resourcesDir, 'components.arxml'), 
               os.path.join(resourcesDir, 'datatypes.arxml'),
               os.path.join(resourcesDir, 'interfaces.arxml')]

invalid_files = [os.path.join(resourcesDir, 'invalid.arxml')]

newInputFiles = list()

for file in input_files:
    file = file.replace("\\","//")
    newInputFiles.append(file)

print(resourcesDir)
print(input_files)
print(newInputFiles)

#Read a list of files
root, status = autosarfactory.read(newInputFiles)

Edit: ran on WIN10 OS

girishchandranc commented 1 year ago

Hi @qCyb3r , Thanks for sharing the code snippet. Are you sure the file name is "interfaces.arxml"? If you took it from "Examples/generated", then the file name is "interface.arxml". Noticed the additional "s" in the file name. I tried it by renaming the file and it seems to work for me. Can you please check again?

Thanks!

qCyb3r commented 1 year ago

Thanks a lot. I missed to see that "tests" is missing from the file path. Editing this line solved the issue: resourcesDir = os.path.join(os.path.dirname(__file__), 'tests\\resources')