Guest User

Untitled

a guest
Oct 17th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. from fbp import outport, inport, state, EOS
  2.  
  3.  
  4. # Decorators
  5.  
  6. @inport('filename', type=str, description='the file path to be read')
  7. @outport('line', type=str, description='a single line read from the file, END-OF-STREAM when the content is over')
  8. @state # tipo il @pass_context di click
  9. async def cat(filename, line, state):
  10. """ Read a file one line at a time. """
  11. async with aiofiles.open(await filename.pop(), mode='r') as f:
  12. # restore state if present
  13. fpos = state.get('fpos', 0)
  14. await f.seek(fpos)
  15. async for fline in f:
  16. await line.push(fline)
  17. # save state
  18. state['fpos'] = await f.tell()
  19. await line.push(EOS)
  20.  
  21.  
  22. @component
  23. async def cat(filename, line, state):
  24. """ Read a file one line at a time.
  25.  
  26. Parameters
  27. ----------
  28. filename: inport(type=str)
  29. the file path to be read
  30. line: outport(type=str)
  31. a single line read from the file, END-OF-STREAM when the content is over
  32. state: state
  33. """
  34. async with aiofiles.open(await filename.pop(), mode='r') as f:
  35. # restore state if present
  36. fpos = state.get('fpos', 0)
  37. await f.seek(fpos)
  38. async for fline in f:
  39. await line.push(fline)
  40. # save state
  41. state['fpos'] = await f.tell()
  42. await line.push(EOS)
Add Comment
Please, Sign In to add comment