Skip to content

Commit 56cd51d

Browse files
Merge pull request #42 from Moduland/minor_edits
Minor edits
2 parents 163718f + b7c5985 commit 56cd51d

12 files changed

+180
-147
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1414
- `dev-requirements.txt` modified
1515
- Test system modified
1616
- `DEBUG` parameter renamed to `debug`
17+
- All parameters moved to `orangetool_params.py`
18+
- Some functions moved to `orangetool_utils.py`
19+
- `ADDRESS` parameter renamed to `address`
20+
- `Zone` parameter renamed to `zone`
21+
- `uptime` and `idletime` functions updated
22+
- Docstrings updated
1723
## [0.35] - 2019-06-01
1824
### Added
1925
- `version_check.py`

orangetool/__init__.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# -*- coding: utf-8 -*-
22
"""Orangetool modules."""
33

4-
from .orangetool_display import *
5-
from .orangetool_ip import *
6-
from .orangetool_system import *
7-
from .orangetool_ram import *
8-
from .orangetool_storage import *
4+
from .orangetool_display import hdmi_on, hdmi_off, hdmi_size
5+
from .orangetool_ip import internet, local_ip, global_ip, set_ip, ping, mac
6+
from .orangetool_system import check_update, get_temp, uptime, idletime, wakeup, version, sleep, hibernate, halt, restart
7+
from .orangetool_ram import ram_total, ram_used, ram_free, ram_percent, freeup
8+
from .orangetool_storage import mount_status, storage_status, unmount, unmount_all, mount, usb_on, usb_off
9+
from .orangetool_params import ORANGETOOL_VERSION
910

1011
__version__ = ORANGETOOL_VERSION

orangetool/orangetool_display.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# -*- coding: utf-8 -*-
22
"""Orangetool display functions."""
3+
from .orangetool_params import GENERAL_ERROR_MESSAGE
34

45

56
def hdmi_controller(command, debug=False):
67
"""
78
Control hdmi port.
89
9-
:param command: inpurt command
10+
:param command: input command
1011
:type command: bool
1112
:param debug: flag for using debug mode
1213
:type debug: bool
@@ -23,7 +24,7 @@ def hdmi_controller(command, debug=False):
2324
except Exception as e:
2425
if debug:
2526
print(str(e))
26-
return "Error"
27+
return GENERAL_ERROR_MESSAGE
2728

2829

2930
def hdmi_on(debug=False):
@@ -53,10 +54,10 @@ def hdmi_size(v=None, h=None, debug=False):
5354
Change hdmi display resolution (need sudo -s) (if call without any argument return current resolution).
5455
5556
:param v: vertical line
56-
:param h: horizental line
57-
:param debug: flag for using debug mode
5857
:type v : int
59-
:type h:int
58+
:param h: horizontal line
59+
:type h: int
60+
:param debug: flag for using debug mode
6061
:type debug:bool
6162
:return: bool
6263
"""
@@ -73,4 +74,4 @@ def hdmi_size(v=None, h=None, debug=False):
7374
except Exception as e:
7475
if debug:
7576
print(str(e))
76-
return "Error"
77+
return GENERAL_ERROR_MESSAGE

orangetool/orangetool_ip.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
# -*- coding: utf-8 -*-
22
"""Orangetool IP functions."""
33
from .orangetool_system import restart as restart_func
4+
from .orangetool_params import IP_PATTERN, GLOBAL_IP_API_1, GENERAL_ERROR_MESSAGE
45
import subprocess as sub
56
import socket
67
import os
78
import requests
89
import re
910
import platform
10-
IP_PATTERN = r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}"
11-
GLOBAL_IP_API_1 = "http://ipinfo.io/ip"
1211

1312

