Python: Pass parameters to file_class

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

Python: Pass parameters to file_class

Nikolaus Rath
Hello,

I would like to pass some additional parameters (mainly the "parent"
fuse object) to the constructor of file_class. Is there a way to
accomplish that?

Best,

   -Nikolaus

--
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
                                                         -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C


-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08
_______________________________________________
fuse-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fuse-devel
Reply | Threaded
Open this post in threaded view
|

Re: Python: Pass parameters to file_class

Csaba Henk
On 2008-07-06, Nikolaus Rath <[hidden email]> wrote:
> I would like to pass some additional parameters (mainly the "parent"
> fuse object) to the constructor of file_class. Is there a way to
> accomplish that?

Yes. Define the file class _within_ the main method of the parent. Then
those variables you set in main() before defining the file class
will be available in the scope of the file class, too.

See eg. this old version of xmp.py where I relied on using information
from the parent in the file class:

  http://mercurial.creo.hu/repos/fuse-python-hg/index.cgi/file/075e6977c270/example/xmp.py

Csaba


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
fuse-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fuse-devel
Reply | Threaded
Open this post in threaded view
|

Re: Python: Pass parameters to file_class

Nikolaus Rath
Csaba Henk <[hidden email]> writes:
> On 2008-07-06, Nikolaus Rath <[hidden email]> wrote:
>> I would like to pass some additional parameters (mainly the "parent"
>> fuse object) to the constructor of file_class. Is there a way to
>> accomplish that?
>
> Yes. Define the file class _within_ the main method of the parent.
> Then those variables you set in main() before defining the file
> class will be available in the scope of the file class, too.

*ugh*. Defining a class within a function seems like a really ugly
hack to me. Can't you provide the fs instance to the file class
constructor? I think that would be a much more elegant solution..


Best,

   -Nikolaus

--
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
                                                         -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
fuse-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fuse-devel
Reply | Threaded
Open this post in threaded view
|

Re: Python: Pass parameters to file_class

Csaba Henk
Hi Nikolaus,

Can I please ask you to cc me your fuse-py related posts to the list?
That would make it sure that I don't skip them.

On 2008-07-18, Nikolaus Rath <[hidden email]> wrote:

> Csaba Henk <[hidden email]> writes:
>> On 2008-07-06, Nikolaus Rath <[hidden email]> wrote:
>>> I would like to pass some additional parameters (mainly the "parent"
>>> fuse object) to the constructor of file_class. Is there a way to
>>> accomplish that?
>>
>> Yes. Define the file class _within_ the main method of the parent.
>> Then those variables you set in main() before defining the file
>> class will be available in the scope of the file class, too.
>
> *ugh*. Defining a class within a function seems like a really ugly
> hack to me. Can't you provide the fs instance to the file class
> constructor? I think that would be a much more elegant solution..

I don't see it ugly. The file class' methods are generic -- they can be
directly mapped to (stateful) FUSE fs methods --, but the file class
itself is usually expected to be particular to the Fuse instance. So a
*class* belongs to an *instance*. Therefore it seems to be plausible to
me that the class in question shall be defined within the scope of the
instance.

Anyway, I'm open to niftifications, I just don't see how to do it
sensibly.

I don't want to break compatibility for this. So what I could imagine
is the following: when the code sets up delegation to the file class
methods, it would check if the file class' __init__ can take keyword
arguments, and if yes, delegation would pass a

  fuse = <reference to the Fuse instance>

keyword argument upon open/create to the file class' constructor.
However, to do this, I should be able to tell if a function takes
keyword args. I don't know how to do this, all I could find is a related
PEP:

  http://www.python.org/dev/peps/pep-0362/

Do you happen to know a way to do this?

Csaba


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
fuse-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fuse-devel
Reply | Threaded
Open this post in threaded view
|

Re: Python: Pass parameters to file_class

Nikolaus Rath
Csaba Henk <[hidden email]> writes:

> On 2008-07-18, Nikolaus Rath <[hidden email]> wrote:
>> Csaba Henk <[hidden email]> writes:
>>> On 2008-07-06, Nikolaus Rath <[hidden email]> wrote:
>>>> I would like to pass some additional parameters (mainly the "parent"
>>>> fuse object) to the constructor of file_class. Is there a way to
>>>> accomplish that?
>>>
>>> Yes. Define the file class _within_ the main method of the parent.
>>> Then those variables you set in main() before defining the file
>>> class will be available in the scope of the file class, too.
>>
>> *ugh*. Defining a class within a function seems like a really ugly
>> hack to me. Can't you provide the fs instance to the file class
>> constructor? I think that would be a much more elegant solution..
>
> I don't see it ugly. The file class' methods are generic -- they can be
> directly mapped to (stateful) FUSE fs methods --, but the file class
> itself is usually expected to be particular to the Fuse instance. So a
> *class* belongs to an *instance*. Therefore it seems to be plausible to
> me that the class in question shall be defined within the scope of the
> instance.

