diff --git a/configure.py b/configure.py deleted file mode 100644 index 28c8be8..0000000 --- a/configure.py +++ /dev/null @@ -1,77 +0,0 @@ -import configobj -import os - -config = "" - -def write_config(): - # Creating a config file - try: - os.chdir(os.path.expanduser(os.path.join('~', ''))) - os.makedirs('.config' + os.sep + "ptcl") - os.chdir('.config' + os.sep + "ptcl") - - except OSError: - # If already exists - pass - - config = configobj.ConfigObj() - DEFAULT = {'mask': '192.168.1.1', 'username': 'admin', 'password': 'admin'} - mask = raw_input("Leave empty for default configuration.\nPlease enter router gateway\t(Default 192.168.1.1)\t: ") - - if mask: - DEFAULT['mask'] = mask - username = raw_input("Please enter router username\t(Default admin)\t: ") - - if username: - DEFAULT['username'] = username - password = raw_input("Please enter router password\t(Default admin)\t: ") - - if password: - DEFAULT['password'] = password - config['Router-Auth'] = DEFAULT - config['Aliases'] = {} - - with open('config.ini', 'w') as configfile: - config.write(configfile) - print('\nConfiguration file Generated.') - - -def set_alias(): - # Defining custom aliases - config = configobj.ConfigObj('config.ini') - - while True: - hostname = raw_input("Set Alias for hosname: ") - if hostname == 'q': - break - macaddress= raw_input("Enter it\'s macaddress: ") - - if macaddress == 'q': - break - - else: - if hostname not in config["Aliases"]: - config["Aliases"][hostname] = macaddress - with open('config.ini', 'r+') as configfile: - config.write(configfile) - else: - print("Already Present.") - - -def get_alias(): - # Return Aliases - config = configobj.ConfigObj('config.ini') - return config["Aliases"] - - -def config_check(): - path = os.path.expanduser(os.path.join('~', '.config' + os.sep + 'ptcl')) - - if os.path.exists(path): - os.chdir(path) - global config - config = configobj.ConfigObj('config.ini') - return True - else: - write_config() - return False diff --git a/ptcl.py b/ptcl.py deleted file mode 100755 index 9760f33..0000000 --- a/ptcl.py +++ /dev/null @@ -1,143 +0,0 @@ -from router import Router -import argparse -import sys -import configobj -import configure -import os -from tabulate import tabulate - -ptcl = Router(password='123motorcross') -ptcl.time_limit(username="rafay1s", mac="64:5a:04:9d:38:ed", days="Mon-Mon") -# ptcl.test_me() -# def show_dhcpinfo(): -# ''' -# Shows DHCP information. -# ''' -# # print(ptcl.dhcpinfo()) -# print(tabulate([ptcl.dhcpinfo()], headers=['HOSTNAME'], tablefmt='fancy_grid')) -# -# show_dhcpinfo() - -# def show_active_dev(): -# ''' -# Shows active devices (Mac Addresses) and their hostnames. -# ''' -# ptcl.stationinfo() -# ptcl.dhcpinfo() -# ptcl.host_and_mac = tuple(zip(ptcl.dev_hostname, ptcl.mac_address)) -# hostnames = [] -# display_list = [] -# aliases = configure.get_alias() -# count = 1 -# print("\nShowing Currently Active Devices.\n") -# for hostname, mac in ptcl.host_and_mac: -# for active_clients in ptcl.active_dev: -# if active_clients in mac: -# if mac in aliases.itervalues(): -# display_list.append([count, aliases.keys()[aliases.values().index(mac)], active_clients]) -# else: -# display_list.append([count, hostname, active_clients]) -# hostnames.append(hostname) -# count += 1 -# print(tabulate(display_list, headers=["DEVICE-NO.", "HOSTNAME", "MAC"], tablefmt="fancy_grid")) -# return hostnames -# -# -# def show_blocked_dev(): -# ''' -# Display blocked devices. -# ''' -# r, soup = ptcl.scrape_page(ptcl.mask + "wlmacflt.cmd?action=view") -# print("Showing blocked devices.\n") -# for i in soup.findAll('td'): -# if not i.find("input"): -# if Router.mac_adr_regex.search(i.text): -# print(tabulate([[i.text]], headers=["BLOCKED-DEV"], tablefmt="fancy_grid")) -# -# -# def main(): -# parser = argparse.ArgumentParser(description="Control PTCL router from command-line.") -# parser.add_argument('-b', '--block', help="Block device.", nargs='?') -# parser.add_argument('-sb', '--blocked_dev', help='Display blocked devices.', action='store_true') -# parser.add_argument('-ub', '--unblock', help="Unblock device.", nargs='?') -# parser.add_argument('-a', '--active-devices', help="Gets number of devices connected to the router.", action='store_true') -# parser.add_argument('-r', '--restart', help="Restart Router.", action='store_true') -# parser.add_argument('-sd', '--show-dhcp', help='Show DHCP Info.', action='store_true') -# parser.add_argument('-s', '--show-active', help='Show Active Devices.', default='.') -# parser.add_argument('--configure', help='Configure router settings.', action='store_true') -# parser.add_argument('-sa', '--set-alias', help='Set custom alias for a device hostname.', action='store_true') -# parser.add_argument('-c', '--cli', help='Silent mode.', nargs='?', default='False') -# args = parser.parse_args() -# -# if configure.config_check() == True: -# if configure.config: -# global ptcl -# ptcl = Router(mask=configure.config["Router-Auth"]["mask"], password=configure.config["Router-Auth"]["password"]) -# -# else: -# configure.config_check() -# sys.exit("Please Re-run.") -# -# my_macs = configure.get_alias() -# -# if args.block: -# if args.block == '1': -# name = show_active_dev() -# ptcl.host_and_mac = dict(ptcl.host_and_mac) -# dev_mac = int(raw_input("Please Enter Device Number: ")) - 1 -# ptcl.block_dev(ptcl.host_and_mac[name[dev_mac]]) -# print("%s has been blocked." % name[dev_mac].capitalize()) -# -# elif args.block != '1' and args.block in my_macs.iterkeys(): -# # print "Calling blocker function - AUTOMATED MODE." -# ptcl.block_dev(my_macs[args.block.lower()]) -# print("%s has been blocked." % args.block.capitalize()) -# if args.block not in my_macs.iterkeys(): -# print("User not found.") -# -# elif args.block != '1' and args.block not in my_macs.iterkeys(): -# print("User not found.") -# -# elif args.unblock: -# if args.unblock == '1': -# show_blocked_dev() -# dev_mac = raw_input("Please enter device mac address: ") -# ptcl.unblock_dev(dev_mac) -# print("%s has been unblocked." % dev_mac) -# -# elif args.unblock != 1 and args.unblock in my_macs.iterkeys(): -# # print( "Calling unblocker function - AUTOMATED MODE") -# ptcl.unblock_dev(my_macs[args.unblock.lower()]) -# print("%s has been unblocked." % args.unblock.capitalize()) -# -# elif args.unblock != 1 and args.unblock not in my_macs.iterkeys(): -# print("User not found.") -# -# elif args.active_devices: -# # print( "Calling Station info Function") -# ptcl.stationinfo() -# print("Currently active devices are:", len(ptcl.active_dev)) -# -# elif args.restart: -# # print( "Calling restart Function") -# ptcl.reboot() -# -# elif args.show_dhcp: -# # print( "Calling DHCP_info Function") -# show_dhcpinfo() -# -# elif args.blocked_dev: -# show_blocked_dev() -# -# elif args.set_alias: -# show_active_dev() -# configure.set_alias() -# -# elif args.show_active == '.': -# # print "Calling show_active Function" -# show_active_dev() -# -# else: -# print("Invalid Argument") -# -# main() diff --git a/router.py b/router.py index 32b4f69..2751587 100755 --- a/router.py +++ b/router.py @@ -106,7 +106,7 @@ def dhcpinfo(self): hostname = td[td.index(i) - 1].text self.dev_info["Hostname"] = hostname self.dev_info[hostname] = [i.text, td[td.index(i) + 1].text, td[td.index(i) + 2].text] - return (self.dev_info) + return self.dev_info def stationinfo(self): @@ -141,6 +141,7 @@ def unblock(self, udevmac): ''' self.session.get(self.mask + "wlmacflt.cmd?action=remove&rmLst=%s&sessionKey=%s" % (udevmac, self.session_key())) + def reboot(self): ''' Reboots Router. @@ -188,16 +189,18 @@ def time_limit(self, username="User_1", mac="", days="", start="1", end="24"): "Everyday": 127} num_lst = [] - def day_to_binary(integer): + def bin_to_dec(integer): # TODO: Add check for integer parameter. ''' Takes an integer and divides it by 2, appends to the num_lst and returns sum of the num_lst when it reaches 1. ''' + # if not isinstance(integer, int): + # raise Exception if 1 in num_lst: return sum(num_lst) num_lst.append(integer / 2) - return day_to_binary(integer / 2) + return bin_to_dec(integer / 2) def convert_time(start_time="1", end_time="23:59"): # TODO : Add test that the numbers after : shouldn't exceed 60 (minutes) @@ -224,9 +227,11 @@ def convert_time(start_time="1", end_time="23:59"): start_time.append(00) if len(end_time) == 1: end_time.append(00) + # if end_time[0] > 24 or start_time[0] > 24 or end_time[1] > 60 or start_time[1] > 60: + # raise custom Exception start_time = (start_time[0] * 60) + start_time[1] end_time = (end_time[0] * 60) + end_time[1] - return start_time, end_time + return (start_time, end_time) gen_params = lambda days: { 'username': username, @@ -240,16 +245,16 @@ def convert_time(start_time="1", end_time="23:59"): for keys, val in week_days.items(): if days and len(days) < 3: if len(days) == 1: - r = self.session.get(self.mask + 'todmngr.tod?action=add&mac=%s' % (mac), params=gen_params(days=week_days[days])) + self.session.get(self.mask + 'todmngr.tod?action=add&mac=%s'.format(mac), params=gen_params(days=week_days[days])) break elif len(days) == 2 and days[0] in week_days and days[1] in week_days: if days[0] == days[1]: - r = self.session.get(self.mask + 'todmngr.tod?action=add&mac=%s' % (mac), params=gen_params(days=week_days["Everyday"])) + self.session.get(self.mask + 'todmngr.tod?action=add&mac=%s'.format(mac), params=gen_params(days=week_days["Everyday"])) break elif days[0] != days[1]: - r = self.session.get(self.mask + 'todmngr.tod?action=add&mac=%s' % (mac), params=gen_params(days=str(week_days[days[1]]))) + self.session.get(self.mask + 'todmngr.tod?action=add&mac=%s'.format(mac), params=gen_params(days=str(week_days[days[1]]))) break # Mon - Sunday, select the value from sunday and add it to the value preceding it. else: diff --git a/tests/convert_time.py b/tests/convert_time.py new file mode 100644 index 0000000..ac05b20 --- /dev/null +++ b/tests/convert_time.py @@ -0,0 +1,34 @@ +def time(start_time="1", end_time="23:59"): + # TODO : Add test that the numbers after : shouldn't exceed 60 (minutes) + ''' + Converts time to minutes. + Takes time and splits it by ":", the first element before ":" is in + hour and the second element is in minutes. + + Parameters: + - start_time: start time to apply limit from. Eg: 1:00 (am) + - end_time: end time to apply limit till. Eg: 13:00 (pm) + + Return (Integer): + sum of start_time and end_time in format (Hours * 60 + minutes). + + Example: + >>> convert_time(13:00, 18:08) + # returns (13 * 60) + 00, (18 * 60) + 08 + 780, 1080 + ''' + start_time = [int(i) for i in start_time.split(':')] + end_time = [int(i) for i in end_time.split(':')] + if len(start_time) == 1: + start_time.append(00) + if len(end_time) == 1: + end_time.append(00) + if end_time[0] > 24 or start_time[0] > 24 or end_time[1] > 60 or start_time[1] > 60: + # Raise Exception here! + print("The first segment of start and end time should be less than or equal to 24 (measured in hours)\ + and the second segment should be less than or equal to 60 (measured in minutes).") + start_time = (start_time[0] * 60) + start_time[1] + end_time = (end_time[0] * 60) + end_time[1] + return (start_time, end_time) + +s = time('25')