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

'str' object has no attribute 'decode' #87

Closed
GSI opened this issue Aug 2, 2016 · 11 comments
Closed

'str' object has no attribute 'decode' #87

GSI opened this issue Aug 2, 2016 · 11 comments

Comments

@GSI
Copy link

GSI commented Aug 2, 2016

Recently (since upgrading some python modules?) I get this error:

3 of 468 vcard files of address book Adressbuch could not be parsed
Use --debug for more information or --skip-unparsable to proceed

... and with --debug:

Error: Could not parse file /path/to/QfG1zOXOOa.vcf
'str' object has no attribute 'decode'

Note: There is no string "str" in that file.

@scheibler
Copy link
Collaborator

Please have a look into #80 and #86 and try out the develop branch.

@GSI
Copy link
Author

GSI commented Aug 6, 2016

I'm having "all kinds" of issues since the vobject update.

Most notably, with the current source code (also the develop branch) the mouse cursor turns into crosshairs and when I then click somewhere that error shows:

# ./khard.py email foo
./khard.py: line 4: from: command not found

Then, when I press Ctrl-C this:

^C./khard.py: line 12: from: command not found
./khard.py: line 13: from: command not found
./khard.py: line 14: from: command not found
./khard.py: line 15: from: command not found
./khard.py: line 16: from: command not found
./khard.py: line 19: syntax error near unexpected token `('
./khard.py: line 19: `def create_new_contact(address_book):'

@scheibler
Copy link
Collaborator

Khard switched to python3 lately. Please have a look into the readme file to find out, how to
install properly (virtual environment recommended).

Furthermore khard now uses relative imports. Therefore

./khard.py email foo

won't work any longer.

If you want to run the source code directly, you have two options instead:

  1. Run module
git clone https://github.com/scheibler/khard --branch develop --single-branch
cd khard
/path/to/your/python -m khard email foo
  1. Create khard-runner.py in the khard project folder
#!/path/to/your/python
# -*- coding: utf-8 -*-

from khard.khard import main
if __name__ == '__main__':
    main()

make the script executable and run

/path/to/khard/khard-runner.py email foo

All of this under the condition, that you point to a python3 executable and have all required
modules installed (atomicwrites, configobj, pyyaml, vobject).

@GSI
Copy link
Author

GSI commented Aug 14, 2016

Thanks for the detailled hints. Using the develop branch it now mostly works.

However, there remains one single VCF in my collection that still causes the original issue. I just sent it to you via email.

@GSI
Copy link
Author

GSI commented Aug 14, 2016

@scheibler wrote:

Please try out the latest commit of the develop branch. Should be working now.

Confirmed! Thanks a lot!

@GSI
Copy link
Author

GSI commented Sep 18, 2016

vcf.zip

When modifying any field of the attached VCF, the issue occurs again. (For byte object in this case; tested with develop branch, commit 79dc119; also just verified in most recent commit 7dc37ce)

Traceback (most recent call last):
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
        "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
        exec(code, run_globals)
  File "/path/to/khard/khard/__main__.py", line 5, in <module>
        main()
  File "/path/to/khard/khard/khard.py", line 1675, in main
        args.open_editor)
  File "/path/to/khard/khard/khard.py", line 967, in modify_subcommand
        modify_existing_contact(selected_vcard)
  File "/path/to/khard/khard/khard.py", line 129, in modify_existing_contact
        new_contact.write_to_file(overwrite=True)
  File "/path/to/khard/khard/carddav_object.py", line 1405, in write_to_file
        vcard_string = self.vcard.serialize()
  File "/usr/lib/python3.5/site-packages/vobject/base.py", line 255, in serialize
        return behavior.serialize(self, buf, lineLength, validate)
  File "/usr/lib/python3.5/site-packages/vobject/behavior.py", line 162, in serialize
        out = base.defaultSerialize(transformed, buf, lineLength)
  File "/usr/lib/python3.5/site-packages/vobject/base.py", line 993, in defaultSerialize
        child.serialize(outbuf, lineLength, validate=False)
  File "/usr/lib/python3.5/site-packages/vobject/base.py", line 255, in serialize
        return behavior.serialize(self, buf, lineLength, validate)
  File "/usr/lib/python3.5/site-packages/vobject/vcard.py", line 227, in serialize
        VCardTextBehavior.serialize(obj, buf, lineLength, validate)
  File "/usr/lib/python3.5/site-packages/vobject/behavior.py", line 162, in serialize
        out = base.defaultSerialize(transformed, buf, lineLength)
  File "/usr/lib/python3.5/site-packages/vobject/base.py", line 1001, in defaultSerialize
        obj.behavior.encode(obj)
  File "/usr/lib/python3.5/site-packages/vobject/vcard.py", line 152, in encode
        line.value = codecs.encode(line.value.encode(coding), "base64").decode("utf-8")
AttributeError: 'bytes' object has no attribute 'encode'

@GSI GSI reopened this Sep 18, 2016
@scheibler
Copy link
Collaborator

You have upgraded to vobject version 0.9.3 (that's the underlying vcard library). They tried to fix
the python3 encoding errors but unfortunately they didn't succeed. Reference at
skarim/vobject#39. Now it's worse for khard cause the decoding of
base64 data works but the encoding back doesn't. So the workaround of khard version 0.11.2 fails.

For the quick fix:

pip install vobject==0.9.2

I consider to pin that vobject version in the install script for now. I'am not sure, how fast they
will fix that library error and the old version at least works with khards workaround in most cases.

@lucc
Copy link
Owner

lucc commented Sep 18, 2016

@scheibler I just tried to write a test for this vcard in my tests branch (not yet uploaded to github) and noticed that we obviously need to create and use a temporary config file and vcard database for tests. But currently the config class doesn't allow that. I was thinking about a way to solve this as said in #79 but I am also waiting for #94 with that as #94 is a change that will conflict any other change in the config class (only b/c of formatting, not functionality).

@guyzmo
Copy link

guyzmo commented Jan 13, 2017

for the note, I ran into that issue, with vobject==0.9.2 and did patch vobject/vcard.py in my site-packages with that:

at line 137:

-         line.value = line.value.decode('base64')
+        line.value = codecs.encode(line.value.encode("ascii"), "base64").decode("utf-8")

it's a very ugly way to go, but it did the job at not skipping entries. I hope it won't screw my contacts though ☺

Looking at the referred issue at skarim/vobject#39, it looks like it's been solved for 0.9.4, but they haven't released that version yet.

Finally, thank you for the neat job with khal/khard, just started using them and I can easily see them fit in my everyday toolbox ☺

@scheibler
Copy link
Collaborator

New versions of khard (0.11.4) and vobject (0.9.4.1) fixed this issue. Make sure to update both programs.

@GSI
Copy link
Author

GSI commented Feb 20, 2017

fixed this issue

Yeah 👍

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

No branches or pull requests

4 participants