from xml.etree.ElementTree import TreeBuilder, XML, tostring
from urllib2 import urlopen, HTTPPasswordMgr, HTTPBasicAuthHandler, install_opener, build_opener

class ResourceInfo(object):
  resource_type = 'abstractResourceType'

  def update(self):
    self.metadata = get_xml(self.href)
    self.name = self.metadata.find('name').text

  def serialize(self):
    builder = TreeBuilder()
    builder.start(self.resource_type, dict())
    self.encode(builder)
    builder.end(self.resource_type)
    return tostring(builder.close())

  def encode(self, builder):
    """
    Add appropriate XML nodes to this object.  The builder will be passed in
    ready to go, with the appropriate top-level node already added.
    """
    pass


def get_xml(url):
  password_manager = HTTPPasswordMgr()
  password_manager.add_password(
    realm='GeoServer Realm',
    uri='http://localhost:8080/geoserver/',
    user='admin',
    passwd='geoserver'
  )

  handler = HTTPBasicAuthHandler(password_manager)
  install_opener(build_opener(handler))
  
  response = urlopen(url).read()
  try:
      return XML(response)
  except:
      print "%s => \n%s" % (url, response)

def atom_link(node):
    return node.find("{http://www.w3.org/2005/Atom}link").get("href")

@@ -54,16 +54,45 @@
 ostring%0A
+from tempfile import mkstemp%0A
 from url
@@ -179,16 +179,44 @@
 d_opener
+%0Afrom zipfile import ZipFile
 %0A%0Aclass 
@@ -783,16 +783,929 @@
   pass%0A%0A
+def prepare_shapefile_bundle(name, data):%0A  %22%22%22Create a ZIP archive with a shapefile and accompanying metadata for upload%5C%0A to GeoServer. GeoServer expects all files in the top directory level of the%5C%0A ZIP archive, with the basename the same as that of the layer to be created.%5C%0A The first argument to this function provides that expected layer name, and the%5C%0A second should be a dict mapping Shapefile file extensions to filesystem paths,%5C%0A or to file-like objects.  The function geoserver.util.shapefile_and_friends%5C%0A can generate such a dict for actual files.  This actually creates a temporary%5C%0A file, which client code should delete after use.%22%22%22%0A  f = mkstemp()%0A  zip = ZipFile(f, 'w')%0A  for ext, stream in data.iteritems():%0A    fname = %22%25s.%25s%22 %25 (name, ext)%0A    if (isinstance(stream, basestring)):%0A      zip.write(stream, fname)%0A    else:%0A      zip.writestr(fname, stream.read())%0A  zip.close()%0A  return f%0A
 %0Adef get

Create helper function to package up shapefiles as a GeoServer-friendly ZIP archive.