Permalink
Browse files

Structuring scratchpy into a python package

  • Loading branch information...
1 parent e0dfe55 commit 78c1d09646c760c27e547c797218e9cb4c6bcead @pilliq committed Dec 27, 2012
Showing with 155 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +22 −0 LICENSE
  3. +4 −0 MANIFEST.in
  4. +14 −0 Makefile
  5. +6 −0 scratch/__init__.py
  6. +14 −0 setup.py
  7. +8 −0 tests/__init__.py
  8. +84 −0 tests/parsing.py
View
@@ -1 +1,4 @@
*.pyc
+build/
+dist/
+scratch.egg-info/
View
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Phillip Quiza
+
+ Permission is hereby granted, free of charge, to any person
+ obtaining a copy of this software and associated documentation
+ files (the "Software"), to deal in the Software without
+ restriction, including without limitation the rights to use,
+ copy, modify, merge, publish, distribute, sublicense, and/or sell
+ copies of the Software, and to permit persons to whom the
+ Software is furnished to do so, subject to the following
+ conditions:
+
+ The above copyright notice and this permission notice shall be
+ included in all copies or substantial portions of the Software.
+
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+ OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+ HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ OTHER DEALINGS IN THE SOFTWARE.
View
@@ -0,0 +1,4 @@
+include LICENSE
+include Makefile
+include README.md
+recursive-include tests *
View
@@ -0,0 +1,14 @@
+all: sdist
+
+build:
+ python setup.py build
+sdist:
+ python setup.py sdist
+test:
+ python setup.py test
+install:
+ python setup.py install
+clean:
+ python setup.py clean --all
+ rm scratch/*.pyc
+ rm tests/*.pyc
View
@@ -0,0 +1,6 @@
+from scratch import Scratch, ScratchError, ScratchConnectionError
+
+__version__ = '0.1.0'
+VERSION = tuple(map(int, __version__.split('.')))
+
+__all__ = ['Scratch', 'ScratchError', 'ScratchConnectionError']
View
@@ -0,0 +1,14 @@
+from setuptools import setup
+from scratch import __version__
+
+setup(name='scratchpy',
+ version=__version__,
+ description='A Python interface to Scratch',
+ keywords=['Scratch'],
+ url='http://github.com/pilliq/scratchpy',
+ author='Phillip Quiza',
+ author_email='pquiza@gmail.com',
+ license='MIT',
+ packages=['scratch'],
+ test_suite='tests.all_tests',
+ zip_safe=False)
View
@@ -0,0 +1,8 @@
+import unittest
+
+from tests.parsing import ParsingTests
+
+def all_tests():
+ suite = unittest.TestSuite()
+ suite.addTest(unittest.makeSuite(ParsingTests))
+ return suite
View
@@ -0,0 +1,84 @@
+import unittest
+from scratch import Scratch
+
+class ParsingTests(unittest.TestCase):
+ """
+ Must have Scratch running with remote sensor connections enabled
+ """
+ def setUp(self):
+ self.client = Scratch()
+
+ def test_broadcast(self):
+ self.assertEquals(
+ self.client._parse('\x00\x00\x00\x0fbroadcast "a b"'),
+ ('broadcast', 'a b')
+ )
+ self.assertEquals(
+ self.client._parse('\x00\x00\x00\x13broadcast """a b"""'),
+ ('broadcast', '"a b"')
+ )
+ self.assertEquals(
+ self.client._parse('\x00\x00\x00\x17broadcast """""a b"""""'),
+ ('broadcast', '""a b""')
+ )
+ self.assertEquals(
+ self.client._parse('\x00\x00\x00\x13broadcast """a"" b"'),
+ ('broadcast', '"a" b')
+ )
+ self.assertEquals(
+ self.client._parse('\x00\x00\x00\x17broadcast """a"" ""b"""'),
+ ('broadcast', '"a" "b"')
+ )
+
+ def test_sensorupdate(self):
+ self.assertEquals(
+ self.client._parse('\x00\x00\x00\x14sensor-update "a" 0 '),
+ ('sensor-update', {'a': 0})
+ )
+ self.assertEquals(
+ self.client._parse('\x00\x00\x00\x16sensor-update "a" "c" '),
+ ('sensor-update', {'a': 'c'})
+ )
+ self.assertEquals(
+ self.client._parse('\x00\x00\x00\x16sensor-update "a b" 0 '),
+ ('sensor-update', {'a b': 0})
+ )
+ self.assertEquals(
+ self.client._parse('\x00\x00\x00\x1asensor-update """a b""" 0 '),
+ ('sensor-update', {'"a b"': 0})
+ )
+ self.assertEquals(
+ self.client._parse('\x00\x00\x003sensor-update """a b""" "hello hi" "a" "c" "a b" 0 '),
+ ('sensor-update', {'"a b"': 'hello hi', 'a': 'c', 'a b': 0})
+ )
+ self.assertEquals(
+ self.client._parse('\x00\x00\x00\x1esensor-update """a"" ""b""" 0 '),
+ ('sensor-update', {'"a" "b"': 0})
+ )
+ self.assertEquals(
+ self.client._parse('\x00\x00\x00*sensor-update """a"" ""b""" """c"" ""d""" '),
+ ('sensor-update', {'"a" "b"': '"c" "d"'})
+ )
+
+ def test_is_msg(self):
+ self.assertTrue(self.client._is_msg('\x00\x00\x00\x14sensor-update "a" 0 '))
+ self.assertTrue(self.client._is_msg('\x00\x00\x00\x0ebroadcast "hi"'))
+ self.assertFalse(self.client._is_msg('\x00\x00\x00\x14sensor-update "a"'))
+ self.assertFalse(self.client._is_msg('\x00\x00\x00\x14benbor-update "a" 0 '))
+ self.assertFalse(self.client._is_msg(''))
+ self.assertFalse(self.client._is_msg(None))
+ self.assertFalse(self.client._is_msg('\x00\x00\x00\x00'))
+ self.assertFalse(self.client._is_msg('\x00\x00\x00'))
+
+ def test_escape(self):
+ self.assertEquals(self.client._escape(''), '')
+ self.assertEquals(self.client._escape('"a"'), '""a""')
+
+ def test_unescape(self):
+ self.assertEquals(self.client._unescape(''), '')
+ self.assertEquals(self.client._unescape('a'), 'a')
+ self.assertEquals(self.client._unescape('""a""'), '"a"')
+ self.assertEquals(self.client._unescape(0), 0)
+
+if __name__ == '__main__':
+ unittest.main()

0 comments on commit 78c1d09

Please sign in to comment.