Pytrivfs
De HurdFr_Wiki.
Cet article est une ébauche. Cela signifie qu'il est n'est pas considéré par son auteur comme terminé.
Vous êtes libre de l'améliorer et de retirer cet avertissement si vous jugez que l'article est maintenant finalisé.
Python bindings for TrivFS.
Sommaire |
TODO
- Safe multithreading with Python
- Finish Read support
- Write support
QUESTIONS
- off_t <=> int ?
- Use inout instead of in parameters ?
HOWTO
How it work ?
Pytrivfs works with callbacks. For each action on the node, you can link a python callback to execute a piece of python code.
Currently, you can only create read-only translators and not all of the functions are implemented.
To start your translator, just use the usual way :
$ settrans -cgap node mytrans.py
or
$ settrans -cgap node python mytrans.py
if you don't want to make the .py file executable.
How to develop a python translator ?
First of all, you have to import the pytrivfs module in your namespace :
import pytrivfs
Then, for each callback, you have to define a function and associate it to an event using set_callback(). Here's an example of setting up a callback :
def start():
dict = list()
fd = open("langage", "r")
lines = fd.readlines()
for line in lines:
dict.append(line.rstrip("\n"))
fd.close()
pytrivfs.set_callback(pytrivfs.START, start)
The first argument is a constant defining where the callback will be hooked. The second one is the callback itself.
Once you have filled in your callbacks (you don't need to define them all, it will skip undefined ones), start with :
pytrivfs.start()
Callbacks
At bootstrap
- Symbol : START
- Arguments : -
- Return : -
When asking to go away
- Symbol : GOAWAY
- Arguments : -
- Return : -
When opening the node
- Symbol : OPEN
- Arguments : hook (A namespace for your needs)
- Return : -
When reading
- Symbol : IO_READ
- Arguments : hook (A namespace for your needs), offs (The offset), amount (how much we want)
- Return : data, offs, amount
When seeking
- Symbol : IO_SEEK
- Arguments : hook (A namespace for your needs), offs (The offset), how (0 : SEEK_SET, 1 : SEEK_CUR, 2 : SEEK_END)
- Return : offs (the new offset)
Examples !
An http fetcher
#!/usr/bin/python
import sys, os, urllib
#Temp line, won't be necessary
sys.path.append('.')
import pytrivfs
# HTTP Translator
# A translator that fetch a webpage
def open_cb(hook):
hook.off = -1
sock = urllib.urlopen("http://www.hurdfr.org")
hook.source = sock.read()
sock.close()
def read_cb(hook, off, amount):
length = len(hook.source)
if off == -1:
off = hook.off
elif off < -1:
off = 0
if off > length:
off = length
if (off + amount) > length:
amount = length - off
hook.off += amount
return hook.source, off, amount
def seek_cb(hook, offs, how):
if how == 0:
hook.off = offs
elif how == 1:
hook.off += offs
elif how == 2:
hook.off = len(hook.source) - offs
return hook.off
pytrivfs.set_callback(pytrivfs.OPEN, open_cb)
pytrivfs.set_callback(pytrivfs.IO_READ, read_cb)
pytrivfs.set_callback(pytrivfs.IO_SEEK, seek_cb)
pytrivfs.start()