Yes, I totally agree. But unfortunately Python does not support the
definition of classes belonging to an instance (Java, for example,
does. If you define an inner class, it automatically creates a closure
of the parent instance it belongs to).

Defining the class within a Python *function* has the desired effect,
but I don't think that one can argue that the file class is related
only to the main() function. Or would you?

> Anyway, I'm open to niftifications, I just don't see how to do it
> sensibly.
>
> I don't want to break compatibility for this. So what I could imagine
> is the following: when the code sets up delegation to the file class
> methods, it would check if the file class' __init__ can take keyword
> arguments, and if yes, delegation would pass a
>
>   fuse = <reference to the Fuse instance>
>
> keyword argument upon open/create to the file class' constructor.
> However, to do this, I should be able to tell if a function takes
> keyword args. I don't know how to do this, all I could find is a related
> PEP:
>
>   http://www.python.org/dev/peps/pep-0362/
>
> Do you happen to know a way to do this?

I don't quite understand what you mean with "keyword args" in this
context. When calling a function, the fname(param=value) notation can
always be used, no matter how the function has been defined. Is that
what you mean?

What one *could* check is whether a function has a parameter called
"fuse":

  import inspect
  if "fuse" in inspect.getargspec(fn)[0]:
     print "takes fuse="
  else:
     print "does not take fuse="
     

So that could be used to introduce the feature without breaking
compatibility. Alternatively, one could also introduce a new
attribute, say "file_class_v2". If the fs would define this attribute,
the constructor could be called with the additional argument.


Since my last post I also came up with the following workaround to
pass the instance. In case you decide against both the above
modifications, maybe it would be nice to put this into the
documentation (it took me a while to come up with it):

class my_fs(Fuse):

    def __init__(self):
        Fuse.__init__(self)

        # Mess around to pass fuse instance to file class constructor
        class file_class (my_file_class):
            def __init__(self2, *a, **kw):
                file_class.__init__(self2, *a, **kw, fuse=self)
        self.file_class = file_class


Best,

   -Nikolaus

--
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
                                                         -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C


-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
fuse-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fuse-devel
Reply | Threaded
Open this post in threaded view
|

Re: Python: Pass parameters to file_class

Robie Basak-2
Hi,

I'm having the same problem. If you use file_class for a real FUSE
filesystem, then you have to check permissions on an open, which is in
file_class.__init__. I can't GetContext() inside the file_class without
having a reference to the Fuse subclass, so having such a reference is
absolutely essential.

I think that the Fuse reference should be passed to the constructor of
the file_class. It doesn't make sense not to. I think that having to
define the file_class inside a function in order to get access to self
is ugly.

I realise that there's a concern for backward compatibility with the
current API, but IMHO the current API is broken.

Nikolaus Rath wrote:

> Since my last post I also came up with the following workaround to
> pass the instance. In case you decide against both the above
> modifications, maybe it would be nice to put this into the
> documentation (it took me a while to come up with it):
>
> class my_fs(Fuse):
>
>     def __init__(self):
>         Fuse.__init__(self)
>
>         # Mess around to pass fuse instance to file class constructor
>         class file_class (my_file_class):
>             def __init__(self2, *a, **kw):
>                 file_class.__init__(self2, *a, **kw, fuse=self)
>         self.file_class = file_class
>  
Thanks - this was an excellent starting point. I had a little bit of
trouble with it though, because my intepreter doesn't like an argument
after **kw. I reworked it a bit too.

Using the same sort of idea, I did:

class MyFilesystem(Fuse):
    ...
    class File:
        ...
    def main(self):
        class file_class(MyFilesystem.File):
            def __init__(self2, *args, **kwargs):
                kwargs['fuse'] = self
                MyFilesystem.File.__init__(self2, *args, **kwargs)
    self.file_class = file_class
    return Fuse.main(self, *args, **kwargs)

This seems like quite a clean workaround to me.

Robie

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
fuse-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/fuse-devel