#!bin/python

import time
from hashlib import sha256

def format_size(size):
    scales = ["bytes", "kB", "MB", "GB", "TB", "PB"]
    count = 0
    while (1 == 1):
        if (size > 1024.0):
            size /= 1024.0
            count += 1
        else:
            break
    return str(round(size, 1)) + " " + scales[count]

def create_new_paste(content, config):
    try:
        paste_id = str(sha256(content.encode('utf-8')).hexdigest())
    except:
        return "ERROR", "An issue occured while handling the paste. "+\
        "Please try again later. If the problem persists, try "+\
        "notifying a system administrator."

    if (len(content.encode('utf-8')) > config['MAX_PASTE_SIZE']):
        return "ERROR", "The paste sent is too large. This TorPaste "+\
        "instance has a maximum allowed paste size of "+\
        format_size(config['MAX_PASTE_SIZE']) + "."

    try:
        config['b'].new_paste(paste_id, content)
    except config['b'].e.ErrorException as errmsg:
        return "ERROR", errmsg
    
    try:
        config['b'].update_paste_metadata(
            paste_id,
            {
                "date": str(int(time.time()))
            }
        )
    except config['b'].e.ErrorException as errmsg:
        return "ERROR", errmsg

    return "OK", paste_id

def view_existing_paste(paste_id, config):
    if (not paste_id.isalnum()):
        return "ERROR", "Invalid Paste ID. Please check the link "+\
        "you used or use the Pastes button above.", 400

    if (len(paste_id) != 64):
        return "ERROR", "Paste ID has invalid length. Paste IDs "+\
        "are 64 characters long. Please make sure the link you "+\
        "clicked is correct or use the Pastes button above.", 400

    if (not config['b'].does_paste_exist(paste_id)):
        return "ERROR", "A paste with this Paste ID could not be "+\
        "found. Sorry.", 404

    try:
        paste_content = config['b'].get_paste_contents(paste_id)
    except config['b'].e.ErrorException as errmsg:
        return "ERROR", errmsg, 500

    try:
        paste_date = config['b'].get_paste_metadata_value(paste_id, "date")
    except config['b'].e.ErrorException as errmsg:
        return "ERROR", errmsg, 500
    except config['b'].e.WarningException as errmsg:
        return "WARNING", errmsg, 500

    return "OK", (paste_content, paste_date), 200

def get_paste_listing(config):
    if (not config['PASTE_LIST_ACTIVE']):
        return "ERROR", "Paste listing has been disabled by the administrator.",
        503

    try:
        paste_list = config['b'].get_all_paste_ids()
    except config['b'].e.ErrorException as errmsg:
        return "ERROR", errmsg, 500

    if (paste_list[0] == "none"):
        return "OK", "none", 200

    return "OK", paste_list, 200

@@ -47,16 +47,17 @@
 sha256%0A%0A
+%0A
 def form
@@ -322,16 +322,17 @@
 count%5D%0A%0A
+%0A
 def crea
@@ -520,19 +520,24 @@
 paste. %22
+ 
 +%5C%0A
+    
         
@@ -591,19 +591,24 @@
 s, try %22
+ 
 +%5C%0A
+    
         
@@ -779,19 +779,24 @@
 rPaste %22
+ 
 +%5C%0A
+    
         
@@ -842,19 +842,24 @@
 ize of %22
+ 
 +%5C%0A
+    
         
@@ -1043,20 +1043,16 @@
  errmsg%0A
-    
 %0A    try
@@ -1312,16 +1312,17 @@
 ste_id%0A%0A
+%0A
 def view
@@ -1455,19 +1455,24 @@
 e link %22
+ 
 +%5C%0A
+    
         
@@ -1615,19 +1615,23 @@
 te IDs %22
+ 
 +%5C%0A
+   
         
@@ -1686,19 +1686,23 @@
 nk you %22
+ 
 +%5C%0A
+   
         
@@ -1879,19 +1879,24 @@
 not be %22
+ 
 +%5C%0A
+    
         
@@ -2394,16 +2394,17 @@
 ), 200%0A%0A
+%0A
 def get_
@@ -2532,16 +2532,34 @@
  by the 
+%22 +%5C%0A            %22
 administ
@@ -2566,24 +2566,16 @@
 rator.%22,
-%0A       
  503%0A%0A  

Completely convert `logic.py` to PEP8