Example #1
0
 def __init__(self, auth_package = None):
     self._containers = None
     self.swift_client = SwiftClientWrapper(auth_package)
Example #2
0
class Hubic(object):
    def __init__(self, auth_package = None):
        self._containers = None
        self.swift_client = SwiftClientWrapper(auth_package)

    @property
    def containers(self):
        if self._containers is None:
            self._containers = SwiftContainers(self.swift_client)
        return self._containers

    def __str__(self):
        return unicode(self).encode('utf-8')

    def __unicode__(self):
        account_info = self.swift_client.head_account()
        l1 = u"| # Objects : {0:^10} | # containers : {1:^4} | Quota : {2:>6}/{3:<6} ({4:^6}%)|".format(
            account_info['x-account-object-count'],
            account_info['x-account-container-count'],
            human_bytes(int(account_info['x-account-bytes-used'])),
            human_bytes(int(account_info['x-account-meta-quota'])),
            round(int(account_info['x-account-bytes-used']) * 100.0/ int(account_info['x-account-meta-quota']),2)
        )
        out = u"-"*len(l1)
        out += "\r\n"
        out += l1
        out += "\r\n"
        out += u"-"*len(l1)
        out += "\r\n"
        return out

    def upload(self, files):
        size = sum([f['bytes'] for f in files])
        logger.warning("%s will be uploaded"%(human_bytes(size)))
        hubic_files_container = self.containers['default']
        hubic_files_container.upload(files)
        logger.warning("Upload done")

    def createDirectory(self, path):
        hubic_files_container = self.containers['default']
        hubic_files_container.createDirectory(path)

    def getAllObjectsMetadataIndexedByPath(self, path=None, ignored_exts=None, prefix=None, limit=None):
        obj_dict = {}
        hubic_files_container = self.containers['default']
        if path is not None and path[0] == "/":
            path = path[1:]
        if prefix is not None and prefix[0] == "/":
            prefix = prefix[1:]
        hubic_files_container.filterBy(path=path, limit=limit)
        for object in hubic_files_container:
            name = object['name']
            if prefix is not None:
                if not name.startswith(prefix):
                    logger.debug("%s is not in %s"%(name, prefix))
                    continue
                name = name.replace(prefix, "")
                if len(name) == 0:
                    continue
            ignore_file = False
            if ignored_exts is not None:
                for ign_ext in ignored_exts:
                    if name.endswith(ign_ext):
                        logger.debug("Ignoring %s (based on file extension)"%name)
                        ignore_file = True
                        continue
            if ignore_file:
                continue
            obj_dict[name] = object
        objects = None
        return obj_dict