#!/usr/bin/env python

import ConfigParser
import requests
import getopt
import sys
import json
import mimetypes
import os
import types

class SwiftPush:

    def __init__(self):
        """
        Load configuration file and other required arguments
        """

        path = os.path.realpath("/usr/local/bin/swiftpush").split('/')
        path.pop()

        self.config = ConfigParser.ConfigParser()
        self.config.readfp(open("/".join(path) + "/" + "swiftpush.conf"))

        mimetypes.init()

        if len(sys.argv) == 1:
            print "swiftpush -f <file> -p <path> -c <container>"
            sys.exit(2)

        try:
            opts, args = getopt.getopt(sys.argv[1:], "hf:p:c:", ["file=", "path=", "container="])
        except getopt.GetoptError:
            print "swiftpush -f <file> -p <path> -c <container>"
            sys.exit(2)

        for opt, arg in opts:
            if opt == "-h":
                print "swiftpush -f <file> -p <path> -c <container>"
            elif opt in ("-f", "-file"):
                self.file = arg
            elif opt in ("-p", "-path"):
                self.path = arg
            elif opt in ("-c", "-container"):
                self.container = arg

        if not os.path.isfile(self.file):
            print "File: " + self.file + " does not exist!"
            sys.exit(1)

        if not os.access(self.file, os.R_OK):
            print "Unable to access file: " + self.file
            sys.exit(1)

    def getToken(self):

        request = {
            "auth": {
                "identity": {
                    "methods": ["password"],
                    "password": {
                        "user": {
                            "name": self.config.get("keystone", "username"),
                            "password": self.config.get("keystone", "password"),
                            "domain": {
                                "id": self.config.get("keystone", "project")
                            }
                        }
                    }
                }
            }
        }

        r = requests.post(self.config.get("keystone", "api_url") + "/auth/tokens", headers={
            "Content-Type": "application/json"
        }, data=json.dumps(request))

        responseJson = json.loads(r.text)

        for catalog in responseJson["token"]["catalog"]:
            if catalog["name"] == "swift":
                for endpoint in catalog["endpoints"]:
                    if endpoint["interface"] == "public":
                        self.token = { "url": endpoint["url"], "token": r.headers["X-Subject-Token"] }

    def progressBar(self, monitor):

        percent = round(100 * float(monitor.bytes_read) / float(self.contentLenght));

        progressBarStart = "["
        progressBarEnd = "]"
        progressBarContents = ""

        count = 0

        for count in range(0, int(round(percent / 5))):
            progressBarContents += "="
            count += 1

        for count in range(0, (20 - count)):
            progressBarContents += " "

        progressBarFinal = progressBarStart + progressBarContents + progressBarEnd + " " + str(percent) + "%"

        sys.stdout.write("\r" + progressBarFinal)

    def uploadFile(self):

        name, ext = os.path.splitext(self.file)

        if ext == "":
            ext = ".txt"

        contentType = mimetypes.types_map[ext]
        self.contentLenght = os.path.getsize(self.file)

        files = {'upload_file': open(self.file,'rb')}

        from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor

        e = MultipartEncoder(fields={'upload_file': ('upload_file', open(self.file, 'rb'), contentType)})
        m = MultipartEncoderMonitor(e, self.progressBar)

        r = requests.put(self.token["url"] + "/" + self.container + "/" + self.path, headers={
            "Content-Type": contentType,
            "Content-Length": self.contentLenght,
            "X-Auth-Token": self.token["token"]
        }, data=m)

        '''r = requests.put(self.token["url"] + "/" + self.container + "/" + self.path, headers={
            "Content-Type": contentType,
            "Content-Length": contentLenght,
            "X-Auth-Token": self.token["token"]
        }, files=files)'''

        print r.text

    def upload(self):
        self.getToken()
        self.uploadFile()

swiftpush = SwiftPush()
swiftpush.upload()
