{"id":22018562,"url":"https://github.com/linuxwhatelse/mapper","last_synced_at":"2025-03-23T09:29:25.463Z","repository":{"id":62577177,"uuid":"54727046","full_name":"linuxwhatelse/mapper","owner":"linuxwhatelse","description":"Simple URL-Scheme resolver","archived":false,"fork":false,"pushed_at":"2018-04-19T09:07:12.000Z","size":37,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-01T06:07:15.551Z","etag":null,"topics":["mapper","python","resolver","routing"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/linuxwhatelse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-25T15:16:38.000Z","updated_at":"2018-04-19T09:07:57.000Z","dependencies_parsed_at":"2022-11-03T19:10:41.521Z","dependency_job_id":null,"html_url":"https://github.com/linuxwhatelse/mapper","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linuxwhatelse%2Fmapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linuxwhatelse%2Fmapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linuxwhatelse%2Fmapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/linuxwhatelse%2Fmapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/linuxwhatelse","download_url":"https://codeload.github.com/linuxwhatelse/mapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245081825,"owners_count":20557853,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mapper","python","resolver","routing"],"created_at":"2024-11-30T05:12:35.689Z","updated_at":"2025-03-23T09:29:25.441Z","avatar_url":"https://github.com/linuxwhatelse.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"mapper - Simple URL-Scheme resolver\n===================================\n[![Build Status](https://travis-ci.org/linuxwhatelse/mapper.svg?branch=master)](https://travis-ci.org/linuxwhatelse/mapper)\n[![pypi](https://img.shields.io/pypi/v/lwe-mapper.svg)](https://pypi.python.org/pypi/lwe-mapper)\n\n**mapper** is a small side-project which I created while working on other *stuff* and was in the need for a super simple url-reslover.  \nThe idea was to keep the footprint as small as possible **without** relying on none-python modules.\n\nWhat you use it for is up to you.  \n\nIf you f.e. need a simple JSON Server, check out [mjs](https://github.com/linuxwhatelse/mjs) as it follows the\nsame principle.  \nSmall footprint, easy to use, and only one dependency - mapper (obviously).\n\nHow it works? It's super simple.  \nCheck [The very basic](#the-very-basic) and go from there.\n\n## Table of Contents\n* [Requirements](#requirements)\n* [Installation](#installation)\n* [Usage](#usage)\n    * [Registering functions](#registering-functions)\n        * [The very basic](#the-very-basic)\n        * [URL with a query](#url-with-a-query)\n        * [Query value type cast](#query-value-type-cast)\n        * [Extracting values from a URLs path](#extracting-values-from-a-urls-path)\n        * [Pythons kwargs](#pythons-kwargs)\n        * [Return values](#return-values)\n        * [Using the \"add\" function instead of the decorator](#using-the-add-function-instead-of-the-decorator)\n\n## Requirements\nWhat you need:\n* Python 2.7 or up\n\n## Installation\nYou have two options:\n\n1. Install via pypi `pip install lwe-mapper`\n2. Download [mapper.py](https://github.com/linuxwhatelse/mapper/blob/master/mapper.py) and place it into the root directory of your project\n\n## Usage\n\n### Registering functions\n\n#### The very basic\n``` python\nfrom mapper import Mapper\n\nmpr = Mapper.get()\n\n# Note: A path will ALWAYS end with a \"/\" regardless\n# if your URL contains a trailing \"/\" or not\n\n# Choose one of the two decorators\n@mpr.url('^/some/path/$')  # Regex pattern\n@mpr.s_url('/some/path/')  # Simple path\ndef func():\n    print('func called')\n\n# What e.g. your webserver would do...\nmpr.call('http://some.url/some/path')\n```\n\n#### URL with a query\n``` python\nfrom mapper import Mapper\n\nmpr = Mapper.get()\n\n# Note: Adding a query does NOT change the fact that\n# the path will end with a \"/\" for the regex pattern\n@mpr.s_url('/some/path/')\ndef func(param1, param2='default'):\n    print(param1, param2)\n\n# We don't supply \"param2\" and \"param3\" which will result in \"param2\" being None and param3 being 'default'\nmpr.call('http://some.url/some/path?param1=123')\n\n# Following would cause a:\n# TypeError: func() missing 1 required positional argument: 'param1'\nmpr.call('http://some.url/some/path')\n```\n\n#### Query value type cast\n``` python\nfrom mapper import Mapper\n\nmpr = Mapper.get()\n\n# By default all parameters will be of type \"string\".\n# You can change the type by supplying a dict where the key matches your parameters name and the value is one of:\n# int, float, bool\n#\n# Note for bool:\n#  1. Casting is case-insensitive.\n#  2. 1 and 0 can be casted as well\n@mpr.s_url('/some/path/', type_cast={'a_int' : int, 'a_float' : float, 'a_bool' : bool})\ndef func(a_int, a_float, a_bool):\n    print(a_int, a_float, a_bool)\n\nmpr.call('http://some.url/some/path?a_int=123\u0026a_float=1.0\u0026a_bool=true')\n```\n\n#### Extracting values from a URLs path\n``` python\nfrom mapper import Mapper\n\nmpr = Mapper.get()\n\n# In pure python regex fashion we define a named capture group within our pattern to\n# match whatever we want.\n# We can use a simplified url as well though.\n# Not that type-casting works as well.\n@mpr.url('^/some/path/(?P\u003cparam1\u003e[^/]*)/(?P\u003cparam2\u003e[0-9]*)/$', type_cast={'param2':int}) # Regex pattern\n@mpr.s_url('/some/path/\u003cparam1\u003e/\u003cparam2\u003e/', type_cast={'param2':int})                    # Simple path\ndef func(param1, param2):\n    print(param1, param2)\n\nmpr.call('http://some.url/some/path/abc/456/')\n```\n\n#### Pythons kwargs\n``` python\nfrom mapper import Mapper\n\nmpr = Mapper.get()\n\n# It's pretty simple and type-casting works as well\n@mpr.s_url('/some/path/', type_cast={'param1' : int, 'param2' : float, 'param3' : bool})\ndef func(param1, **kwargs):\n    print(param1, kwargs)\n\nmpr.call('http://some.url/some/path?param1=123\u0026param2=1.0\u0026param3=true')\n```\n\n#### Return values\n``` python\nfrom mapper import Mapper\n\nmpr = Mapper.get()\n\n# Whatever you return will be returned by mapper\n@mpr.s_url('/some/path/')\ndef func():\n    return ('str', 1, 1.0, True)\n\na_str, a_int, a_float, a_bool = mpr.call('http://some.url/some/path')\n```\n\n#### Using the \"add\" function instead of the decorator\nSometimes you might have to register a function with the mapper at a later point. This can easily be achieved by using the mappers \"add\" function.\n``` python\nfrom mapper import Mapper\n\nmpr = Mapper.get()\n\ndef func(param1, param2):\n    print(param1, param2)\n\n# It works the same way as the decorator.\n# The only difference is, that we have to specify the function ourselves.\nmpr.add('^/some/path/(?P\u003cparam1\u003e[0-9]*)/$', func, type_cast={'param1' : int, 'param2' : int})\nmpr.s_add('/some/path/\u003cparam1\u003e/', func, type_cast={'param1' : int, 'param2' : int})\n\nmpr.call('http://some.url/some/path/123?param2=456')\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinuxwhatelse%2Fmapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flinuxwhatelse%2Fmapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flinuxwhatelse%2Fmapper/lists"}