Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_children_of_type: AttributeError: 'str' object has no attribute '__name__' #117

Closed
mrkafk opened this issue Nov 19, 2018 · 5 comments
Closed
Labels

Comments

@mrkafk
Copy link

mrkafk commented Nov 19, 2018

Parsed string:

host example1 {
 hardware ethernet aa:bb:ff:20:fa:12;
 fixed-address 192.168.1.181;
 option host-name "example1";
 ddns-hostname "example1";
}

host example2 {
 hardware ethernet aa:bb:ff:20:fa:13;
 fixed-address 192.168.1.191;
 option host-name "example2";
 ddns-hostname "example2";
}

Code:

def get_hosts(s):
    grammar = """
    config: hosts*=host ;

    host: 'host' hostname=ID '{'
        'hardware ethernet' hardware_ethernet=/[0-9a-fA-F:]+/';'
        'fixed-address' fixed_address=/([0-9]{1,3}\.){3}[0-9]{1,3}/';'
        'option host-name' option_host_name=STRING';'
        'ddns-hostname' ddns_hostname=STRING';'
    '}'
    ;
    """
    mm = metamodel_from_str(grammar)
    model = mm.model_from_str(s)
    hosts = get_children_of_type('host', model)

Exception:

  File "./prog.py", line 58, in get_hosts
    hosts = get_children_of_type('host', model)
  File "/ve/local/lib/python2.7/site-packages/textx/model.py", line 125, in get_children_of_type
    typ = typ.__name__
AttributeError: 'str' object has no attribute '__name__'

Per get_children_of_type docstring, typ should be allowed to be a string?

@goto40
Copy link
Member

goto40 commented Nov 19, 2018

I checked your example with python 3.6 and the current textX version. I do not get the effect.

Which version of textX and python do you use?

from textx import metamodel_from_str, get_children_of_type

grammar = """
config: hosts*=host ;

host: 'host' hostname=ID '{'
    'hardware ethernet' hardware_ethernet=/[0-9a-fA-F:]+/';'
    'fixed-address' fixed_address=/([0-9]{1,3}\.){3}[0-9]{1,3}/';'
    'option host-name' option_host_name=STRING';'
    'ddns-hostname' ddns_hostname=STRING';'
'}'
;
"""
mm = metamodel_from_str(grammar)
model = mm.model_from_str('''
host example1 {
 hardware ethernet aa:bb:ff:20:fa:12;
 fixed-address 192.168.1.181;
 option host-name "example1";
 ddns-hostname "example1";
}

host example2 {
 hardware ethernet aa:bb:ff:20:fa:13;
 fixed-address 192.168.1.191;
 option host-name "example2";
 ddns-hostname "example2";
}
''')
hosts = get_children_of_type('host', model)

for h in hosts:
    print(h.hostname)

@igordejanovic
Copy link
Member

I think that is related to #105 . Inputs to textX must be unicode. Make sure your variable s is of unicode type. We will provide type checks for better error report in the future.

@igordejanovic
Copy link
Member

BTW, this is an example from this SO question. I'm linking here as it is an interesting use-case that should be covered in the docs (FAQ probably). Will make an issue for that.

@igordejanovic
Copy link
Member

I reproduced this problem with Python 2.7 and it is a unicode problem. Putting from __future__ import unicode_literals at the top of the file solves the issue.

igordejanovic added a commit that referenced this issue Nov 21, 2018
This should prevent problems with Python 2.
See:
#105
#99
#117
igordejanovic added a commit that referenced this issue Nov 21, 2018
This should prevent problems with Python 2.
See:
#105
#99
#117
@igordejanovic
Copy link
Member

This should not be an issue anymore as of #120 unicode is required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants