appstate (version 0.1)
index
/usr/lib/python2.5/site-packages/appstate.py

Distributed Application Shared State
 
This module allows your Python programs to easily have a persistent
global distributed shared state.  This state can be used to store
things like number of users, game high scores, message of the day from
the author, etc.  The state has an interface like a dictionary with
some additional synchronization functionality and some restrictions.
This module connects to a server on the Google App Engine to store and
manage data.

 
Modules
       
md5
prpc
rencode
sys

 
Classes
       
DistributedState
exceptions.Exception(exceptions.BaseException)
AppIdError
DataUnchangedError
DuplicateError
InvalidAddressError
LoginError
PermissionError
SizeError
UnexpectedError
UnjoinedError
UpdateFailedError

 
class AppIdError(exceptions.Exception)
    
Method resolution order:
AppIdError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.Exception:
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object at 0x81419c0>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message
exception message

 
class DataUnchangedError(exceptions.Exception)
    
Method resolution order:
DataUnchangedError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.Exception:
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object at 0x81419c0>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message
exception message

 
class DistributedState
    Distributed Persistent Data Store
 
  Methods defined here:
__delitem__(self, key)
Delete the value for a given key
 
Will raise PermissionError if you do not have permission to
write to the state.  Will raise KeyError if the key has no
value to delete.
__getitem__(self, key)
Retrieve the most current value for the given key
 
Will raise KeyError if there have not been any calls setting
the value of the key.  Will raise PermissionError if you do
not have permission to read the key value.  May raise other
various exceptions if the connection times out, if the server
reports a problem, or if the application data gets corrupted.
 
The return value will be Python data.
__init__(self, server='pygameserver.appspot.com')
Create new connection to server
 
This function establishes a connection to Google App Engine.
Server will normally be pygameserver.appspot.com
__setitem__(self, key, value)
Set the value for a given key
 
This function accepts any Python data for the value that is not
too big.  The size of the data when serialized and encoded is
limited to 20K.
 
Note that this function does not care what the previous state
of the application was.  Other copies of the application may
have already updated the value by the time you call set().  It
is recommended to use update() rather than this function.
 
Will raise UnjoinedError if you have not joined the
application.  Will raise PermissionError if you do not have
permission to write to the state.  Will raise SizeError if the
value is too big.  May raise various exceptions if the
connection times out, if the server reports a problem, or if
the application state data gets corrupted.
apply_op(self, key, func, create=False, defaultvalue=None)
Apply a function to the value stored at a key
 
The function func must take one argument and return one
argument.  The state will be updated to reflect applying the
function to the value stored at the given key.  The function
may be called more than once if the value is being changed
by other instances of the application.  To make debugging
easier, try to limit side effects in the function.
 
If the key has no value, and create is false, will raise
a KeyError exception.  If create is true, the function
will apply to the given defaultvalue.
 
This function attempts to guarantee that even if many
instances are simultaneously attempting to update the same
value in the state, all the functions will be applied in some
order.  For example, if the value is a list and the operation
is inserting an element into the list, using this method will
guarantee that all elements will be inserted.
 
This function cannot guarantee that two instances will not
simultaneously create a new value.  If you need absolute
consistency in this case, create default values in the state
before distributing multiple instances of the application.
authorize(self, email)
Authorize a user
 
You must be logged in as administrator and have joined the
application.  Note that in some read and write modes,
authorizing users has no effect.
 
Raises UnjoinedError if you haven't joined an application
state.  Raises DuplicateError if the email has already been
authorized.  Raises PermissionError if you are not logged in
as admin.
ban(self, email)
Ban a user
 
You must be logged in as administrator and have joined the
application.  Note that in some read and write modes,
banning users has no effect.
 
Raises UnjoinedError if you haven't joined an application
state.  Raises DuplicateError if the email has already been
banned.  Raises PermissionError if you are not logged in as
admin.
delete_app(self)
Delete the application
 
You must be logged in and have joined the application.  Your
account muse be the admin account for the application.  Cannot
be undone.
 
Will raise PermissionError if you are not logged in and the
application admin.  Will raise UnjoinedError if you have not
joined the application.
email(self, addr, subj, body)
Send email
 
You must be logged in as administrator and have joined the
application.  Destination address must be a valid email
address.
 
Raises UnjoinedError if you have not joined the application.
Raises PermissionError if you are not admin.
get_if_changed(self, key, oldhash)
Retrieve the value for the given key if it has changed
 
You pass a key and the hash value that you already know about,
and the server will either send you the most current value
that has a different hash, or raise DataUnchangedError if
there are no changes to report.
 
Will raise KeyError if there have not been any calls setting
the value of the key.  Will raise PermissionError if you do
not have permission to read the key value.  May raise other
various exceptions if the connection times out, if the server
reports a problem, or if the application data gets corrupted.
 
The return value will be Python data.
join(self, appid=None)
Join an existing application
 
Establishes connection to the pygame application distributed
state using application id (appid).  Will raise AppIdError if
the appid does not exist.
login(self, email, password)
Login to Google account
 
Calling this function logs into a Google account with the
given email and password.  Will raise LoginError if there is a
problem.  Check the 'reasons' field of the exception to see
why the login was denied if there was an exception raised.
 
