Skip to content

Instantly share code, notes, and snippets.

@jonasfj
Created October 29, 2013 20:56
Show Gist options
  • Save jonasfj/7222444 to your computer and use it in GitHub Desktop.
Save jonasfj/7222444 to your computer and use it in GitHub Desktop.
def list_partitions(bucket, prefix='', level=0, schema=None, include_keys=False):
#print "Listing...", prefix, level
if schema is not None:
allowed_values = schema.sanitize_allowed_values()
delimiter = '/'
if level > 3:
delimiter = '.'
for k in bucket.list(prefix=prefix, delimiter=delimiter):
partitions = k.name.split("/")
if level > 3:
# split the last couple of partition components by "." instead of "/"
partitions.extend(partitions.pop().split(".", 2))
if schema is None or schema.is_allowed(partitions[level], allowed_values[level]):
if level >= 5:
if include_keys:
for f in bucket.list(prefix=k.name):
yield f
else:
yield k.name
else:
for prefix in list_partitions(bucket, k.name, level + 1, schema, include_keys):
yield prefix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment