Distribute "data files" with a package

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Distribute "data files" with a package

Daniel Kraft-2
Hi!

I'm currently working on an Octave package, and want to distribute
certain "data files" with it.  (In my particular case, it is an image
file to be used by one of the package's "demo" functions.)

When I include the file in the package's "inst/" directory, it gets
installed correctly to the package installation folder.  However, now
I'm wondering how I can access it from within a demo function.  I tried
without an explicit directory specification, but that fails (since it
looks in the current working directory as opposed to the .m file's
directory).

This must be something others have already stumbled upon -- how can one
access such "data files" distributed with a package?  Is there a
function that can be called from within an .m file and that returns the
directory that contains the .m file?

The best I could find so far is "pkg list", and then sifting through the
list of packages looking for my package's name and then using the
installation directory.  But that seems to be too complicated for such a
"seemingly easy" task.  Is there no better way to query for the
installation directory of a particular package, for instance?  Or even
of "this package" from within a package's functions?

Thanks!  Yours,
Daniel

--
http://www.domob.eu/
OpenPGP: 901C 5216 0537 1D2A F071  5A0E 4D94 6EED 04F7 CF52
Namecoin: id/domob -> https://nameid.org/?name=domob
--
Done:  Arc-Bar-Cav-Hea-Kni-Ran-Rog-Sam-Tou-Val-Wiz
To go: Mon-Pri


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

smime.p7s (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Distribute "data files" with a package

Andreas Weber-2
On 31.03.2014 13:41, Daniel Kraft wrote:

> ... Is there a
> function that can be called from within an .m file and that returns the
> directory that contains the .m file?

help mfilename

I don't know if this is a good design choice but I think you can find
your file with mfilename ("fullpath") and fileparts, fullfile...

-- Andy

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Distribute "data files" with a package

Sergei Steshenko
In reply to this post by Daniel Kraft-2




----- Original Message -----

> From: Daniel Kraft <[hidden email]>
> To: Octave User <[hidden email]>
> Cc:
> Sent: Monday, March 31, 2014 2:41 PM
> Subject: Distribute "data files" with a package
>
> Hi!
>
> I'm currently working on an Octave package, and want to distribute
> certain "data files" with it.  (In my particular case, it is an image
> file to be used by one of the package's "demo" functions.)
>
> When I include the file in the package's "inst/" directory, it
> gets
> installed correctly to the package installation folder.  However, now
> I'm wondering how I can access it from within a demo function.  I tried
> without an explicit directory specification, but that fails (since it
> looks in the current working directory as opposed to the .m file's
> directory).
>
> This must be something others have already stumbled upon -- how can one
> access such "data files" distributed with a package?  Is there a
> function that can be called from within an .m file and that returns the
> directory that contains the .m file?
>
> The best I could find so far is "pkg list", and then sifting through
> the
> list of packages looking for my package's name and then using the
> installation directory.  But that seems to be too complicated for such a
> "seemingly easy" task.  Is there no better way to query for the
> installation directory of a particular package, for instance?  Or even
> of "this package" from within a package's functions?
>
> Thanks!  Yours,
> Daniel
>
> --
> http://www.domob.eu/
> OpenPGP: 901C 5216 0537 1D2A F071  5A0E 4D94 6EED 04F7 CF52
> Namecoin: id/domob -> https://nameid.org/?name=domob
> --
> Done:  Arc-Bar-Cav-Hea-Kni-Ran-Rog-Sam-Tou-Val-Wiz
> To go: Mon-Pri
>


Well, Octave packaging needs to redefined and redone from scratch.

Anyway, since functions work, and since in functional paradigm everything, including constants, is a function, you can probably solve your problem wrapping your data in a function.

I.e. something like this:

my_data = function get_my_data()
  my_data = [1, 2, 3 ...]; # this line should be autogenerated based on real data
endfunction
.

Then you can call get_my_data(), created a file in CWD and use the file for your demo.

Regards,
  Sergei.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Distribute "data files" with a package

Sergei Steshenko
In reply to this post by Andreas Weber-2




----- Original Message -----

> From: Andreas Weber <[hidden email]>
> To: Daniel Kraft <[hidden email]>
> Cc: Octave User <[hidden email]>
> Sent: Monday, March 31, 2014 3:22 PM
> Subject: Re: Distribute "data files" with a package
>
> On 31.03.2014 13:41, Daniel Kraft wrote:
>
>>  ... Is there a
>>  function that can be called from within an .m file and that returns the
>>  directory that contains the .m file?
>
> help mfilename
>
> I don't know if this is a good design choice but I think you can find
> your file with mfilename ("fullpath") and fileparts, fullfile...
>
> -- Andy
>
>


Yep, 'mfilename; is a good solution.

I use it to 'source' other .m file and which current .m file depends, and I define directory structure, so I know where to look for the needed files.

Regards,
  Sergei.


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Distribute "data files" with a package

Philip Nienhuis
In reply to this post by Daniel Kraft-2
Daniel Kraft-2 wrote
Hi!

I'm currently working on an Octave package, and want to distribute
certain "data files" with it.  (In my particular case, it is an image
file to be used by one of the package's "demo" functions.)

When I include the file in the package's "inst/" directory, it gets
installed correctly to the package installation folder.  However, now
I'm wondering how I can access it from within a demo function.  I tried
without an explicit directory specification, but that fails (since it
looks in the current working directory as opposed to the .m file's
directory).

This must be something others have already stumbled upon -- how can one
access such "data files" distributed with a package?  Is there a
function that can be called from within an .m file and that returns the
directory that contains the .m file?

The best I could find so far is "pkg list", and then sifting through the
list of packages looking for my package's name and then using the
installation directory.  But that seems to be too complicated for such a
"seemingly easy" task.  Is there no better way to query for the
installation directory of a particular package, for instance?  Or even
of "this package" from within a package's functions?
I think it is better to put data files, templates, etc. in a separate subdir, and not mix them with function .m files.
You can add any subdir to a package (I'd suggest in the same level as e.g., inst), and after installation that subdir + its contents gets installed in share/octave/..../<package>/<subdir>

How to find out the path to that subdir?
Easy, if the package is loaded, you can ask where a function from your package is, using 'which':

loc = which ('<function>);

and the answer is the full path to that function in your package. Just replace the last entry (the function file name) by <subdir>/<datafilename> and you're all set.

Philip
Reply | Threaded
Open this post in threaded view
|

Re: Distribute "data files" with a package

martin_helm
Am 31.03.2014 17:31, schrieb Philip Nienhuis:
> loc = which ('<function>);
>
> and the answer is the full path to that function in your package. Just
> replace the last entry (the function file name) by <subdir>/<datafilename>
> and you're all set.
Something like

strjoin({fileparts(which("func_name")),"subdir","test.jpg"}, filesep)

will do it in a operating system independent way.

_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Distribute "data files" with a package

Julien Bect
Le 31/03/2014 17:40, Martin Helm a écrit :
> Am 31.03.2014 17:31, schrieb Philip Nienhuis:
>> loc = which ('<function>);
>>
>> and the answer is the full path to that function in your package. Just
>> replace the last entry (the function file name) by <subdir>/<datafilename>
>> and you're all set.
> Something like
>
> strjoin({fileparts(which("func_name")),"subdir","test.jpg"}, filesep)

Why not simply :

fullfile (fileparts (which ("func_name")), "subdir", "test.jpg")

?


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave
Reply | Threaded
Open this post in threaded view
|

Re: Distribute "data files" with a package

Daniel Kraft-2
In reply to this post by Sergei Steshenko
Hi!

On 2014-03-31 16:11, Sergei Steshenko wrote:

>> On 31.03.2014 13:41, Daniel Kraft wrote:
>>
>>>  ... Is there a
>>>  function that can be called from within an .m file and that returns the
>>>  directory that contains the .m file?
>>
>> help mfilename
>>
>> I don't know if this is a good design choice but I think you can find
>> your file with mfilename ("fullpath") and fileparts, fullfile...
>
> Yep, 'mfilename; is a good solution.
>
> I use it to 'source' other .m file and which current .m file depends, and I define directory structure, so I know where to look for the needed files.
Thanks, I didn't know about mfilename.  This sounds indeed like what I
need.  (Also thanks for those who suggested to use "which", this is also
a nice idea I didn't think about.  For my case, using mfilename feels
cleaner, though.)

Yours,
Daniel

--
http://www.domob.eu/
OpenPGP: 901C 5216 0537 1D2A F071  5A0E 4D94 6EED 04F7 CF52
Namecoin: id/domob -> https://nameid.org/?name=domob
--
Done:  Arc-Bar-Cav-Hea-Kni-Ran-Rog-Sam-Tou-Val-Wiz
To go: Mon-Pri


_______________________________________________
Help-octave mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-octave

smime.p7s (5K) Download Attachment