1413
def internet(host="8.8.8.8", port=53, timeout=3):
1514
"""
1615
Check internet connections.
1716
1817
:param host: the host that check connection to
19-
:param port: port that check connection with
20-
:param timeout: times that check the connection
2118
:type host:str
19+
:param port: port that check connection with
2220
:type port:int
21+
:param timeout: times that check the connection
2322
:type timeout:int
2423
:return bool: True if Connection is Stable
2524
>>> internet() # if there is stable internet connection
@@ -58,13 +57,13 @@ def local_ip(debug=False):
5857
response = list(command.communicate())
5958
if len(response[0]) > 0:
6059
return str(response[0])[2:-4]
61-
return "Error"
62-
return "Error"
60+
return GENERAL_ERROR_MESSAGE
61+
return GENERAL_ERROR_MESSAGE
6362

6463
except Exception as e:
6564
if debug:
6665
print(str(e))
67-
return "Error"
66+
return GENERAL_ERROR_MESSAGE
6867

6968

7069
def global_ip(debug=False):
@@ -84,19 +83,19 @@ def global_ip(debug=False):
8483
except Exception as e:
8584
if debug:
8685
print(str(e))
87-
return "Error"
86+
return GENERAL_ERROR_MESSAGE
8887

8988

9089
def set_ip(ip, restart=False, device="eth0", debug=False):
9190
"""
9291
Set static ip in interfaces file (need sudo).
9392
93+
:param ip: static ip
94+
:type ip :str
9495
:param restart : restart flag
9596
:type restart : bool
9697
:param device: network device name
9798
:type device:str
98-
:param ip: static ip
99-
:type ip :str
10099
:param debug: flag for using debug mode
101100
:type debug:bool
102101
:return: True in successful
@@ -127,18 +126,18 @@ def set_ip(ip, restart=False, device="eth0", debug=False):
127126
except Exception as e:
128127
if debug:
129128
print(str(e))
130-
return "Error"
129+
return GENERAL_ERROR_MESSAGE
131130

132131

133132
def ping(ip, packet_number=3, debug=False):
134133
"""
135134
Ping ip and return True if this ip is available and False otherwise.
136135
137136
:param ip: target ip
138-
:param packet_number: number of packet to size
139-
:param debug: flag for using debug mode
140137
:type ip :str
138+
:param packet_number: number of packet to size
141139
:type packet_number:int
140+
:param debug: flag for using debug mode
142141
:type debug:bool
143142
:return: a boolean value (True if ip is available and False otherwise)
144143
"""
@@ -157,7 +156,7 @@ def ping(ip, packet_number=3, debug=False):
157156
except Exception as e:
158157
if debug:
159158
print(str(e))
160-
return "Error"
159+
return GENERAL_ERROR_MESSAGE
161160

162161

163162
def mac(debug=False):
@@ -180,4 +179,4 @@ def mac(debug=False):
180179
except Exception as e:
181180
if debug:
182181
print(str(e))
183-
return "Error"
182+
return GENERAL_ERROR_MESSAGE

orangetool/orangetool_params.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# -*- coding: utf-8 -*-
2+
"""Orangetool params."""
3+
4+
ORANGETOOL_VERSION = "0.35"
5+
UPDATE_URL = "http://www.orangetool.ir/version"
6+
IP_PATTERN = r"(?:[0-9]{1,3}\.){3}[0-9]{1,3}"
7+
GLOBAL_IP_API_1 = "http://ipinfo.io/ip"
8+
GENERAL_ERROR_MESSAGE = "Error"
9+
ROOT_ERROR_MESSAGE = "Root Error"

orangetool/orangetool_ram.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,8 @@
11
# -*- coding: utf-8 -*-
22
"""Orangetool RAM functions."""
33
import psutil
4-
5-
6-
def convert_bytes(num):
7-
"""
8-
Convert num to idiomatic byte unit.
9-
10-
:param num: the input number.
11-
:type num:int
12-
:return: str
13-
>>> convert_bytes(200)
14-
'200.0 bytes'
15-
>>> convert_bytes(6000)
16-
'5.9 KB'
17-
>>> convert_bytes(80000)
18-
'78.1 KB'
19-
"""
20-
for x in ['bytes', 'KB', 'MB', 'GB', 'TB']:
21-
if num < 1024.0:
22-
return "%3.1f %s" % (num, x)
23-
num /= 1024.0
24-
4+
from .orangetool_params import GENERAL_ERROR_MESSAGE
5+
from .orangetool_utils import convert_bytes
256

267
def ram_total(convert=True):
278
"""
@@ -69,7 +50,7 @@ def ram_percent():
6950
"""
7051
Return available ram percentage.
7152
72-
:return: availabe ram percentage as string with %
53+
:return: available ram percentage as string with %
7354
"""
7455
response = list(psutil.virtual_memory())
7556
return str(response[2]) + " %"
@@ -81,7 +62,7 @@ def freeup(debug=False):
8162
8263
:param debug: flag for using debug mode
8364
:type debug:bool
84-
:return: Amount of freeuped ram as string and converted by convert_bytes()
65+
:return: amount of freeuped ram as string and converted by convert_bytes()
8566
"""
8667
try:
8768
RAM_before = int(ram_free(convert=False))
@@ -96,4 +77,4 @@ def freeup(debug=False):
9677
except Exception as e:
9778
if debug:
9879
print(str(e))
99-
return "Error"
80+
return GENERAL_ERROR_MESSAGE

