{ "info": { "author": "Roman Bystritskiy", "author_email": "rbystrit@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 4 - Beta", "Intended Audience :: Developers", "License :: OSI Approved :: Apache Software License", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: 3.6" ], "description": "AVRO-GEN\r\n========\r\n\r\n[![Build Status](https://travis-ci.org/rbystrit/avro_gen.svg?branch=master)](https://travis-ci.org/rbystrit/avro_gen)\r\n[![codecov](https://codecov.io/gh/rbystrit/avro_gen/branch/master/graph/badge.svg)](https://codecov.io/gh/rbystrit/avro_gen)\r\n##### Avro record class and specific record reader generator.\r\n\r\nCurrent Avro implementation in Python is completely typelss and operates on dicts. \r\nWhile in many cases this is convenient and pythonic, not being able to discover the schema\r\nby looking at the code, not enforcing schema during record constructions, and not having any \r\ncontext help from the IDE could hamper developer performance and introduce bugs. \r\n\r\nThis project aims to rectify this situation by providing a generator for constructing concrete\r\nrecord classes and constructing a reader which wraps Avro DatumReader and returns concrete classes\r\ninstead of dicts. In order not to violate Avro internals, this functionality is built strictly\r\non top of the DatumReader and all the specific record classes dict wrappers which define accessor\r\nproperties with proper type hints for each field in the schema. For this exact reason the \r\ngenerator does not provide an overloaded DictWriter; each specific record appears just to be a \r\nregular dictionary.\r\n \r\n##### Usage:\r\n schema_json = \".....\"\r\n output_directory = \".....\"\r\n from avrogen import write_schema_files\r\n \r\n write_schema_files(schema_json, output_directory)\r\n \r\nThe generator will create output directory if it does not exist and put generated files there. \r\nThe generated files will be:\r\n\r\n> OUTPUT_DIR\r\n> + \\_\\_init\\_\\_.py \r\n> + schema_classes.py \r\n> + submodules*\r\n \r\nIn order to deal with Avro namespaces, since python doesn't support circular imports, the generator\r\n will emit all records into schema_classes.py as nested classes. The top level class there will be\r\n SchemaClasses, whose children will be classes representing namespaces. Each namespace class will \r\n in turn contain classes for records belonging to that namespace. \r\n \r\n Consider following schema:\r\n \r\n {\"type\": \"record\", \"name\": \"tweet\", \"namespace\": \"com.twitter.avro\", \"fields\": [{\"name\": \"ID\", \"type\": \"long\" }\r\n \r\n Then schema_classes.py would contain:\r\n \r\n class SchemaClasses(object):\r\n class com(object):\r\n class twitter(object):\r\n class acro(object):\r\n class tweetClass(DictWrapper):\r\n def __init__(self, inner_dict=None):\r\n ....\r\n @property\r\n def ID(self):\r\n \"\"\"\r\n :rtype: long\r\n \"\"\"\r\n return self._inner_dict.get('ID', None)\r\n \r\n @ID.setter\r\n def ID(self, value):\r\n #\"\"\"\r\n #:param long value:\r\n #\"\"\"\r\n self._inner_dict['ID'] = value \r\n \r\n In order to map specific record types and namespaces to modules, so that proper importing can\r\n be supported, there generator will create a sub-module under the output directory for each namespace\r\n which will export names of all types contained in that namespace. Types declared with empty \r\n namespace will be exported from the root module. \r\n \r\n So for the example above, output directory will look as follows:\r\n \r\n > OUTPUT_DIR\r\n > + \\_\\_init\\_\\_.py\r\n > + schema_classes.py\r\n > + com\r\n > + twitter\r\n > + avro\r\n > + \\_\\_init\\_\\_.py \r\n\r\nThe contents of OUTPUT_DIR/com/twitter/avro/\\_\\_init\\_\\_.py will be:\r\n \r\n from ....schema_classes import SchemaClasses\r\n tweet = SchemaClasses.com.twitter.avro.tweet\r\n \r\nSo in your code you will be able to say:\r\n \r\n from OUTPUT_DIR.com.twitter.avro import tweet\r\n from OUTPUT_DIR import SpecificDatumReader as TweetReader, SCHEMA as your_schema\r\n from avro import datafile, io\r\n my_tweet = tweet()\r\n \r\n my_tweet.ID = 1\r\n with open('somefile', 'w+b') as f:\r\n writer = datafile.DataFileWriter(f,io.DatumWriter(), your_schema)\r\n writer.append(my_tweet)\r\n writer.close()\r\n \r\n with open('somefile', 'rb') as f:\r\n reader = datafile.DataFileReader(f,TweetReader(readers_schema=your_schema))\r\n my_tweet1 = reader.next()\r\n reader.close()\r\n \r\n \r\n### Avro protocol support\r\n\r\nAvro protocol support is implemented the same way as schema support. To generate classes \r\nfor a protocol:\r\n\r\n protocol_json = \".....\"\r\n output_directory = \".....\"\r\n from avrogen import write_protocol_files\r\n \r\n write_protocol_files(protocol_json, output_directory)\r\n \r\nThe structure of the generated code will be exactly same as for schema, but in addition to\r\nregular types, *Request types will be generated in the root namespace of the protocol for each \r\neach message defined.\r\n\r\n### Logical types support\r\n\r\nAvrogen implements logical types on top of standard avro package and supports generation of \r\nclasses thus typed. To enable logical types support, pass **use_logical_types=True** to schema \r\nand protocol generators. If custom logical types are implemented and such types map to types \r\nother than simple types or datetime.* or decimal.* then pass **custom_imports** parameter to \r\ngenerator functions so that your types are imported. Types implemented out of the box are:\r\n\r\n- decimal (using string representation only)\r\n- date\r\n- time-millis\r\n- time-micros\r\n- timestamp-millis\r\n- timestamp-micros\r\n\r\nTo register your custom logical type, inherit from avrogen.logical.LogicalTypeProcessor, implement\r\nabstract methods, and add an instance to avrogen.logical.DEFAULT_LOGICAL_TYPES dictionary under the \r\nname of your logical type. A sample implementation looks as follows:\r\n\r\n class DateLogicalTypeProcessor(LogicalTypeProcessor):\r\n _matching_types = {'int', 'long', 'float', 'double'}\r\n \r\n def can_convert(self, writers_schema):\r\n return isinstance(writers_schema, schema.PrimitiveSchema) and writers_schema.type == 'int'\r\n \r\n def validate(self, expected_schema, datum):\r\n return isinstance(datum, datetime.date)\r\n \r\n def convert(self, writers_schema, value):\r\n if not isinstance(value, datetime.date):\r\n raise Exception(\"Wrong type for date conversion\")\r\n return (value - EPOCH_DATE).total_seconds() // SECONDS_IN_DAY\r\n \r\n def convert_back(self, writers_schema, readers_schema, value):\r\n return EPOCH_DATE + datetime.timedelta(days=int(value))\r\n \r\n def does_match(self, writers_schema, readers_schema):\r\n if isinstance(writers_schema, schema.PrimitiveSchema):\r\n if writers_schema.type in DateLogicalTypeProcessor._matching_types:\r\n return True\r\n return False\r\n \r\n def typename(self):\r\n return 'datetime.date'\r\n \r\n def initializer(self, value=None):\r\n return ((\r\n 'logical.DateLogicalTypeProcessor().convert_back(None, None, %s)' % value) if value is not None\r\n else 'datetime.datetime.today().date()')\r\n\r\n\r\nTo read/write data with logical type support, use generated SpecificDatumReader \r\nand a LogicalDatumWriter from avro.logical.\r\n \r\n\r\n\r\n\r\n ", "description_content_type": null, "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/rbystrit/avro_gen", "keywords": "avro class generator", "license": "License :: OSI Approved :: Apache Software License", "maintainer": "", "maintainer_email": "", "name": "avro-gen", "package_url": "https://pypi.org/project/avro-gen/", "platform": "", "project_url": "https://pypi.org/project/avro-gen/", "project_urls": { "Homepage": "https://github.com/rbystrit/avro_gen" }, "release_url": "https://pypi.org/project/avro-gen/0.3.0/", "requires_dist": null, "requires_python": "", "summary": "Avro record class and specific record reader generator", "version": "0.3.0" }, "last_serial": 3487566, "releases": { "0.1.0b1": [ { "comment_text": "", "digests": { "md5": "5ee45c4ab18368e2a7101636c44773ff", "sha256": "02f61e60c3b14990704b2c0c2c9cf1e2bc1c70ade8980b6889cef20325cc66dc" }, "downloads": -1, "filename": "avro_gen-0.1.0b1-py2-none-any.whl", "has_sig": false, "md5_digest": "5ee45c4ab18368e2a7101636c44773ff", "packagetype": "bdist_wheel", "python_version": "2.7", "requires_python": null, "size": 9633, "upload_time": "2016-08-07T13:20:09", "url": "https://files.pythonhosted.org/packages/09/4d/8a0a4a8125b00a87a66f6ec8196250486d6bde0333c0717ffdbc702364ca/avro_gen-0.1.0b1-py2-none-any.whl" } ], "0.1.0b2": [ { "comment_text": "", "digests": { "md5": "3abd1e97486939785f00f24618477d91", "sha256": "fee665f6c63a5491390797c37d05652407aab89eba8031fd3cd624380a2c0255" }, "downloads": -1, "filename": "avro_gen-0.1.0b2-py2-none-any.whl", "has_sig": false, "md5_digest": "3abd1e97486939785f00f24618477d91", "packagetype": "bdist_wheel", "python_version": "2.7", "requires_python": null, "size": 15430, "upload_time": "2016-08-09T23:55:23", "url": "https://files.pythonhosted.org/packages/d7/d4/f89ebb30c89a8e286b398d62c2ee52df62149bd8b46b5a74f67cfdaad358/avro_gen-0.1.0b2-py2-none-any.whl" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "5f3a37c868965949746d0d9d18f724b1", "sha256": "89df93d7c96a55f2646bd8a0dfbbf3474a43f130b3bf07d6c06d7cd3b367dcb4" }, "downloads": -1, "filename": "avro-gen-0.2.0.zip", "has_sig": false, "md5_digest": "5f3a37c868965949746d0d9d18f724b1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 21707, "upload_time": "2018-01-04T20:41:06", "url": "https://files.pythonhosted.org/packages/2b/ad/34b22acf163307b3aac8c77cd9d1eb1d6b4f6636e29a717c59fb8a9fef0b/avro-gen-0.2.0.zip" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "591a2c8ad87f45b99b773edc93ad34e0", "sha256": "fc955256d8d566b7a3d0a6c3142d5dc709b5c3923604f9871232032836336e00" }, "downloads": -1, "filename": "avro-gen-0.2.1.zip", "has_sig": false, "md5_digest": "591a2c8ad87f45b99b773edc93ad34e0", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 21721, "upload_time": "2018-01-04T20:47:51", "url": "https://files.pythonhosted.org/packages/f3/4e/b7285f2e876a6c8956a161616661fd7932061acedc88f8c6f36dcef37362/avro-gen-0.2.1.zip" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "726a2a1521c5c8baf648d19dee076902", "sha256": "921fad7704feaabc196c8f55aacb6429faaa2895b004ec5566d6d2e26ca7a8de" }, "downloads": -1, "filename": "avro-gen-0.2.2.zip", "has_sig": false, "md5_digest": "726a2a1521c5c8baf648d19dee076902", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 21741, "upload_time": "2018-01-04T20:50:43", "url": "https://files.pythonhosted.org/packages/d1/59/68e41f511181b5b439f9f9b51cdeba35cfecb91a566fefeee87fdf4418cc/avro-gen-0.2.2.zip" } ], "0.2.3": [ { "comment_text": "", "digests": { "md5": "0a8968bef0e3fda393455fb5049c99a7", "sha256": "7b099e633189b99caa35fb06f8f0bb2e9d43cc6896295082601e4268236b391c" }, "downloads": -1, "filename": "avro-gen-0.2.3.zip", "has_sig": false, "md5_digest": "0a8968bef0e3fda393455fb5049c99a7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 21754, "upload_time": "2018-01-04T20:53:40", "url": "https://files.pythonhosted.org/packages/cc/7e/c6f2f965a5dc66eeff6c4075802c903928c7a89e64945a165174a103845c/avro-gen-0.2.3.zip" } ], "0.2.4": [ { "comment_text": "", "digests": { "md5": "6d9eddfcb3b098c0ca4b05f187082b34", "sha256": "c285911ddee8efce865bd4bccd0b5682e963991f4548dfa1a943638a7a672433" }, "downloads": -1, "filename": "avro-gen-0.2.4.zip", "has_sig": false, "md5_digest": "6d9eddfcb3b098c0ca4b05f187082b34", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 21805, "upload_time": "2018-01-05T21:20:55", "url": "https://files.pythonhosted.org/packages/7c/96/d041433921a4dadb3e4ae0af76fb2915bbdb9e5bffef7d4e5e2c38af4f82/avro-gen-0.2.4.zip" } ], "0.2.5": [ { "comment_text": "", "digests": { "md5": "1feef8b2cd3fc16d0d2e8a8cd4a86c30", "sha256": "c57cc45d53c0cfad90f4758190673c00398c767486396d4878df3aa510f0969e" }, "downloads": -1, "filename": "avro-gen-0.2.5.zip", "has_sig": false, "md5_digest": "1feef8b2cd3fc16d0d2e8a8cd4a86c30", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 23216, "upload_time": "2018-01-06T03:22:32", "url": "https://files.pythonhosted.org/packages/c7/d3/a45454dc6793bb466e7f34a4b9b3d7370e59b74040747d72e3114e3a27ba/avro-gen-0.2.5.zip" } ], "0.2.7": [ { "comment_text": "", "digests": { "md5": "8b55fa08727d727d762c9ac28f6d77d6", "sha256": "46b64ee61b97be35a8f9b8531f628a1217413fe1558349f1dfbd40293ae8bc78" }, "downloads": -1, "filename": "avro-gen-0.2.7.zip", "has_sig": false, "md5_digest": "8b55fa08727d727d762c9ac28f6d77d6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 23265, "upload_time": "2018-01-06T22:59:46", "url": "https://files.pythonhosted.org/packages/8d/e5/d54e9ab2bc60e46c5e89db7e31037dade2c45af0adb03e4921dc11938abe/avro-gen-0.2.7.zip" } ], "0.2.8": [ { "comment_text": "", "digests": { "md5": "76241fc126bc62bf8856a0a2f5b1ebf2", "sha256": "d9200d2b12c724057be9f1ae6b84209277c1e6e591c675a7bdf1779cb71fc134" }, "downloads": -1, "filename": "avro-gen-0.2.8.zip", "has_sig": false, "md5_digest": "76241fc126bc62bf8856a0a2f5b1ebf2", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 23373, "upload_time": "2018-01-11T15:18:31", "url": "https://files.pythonhosted.org/packages/8a/39/4275ae8553db25683884e2dbe61ef146f8343117b112984077ad3df429f2/avro-gen-0.2.8.zip" } ], "0.3.0": [ { "comment_text": "", "digests": { "md5": "48bd51e0aae9fc2253da6e7c904e299a", "sha256": "885c24ef4dcf17c8dcbcc28f9d23187c27ea9c94f04950b08442c20ad5736c6f" }, "downloads": -1, "filename": "avro-gen-0.3.0.zip", "has_sig": false, "md5_digest": "48bd51e0aae9fc2253da6e7c904e299a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 26758, "upload_time": "2018-01-13T21:32:49", "url": "https://files.pythonhosted.org/packages/0f/25/c9de407144b172a4a40cefe4dfc153ca23d14436e81439b677d80d9ad09a/avro-gen-0.3.0.zip" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "48bd51e0aae9fc2253da6e7c904e299a", "sha256": "885c24ef4dcf17c8dcbcc28f9d23187c27ea9c94f04950b08442c20ad5736c6f" }, "downloads": -1, "filename": "avro-gen-0.3.0.zip", "has_sig": false, "md5_digest": "48bd51e0aae9fc2253da6e7c904e299a", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 26758, "upload_time": "2018-01-13T21:32:49", "url": "https://files.pythonhosted.org/packages/0f/25/c9de407144b172a4a40cefe4dfc153ca23d14436e81439b677d80d9ad09a/avro-gen-0.3.0.zip" } ] }