Skip to main content

IO tools for Hy

Project description

#+TITLE: iotools.hy

IO tools for Hy.

* iotools

** iotools.buffer

Flexible buffer data struct.

#+begin_src hy
(setv buffer (Buffer b"hello"))

#(buffer.pos buffer.data) ; 0 b"hello"

(.read buffer 2) ; b"he"
#(buffer.pos buffer.data) ; 2 b"hello"

(.strip buffer)
#(buffer.pos buffer.data) ; 0 b"llo"
#+end_src

=Buffer.readexactly= or =Buffer.readuntil= will raise
=BufferWantReadError= if there has no enough data, in this case, user
should revert buffer.pos, push more data to buffer, and then read
again.

** iotools.struct

DSL used to describe data structures.

*** simple struct spec

#+begin_src hy
(defstruct Short [int :len 2])
(Short.pack 1) ; b"\x00\x01"
(Short.unapck b"\x00\x01") ; 1
#+end_src

*** complex struct spec

#+begin_src hy
(defstruct ShortPair
[[first [int :len 2]]
[second [int :len 2]]])
(ShortPair.pack #(1 2)) ; b"\x00\x01\x00\x02"
(ShortPair.unpack b"\x00\x01\x00\x02") ; #(1 2)
#+end_src

*** class struct spec

#+begin_src hy
(defstruct int2 [int :len 2])
(defstruct Short int2)
(defstruct ShortPair [[first Short] [second Short]])
#+end_src

*** more complex struct spec

See =iotools.proto= or =iotools-tls13.struct= for more examples.

** iotools.stream

Sync/Async stream abstraction, based on =dash.do/a!=.

This module inspired by Python built-in =ssl.SSLObject=: for any read
function =f= that read data from read buffer, reapeatedly push new
data from stream to read buffer and call =f= on read buffer, while
=BufferWantReadError= was raised.

** iotools.util

Sync/Async object alias, and basic stream implementation.

Alias:

- =(name/a! sleep)=: =time.sleep= =asyncio.sleep=
- =(name/a! Lock)=: =threading.Lock= =asyncio.Look=
- =(name/a! Queue)=: =queue.Queue= =asyncio.Queue=
- ...


Basic streams: =IOStream=, =FileStream=, =BytesStream=,
=SocketStream=, =AIOStream=, =(name/a! TCPStream)=.

* iotools.proto

Simple protocol implementation based on iotools.

** iotools.proto.ssl

=ssl.SSLObject= wrapper.

#+begin_src hy
(with [stream (SyncTCPStream.open host port)]
(let [connector (SyncSSLConnector :ssl-context (ssl.create-default-context)
:server-hostname host)]
(with [stream (.handshake connector stream)]
...)))
#+end_src

** iotools.proto.http

*** HTTPChunkedStream

#+begin_src hy
(with stream [SyncTCPStream.open host port]
(.write stream (HTTPRequest.pack "GET" "/" "HTTP/1.1" {"Host" host}))
(.flush stream)
(let [#(version status reason headers) (.read-loop stream HTTPResponse.read)
stream (if (= (-get headers "Transfer-Encoding") "chunked")
(SyncHTTPChunkedStream :next-layer stream)
stream)]
...))
#+end_src

*** HTTPProxyConnector/Acceptor

#+begin_src hy
(with [stream (SyncTCPStream.open proxy-host proxy-port)]
(let [connector (SyncHTTPProxyConnector :host host :port port)]
(with [stream (.handshake connector stream)]
...)))
#+end_src

** iotools.proto.ws

#+begin_src hy
(with [stream (SyncTCPStream.open host port)]
(let [connector (SyncWSConnector :host host)]
(with [stream (.handshake connector stream)]
...)))
#+end_src

** iotools.proto.socks5

Similar with =iotools.proto.http=.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

iotools_hy-0.1.0.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

iotools_hy-0.1.0-py3-none-any.whl (15.9 kB view details)

Uploaded Python 3

File details

Details for the file iotools_hy-0.1.0.tar.gz.

File metadata

  • Download URL: iotools_hy-0.1.0.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.2 Linux/6.5.0-17-generic

File hashes

Hashes for iotools_hy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7e88e11cf2dbf0e7d50a50443e7a95728b4e303deca7aa84fef440c6e37126be
MD5 d0f595aca5b8eed04bad3fb021e8264d
BLAKE2b-256 a360a719d1ed41f6d47bed97bf43b784a713fe28e77f4aa02b2d21d6e8465dfc

See more details on using hashes here.

File details

Details for the file iotools_hy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: iotools_hy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.1 CPython/3.12.2 Linux/6.5.0-17-generic

File hashes

Hashes for iotools_hy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a41bf93898328ef619c138effa2885c9f207969f1a5917cb1359c45ab1338e89
MD5 5b7c92b3eaa995307043093075c57644
BLAKE2b-256 2919db47b0f8c4ff5fd52d7114ff1d06a017228064e39470d3b4a867204f1c1e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page