diff options
Diffstat (limited to 'contrib/libucl/python')
-rw-r--r-- | contrib/libucl/python/MANIFEST.in | 5 | ||||
-rw-r--r-- | contrib/libucl/python/setup.py | 75 | ||||
-rw-r--r-- | contrib/libucl/python/src/uclmodule.c | 335 | ||||
-rw-r--r-- | contrib/libucl/python/tests/__init__.py | 0 | ||||
-rw-r--r-- | contrib/libucl/python/tests/compat.py | 8 | ||||
-rw-r--r-- | contrib/libucl/python/tests/test_dump.py | 66 | ||||
-rw-r--r-- | contrib/libucl/python/tests/test_example.py | 59 | ||||
-rw-r--r-- | contrib/libucl/python/tests/test_load.py | 122 | ||||
-rw-r--r-- | contrib/libucl/python/tests/test_validation.py | 50 | ||||
-rw-r--r-- | contrib/libucl/python/ucl.pyi | 15 |
10 files changed, 0 insertions, 735 deletions
diff --git a/contrib/libucl/python/MANIFEST.in b/contrib/libucl/python/MANIFEST.in deleted file mode 100644 index 336a4b3a7a07..000000000000 --- a/contrib/libucl/python/MANIFEST.in +++ /dev/null @@ -1,5 +0,0 @@ -include COPYING -recursive-include include *.h -recursive-include src *.h -recursive-include klib *.h -recursive-include uthash *.h diff --git a/contrib/libucl/python/setup.py b/contrib/libucl/python/setup.py deleted file mode 100644 index 8da832bac381..000000000000 --- a/contrib/libucl/python/setup.py +++ /dev/null @@ -1,75 +0,0 @@ -try: - from setuptools import setup, Extension - # setuptools doesn't support template param for MANIFEST.in - from setuptools.command.egg_info import manifest_maker - manifest_maker.template = 'python/MANIFEST.in' -except ImportError: - from distutils.core import setup, Extension - -import os -import sys - -LIB_ROOT = os.path.abspath(os.path.join(__file__, os.pardir, os.pardir)) -if os.getcwd() != LIB_ROOT: - os.chdir(LIB_ROOT) -if LIB_ROOT not in sys.path: - sys.path.append(LIB_ROOT) - -tests_require = [] - -if sys.version < '2.7': - tests_require.append('unittest2') - -uclmodule = Extension( - 'ucl', - libraries=['ucl', 'curl'], - sources=['python/src/uclmodule.c'], - include_dirs=['include'], - language='c', -) - -ucl_lib = { - 'sources': ['src/' + fn for fn in os.listdir('src') if fn.endswith('.c')], - 'include_dirs': ['include', 'src', 'uthash', 'klib'], - 'macros': [('CURL_FOUND', '1')], -} - -# sdist setup() will pull in the *.c files automatically, but not headers -# MANIFEST.in will include the headers for sdist only -template = 'python/MANIFEST.in' - -# distutils assume setup.py is in the root of the project -# we need to include C source from the parent so trick it -in_ucl_root = 'setup.py' in os.listdir('python') -if in_ucl_root: - os.link('python/setup.py', 'setup.py') - -setup( - name = 'ucl', - version = '0.8.1', - description = 'ucl parser and emitter', - ext_modules = [uclmodule], - template=template, # no longer supported with setuptools but doesn't hurt - libraries = [('ucl', ucl_lib)], - test_suite = 'tests', - tests_require = tests_require, - author = "Eitan Adler, Denis Volpato Martins", - author_email = "lists@eitanadler.com", - url = "https://github.com/vstakhov/libucl/", - license = "MIT", - classifiers = [ - "Development Status :: 3 - Alpha", - "Intended Audience :: Developers", - "License :: DFSG approved", - "License :: OSI Approved :: MIT License", - "Programming Language :: C", - "Programming Language :: Python :: 2", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: Implementation :: CPython", - "Topic :: Software Development :: Libraries", - ] -) - -# clean up the trick after the build -if in_ucl_root: - os.unlink("setup.py") diff --git a/contrib/libucl/python/src/uclmodule.c b/contrib/libucl/python/src/uclmodule.c deleted file mode 100644 index d1051fbb0d12..000000000000 --- a/contrib/libucl/python/src/uclmodule.c +++ /dev/null @@ -1,335 +0,0 @@ -// Attempts to load a UCL structure from a string -#include <ucl.h> -#include <Python.h> - -static PyObject *SchemaError; - -static PyObject * -_basic_ucl_type (ucl_object_t const *obj) -{ - switch (obj->type) { - case UCL_INT: - return Py_BuildValue ("L", (long long)ucl_object_toint (obj)); - case UCL_FLOAT: - return Py_BuildValue ("d", ucl_object_todouble (obj)); - case UCL_STRING: - return Py_BuildValue ("s", ucl_object_tostring (obj)); - case UCL_BOOLEAN: - return PyBool_FromLong (ucl_object_toboolean (obj)); - case UCL_TIME: - return Py_BuildValue ("d", ucl_object_todouble (obj)); - case UCL_NULL: - Py_RETURN_NONE; - } - return NULL; -} - -static PyObject * -_iterate_valid_ucl (ucl_object_t const *obj) -{ - const ucl_object_t *tmp; - ucl_object_iter_t it = NULL; - - tmp = obj; - - while ((obj = ucl_object_iterate (tmp, &it, false))) { - PyObject *val; - - val = _basic_ucl_type(obj); - if (!val) { - PyObject *key = NULL; - - if (obj->key != NULL) { - key = Py_BuildValue("s", ucl_object_key(obj)); - } - - if (obj->type == UCL_OBJECT) { - const ucl_object_t *cur; - ucl_object_iter_t it_obj = NULL; - - val = PyDict_New(); - - while ((cur = ucl_object_iterate (obj, &it_obj, true))) { - PyObject *keyobj = Py_BuildValue("s",ucl_object_key(cur)); - PyDict_SetItem(val, keyobj, _iterate_valid_ucl(cur)); - } - } else if (obj->type == UCL_ARRAY) { - const ucl_object_t *cur; - ucl_object_iter_t it_obj = NULL; - - val = PyList_New(0); - - while ((cur = ucl_object_iterate (obj, &it_obj, true))) { - PyList_Append(val, _iterate_valid_ucl(cur)); - } - } else if (obj->type == UCL_USERDATA) { - // XXX: this should be - // PyBytes_FromStringAndSize; where is the - // length from? - val = PyBytes_FromString(obj->value.ud); - } - } - return val; - } - - PyErr_SetString(PyExc_SystemError, "unhandled type"); - return NULL; -} - -static PyObject * -_internal_load_ucl (char *uclstr) -{ - PyObject *ret; - struct ucl_parser *parser = - ucl_parser_new (UCL_PARSER_NO_TIME|UCL_PARSER_NO_IMPLICIT_ARRAYS); - bool r = ucl_parser_add_string(parser, uclstr, 0); - - if (r) { - if (ucl_parser_get_error (parser)) { - PyErr_SetString(PyExc_ValueError, ucl_parser_get_error(parser)); - ucl_parser_free(parser); - ret = NULL; - goto return_with_parser; - } else { - ucl_object_t *uclobj = ucl_parser_get_object(parser); - ret = _iterate_valid_ucl(uclobj); - ucl_object_unref(uclobj); - goto return_with_parser; - } - } - else { - PyErr_SetString(PyExc_ValueError, ucl_parser_get_error (parser)); - ret = NULL; - goto return_with_parser; - } - -return_with_parser: - ucl_parser_free(parser); - return ret; -} - -static PyObject* -ucl_load (PyObject *self, PyObject *args) -{ - char *uclstr; - - if (PyArg_ParseTuple(args, "z", &uclstr)) { - if (!uclstr) { - Py_RETURN_NONE; - } - - return _internal_load_ucl(uclstr); - } - - return NULL; -} - -static ucl_object_t * -_iterate_python (PyObject *obj) -{ - if (obj == Py_None) { - return ucl_object_new(); - } - else if (PyBool_Check (obj)) { - return ucl_object_frombool (obj == Py_True); - } -#if PY_MAJOR_VERSION < 3 - else if (PyInt_Check (obj)) { - return ucl_object_fromint (PyInt_AsLong (obj)); - } -#endif - else if (PyLong_Check (obj)) { - return ucl_object_fromint (PyLong_AsLong (obj)); - } - else if (PyFloat_Check (obj)) { - return ucl_object_fromdouble (PyFloat_AsDouble (obj)); - } - else if (PyUnicode_Check (obj)) { - ucl_object_t *ucl_str; - PyObject *str = PyUnicode_AsASCIIString(obj); - ucl_str = ucl_object_fromstring (PyBytes_AsString (str)); - Py_DECREF(str); - return ucl_str; - } -#if PY_MAJOR_VERSION < 3 - else if (PyString_Check (obj)) { - return ucl_object_fromstring (PyString_AsString (obj)); - } -#endif - else if (PyDict_Check(obj)) { - PyObject *key, *value; - Py_ssize_t pos = 0; - ucl_object_t *top, *elm; - char *keystr = NULL; - - top = ucl_object_typed_new (UCL_OBJECT); - - while (PyDict_Next(obj, &pos, &key, &value)) { - elm = _iterate_python(value); - - if (PyUnicode_Check(key)) { - PyObject *keyascii = PyUnicode_AsASCIIString(key); - keystr = PyBytes_AsString(keyascii); - Py_DECREF(keyascii); - } -#if PY_MAJOR_VERSION < 3 - else if (PyString_Check(key)) { - keystr = PyString_AsString(key); - } -#endif - else { - PyErr_SetString(PyExc_TypeError, "Unknown key type"); - return NULL; - } - - ucl_object_insert_key (top, elm, keystr, 0, true); - } - - return top; - } - else if (PySequence_Check(obj)) { - PyObject *value; - Py_ssize_t len, pos; - ucl_object_t *top, *elm; - - len = PySequence_Length(obj); - top = ucl_object_typed_new (UCL_ARRAY); - - for (pos = 0; pos < len; pos++) { - value = PySequence_GetItem(obj, pos); - elm = _iterate_python(value); - ucl_array_append(top, elm); - } - - return top; - } - else { - PyErr_SetString(PyExc_TypeError, "Unhandled object type"); - return NULL; - } - - return NULL; -} - -static PyObject * -ucl_dump (PyObject *self, PyObject *args) -{ - PyObject *obj; - ucl_emitter_t emitter; - ucl_object_t *root = NULL; - - emitter = UCL_EMIT_CONFIG; - - if (!PyArg_ParseTuple(args, "O|i", &obj, &emitter)) { - PyErr_SetString(PyExc_TypeError, "Unhandled object type"); - return NULL; - } - - if (emitter >= UCL_EMIT_MAX) { - PyErr_SetString(PyExc_TypeError, "Invalid emitter type"); - return NULL; - } - - if (obj == Py_None) { - Py_RETURN_NONE; - } - - root = _iterate_python(obj); - if (root) { - PyObject *ret; - char *buf; - - buf = (char *) ucl_object_emit (root, emitter); - ucl_object_unref (root); -#if PY_MAJOR_VERSION < 3 - ret = PyString_FromString (buf); -#else - ret = PyUnicode_FromString (buf); -#endif - free(buf); - - return ret; - } - - return NULL; -} - -static PyObject * -ucl_validate (PyObject *self, PyObject *args) -{ - PyObject *dataobj, *schemaobj; - ucl_object_t *data, *schema; - bool r; - struct ucl_schema_error err; - - if (!PyArg_ParseTuple (args, "OO", &schemaobj, &dataobj)) { - PyErr_SetString (PyExc_TypeError, "Unhandled object type"); - return NULL; - } - - schema = _iterate_python(schemaobj); - if (!schema) - return NULL; - - data = _iterate_python(dataobj); - if (!data) - return NULL; - - // validation - r = ucl_object_validate (schema, data, &err); - ucl_object_unref (schema); - ucl_object_unref (data); - - if (!r) { - PyErr_SetString (SchemaError, err.msg); - return NULL; - } - - Py_RETURN_TRUE; -} - -static PyMethodDef uclMethods[] = { - {"load", ucl_load, METH_VARARGS, "Load UCL from stream"}, - {"dump", ucl_dump, METH_VARARGS, "Dump UCL to stream"}, - {"validate", ucl_validate, METH_VARARGS, "Validate ucl stream against schema"}, - {NULL, NULL, 0, NULL} -}; - -static void -init_macros(PyObject *mod) -{ - PyModule_AddIntMacro(mod, UCL_EMIT_JSON); - PyModule_AddIntMacro(mod, UCL_EMIT_JSON_COMPACT); - PyModule_AddIntMacro(mod, UCL_EMIT_CONFIG); - PyModule_AddIntMacro(mod, UCL_EMIT_YAML); - PyModule_AddIntMacro(mod, UCL_EMIT_MSGPACK); - - SchemaError = PyErr_NewException("ucl.SchemaError", NULL, NULL); - Py_INCREF(SchemaError); - PyModule_AddObject(mod, "SchemaError", SchemaError); -} - -#if PY_MAJOR_VERSION >= 3 -static struct PyModuleDef uclmodule = { - PyModuleDef_HEAD_INIT, - "ucl", - NULL, - -1, - uclMethods -}; - -PyMODINIT_FUNC -PyInit_ucl (void) -{ - PyObject *mod = PyModule_Create (&uclmodule); - init_macros (mod); - - return mod; -} -#else -void initucl (void) -{ - PyObject *mod = Py_InitModule ("ucl", uclMethods); - init_macros (mod); -} -#endif diff --git a/contrib/libucl/python/tests/__init__.py b/contrib/libucl/python/tests/__init__.py deleted file mode 100644 index e69de29bb2d1..000000000000 --- a/contrib/libucl/python/tests/__init__.py +++ /dev/null diff --git a/contrib/libucl/python/tests/compat.py b/contrib/libucl/python/tests/compat.py deleted file mode 100644 index 50138262e9b7..000000000000 --- a/contrib/libucl/python/tests/compat.py +++ /dev/null @@ -1,8 +0,0 @@ -try: - import unittest2 as unittest -except ImportError: - import unittest - -# Python 2.7 - 3.1 -if not hasattr(unittest.TestCase, 'assertRaisesRegex'): - unittest.TestCase.assertRaisesRegex = unittest.TestCase.assertRaisesRegexp diff --git a/contrib/libucl/python/tests/test_dump.py b/contrib/libucl/python/tests/test_dump.py deleted file mode 100644 index 369241468509..000000000000 --- a/contrib/libucl/python/tests/test_dump.py +++ /dev/null @@ -1,66 +0,0 @@ -from .compat import unittest -import ucl -import sys - -class DumpTest(unittest.TestCase): - def test_no_args(self): - with self.assertRaises(TypeError): - ucl.dump() - - def test_none(self): - self.assertEqual(ucl.dump(None), None) - - def test_null(self): - data = { "a" : None } - valid = "a = null;\n" - self.assertEqual(ucl.dump(data), valid) - - def test_int(self): - data = { "a" : 1 } - valid = "a = 1;\n" - self.assertEqual(ucl.dump(data), valid) - - def test_nested_int(self): - data = { "a" : { "b" : 1 } } - valid = "a {\n b = 1;\n}\n" - self.assertEqual(ucl.dump(data), valid) - - def test_int_array(self): - data = { "a" : [1,2,3,4] } - valid = "a [\n 1,\n 2,\n 3,\n 4,\n]\n" - self.assertEqual(ucl.dump(data), valid) - - def test_str(self): - data = { "a" : "b" } - valid = "a = \"b\";\n" - self.assertEqual(ucl.dump(data), valid) - - @unittest.skipIf(sys.version_info[0] > 2, "Python3 uses unicode only") - def test_unicode(self): - data = { unicode("a") : unicode("b") } - valid = unicode("a = \"b\";\n") - self.assertEqual(ucl.dump(data), valid) - - def test_float(self): - data = { "a" : 1.1 } - valid = "a = 1.100000;\n" - self.assertEqual(ucl.dump(data), valid) - - def test_boolean(self): - data = { "a" : True, "b" : False } - valid = [ - "a = true;\nb = false;\n", - "b = false;\na = true;\n" - ] - self.assertIn(ucl.dump(data), valid) - - def test_empty_ucl(self): - self.assertEqual(ucl.dump({}), "") - - def test_json(self): - data = { "a" : 1, "b": "bleh;" } - valid = [ - '{\n "a": 1,\n "b": "bleh;"\n}', - '{\n "b": "bleh;",\n "a": 1\n}' - ] - self.assertIn(ucl.dump(data, ucl.UCL_EMIT_JSON), valid) diff --git a/contrib/libucl/python/tests/test_example.py b/contrib/libucl/python/tests/test_example.py deleted file mode 100644 index f0785531f4e2..000000000000 --- a/contrib/libucl/python/tests/test_example.py +++ /dev/null @@ -1,59 +0,0 @@ -from .compat import unittest -import json -import ucl - -_ucl_inp = ''' -param = value; -section { - param = value; - param1 = value1; - flag = true; - number = 10k; - time = 0.2s; - string = "something"; - subsection { - host = { - host = "hostname"; - port = 900; - } - host = { - host = "hostname"; - port = 901; - } - } -} -''' - -_json_res = { - 'param': 'value', - 'section': { - 'param': 'value', - 'param1': 'value1', - 'flag': True, - 'number': 10000, - 'time': '0.2s', - 'string': 'something', - 'subsection': { - 'host': [ - { - 'host': 'hostname', - 'port': 900, - }, - { - 'host': 'hostname', - 'port': 901, - } - ] - } - } -} - -class TestExample(unittest.TestCase): - def test_example(self): - # load in sample UCL - u = ucl.load(_ucl_inp) - - # Output and read back the JSON - uj = json.loads(json.dumps(u)) - - self.assertEqual(uj, _json_res) diff --git a/contrib/libucl/python/tests/test_load.py b/contrib/libucl/python/tests/test_load.py deleted file mode 100644 index 73d43188f3d5..000000000000 --- a/contrib/libucl/python/tests/test_load.py +++ /dev/null @@ -1,122 +0,0 @@ -from .compat import unittest -import ucl - -class LoadTest(unittest.TestCase): - def test_no_args(self): - with self.assertRaises(TypeError): - ucl.load() - - def test_multi_args(self): - with self.assertRaises(TypeError): - ucl.load(0,0) - - def test_none(self): - self.assertEqual(ucl.load(None), None) - - def test_null(self): - data = "a: null" - valid = { "a" : None } - self.assertEqual(ucl.load(data), valid) - - def test_int(self): - data = "a : 1" - valid = { "a" : 1 } - self.assertEqual(ucl.load(data), valid) - - def test_braced_int(self): - data = "{a : 1}" - valid = { "a" : 1 } - self.assertEqual(ucl.load(data), valid) - - def test_nested_int(self): - data = "a : { b : 1 }" - valid = { "a" : { "b" : 1 } } - self.assertEqual(ucl.load(data), valid) - - def test_str(self): - data = "a : b" - valid = { "a" : "b" } - self.assertEqual(ucl.load(data), valid) - - def test_float(self): - data = "a : 1.1" - valid = {"a" : 1.1} - self.assertEqual(ucl.load(data), valid) - - def test_boolean(self): - data = ( - "a : True;" \ - "b : False" - ) - valid = { "a" : True, "b" : False } - self.assertEqual(ucl.load(data), valid) - - def test_empty_ucl(self): - self.assertEqual(ucl.load("{}"), {}) - - def test_single_brace(self): - self.assertEqual(ucl.load("{"), {}) - - def test_single_back_brace(self): - self.assertEqual(ucl.load("}"), {}) - - def test_single_square_forward(self): - self.assertEqual(ucl.load("["), []) - - def test_invalid_ucl(self): - with self.assertRaisesRegex(ValueError, "unfinished key$"): - ucl.load('{ "var"') - - def test_comment_ignored(self): - self.assertEqual(ucl.load("{/*1*/}"), {}) - - def test_1_in(self): - valid = { - 'key1': [ - 'value', - 'value2', - 'value;', - 1.0, - -0xdeadbeef, - '0xdeadbeef.1', - '0xreadbeef', - -1e-10, - 1, - True, - False, - True, - ] - } - with open("../tests/basic/1.in", "r") as in1: - self.assertEqual(ucl.load(in1.read()), valid) - - def test_every_type(self): - data = ("""{ - "key1": value; - "key2": value2; - "key3": "value;" - "key4": 1.0, - "key5": -0xdeadbeef - "key6": 0xdeadbeef.1 - "key7": 0xreadbeef - "key8": -1e-10, - "key9": 1 - "key10": true - "key11": no - "key12": yes - }""") - valid = { - 'key1': 'value', - 'key2': 'value2', - 'key3': 'value;', - 'key4': 1.0, - 'key5': -3735928559, - 'key6': '0xdeadbeef.1', - 'key7': '0xreadbeef', - 'key8': -1e-10, - 'key9': 1, - 'key10': True, - 'key11': False, - 'key12': True, - } - self.assertEqual(ucl.load(data), valid) diff --git a/contrib/libucl/python/tests/test_validation.py b/contrib/libucl/python/tests/test_validation.py deleted file mode 100644 index f7c853ad69a7..000000000000 --- a/contrib/libucl/python/tests/test_validation.py +++ /dev/null @@ -1,50 +0,0 @@ -from .compat import unittest -import ucl -import json -import os.path -import glob -import re - -TESTS_SCHEMA_FOLDER = '../tests/schema/*.json' - -comment_re = re.compile('\/\*((?!\*\/).)*?\*\/', re.DOTALL | re.MULTILINE) -def json_remove_comments(content): - return comment_re.sub('', content) - -class ValidationTest(unittest.TestCase): - def validate(self, jsonfile): - def perform_test(schema, data, valid, description): - msg = '%s (valid=%r)' % (description, valid) - if valid: - self.assertTrue(ucl.validate(schema, data), msg) - else: - with self.assertRaises(ucl.SchemaError): - ucl.validate(schema, data) - self.fail(msg) # fail() will be called only if SchemaError is not raised - - with open(jsonfile) as f: - try: - # data = json.load(f) - data = json.loads(json_remove_comments(f.read())) - except ValueError as e: - raise self.skipTest('Failed to load JSON: %s' % str(e)) - - for testgroup in data: - for test in testgroup['tests']: - perform_test(testgroup['schema'], test['data'], - test['valid'], test['description']) - - @classmethod - def setupValidationTests(cls): - """Creates each test dynamically from a folder""" - def test_gen(filename): - def run_test(self): - self.validate(filename) - return run_test - - for jsonfile in glob.glob(TESTS_SCHEMA_FOLDER): - testname = os.path.splitext(os.path.basename(jsonfile))[0] - setattr(cls, 'test_%s' % testname, test_gen(jsonfile)) - - -ValidationTest.setupValidationTests() diff --git a/contrib/libucl/python/ucl.pyi b/contrib/libucl/python/ucl.pyi deleted file mode 100644 index 79fa2901ba9f..000000000000 --- a/contrib/libucl/python/ucl.pyi +++ /dev/null @@ -1,15 +0,0 @@ -# Stubs for ucl (Python 3.6) -# -# NOTE: This dynamically typed stub was automatically generated by stubgen. - -UCL_EMIT_CONFIG = ... # type: int -UCL_EMIT_JSON = ... # type: int -UCL_EMIT_JSON_COMPACT = ... # type: int -UCL_EMIT_MSGPACK = ... # type: int -UCL_EMIT_YAML = ... # type: int - -def dump(*args, **kwargs): ... -def load(*args, **kwargs): ... -def validate(*args, **kwargs): ... - -class SchemaError(Exception): ... |