orangetool/orangetool_storage.py

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
import subprocess as sub
44
import os
55
import string
6-
import random
6+
from .orangetool_params import GENERAL_ERROR_MESSAGE, ROOT_ERROR_MESSAGE
7+
from .orangetool_utils import random_generator
78

89

910
def mount_status(device_name, debug=False):
1011
"""
1112
Return addresses of mounted memory devices in dev by device name.
1213
14+
:param device_name: name of device
15+
:type device_name: str
1316
:param debug: flag for using debug mode
1417
:type debug:bool
1518
:return: list of memory devices
@@ -29,7 +32,7 @@ def mount_status(device_name, debug=False):
2932
except Exception as e:
3033
if debug:
3134
print(str(e))
32-
return "Error"
35+
return GENERAL_ERROR_MESSAGE
3336

3437

3538
def storage_status(debug=False):
@@ -53,21 +56,21 @@ def storage_status(debug=False):
5356
except Exception as e:
5457
if debug:
5558
print(str(e))
56-
return "Error"
59+
return GENERAL_ERROR_MESSAGE
5760

5861

59-
def unmount(ADDRESS, debug=False):
62+
def unmount(address, debug=False):
6063
"""
6164
Unmount memory devices by addresses.
6265
63-
:param ADDRESS: address of that device mount on
64-
:type ADDRESS:str
66+
:param address: address of that device mount on
67+
:type address:str
6568
:param debug: flag for using debug mode
6669
:type debug:bool
6770
:return: True if device unmount correctly and False other wise
6871
"""
6972
try:
70-
command = sub.Popen(["umount", ADDRESS],
73+
command = sub.Popen(["umount", address],
7174
stdout=sub.PIPE, stderr=sub.PIPE)
7275
output = list(command.communicate())
7376
if len(output[0]) == 0 and len(output[1]) == 0:
@@ -76,7 +79,7 @@ def unmount(ADDRESS, debug=False):
7679
except Exception as e:
7780
if debug:
7881
print(str(e))
79-
return "Error"
82+
return GENERAL_ERROR_MESSAGE
8083

8184

8285
def unmount_all(debug=False):
@@ -100,34 +103,17 @@ def unmount_all(debug=False):
100103
except Exception as e:
101104
if debug:
102105
print(str(e))
103-
return "Error"
104-
105-
106-
def random_generator(number):
107-
"""
108-
Generate random number.
109-
110-
:param number: random number digits
111-
:type number: int
112-
:return: random number as str
113-
"""
114-
response = ""
115-
i = 0
116-
while(i < number):
117-
i += 1
118-
response += str(random.randint(0, 9))
119-
return response
120-
106+
return GENERAL_ERROR_MESSAGE
121107

122108
def mount(device_name, mount_address=None, debug=False):
123109
"""
124110
Mount memory devices by addresses.
125111
126112
:param device_name: name of device for mounted example = sda1
127-
: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
128-
:param debug: flag for using debug mode
129113
:type device_name:str
114+
: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
130115
:type mount_address:str
116+
:param debug: flag for using debug mode
131117
:type debug:bool
132118
:return: True if device mount correctly and False other wise
133119
"""
@@ -150,7 +136,7 @@ def mount(device_name, mount_address=None, debug=False):
150136
except Exception as e:
151137
if debug:
152138
print(str(e))
153-
return "Error"
139+
return GENERAL_ERROR_MESSAGE
154140

155141

156142
def usb_control(code, debug=False):
@@ -165,17 +151,17 @@ def usb_control(code, debug=False):
165151
"""
166152
try:
167153
command = sub.Popen(
168-
["chmod", "-R", code,"/media/"],
154+
["chmod", "-R", code, "/media/"],
169155
stderr=sub.PIPE,
170156
stdout=sub.PIPE,
171157
stdin=sub.PIPE)
172158
response = list(command.communicate())
173159
if len(response[1]) > 0:
174-
raise Exception('Root Error')
160+
raise Exception(ROOT_ERROR_MESSAGE)
175161
except Exception as e:
176162
if debug:
177163
print(str(e))
178-
return "Error"
164+
return GENERAL_ERROR_MESSAGE
179165

180166

181167
def usb_on(debug=False):

0 commit comments

Comments
 (0)