Giter Club home page Giter Club logo

scopes's Introduction

scopes

build status License coveralls pypi

scopes exposes the Set class, a container type intended to simplify checking authorization scope.

Installation

You can install scopes from PyPi:

> pip install scopes

That should install all the dependencies for you. If you want to install directly from source, clone the git repository and run the standard python setup.py install command.

Dependencies

  • Python 2.7, 3.2+

Usage

scopes.Set implements the __contains__ magic method, making it easy to check if a particular scope and permission is expressed in a set of scopes.

>>> from scopes import Set
>>> Set(['user/emails+r'])
Set(['user/emails'])
>>> 'user/emails' in Set(['user/emails'])
True
>>> ('foo/bar', 'foo/baz') <= Set('foo')
True
>>> ['foo/bar', 'foo/baz', 'extra'] <= Set(['foo', 'bar'])
False
>>> Set(['foo', 'bar']) >= ('foo/bar', 'foo/baz')
True

A Set in fact works almost like any set type.

>>> len(Set(['user/emails', 'user/repo']))
2
>>> list(Set(['user/emails+r', 'user/repo+aaaaa']).formatted())
['user/emails', 'user/repo+a']

They can be quickly parsed from strings too.

>>> Set("user/emails+r user/emails+n")
Set(['user/emails', 'user/emails+n'])

This method uses a single space as a separator.

Permissions

You can append letters to scope items to express certain permissions. Any ascii letter that follows the permission separator (+ by default) is interpreted as a permission. When checking for an item in the scope list, both its value and permission must match at least one item in the list.

>>> 'user/emails+a' in Set(['user/emails'])
False
>>> 'user/emails+a' in Set(['user/emails+a'])
True

Indicate multiple permissions in a scope list item by including more than one letter after the + symbol. Duplicate permissions are ignored.

>>> 'user/repo+w' in Set(['user/repo+abcd', 'user/repo+rw'])
True

Permissions are totally arbitrary, except that +r is assumed by default when no permissions are explicitly given.

>>> 'user/emails+r' in Set(['user/emails'])
True

You can change the default permissions to whatever you like.

>>> 'user/emails+n' in Set(['user/emails'], default_permissions='n')
True
>>> 'user/emails+q' in Set(['user/emails'], default_permissions='pq')
True
>>> 'user/emails+p' in Set(['user/emails'], default_permissions='pq')
True

The permissions separator is also configurable.

>>> 'user/emails|r' in Set(['user/emails'], permission_sep='|')
True

Parents

The / symbol is the default child separator. Parent scope items automatically 'contain' child items in the scope list.

>>> 'user/emails+r' in Set(['user'])
True
>>> 'user/emails+w' in Set(['user'])
False
>>> 'user/emails+rw' in Set(['user+w', 'user/emails+r'])
True

The child separator can also be changed:

>>> 'user:emails+r' in Set(['user'], child_sep=':')
True

scopes's People

Contributors

mr-rodgers avatar

Watchers

 avatar  avatar

scopes's Issues

Doesn't work with unicode type on Python 2

The commit ed2632b introduced a change that breaks unicode scope items on Python 2:

>>> u'user' in ScopeList([u'user'])
[snipped]
  File "scopelist.py", line 139, in <genexpr>
    return all(o in self for o in item)
  File "scopelist.py", line 138, in __contains__
    if not isinstance(item, str) and isinstance(item, Iterable):
  File "C:\Dev\py\env27\lib\abc.py", line 132, in __instancecheck__
    if subclass is not None and subclass in cls._abc_cache:
  File "C:\Dev\py\env27\lib\_weakrefset.py", line 75, in __contains__
    return wr in self.data
RuntimeError: maximum recursion depth exceeded in cmp

It seems that takes a unicode type as an iterable of scope items, and doesn't make a special case for it as it does for str.

As part of the fix for this, the entire module should probably be updated to work correctly with unicode rather than str. That would include switching to unicode literals (which Python 3.3 and later support) and maybe rejecting bytes types outright, even on Python 2.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.