diff --git a/CHANGELOG.md b/CHANGELOG.md index ec23200..7250c5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Added - `FUNDING.yml` +- `__version__` variable - `hibernate` function ### Changed - `dev-requirements.txt` modified - Test system modified +- `DEBUG` parameter renamed to `debug` ## [0.35] - 2019-06-01 ### Added - `version_check.py` diff --git a/orangetool/__init__.py b/orangetool/__init__.py index 30253fa..b6416d5 100644 --- a/orangetool/__init__.py +++ b/orangetool/__init__.py @@ -1,31 +1,10 @@ # -*- coding: utf-8 -*- """Orangetool modules.""" -""" -MIT License - -Copyright (c) 2017 Moduland Co - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. -""" from .orangetool_display import * from .orangetool_ip import * from .orangetool_system import * from .orangetool_ram import * from .orangetool_storage import * + +__version__ = ORANGETOOL_VERSION diff --git a/orangetool/orangetool_display.py b/orangetool/orangetool_display.py index 5042cd9..21703d2 100644 --- a/orangetool/orangetool_display.py +++ b/orangetool/orangetool_display.py @@ -2,14 +2,14 @@ """Orangetool display functions.""" -def hdmi_controller(command, DEBUG=False): +def hdmi_controller(command, debug=False): """ Control hdmi port. :param command: inpurt command :type command: bool - :param DEBUG: flag for using Debug mode - :type DEBUG: bool + :param debug: flag for using debug mode + :type debug: bool :return: bool """ try: @@ -21,43 +21,43 @@ def hdmi_controller(command, DEBUG=False): hdmi_control.close() return True except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def hdmi_on(DEBUG=False): +def hdmi_on(debug=False): """ Turn on hdmi port (need sudo -s). - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: bool """ - hdmi_controller(True, DEBUG) + hdmi_controller(True, debug) -def hdmi_off(DEBUG=False): +def hdmi_off(debug=False): """ Turn off hdmi port (need sudo -s). - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: bool """ - hdmi_controller(False, DEBUG) + hdmi_controller(False, debug) -def hdmi_size(v=None, h=None, DEBUG=False): +def hdmi_size(v=None, h=None, debug=False): """ Change hdmi display resolution (need sudo -s) (if call without any argument return current resolution). :param v: vertical line :param h: horizental line - :param DEBUG: flag for using Debug mode + :param debug: flag for using debug mode :type v : int :type h:int - :type DEBUG:bool + :type debug:bool :return: bool """ try: @@ -71,6 +71,6 @@ def hdmi_size(v=None, h=None, DEBUG=False): hdmi_control.close() return True except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" diff --git a/orangetool/orangetool_ip.py b/orangetool/orangetool_ip.py index e872ba1..1a70160 100644 --- a/orangetool/orangetool_ip.py +++ b/orangetool/orangetool_ip.py @@ -7,8 +7,8 @@ import requests import re import platform -ip_pattern = r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}" -api_1 = "http://ipinfo.io/ip" +IP_PATTERN = r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}" +GLOBAL_IP_API_1 = "http://ipinfo.io/ip" def internet(host="8.8.8.8", port=53, timeout=3): @@ -36,12 +36,12 @@ def internet(host="8.8.8.8", port=53, timeout=3): return False -def local_ip(DEBUG=False): +def local_ip(debug=False): """ Return local ip of computer in windows by socket module and in unix with hostname command in shell. - :param DEBUG:flag for using Debug Mode - :type DEBUG:bool + :param debug:flag for using debug Mode + :type debug:bool :return: local ip as string """ try: @@ -62,32 +62,32 @@ def local_ip(DEBUG=False): return "Error" except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def global_ip(DEBUG=False): +def global_ip(debug=False): """ Return ip with by http://ipinfo.io/ip api. - :param DEBUG:flag for using Debug mode - :type DEBUG:bool + :param debug:flag for using debug mode + :type debug:bool :return: global ip as string """ try: new_session = requests.session() - response = new_session.get(api_1) - ip_list = re.findall(ip_pattern, response.text) + response = new_session.get(GLOBAL_IP_API_1) + ip_list = re.findall(IP_PATTERN, response.text) new_session.close() return ip_list[0] except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def set_ip(ip, restart=False, device="eth0", DEBUG=False): +def set_ip(ip, restart=False, device="eth0", debug=False): """ Set static ip in interfaces file (need sudo). @@ -97,8 +97,8 @@ def set_ip(ip, restart=False, device="eth0", DEBUG=False): :type device:str :param ip: static ip :type ip :str - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: True in successful """ static_string = ''' @@ -111,7 +111,7 @@ def set_ip(ip, restart=False, device="eth0", DEBUG=False): dns-nameservers 8.8.8.8 8.8.4.4 ''' try: - if bool(re.match(ip_pattern, ip)) == False or ip.find( + if bool(re.match(IP_PATTERN, ip)) == False or ip.find( "192.168.") == -1 or device not in mac().keys(): raise Exception("IP Formation Error") static_string = static_string.replace("ip", ip) @@ -125,25 +125,25 @@ def set_ip(ip, restart=False, device="eth0", DEBUG=False): restart_func() return True except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def ping(ip, packet_number=3, DEBUG=False): +def ping(ip, packet_number=3, debug=False): """ Ping ip and return True if this ip is available and False otherwise. :param ip: target ip :param packet_number: number of packet to size - :param DEBUG: flag for using Debug mode + :param debug: flag for using debug mode :type ip :str :type packet_number:int - :type DEBUG:bool + :type debug:bool :return: a boolean value (True if ip is available and False otherwise) """ try: - if re.match(ip_pattern, ip) == False: + if re.match(IP_PATTERN, ip) == False: raise Exception("IP Formation Error") output = str(list(sub.Popen(["ping", ip, @@ -155,17 +155,17 @@ def ping(ip, packet_number=3, DEBUG=False): return True return False except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def mac(DEBUG=False): +def mac(debug=False): """ Return mac addresses of net devices. - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: return mac addresses as dict with name as keys and mac addresses as values """ try: @@ -178,6 +178,6 @@ def mac(DEBUG=False): mac_addr.close() return dict(zip(dir_list, mac_list)) except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" diff --git a/orangetool/orangetool_ram.py b/orangetool/orangetool_ram.py index d08ea3e..05f3598 100644 --- a/orangetool/orangetool_ram.py +++ b/orangetool/orangetool_ram.py @@ -75,12 +75,12 @@ def ram_percent(): return str(response[2]) + " %" -def freeup(DEBUG=False): +def freeup(debug=False): """ To free pagecache, dentries and inodes. - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: Amount of freeuped ram as string and converted by convert_bytes() """ try: @@ -94,6 +94,6 @@ def freeup(DEBUG=False): return convert_bytes(freeuped_ram) return convert_bytes(0) except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" diff --git a/orangetool/orangetool_storage.py b/orangetool/orangetool_storage.py index c8cd746..dc7b0e0 100644 --- a/orangetool/orangetool_storage.py +++ b/orangetool/orangetool_storage.py @@ -6,12 +6,12 @@ import random -def mount_status(device_name, DEBUG=False): +def mount_status(device_name, debug=False): """ Return addresses of mounted memory devices in dev by device name. - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: list of memory devices """ try: @@ -27,17 +27,17 @@ def mount_status(device_name, DEBUG=False): else: return memory_list except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def storage_status(DEBUG=False): +def storage_status(debug=False): """ Return all of the inserted memory and their status. - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: all of the inserted memory and their status as dictionary ( device name as keys and mount status (mounted_addresses as list and u --> unmounted) as values """ try: @@ -51,19 +51,19 @@ def storage_status(DEBUG=False): memory_status.append(mount_status(item)) return dict(zip(memory_items, memory_status)) except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def unmount(ADDRESS, DEBUG=False): +def unmount(ADDRESS, debug=False): """ Unmount memory devices by addresses. :param ADDRESS: address of that device mount on :type ADDRESS:str - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: True if device unmount correctly and False other wise """ try: @@ -74,17 +74,17 @@ def unmount(ADDRESS, DEBUG=False): return True return False except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def unmount_all(DEBUG=False): +def unmount_all(debug=False): """ Unmount all of the mounted devices. - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: return True if all of the mounted devices unmount correctly """ try: @@ -98,7 +98,7 @@ def unmount_all(DEBUG=False): unmount(j) return True except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" @@ -119,16 +119,16 @@ def random_generator(number): return response -def mount(device_name, mount_address=None, DEBUG=False): +def mount(device_name, mount_address=None, debug=False): """ Mount memory devices by addresses. :param device_name: name of device for mounted example = sda1 :param mount_address: address for mounting device example = /mnt/usb , default value is None in this case function generate random number for mount folder name - :param DEBUG: flag for using Debug mode + :param debug: flag for using debug mode :type device_name:str :type mount_address:str - :type DEBUG:bool + :type debug:bool :return: True if device mount correctly and False other wise """ try: @@ -148,6 +148,6 @@ def mount(device_name, mount_address=None, DEBUG=False): return True return False except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" diff --git a/orangetool/orangetool_system.py b/orangetool/orangetool_system.py index 2d4b02b..0fb9eed 100644 --- a/orangetool/orangetool_system.py +++ b/orangetool/orangetool_system.py @@ -4,40 +4,39 @@ import time import requests from art import tprint -ip_pattern = r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}" -api_1 = "http://ipinfo.io/ip" -VERSION = "0.35" + +ORANGETOOL_VERSION = "0.35" UPDATE_URL = "http://www.orangetool.ir/version" -def check_update(DEBUG=False): +def check_update(debug=False): """ Check orangetool site for new version. - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: True if new version is available """ try: new_version = requests.get(UPDATE_URL).text - if float(new_version) > float(VERSION): + if float(new_version) > float(ORANGETOOL_VERSION): print("New Version (" + new_version + ") Of Orangetool Is Available") return True print("Update!") return False except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def get_temp(Zone=0, DEBUG=False): +def get_temp(Zone=0, debug=False): """ Read cpu temperature. - :param DEBUG : flag for using Debug mode + :param debug : flag for using debug mode :param Zone : thermal Zone Index - :type DEBUG:bool + :type debug:bool :type Zone:int :return: board temp as string in celsius """ @@ -52,7 +51,7 @@ def get_temp(Zone=0, DEBUG=False): # else: # return "Error" except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" @@ -89,12 +88,12 @@ def time_convert(input_string): zero_insert(str(input_minute)) + " minutes, " + zero_insert(str(input_sec)) + " seconds" -def uptime(DEBUG=False): +def uptime(debug=False): """ Return system uptime. - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: system uptime as string """ try: @@ -102,17 +101,17 @@ def uptime(DEBUG=False): response = command.read() return time_convert(response[:-1].split(" ")[0]) except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def idletime(DEBUG=False): +def idletime(debug=False): """ Return system idletime. - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: system idle as string """ try: @@ -120,7 +119,7 @@ def idletime(DEBUG=False): response = command.read() return time_convert(response[:-1].split(" ")[1]) except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" @@ -132,21 +131,21 @@ def version(): :return: return orangetool-version number as string """ tprint("orangetool", font="bulbhead") - tprint("v"+VERSION,font="bulbhead") + tprint("v"+ORANGETOOL_VERSION,font="bulbhead") -def wakeup(day=0, hour=0, minute=0, DEBUG=False): +def wakeup(day=0, hour=0, minute=0, debug=False): """ Set wakeup time for kernel RTC (need sudo). :param day: days for wakeup :param hour: hout for wakeup :param minute: minute for wakeup - :param DEBUG: flag for using Debug mode + :param debug: flag for using debug mode :type day:int :type hour:int :type minute:int - :type DEBUG:bool + :type debug:bool :return: bool """ try: @@ -160,19 +159,19 @@ def wakeup(day=0, hour=0, minute=0, DEBUG=False): file.close() return True except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def power_control(command, DEBUG=False): +def power_control(command, debug=False): """ Control different power options. :param command: input command :type command: str - :param DEBUG: flag for using Debug mode - :type DEBUG: bool + :param debug: flag for using debug mode + :type debug: bool :return: None """ try: @@ -185,49 +184,49 @@ def power_control(command, DEBUG=False): if len(response[1]) > 0: raise Exception('Root Error') except Exception as e: - if DEBUG: + if debug: print(str(e)) return "Error" -def sleep(DEBUG=False): +def sleep(debug=False): """ Shortcut for sleep command (need sudo). - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: None """ - power_control("pm-suspend", DEBUG) + power_control("pm-suspend", debug) -def hibernate(DEBUG=False): +def hibernate(debug=False): """ Shortcut for hibernate command (need sudo). - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: None """ - power_control("pm-hibernate", DEBUG) + power_control("pm-hibernate", debug) -def halt(DEBUG=False): +def halt(debug=False): """ Shortcut for poweroff (need sudo). - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: None """ - power_control("poweroff", DEBUG) + power_control("poweroff", debug) -def restart(DEBUG=False): +def restart(debug=False): """ Shortcut for reboot (need sudo). - :param DEBUG: flag for using Debug mode - :type DEBUG:bool + :param debug: flag for using debug mode + :type debug:bool :return: None """ - power_control("reboot", DEBUG) + power_control("reboot", debug) diff --git a/setup.py b/setup.py index 53371e4..f73dab4 100644 --- a/setup.py +++ b/setup.py @@ -48,6 +48,8 @@ def read_description(): 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8', + 'Programming Language :: Python :: 3.9', 'Topic :: Scientific/Engineering', ], install_requires=get_requires(),