how to check with vobject either string is valid vcard or not when dealing with UTF-8 characters?

StackOverflow https://stackoverflow.com/questions/22936720

  •  29-06-2023
  •  | 
  •  

質問

how to check with vobject either string is valid vcard or not?

Is there some extra method or generall approach with try and catch?

For now I do it as follows:

 try:
            vobj = vobject.readOne(vcard_readable)
 except Exception as e:
            error_message = {
                "valid": False,
                "reason": "Invalid vCard\n{0}".format(e)}

How to deal with unicode with VOBJECT?

役に立ちましたか?

解決

Your current code works fine, but generally you don't want to catch Exception because this will mask other errors in your code. For example, if I take your code snippet and put it in a file and then run it...I get no error messages, even though I have not imported the vobject module. This is because that code is actually raising a NameError:

Traceback (most recent call last):
  File "foo.py", line 2, in <module>
    vobj = vobject.readOne(vcard_readable)
NameError: name 'vobject' is not defined

but because you're catching all exceptions, you're hiding it. A better technique is to catch only the specific exceptions you expect to receive from the vobject module, and let others percolate up normally.

For vobject, all of the exceptions it raises are going to be subclasses of vobject.base.VObjectError, so the following code would suffice:

 try:
            vobj = vobject.readOne(vcard_readable)
 except vobject.base.VObjectError as e:
            error_message = {
                "valid": False,
                "reason": "Invalid vCard\n{0}".format(e)}

他のヒント

 vcard = put.get('vcard')
            try:
                vcard_readable = base64.decodestring(vcard)
                quoted_printable_vcard = quopri.encodestring(vcard_readable)
                vobj = vobject.readOne(quoted_printable_vcard)
            except UnicodeEncodeError as e:  # case of bad encoding
                error_message = {
                    "valid": False,
                    "reason": "Invalid vCard\n{0}".format(e)}
                return HttpResponse(json.dumps(error_message), status=200)
            except vobject.base.VObjectError as e2:  # case of invalid vcard
                error_message = {
                    "valid": False,
                    "reason": "Invalid vCard format\n{0}".format(e2)}
                return HttpResponse(json.dumps(error_message), status=200)
            except:
                error_message = {
                    "valid": False,
                    "reason": "Invalid vCard."}
                return HttpResponse(json.dumps(error_message), status=200)

OK. I solved it. For vobject to work with UNICODE (UTF-8) it is required to use:

quoted printable encoding. - example of what it is below:

>>> s = "gżegżółka"
>>> s
'g\xc5\xbceg\xc5\xbc\xc3\xb3\xc5\x82ka'
>>> import quopri
>>> quopri.encodestring(s)
'g=C5=BCeg=C5=BC=C3=B3=C5=82ka'
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top