# -*- coding: utf-8 -*-
"""Orangetool RAM functions."""
import psutil
def convert_bytes(num):
    """
    convert num to idiomatic byte unit
    :param num: the input number.
    :type num:int
    :return: str
    >>> convert_bytes(200)
    '200.0 bytes'
    >>> convert_bytes(6000)
    '5.9 KB'
    >>> convert_bytes(80000)
    '78.1 KB'
    """
    for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
        if num < 1024.0:
            return "%3.1f %s" % (num, x)
        num /= 1024.0
def ram_total(convert=True):
    '''
    Return total ram of board
    :param convert: Flag for convert mode (using of convert_byte function)
    :type convert:bool
    :return: total ram of board as string
    '''
    response=list(psutil.virtual_memory())
    if convert==True:
        return convert_bytes(int(response[0]))
    else:
        return str(response[0])
def ram_used(convert=True):
    '''
    Return how much ram is using
    :param convert: Flag for convert mode (using of convert_byte function)
    :type convert:bool
    :return: how much ram is using as string
    '''
    response=list(psutil.virtual_memory())
    if convert == True:
        return convert_bytes(int(response[3]))
    else:
        return str(response[3])
def ram_free(convert=True):
    '''
    Return how much ram is available
    :param convert: Flag for convert mode (using of convert_byte function)
    :type convert : bool
    :return: how much ram is available
    '''
    response=list(psutil.virtual_memory())
    if convert == True:
        return convert_bytes(int(response[1]))
    else:
        return str(response[1])
def ram_percent():
    '''
    Return available ram percentage
    :return: availabe ram percentage as string with %
    '''
    response=list(psutil.virtual_memory())
    return str(response[2])+" %"
def freeup(DEBUG=False):
    '''
    To free pagecache, dentries and inodes:
    :param DEBUG: Flag for using Debug mode
    :type DEBUG:bool
    :return: Amount of freeuped ram as string and converted by convert_bytes()
    '''
    try:
        RAM_before=int(ram_free(convert=False))
        caches_control=open("/proc/sys/vm/drop_caches","w")
        caches_control.write("3")
        caches_control.close()
        RAM_after=int(ram_free(convert=False))
        freeuped_ram=RAM_after - RAM_before
        if freeuped_ram>0:
            return convert_bytes(freeuped_ram)
        else:
            return convert_bytes(0)
    except Exception as e:
        if DEBUG==True:
            print(str(e))
        return "Error"


















@@ -99,17 +99,17 @@
 %22%22%22%0A    
-c
+C
 onvert n
@@ -133,16 +133,18 @@
 yte unit
+.%0A
 %0A    :pa
@@ -503,35 +503,35 @@
 vert=True):%0A    
-'''
+%22%22%22
 %0A    Return tota
@@ -544,16 +544,18 @@
 of board
+.%0A
 %0A    :pa
@@ -559,33 +559,33 @@
 :param convert: 
-F
+f
 lag for convert 
@@ -683,35 +683,35 @@
 d as string%0A    
-'''
+%22%22%22
 %0A    response=li
@@ -873,35 +873,35 @@
 vert=True):%0A    
-'''
+%22%22%22
 %0A    Return how 
@@ -917,16 +917,18 @@
 is using
+.%0A
 %0A    :pa
@@ -932,33 +932,33 @@
 :param convert: 
-F
+f
 lag for convert 
@@ -1063,27 +1063,27 @@
  string%0A    
-'''
+%22%22%22
 %0A    respons
@@ -1255,27 +1255,27 @@
 =True):%0A    
-'''
+%22%22%22
 %0A    Return 
@@ -1295,24 +1295,26 @@
 is available
+.%0A
 %0A    :param 
@@ -1322,17 +1322,17 @@
 onvert: 
-F
+f
 lag for 
@@ -1441,27 +1441,27 @@
 ailable%0A    
-'''
+%22%22%22
 %0A    respons
@@ -1624,27 +1624,27 @@
 cent():%0A    
-'''
+%22%22%22
 %0A    Return 
@@ -1667,16 +1667,18 @@
 rcentage
+.%0A
 %0A    :re
@@ -1724,27 +1724,27 @@
  with %25%0A    
-'''
+%22%22%22
 %0A    respons
@@ -1833,27 +1833,27 @@
 False):%0A    
-'''
+%22%22%22
 %0A    To free
@@ -1883,17 +1883,18 @@
 d inodes
-:
+.%0A
 %0A    :pa
@@ -1904,17 +1904,17 @@
  DEBUG: 
-F
+f
 lag for 
@@ -2038,11 +2038,11 @@
     
-'''
+%22%22%22
 %0A   

fix : orangetool_ram docstrings modified