Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Replacement for ndb.Key in systems that do not have the AppEngine SDK available. Intended for systems that need to parse urlsafes

Notifications You must be signed in to change notification settings

projetoeureka/ndb-key-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DEPRECATED: Use glibs-ndb instead.

glibs-ndbkeyparser

Very small library for parsing AppEngine keys as urlsafes when outside the AppEngine env.

Note: The code here belongs to Google and was modified to suit our needs.

Installation

Install directly from this repository.

$ pip install https://github.com/projetoeureka/ndb-key-parser.git

Or use with a tool like poetry:

[tool.poetry.dependencies]
# ...
glibs-ndbkeyparser = { git = "https://github.com/projetoeureka/ndb-key-parser.git" }

Usage

ndb.Key()

There are two main usages of ndb.Key(): to get the ID from an urlsafe string, or to get the urlsafe string from an ID.

  • ndb.Key(urlsafe=urlsafe, app=app)

    from glibs.ndbkeyparser import ndb
    
    key = ndb.Key(urlsafe="<urlsafe string>", app="<app name>")
    key.id()
  • ndb.Key(kind, id, app=app)

    from glibs.ndbkeyparser import ndb
    
    key = ndb.Key("Kind", 123, app="<app name>")
    key.urlsafe()

ndb.ConverterHelper

It's a convenience class to ensure you're working with an ID or an urlsafe string, in cases you might get both.

  • ensure_key(urlsafe_or_id, kind=None) -> returns an urlsafe

    If urlsafe_or_id is an urlsafe string, it will only check if the kind matches (if passed).

    if urlsafe_or_id is an ID, then kind can't be None and it will build the Key and return its corresponding urlsafe.

    from glibs.ndbkeyparser import ndb
    
    helper = ndb.ConverterHelper("<app name>")
    
    urlsafe = helper.ensure_key(urlsafe_or_id, kind="Kind")
  • ensure_id(urlsafe_or_id) -> returns a numerical ID (as string)

    If urlsafe_or_id is an urlsafe string, it will build the Key and return its id.

    if urlsafe_or_id is an ID, it will just cast to string (if necessary).

    from glibs.ndbkeyparser import ndb
    
    helper = ndb.ConverterHelper("<app name>")
    
    urlsafe = helper.ensure_key(urlsafe_or_id, kind="Kind")

About

Replacement for ndb.Key in systems that do not have the AppEngine SDK available. Intended for systems that need to parse urlsafes

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages