Skip to content

Commit

Permalink
Reorganize lib to fit distribution conventions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kronovet committed Aug 28, 2017
1 parent 19083e1 commit 71e8357
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
build
dist
MANIFEST
__pycache__

2 changes: 1 addition & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include *.py AUTHORS README.rst *.h MANIFEST.in LICENSE
include *.py AUTHORS README.rst snappy/*.h MANIFEST.in LICENSE
10 changes: 5 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,21 @@ Compressing and decompressing a file:

::

$ python -m snappy -c uncompressed_file compressed_file.snappy
$ python -m snappy -d compressed_file.snappy uncompressed_file
$ python -m snappy.cmd -c uncompressed_file compressed_file.snappy
$ python -m snappy.cmd -d compressed_file.snappy uncompressed_file

Compressing and decompressing a stream:

::

$ cat uncompressed_data | python -m snappy -c > compressed_data.snappy
$ cat compressed_data.snappy | python -m snappy -d > uncompressed_data
$ cat uncompressed_data | python -m snappy.cmd -c > compressed_data.snappy
$ cat compressed_data.snappy | python -m snappy.cmd -d > uncompressed_data

You can get help by running

::

$ python -m snappy --help
$ python -m snappy.cmd --help


Snappy - compression library from Google (c)
Expand Down
6 changes: 2 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@
"""


snappymodule = Extension('_snappy',
snappymodule = Extension('snappy._snappy',
libraries=['snappy'],
sources=['snappymodule.cc', 'crc32c.c'])
sources=['snappy/snappymodule.cc', 'snappy/crc32c.c'])

ext_modules = [snappymodule]
packages = ['snappy']
package_dir = {'snappy': ''}
install_requires = []

if 'PyPy' in sys.version:
Expand Down Expand Up @@ -76,6 +75,5 @@
],
ext_modules = ext_modules,
packages = packages,
package_dir = package_dir,
install_requires = install_requires
)
9 changes: 9 additions & 0 deletions snappy/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from snappy import (
compress,
uncompress,
decompress,
StreamCompressor,
StreamDecompressor,
stream_compress,
stream_decompress,
)
40 changes: 40 additions & 0 deletions snappy/cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from snappy import stream_compress, stream_decompress

def cmdline_main():
"""This method is what is run when invoking snappy via the commandline.
Try python -m snappy.cmd --help
"""
import sys
if (len(sys.argv) < 2 or len(sys.argv) > 4 or "--help" in sys.argv or
"-h" in sys.argv or sys.argv[1] not in ("-c", "-d")):
print("Usage: python -m snappy.cmd <-c/-d> [src [dst]]")
print(" -c compress")
print(" -d decompress")
print("output is stdout if dst is omitted or '-'")
print("input is stdin if src and dst are omitted or src is '-'.")
sys.exit(1)

if len(sys.argv) >= 4 and sys.argv[3] != "-":
dst = open(sys.argv[3], "wb")
elif hasattr(sys.stdout, 'buffer'):
dst = sys.stdout.buffer
else:
dst = sys.stdout

if len(sys.argv) >= 3 and sys.argv[2] != "-":
src = open(sys.argv[2], "rb")
elif hasattr(sys.stdin, "buffer"):
src = sys.stdin.buffer
else:
src = sys.stdin

if sys.argv[1] == "-c":
method = stream_compress
else:
method = stream_decompress

method(src, dst)


if __name__ == "__main__":
cmdline_main()
File renamed without changes.
File renamed without changes.
40 changes: 0 additions & 40 deletions snappy.py → snappy/snappy.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,43 +287,3 @@ def stream_decompress(src, dst, blocksize=_STREAM_TO_STREAM_BLOCK_SIZE):
buf = decompressor.decompress(buf)
if buf: dst.write(buf)
decompressor.flush() # makes sure the stream ended well


def cmdline_main():
"""This method is what is run when invoking snappy via the commandline.
Try python -m snappy --help
"""
import sys
if (len(sys.argv) < 2 or len(sys.argv) > 4 or "--help" in sys.argv or
"-h" in sys.argv or sys.argv[1] not in ("-c", "-d")):
print("Usage: python -m snappy <-c/-d> [src [dst]]")
print(" -c compress")
print(" -d decompress")
print("output is stdout if dst is omitted or '-'")
print("input is stdin if src and dst are omitted or src is '-'.")
sys.exit(1)

if len(sys.argv) >= 4 and sys.argv[3] != "-":
dst = open(sys.argv[3], "wb")
elif hasattr(sys.stdout, 'buffer'):
dst = sys.stdout.buffer
else:
dst = sys.stdout

if len(sys.argv) >= 3 and sys.argv[2] != "-":
src = open(sys.argv[2], "rb")
elif hasattr(sys.stdin, "buffer"):
src = sys.stdin.buffer
else:
src = sys.stdin

if sys.argv[1] == "-c":
method = stream_compress
else:
method = stream_decompress

method(src, dst)


if __name__ == "__main__":
cmdline_main()
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test_snappy.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import os
import sys
import random
import snappy
from snappy import snappy
import struct
from unittest import TestCase

Expand Down
4 changes: 2 additions & 2 deletions test_snappy_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
py3k = True

def test_snappy_cffi_enum():
from snappy_cffi import C
from snappy.snappy_cffi import C

assert 0 == C.SNAPPY_OK
assert 1 == C.SNAPPY_INVALID_INPUT
assert 2 == C.SNAPPY_BUFFER_TOO_SMALL

def test_snappy_all_cffi():
from snappy_cffi import ffi, C
from snappy.snappy_cffi import ffi, C

import os
data = 'string to be compressed'
Expand Down

0 comments on commit 71e8357

Please sign in to comment.