[wxPython] XmlResources

hello list,

could please someone give me a hint for using xml resources embedded in
python??

thanks in advance.

regards
achim

could please someone give me a hint for using xml resources embedded in
python??

It doesn't look like resources can be loaded from anyting but a file
currently. Is this true Vaclav?

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

It doesn't look like resources can be loaded from anyting but a
file currently. Is this true Vaclav?

No :slight_smile: They can be loaded from wxFileSystem, too. See the output of
"wxrc -c foo.xrc", it generates a C++ file with embedded resources,
with the help of wxMemoryFSHandler. Something like that should be
possible in Python, too, it's the matter of translating Python string
into char* buffer.

Vaclav

No :slight_smile: They can be loaded from wxFileSystem, too. See the output of
"wxrc -c foo.xrc", it generates a C++ file with embedded resources,
with the help of wxMemoryFSHandler. Something like that should be
possible in Python, too, it's the matter of translating Python string
into char* buffer.

hello vaclav,.

sorry, but i can't find wxrc within the wxWindows package.
i looked in wxMotif-2.2.9.tgz, wxGTK-2.2.9.tar.gz (there is the
directory, but it's empty) and wxBase-2.2.9.tar.gz.

could you please explain the process of embedding xml in python a little
bit more detailed??

thanks in advance.

achim

No :slight_smile: They can be loaded from wxFileSystem, too. See the output of
"wxrc -c foo.xrc", it generates a C++ file with embedded resources,
with the help of wxMemoryFSHandler. Something like that should be
possible in Python, too, it's the matter of translating Python
string into char* buffer.

sorry, but i can't find wxrc within the wxWindows package. i looked
in wxMotif-2.2.9.tgz, wxGTK-2.2.9.tar.gz (there is the directory,
but it's empty) and wxBase-2.2.9.tar.gz.

You need 2.3.x, and it is found under the wx/contrib heirarchy. (I'm not
sure if or which tarball it is included in though...)

could you please explain the process of embedding xml in python a
little bit more detailed??

I'll need to do a wee bit of work to make it work in wxPython. I'll be
doing that soon.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

You need 2.3.x, and it is found under the wx/contrib
heirarchy. (I'm not sure if or which tarball it is included
in though...)

ok, i'll take a look.

> could you please explain the process of embedding xml in python a
> little bit more detailed??

I'll need to do a wee bit of work to make it work in
wxPython. I'll be doing that soon.

that would be nice, thanks!!

for now i do a workaround with temp files. works fine, but i don't like
it.

regards,
achim

hi list,

does anyone have a hint of how to get the ID of an object which is
defined within a xml resource?

i need that for calling the EVT_BUTTON() properly.

thanks in advance,

/achim

Achim Gosse wrote:

does anyone have a hint of how to get the ID of an object which is
defined within a xml resource?

XRCID("object_name")

VS

Robin Dunn wrote:

> could you please explain the process of embedding xml in python a
> little bit more detailed??

I'll need to do a wee bit of work to make it work in wxPython.
I'll be doing that soon.

I forgot: once you are done with it, can you please tell me how to
generate neccessary Python code so that I can add it to wxrc? (But I
wouldn't mind if you prefered to modify wxrc yourself :wink:

Thanks,
Vaclav

Achim Gosse wrote:
> does anyone have a hint of how to get the ID of an object which is
> defined within a xml resource?

XRCID("object_name")

VS

ok, now i got it.

the wxPython equivalent is called XMLID('obj_name')

thanks!

/achim

> Achim Gosse wrote:
> > does anyone have a hint of how to get the ID of an object which is
> > defined within a xml resource?
>
> XRCID("object_name")
>
> VS

ok, now i got it.

the wxPython equivalent is called XMLID('obj_name')

XRCID should be there too. Or maybe it's only in 2.3.3, I don't remember
when the change was... If that's the case please add a "XRCID = XMLID" in
wxPython/xrc.py and use XRCID in your code, as eventually XMLID will go
away.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

>
> ok, now i got it.
>
> the wxPython equivalent is called XMLID('obj_name')

XRCID should be there too. Or maybe it's only in 2.3.3, I
don't remember when the change was... If that's the case
please add a "XRCID = XMLID" in wxPython/xrc.py and use XRCID
in your code, as eventually XMLID will go away.

thanks for your hint.

what about XMLCTRL() ?
will that function be available in future versions?

best regards
achim

what about XMLCTRL() ?
will that function be available in future versions?

Yes, as XRCCTRL.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Achim Gosse wrote:

the wxPython equivalent is called XMLID('obj_name')

No, the wxPython equivalent is called XRCID, too. In latest wxPython
version (maybe only CVS?), that is -- it used to be XMLID in older
versions...

VS

I forgot: once you are done with it, can you please tell me how to
generate neccessary Python code so that I can add it to wxrc? (But I
wouldn't mind if you prefered to modify wxrc yourself :wink:

This will do it (in the current CVS or the next release):

        res = wxEmptyXmlResource()
        res.LoadFromString(resourceText)

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

> I forgot: once you are done with it, can you please tell me how
> to generate neccessary Python code so that I can add it to wxrc?
> (But I wouldn't mind if you prefered to modify wxrc yourself :wink:

This will do it (in the current CVS or the next release):

        res = wxEmptyXmlResource()
        res.LoadFromString(resourceText)

Yes, but how should resourceText look like? wxrc needs to generate a
.py file similar to the C++ one it already can create. Currently C++
embedded resource can contain multiple source files (of any type:
.xrc, .gif, .html, ...)

Vaclav

>
> This will do it (in the current CVS or the next release):
>
> res = wxEmptyXmlResource()
> res.LoadFromString(resourceText)

Yes, but how should resourceText look like?

It's just a string.

wxrc needs to generate a
.py file similar to the C++ one it already can create. Currently C++
embedded resource can contain multiple source files (of any type:
.xrc, .gif, .html, ...)

I hadn't realized that XRC could deal with images and such directly. Is it
just a matter of getting them into the wxMemoryFS and everything will be
taken care of? How should the "files" be named?

Here's the C++ implementation for my LoadFromString:

bool LoadFromString(const wxString& data) {
    static int s_memFileIdx = 0;

    // Check for memory FS. If not present, load the handler:
    wxMemoryFSHandler::AddFile(wxT("XRC_resource/dummy_file"),
                               wxT("dummy data"));
    wxFileSystem fsys;
    wxFSFile *f = fsys.OpenFile(wxT("memory:XRC_resource/dummy_file"));
    wxMemoryFSHandler::RemoveFile(wxT("XRC_resource/dummy_file"));
    if (f)
        delete f;
    else
        wxFileSystem::AddHandler(new wxMemoryFSHandler);

    // Now put the resource data into the memory FS
    wxString filename(wxT("XRC_resource/data_string_"));
    filename << s_memFileIdx;
    s_memFileIdx += 1;
    wxMemoryFSHandler::AddFile(filename, data);

    // Load the "file" into the resource object
    bool retval = self->Load(wxT("memory:") + filename );

    return retval;
}

Would it be better to just require the user to fill up the wxMemoryFS
themselves? This should work already in the current releases:

    wxFileSystem_AddHandler(wxMemoryFSHandler())
    wxMemoryFSHandler_AddFile("XRC_Resources/data_file", resourceText)
    res = wxXmlResource("memory:XRC_Resources/data_file")

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Robin Dunn wrote:

Would it be better to just require the user to fill up the
wxMemoryFS themselves?

No, I think it is good to have what you implemented. But I think that
it would be good to have exactly same mechanism as in C++ in addition
to it, that's all. I'll try to write the needed generator....

Vaclav Slavik wrote:

I'll try to write the needed generator....

I have added -p option to wxrc to generate (wx)Python code, but I'm
not sure I did the right thing...

BTW, is there some limit to string length in Python?

Vaclav

Vaclav Slavik wrote:

Vaclav Slavik wrote:

I'll try to write the needed generator....

I have added -p option to wxrc to generate (wx)Python code, but I'm not sure I did the right thing...

BTW, is there some limit to string length in Python?

Yes 2**31 on win32

HTH
Niki Spahiev