{ "info": { "author": "Oleg Semenovsky", "author_email": "o.semenovsky@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 2 - Pre-Alpha", "Environment :: Plugins", "Intended Audience :: Developers", "License :: OSI Approved :: GNU General Public License v2 (GPLv2)", "Natural Language :: English", "Operating System :: POSIX", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", "Programming Language :: Python :: Implementation :: CPython", "Topic :: Software Development", "Topic :: Software Development :: Libraries", "Topic :: Software Development :: Libraries :: Python Modules" ], "description": "oshlex\n======\n\n**oshlex** (Object shlex) - Rule based unix-style config parser written\nwith shlex\n\nThis is a project made to simplify the task of reading and parsing\nUNIX-style configuration files. It's simply an abstraction, using shlex\nlibrary, that provides an easy way to parse configuration files, by\ncreating a set of rules and handlers, which are then used to transform\ntokens into python data structures.\n\n**Warning**: At this moment only python >3.4 is supported, python2.7\nsupport is planned in the near future.\n\nInstallation\n~~~~~~~~~~~~\nYou can install this package from pypi using pip.\nGiven that the python 2.7 isn't supported (yet), you can do it as \nfollows:\n\n::\n\n pip3.4 install oshlex\n\n\nConfiguration syntax\n~~~~~~~~~~~~~~~~~~~~\n\nThe configuration files syntax by default tries to follow the standard\nUNIX configuration files syntax, as found in, for example nginx configs.\nThis is changed by simply subclassing Tokenizer class, or creating your\nown class, that works identically to shlex.shlex class (Read `shlex\ndocumentation `__ for\ndetails)\n\nSample configuration file\n^^^^^^^^^^^^^^^^^^^^^^^^^\n\n::\n\n server {\n host 127.0.0.1;\n port 10000;\n user {\n username admin;\n password admin;\n }\n user {\n username test;\n password test;\n }\n }\n\nUsage\n~~~~~\n\n*oshlex.config* has two basic classes, **Rule** and **Configuration**.\n\n*oshlex.handlers* has a couple of predefined handlers (text and integer)\n\nCreating rules\n^^^^^^^^^^^^^^\n\nIn this example we define some rules, to parse the example configuration\nfile, as shown above.\n\nThe basic idea is to create root rule and pass it to Configuration\nobject while initializing. Every other rule is therefore a subrule of\n\"root\" rule.\n\nThe **Rule** class accepts following parameters:\n\n*name* - The name of rule, defaults to \"root\"\n\n*handler* - Handler function, that accepts the list of tokens and\noutputs python data structure to be used in the application.\n*oshlex.handlers* module has two predefined handler functions - *text*,\nwhich accepts a list of tokens and returns the first one as a string, or\nraises an UnacceptableTokenCount exception, if list of tokens contains\nmore than one element, and *integer*, which does basically the same\nthing, only returns python *int* object, or raises *UnacceptableToken*\nexception if token is not convertable to the *int* type. defaults to\n*None*, which means that the token is represented by python dictionary\nwith subtokens.\n\n*unique* - bool, defines if it is acceptable or not for said option to\nbe defined in the config file more than one once. If *True*, raises a\nConfigError exception if option is defined in two places at the same\ntime. If *False*, appends every occurence of the option definition to\nthe list. Defaults to *False*\n\n*mandatory* - bool, pretty self-explanatory, if option is mandatory, but\nnot defined - raises *MandatoryOptionMissing* exception, otherwise does\nnothing.\n\nBasic example\n^^^^^^^^^^^^^\n\n.. code:: python\n\n from oshlex.config import Rule, Configuration\n from oshlex import handlers\n\n # Defining rules\n root = Rule('root')\n server = Rule('server', unique=True, mandatory=True)\n host = Rule('host', handler=handlers.text, unique=True, mandatory=True)\n port = Rule('port', handler=handlers.integer, unique=True, mandatory=True)\n user = Rule('user', unique=False, mandatory=False)\n username = Rule('username', handler=handlers.text, unique=True, mandatory=True)\n password = Rule('password', handler=handlers.text, unique=True, mandatory=True)\n\n # Chaining rules together\n user.add(username)\n user.add(password)\n server.add(host)\n server.add(port)\n server.add(user)\n root.add(server)\n\n conf = Configuration(root)\n conf.read('./example.conf')\n\nThis way we get the Configuration object (conf) with following\nstructure:\n\n.. code:: python\n\n {\n 'server': {\n 'host': '127.0.0.1',\n 'port': 10000,\n 'user': [\n {'password': 'admin', 'username': 'admin'},\n {'password': 'test', 'username': 'test'}\n ]\n }}\n\nConfiguration object is subscriptable, so everything you can do with\ndictionaries is allowed here:\n\n.. code:: python\n\n >>> conf['server']['host']\n '127.0.0.1'\n\nYou can define your own handlers, which are just functions that accept a\nlist of tokens and return something that python can work with, if you\nneed to parse given tokens in some other ways, for example, we have an\noption that accepts ranges (e.g 1-10), we then need to define a hadler\nto convert these ranges into lists of integers, so we write a handler:\n\n.. code:: python\n\n from oshlex.handlers import UnacceptableToken, UnacceptableTokenCount\n def range(tokens):\n if len(tokens) > 1:\n raise UnacceptableTokenCount('This handler accepts 1 token at most')\n\n try:\n start, end = [int(token) for token in tokens[0].split('-')]\n except Exception as e:\n raise UnacceptableToken('Couldn\\'t process token {}, got {}'.format(tokens[0], e))\n\n return [i for i in range(start, end)]", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/osemenovsky/oshlex", "keywords": null, "license": "GNU General Public License v2 (GPLv2)", "maintainer": null, "maintainer_email": null, "name": "oshlex", "package_url": "https://pypi.org/project/oshlex/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/oshlex/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/osemenovsky/oshlex" }, "release_url": "https://pypi.org/project/oshlex/0.0.1.5/", "requires_dist": null, "requires_python": null, "summary": "Rule based unix-style config parser written with shlex", "version": "0.0.1.5" }, "last_serial": 2223035, "releases": { "0.0.1.2": [ { "comment_text": "", "digests": { "md5": "c71574805b2cd037dfa9da5c67c77dc1", "sha256": "0e977c6cc50f3508b32a25395782e87e309f1428b6b0d824090ea18626a89933" }, "downloads": -1, "filename": "oshlex-0.0.1.2.tar.gz", "has_sig": false, "md5_digest": "c71574805b2cd037dfa9da5c67c77dc1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5484, "upload_time": "2016-07-13T21:43:43", "url": "https://files.pythonhosted.org/packages/15/ed/4c54e85e00f83b4332bae406e24f29b8d0f93b6f5650f7fcb4ed19d4b1d8/oshlex-0.0.1.2.tar.gz" } ], "0.0.1.3": [ { "comment_text": "", "digests": { "md5": "510409de1196cdefb8a28f05ffdaf209", "sha256": "326255f62483cbf2d2c1dd1c63d42f03c50ad133d9439d1f10fcad56ef871bbb" }, "downloads": -1, "filename": "oshlex-0.0.1.3.tar.gz", "has_sig": false, "md5_digest": "510409de1196cdefb8a28f05ffdaf209", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5561, "upload_time": "2016-07-14T08:36:53", "url": "https://files.pythonhosted.org/packages/56/26/bd65935a3ae4a548d3238464f37b2e1a6924dc6ca89aa840522efd8b5b42/oshlex-0.0.1.3.tar.gz" } ], "0.0.1.4": [ { "comment_text": "", "digests": { "md5": "4343732fe54e5362200e61d93a5d02ce", "sha256": "31b85653e4bc35446339f4138e50242fe8c9f388a0a1ff24e380fda16cefd2b8" }, "downloads": -1, "filename": "oshlex-0.0.1.4.tar.gz", "has_sig": false, "md5_digest": "4343732fe54e5362200e61d93a5d02ce", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5588, "upload_time": "2016-07-14T14:32:21", "url": "https://files.pythonhosted.org/packages/c4/3f/3e4e002e22953cda0b0c8bd5f0c8d27b6a70c64d760b01ba5fa530c0e4e5/oshlex-0.0.1.4.tar.gz" } ], "0.0.1.5": [ { "comment_text": "", "digests": { "md5": "2a903535b347b5b266792a2079e190d7", "sha256": "4e46e637cebcfb0b56ae11337fbf59eab7d8633f88762f89e633eb2d005b5d99" }, "downloads": -1, "filename": "oshlex-0.0.1.5.tar.gz", "has_sig": false, "md5_digest": "2a903535b347b5b266792a2079e190d7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5670, "upload_time": "2016-07-15T12:17:40", "url": "https://files.pythonhosted.org/packages/a7/fd/22b49b5bebc8ff069cc84a49b8ba7816715a8d49d7c01d1d1361f63d17d8/oshlex-0.0.1.5.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "2a903535b347b5b266792a2079e190d7", "sha256": "4e46e637cebcfb0b56ae11337fbf59eab7d8633f88762f89e633eb2d005b5d99" }, "downloads": -1, "filename": "oshlex-0.0.1.5.tar.gz", "has_sig": false, "md5_digest": "2a903535b347b5b266792a2079e190d7", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 5670, "upload_time": "2016-07-15T12:17:40", "url": "https://files.pythonhosted.org/packages/a7/fd/22b49b5bebc8ff069cc84a49b8ba7816715a8d49d7c01d1d1361f63d17d8/oshlex-0.0.1.5.tar.gz" } ] }