0

I have a file __manifest__.py in which I have a python dict :

{
  'name': "example",
  'verbose_name': "Example",
  'version': "0.1",
  'summary': "This is a summary",
  'description': """
     This
     is
     a
     multiline
     description
   """,
  'some_other_attr': True
}

And I'd like to be able to open it and transform its content in a regular python dict. I'm already able to open it :

manifest = open(path_to_manifest, 'r')

where path_to_manifest is the..path to the manifest. And also to read it :

manifest_content = manifest.read()

The thing is that it returns obviously a string : print('manifest_content : ' + str(manifest_content))

manifest_content : {
  'name': "example",
  'verbose_name': "Example",
  'version': "0.1",
  'summary': "This is a summary",
  'description': """
    This
    is
    a
    multiline
    description
  """,
  'some_other_attr': True
}

And I want to be able to transform this content into a python dict.

I know you will probably tell me about json and json.loads() but this doesn't support multiline as my description is. How could I achieve that ?

Thanks in advance for your help

lbris
  • 1,068
  • 11
  • 34
  • 5
    The contents aren't JSON, so `json` doesn't help here. The contents are Python code, as you also indicate with the `.py` ending. Essentially it's a noop, as the dict expression isn't assigned to anything and immediately discarded. While there are ways to parse this from text, why don't you simply assign the dict to something, e.g. `manifest = { ... }`, and then `from __manifest__ import manifest`…?! – deceze Feb 04 '20 at 10:34
  • 2
    If you _don't_ want to treat this as actual Python code, you should choose an established data serialisation format, like JSON or YAML. It's highly unusual to have a Python dict literal in a file that's then parsed from text instead of simply being `import`ed. The point of having a machine readable file format is that it can also be *generated* by other tools; it's not easy to generate such a `__manifest__.py` programatically, especially from anything other than Python. Using JSON or YAML or one of a gazillion other well supported formats would make this trivial. – deceze Feb 04 '20 at 10:39
  • The thing is that from an `__init__.py` I'm browsing directories of my project structure to find those `__manifest__.py` and create objects from them. – lbris Feb 04 '20 at 10:42
  • 2
    That doesn't really change anything about what I explained. Are you already married to this particular format of `__manifest__.py`? If not, then you should consider what I explained above. – deceze Feb 04 '20 at 10:43
  • Well I'll try what you explained and change the requirements. Thanks. – lbris Feb 04 '20 at 10:53
  • @deceze yaml finally suits my needs. – lbris Feb 04 '20 at 13:27

0 Answers0