Skip to content

Commit

Permalink
started email settings api, but we don't have access. migrated to lat…
Browse files Browse the repository at this point in the history
…est gdata
  • Loading branch information
progrium committed Jun 25, 2012
1 parent a7c7422 commit a530f3a
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 72 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
*.pyc
**/*.pyc

.DS_Store
24 changes: 18 additions & 6 deletions gdomain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
# Pulled from my work on hackerdojo/hd-domain on GitHub.
# Eventually could be a generic library

import gdata.apps.service
import gdata.apps.groups.service
import gdata.apps.client
import gdata.apps.emailsettings.client
import gdata.apps.groups.client

def _user(self, user):
return {
Expand All @@ -26,17 +27,28 @@ def flatten(l):
class Domain(object):
def __init__(self, name, username, password):
self.name = name
self.source = 'hackparty'
self.username = username
self.password = password
self.users = DomainUsers(self)
self.groups = DomainGroups(self)
self.email = DomainEmail(self)

class DomainEmail(object):
def __init__(self, domain):
self.domain = domain
self.client = gdata.apps.emailsettings.client.EmailSettingsClient(domain=domain.name)
self.client.ClientLogin(domain.username, domain.password, domain.source)

def get_forwarding(self, username):
return self.client.RetrieveForwarding(username)

class DomainGroups(object):
def __init__(self, domain):
self.domain = domain
self.client = gdata.apps.groups.service.GroupsService(domain=domain.name)
self.client = gdata.apps.groups.client.GroupsProvisioningClient(domain=domain.name)
#self.groups_client.SetClientLoginToken(token)
self.client.ClientLogin(domain.username, domain.password)
self.client.ClientLogin(domain.username, domain.password, domain.source)

def all(self):
return [g['groupId'].split('@')[0] for g in self.client.RetrieveAllGroups()]
Expand All @@ -49,8 +61,8 @@ def get(self, group_id):
class DomainUsers(object):
def __init__(self, domain):
self.domain = domain
self.client = gdata.apps.service.AppsService(domain=domain.name)
self.client.ClientLogin(domain.username, domain.password)
self.client = gdata.apps.client.AppsClient(domain=domain.name)
self.client.ClientLogin(domain.username, domain.password, domain.source)
#self.apps_client.SetClientLoginToken(token)

def all(self, include_suspended=False):
Expand Down
2 changes: 1 addition & 1 deletion hackparty/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def hello_world():
@app.route('/users')
def users():
d = Domain("hackparty.org", "jeff.lindsay@hackparty.org", memcache.get("domain_pass"))
return json.dumps(d.groups.all())
return json.dumps(d.email.get_forwarding('jeff.lindsay'))
Empty file modified pkgs/atom/__init__.py
100644 → 100755
Empty file.
54 changes: 48 additions & 6 deletions pkgs/atom/client.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ class AtomPubClient(object):
host = None
auth_token = None
ssl = False # Whether to force all requests over https
xoauth_requestor_id = None

def __init__(self, http_client=None, host=None,
auth_token=None, source=None, **kwargs):
def __init__(self, http_client=None, host=None, auth_token=None, source=None,
xoauth_requestor_id=None, **kwargs):
"""Creates a new AtomPubClient instance.
Args:
Expand All @@ -60,6 +61,7 @@ def __init__(self, http_client=None, host=None,
self.host = host
if auth_token is not None:
self.auth_token = auth_token
self.xoauth_requestor_id = xoauth_requestor_id
self.source = source

def request(self, method=None, uri=None, auth_token=None,
Expand Down Expand Up @@ -93,7 +95,10 @@ def request(self, method=None, uri=None, auth_token=None,
# HTTP request.
for name, value in kwargs.iteritems():
if value is not None:
value.modify_request(http_request)
if hasattr(value, 'modify_request'):
value.modify_request(http_request)
else:
http_request.uri.query[name] = str(value)
# Default to an http request if the protocol scheme is not set.
if http_request.uri.scheme is None:
http_request.uri.scheme = 'http'
Expand Down Expand Up @@ -150,7 +155,7 @@ def delete(self, uri=None, auth_token=None, http_request=None, **kwargs):

def modify_request(self, http_request):
"""Changes the HTTP request before sending it to the server.
Sets the User-Agent HTTP header and fills in the HTTP host portion
of the URL if one was not included in the request (for this it uses
the self.host member if one is set). This method is called in
Expand All @@ -171,12 +176,49 @@ def modify_request(self, http_request):
if self.host is not None and http_request.uri.host is None:
http_request.uri.host = self.host

if self.xoauth_requestor_id is not None:
http_request.uri.query['xoauth_requestor_id'] = self.xoauth_requestor_id

# Set the user agent header for logging purposes.
if self.source:
http_request.headers['User-Agent'] = '%s gdata-py/2.0.6' % self.source
http_request.headers['User-Agent'] = '%s gdata-py/2.0.17' % self.source
else:
http_request.headers['User-Agent'] = 'gdata-py/2.0.6'
http_request.headers['User-Agent'] = 'gdata-py/2.0.17'

return http_request

ModifyRequest = modify_request


class CustomHeaders(object):
"""Add custom headers to an http_request.
Usage:
>>> custom_headers = atom.client.CustomHeaders(header1='value1',
header2='value2')
>>> client.get(uri, custom_headers=custom_headers)
"""

def __init__(self, **kwargs):
"""Creates a CustomHeaders instance.
Initialize the headers dictionary with the arguments list.
"""
self.headers = kwargs

def modify_request(self, http_request):
"""Changes the HTTP request before sending it to the server.
Adds the custom headers to the HTTP request.
Args:
http_request: An atom.http_core.HttpRequest().
Returns:
An atom.http_core.HttpRequest() with the added custom headers.
"""

for name, value in self.headers.iteritems():
if value is not None:
http_request.headers[name] = value
return http_request
32 changes: 24 additions & 8 deletions pkgs/atom/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
from elementtree import ElementTree


try:
from xml.dom.minidom import parseString as xmlString
except ImportError:
xmlString = None

STRING_ENCODING = 'utf-8'


Expand Down Expand Up @@ -341,10 +346,16 @@ def _attach_members(self, tree, version=1, encoding=None):
else:
tree.text = self.text.decode(encoding)

def to_string(self, version=1, encoding=None):
def to_string(self, version=1, encoding=None, pretty_print=None):
"""Converts this object to XML."""
return ElementTree.tostring(self._to_tree(version, encoding))

tree_string = ElementTree.tostring(self._to_tree(version, encoding))

if pretty_print and xmlString is not None:
return xmlString(tree_string).toprettyxml()

return tree_string

ToString = to_string

def __str__(self):
Expand Down Expand Up @@ -379,7 +390,9 @@ def __set_extension_attributes(self, attributes):

def _get_tag(self, version=1):
qname = _get_qname(self, version)
return qname[qname.find('}')+1:]
if qname:
return qname[qname.find('}')+1:]
return None

def _get_namespace(self, version=1):
qname = _get_qname(self, version)
Expand All @@ -396,23 +409,26 @@ def _set_tag(self, tag):
else:
self._qname[0] = tag
else:
if self._qname.startswith('{'):
if self._qname is not None and self._qname.startswith('{'):
self._qname = '{%s}%s' % (self._get_namespace(), tag)
else:
self._qname = tag

def _set_namespace(self, namespace):
tag = self._get_tag(1)
if tag is None:
tag = ''
if isinstance(self._qname, tuple):
self._qname = self._qname.copy()
if namespace:
self._qname[0] = '{%s}%s' % (namespace, self._get_tag(1))
self._qname[0] = '{%s}%s' % (namespace, tag)
else:
self._qname[0] = self._get_tag(1)
self._qname[0] = tag
else:
if namespace:
self._qname = '{%s}%s' % (namespace, self._get_tag(1))
self._qname = '{%s}%s' % (namespace, tag)
else:
self._qname = self._get_tag(1)
self._qname = tag

tag = property(_get_tag, _set_tag,
"""Provides backwards compatibility for v1 atom.AtomBase classes.""")
Expand Down
5 changes: 2 additions & 3 deletions pkgs/atom/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import atom.core


XML_TEMPLATE = '{http://www.w3.org/XML/1998/namespace}%s'
ATOM_TEMPLATE = '{http://www.w3.org/2005/Atom}%s'
APP_TEMPLATE_V1 = '{http://purl.org/atom/app#}%s'
APP_TEMPLATE_V2 = '{http://www.w3.org/2007/app}%s'
Expand Down Expand Up @@ -184,7 +185,7 @@ class LinkFinder(object):
"""

def find_url(self, rel):
"""Returns the URL in a link with the desired rel value."""
"""Returns the URL (as a string) in a link with the desired rel value."""
for link in self.link:
if link.rel == rel and link.href:
return link.href
Expand Down Expand Up @@ -335,5 +336,3 @@ def __init__(self, tag=None, namespace=None, attributes=None,
self.text = text

_BecomeChildElement = atom.core.XmlElement._become_child


Loading

0 comments on commit a530f3a

Please sign in to comment.