Your account must already exist and be in good standing. To
create new accounts or manage problems with your existing
account, go to:
 
https://www.google.com/accounts/Login
 
If you have been locked out of your account for attempting to
login too quickly with incorrect passwords, unlock the account
by going to:
 
https://www.google.com/accounts/DisplayUnlockCaptcha
new_app(self, appid, readmode=0, writemode=0)
Register a new application
 
You must be logged in.  Your account will be the admin
account for the application.  After creating the app,
joins it.
 
Each application must have a unique id.  Your appid is shared
between all instances of your application that are running.
 
Recommended naming scheme for appids:
(your name OR your domain name) PLUS application name
 
So one of my hypothetical application ids is:
NathanWhitehead+AstroMaxBlaster
 
The readmode and writemode arguments indicate how permissions
work.  They can be the following values:
 
ANY - means anyone can do the operation, even if not logged in
 
ADMIN_ONLY - only the admin can do the operation
 
AUTHORIZED_ONLY - only users that have been explicitly
authorized can do the operation
 
UNBANNED_ONLY - any logged in user that has not been banned
can do the operation
 
For example, setting readmode=ANY and writemode=ADMIN_ONLY
means that anyone can read the application state, but only the
admin can make any changes.  You cannot change readmode or
writemode once the application has been registered.  Only the
admin can authorize users or ban them.
 
Will raise PermissionError if you are not logged in.  Will raise
DuplicateError if the application id is already used.
unauthorize(self, email)
Unauthorize a user
 
You must be logged in as administrator and have joined the
application.  Note that in some read and write modes,
authorizing users has no effect.
 
Raises UnjoinedError if you haven't joined an application
state.  Raises DuplicateError if the email is not authorized.
Raises PermissionError if you are not logged in as admin.
unban(self, email)
Unban a user
 
You must be logged in as administrator and have joined the
application.  Note that in some read and write modes,
banning users has no effect.
 
Raises UnjoinedError if you haven't joined an application
state.  Raises DuplicateError if the email is not banned.
Raises PermissionError if you are not logged in as admin.
update(self, key, oldhash, value)
Update the value associated with a given key
 
This function checks that the current value matches the given
hash value, then updates the value associated with the key.
The size of the new value when serialized and encoded is
limited to 20K.  If the hash value you give does not match
the hash of the current value, this function will raise
UpdateFailedError.
 
To calculate the hash of a value v, use: hash_value(v)
version(self)
Get version string returned by server

 
class DuplicateError(exceptions.Exception)
    
Method resolution order:
DuplicateError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.Exception:
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object at 0x81419c0>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message
exception message

 
class InvalidAddressError(exceptions.Exception)
    
Method resolution order:
InvalidAddressError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.Exception:
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object at 0x81419c0>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message
exception message

 
class LoginError(exceptions.Exception)
    
Method resolution order:
LoginError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.Exception:
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object at 0x81419c0>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message
exception message

 
class PermissionError(exceptions.Exception)
    
Method resolution order:
PermissionError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.Exception:
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object at 0x81419c0>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message
exception message

 
class SizeError(exceptions.Exception)
    
Method resolution order:
SizeError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.Exception:
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object at 0x81419c0>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message
exception message

 
class UnexpectedError(exceptions.Exception)
    
Method resolution order:
UnexpectedError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.Exception:
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object at 0x81419c0>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message
exception message

 
class UnjoinedError(exceptions.Exception)
    
Method resolution order:
UnjoinedError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.Exception:
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object at 0x81419c0>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message
exception message

 
class UpdateFailedError(exceptions.Exception)
    
Method resolution order:
UpdateFailedError
exceptions.Exception
exceptions.BaseException
__builtin__.object

Data descriptors defined here:
__weakref__
list of weak references to the object (if defined)

Methods inherited from exceptions.Exception:
__init__(...)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Data and other attributes inherited from exceptions.Exception:
__new__ = <built-in method __new__ of type object at 0x81419c0>
T.__new__(S, ...) -> a new object with type S, a subtype of T

Methods inherited from exceptions.BaseException:
__delattr__(...)
x.__delattr__('name') <==> del x.name
__getattribute__(...)
x.__getattribute__('name') <==> x.name
__getitem__(...)
x.__getitem__(y) <==> x[y]
__getslice__(...)
x.__getslice__(i, j) <==> x[i:j]
 
Use of negative indices is not supported.
__reduce__(...)
__repr__(...)
x.__repr__() <==> repr(x)
__setattr__(...)
x.__setattr__('name', value) <==> x.name = value
__setstate__(...)
__str__(...)
x.__str__() <==> str(x)

Data descriptors inherited from exceptions.BaseException:
__dict__
args
message
exception message

 
Functions
       
hash(x)
hash_value(x)
length_data(x)
serialize(x)
unserialize(s)

 
Data
        ADMIN_ONLY = 1
ANY = 0
AUTHORIZED_ONLY = 2
SERVER = 'pygameserver.appspot.com'
UNBANNED_ONLY = 3
__author__ = 'Nathan Whitehead'
__version__ = '0.1'

 
Author
        Nathan Whitehead