瀏覽代碼

conformed to pep8 style guide

master
theycallmemac 3 年之前
父節點
當前提交
384384954a
共有 14 個檔案被更改,包括 140 行新增56 行删除
  1. +5
    -0
      .travis.yml
  2. +0
    -1
      scripts/__init__.py
  3. +9
    -8
      scripts/lab_booking.py
  4. +9
    -4
      scripts/lookup.py
  5. +33
    -5
      scripts/main.py
  6. +22
    -10
      scripts/now.py
  7. +7
    -1
      scripts/opts.py
  8. +6
    -2
      scripts/room_booking.py
  9. +19
    -7
      scripts/utils.py
  10. +10
    -10
      setup.py
  11. +3
    -2
      tests/test_available_now_output.py
  12. +7
    -3
      tests/test_lookup_output.py
  13. +4
    -1
      tests/test_now.py
  14. +6
    -2
      tests/test_required.py

+ 5
- 0
.travis.yml 查看文件

@@ -4,10 +4,15 @@ python:
- "3.4"
- "3.5"
- "3.6"

before_install:
- pip install pep8

install:
- pip install -r requirements.txt

script:
- find . -name '*\*.py' -exec pep8 {} +
- python tests/test_lookup_output.py
- python tests/test_available_now_output.py
- python tests/test_now.py


+ 0
- 1
scripts/__init__.py 查看文件

@@ -1 +0,0 @@


+ 9
- 8
scripts/lab_booking.py 查看文件

@@ -10,7 +10,7 @@ __license__ = 'GPL-3.0'

class LabBooking(object):
arguments = []
email= ""
email = ""
password = ""
name = ""
society = ""
@@ -26,14 +26,16 @@ class LabBooking(object):
FROM = self.email
TO = ['irene.mcevoy@dcu.ie']
SUBJECT = 'Lab Booking'
BODY = "Just wondering if you could book " + self.arguments[0] + " on the " + \
self.arguments[1] + " from " + self.arguments[2][:2] + ":" + \
self.arguments[2][2:] + " to " + self.arguments[3][:2] + ":" + \
self.arguments[3][2:] + " for " + self.society + ".\n\nThank you,\n" + self.name + "."
message = """\nFrom: %s\nTo: %s\nSubject: %s\n\n%s""" % (FROM, ", ".join(TO), SUBJECT, BODY)
BODY = "Just wondering if you could book " + \
self.arguments[0] + " on the " + \
self.arguments[1] + " from " + self.arguments[2][:2] + ":" + \
self.arguments[2][2:] + " to " + self.arguments[3][:2] + ":" + \
self.arguments[3][2:] + " for " + self.society + \
".\n\nThank you,\n" + self.name + "."
message = """\nFrom: %s\nTo: %s\nSubject: %s\n\n%s""" % (
FROM, ", ".join(TO), SUBJECT, BODY)
return (FROM, TO), message


def send(self, FROM, TO, message):
try:
server = smtplib.SMTP("smtp.gmail.com", 587)
@@ -45,4 +47,3 @@ class LabBooking(object):
return "Your email has been sent."
except BaseException:
return "Email failed to send."


+ 9
- 4
scripts/lookup.py 查看文件

@@ -13,6 +13,7 @@ __version__ = '2.0.0'
__copyright__ = 'Copyright (c) 2018 theycallmemac'
__license__ = 'GPL-3.0'


class LookUp(object):
week = ""
day = ""
@@ -38,9 +39,11 @@ class LookUp(object):
for room in lst:
status = LookUp.build_timetable(self, room)
if len(status) <= 9:
print("\033[1;92m{0}\033[00m".format(room) + ": " + "\033[1;97m{0}\033[00m".format(status))
print("\033[1;92m{0}\033[00m".format(room) +
": " + "\033[1;97m{0}\033[00m".format(status))
else:
print("\033[1;91m{0}\033[00m".format(room) + ": " + "\033[1;90m{0}\033[00m".format(status))
print("\033[1;91m{0}\033[00m".format(room) +
": " + "\033[1;90m{0}\033[00m".format(status))

def room_option(self, room):
status = LookUp.build_timetable(self, room)
@@ -56,8 +59,10 @@ class LookUp(object):
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/58.0.3029.110
Safari/537.36""")]
url = "https://www.dcu.ie/timetables/feed.php?room=GLA." + room + "&week1=" + self.week + "&hour=" + str(self.hour) + "&day=" + self.day + "&template=location"
url = "https://www.dcu.ie/timetables/feed.php?room=GLA." + \
room + "&week1=" + self.week + \
"&hour=" + str(self.hour) + \
"&day=" + self.day + "&template=location"
browser.open(url)
result = utils.check_room(url)
return result


+ 33
- 5
scripts/main.py 查看文件

@@ -17,12 +17,19 @@ __version__ = '2.0.0'
__copyright__ = 'Copyright (c) 2018 theycallmemac'
__license__ = 'GPL-3.0'


def setup_options():
parser = OptionParser(
description="\033[1;97m{0}\033[00m".format('Displays info and books room around DCU.'),
prog="\033[1;97m{0}\033[00m".format('dcurooms'), version='%prog ' + __version__, usage='%prog [' + '\033[1;92m{0}\033[00m'.format("option") + ']')
description="\033[1;97m{0}\033[00m".format(
'Displays info and books room around DCU.'),
prog="\033[1;97m{0}\033[00m".format('dcurooms'),
version='%prog ' + __version__,
usage='%prog [' + '\033[1;92m{0}\033[00m'.format("option") + ']')
parser.add_option(
"-l", "--lookup", action="store_true", help="returns information given a specific room, week, day and hour")
"-l",
"--lookup",
action="store_true",
help="returns information given a specific room, week, day and hour")
parser.add_option(
"-b", "--book", action="store_true",
help="books a room by providing the room, D/M/YYYY, start, and end")
@@ -41,9 +48,24 @@ def setup_options():
help="displays the status of rooms in the Henry Grattan building")
return parser


def get_data():
times, c, g, details = {'0800': '1', '0830': '2', '0900': '3', '0930': '4', '1000': '5', '1030': '6', '1100': '7', '1130': '8', '1200': '9', '1230': '10', '1300': '11', '1330': '12', '1400': '13','1430': '14', '1500': '15', '1530': '16', '1600': '17', '1630': '18', '1700': '19', '1730': '20', '1800': '21', '1830': '22', '1900': '23', '1930': '24', '2000': '25', '2030': '26', '2100': '27', '2130': '28', '2200': '29', '2230': '30'}, ['LG25', 'LG26', 'LG27', 'L101', 'L114', 'L125', 'L128'], ['CG01', 'CG02', 'CG03', 'CG04', 'CG05', 'CG06', 'CG11', 'CG12', 'CG20', 'CG68', 'C166'], sys.argv[2:]
return times, c, g, details
times = {'0800': '1', '0830': '2', '0900': '3',
'0930': '4', '1000': '5', '1030': '6',
'1100': '7', '1130': '8', '1200': '9',
'1230': '10', '1300': '11', '1330': '12',
'1400': '13', '1430': '14', '1500': '15',
'1530': '16', '1600': '17', '1630': '18',
'1700': '19', '1730': '20', '1800': '21',
'1830': '22', '1900': '23', '1930': '24',
'2000': '25', '2030': '26', '2100': '27',
'2130': '28', '2200': '29', '2230': '30'}
c = ['LG25', 'LG26', 'LG27', 'L101', 'L114', 'L125', 'L128']
g = ['CG01', 'CG02', 'CG03', 'CG04', 'CG05', 'CG06', 'CG11',
'CG12', 'CG20', 'CG68', 'C166']
details = sys.argv[2:]
return times, c, g, details


def required():
parser = setup_options()
@@ -51,21 +73,25 @@ def required():
times, c, g, details = get_data()
return parser, (options, arguments), (c, g), (times, details)


def booking(options, info):
if options.book and info[1][0][0] == "L":
opts.booking_lab(info)
elif options.book and info[1][0][0] == "C":
opts.booking_room(info)


def lookup(options, rooms, info):
if options.lookup and (options.grattan or options.computing):
opts.lookup_building(rooms, info, options)
elif options.lookup:
opts.lookup_room(info)


def now(options, rooms, info):
opts.now(rooms, info, options)


def main():
parser, (options, arguments), rooms, info = required()
if options.book:
@@ -74,5 +100,7 @@ def main():
lookup(options, rooms, info)
elif options.now:
now(options, rooms, info)


if __name__ == "__main__":
main()

+ 22
- 10
scripts/now.py 查看文件

@@ -15,6 +15,7 @@ __version__ = '2.0.0'
__copyright__ = 'Copyright (c) 2018 theycallmemac'
__license__ = 'GPL-3.0'


class Now(object):
week = ""
day = ""
@@ -28,15 +29,23 @@ class Now(object):
self.minute = minute

def check_args(self):
if int(self.week) not in range(1, 53) or int(self.day) not in range(1, 7):
print("\033[1;91m{0}\033[00m".format("Incorrect parameters passed."))
if int(
self.week) not in range(
1,
53) or int(
self.day) not in range(
1,
7):
print("\033[1;91m{0}\033[00m".format(
"Incorrect parameters passed."))
sys.exit()
else:
pass

def round_it(self):
if int(self.hour) < 8 or int(self.hour) >= 23:
print("\033[1;93m{0}\033[00m".format("Outside scheduled timetables. Try again at 08:00."))
print("\033[1;93m{0}\033[00m".format(
"Outside scheduled timetables. Try again at 08:00."))
sys.exit()
if int(self.minute) >= 30:
self.minute = '30'
@@ -46,7 +55,8 @@ class Now(object):
def check_time(self, times):
time = self.hour + self.minute
if time not in times:
print("\033[1;93m{0}\033[00m".format("Outside scheduled timetables. Please try again at 08:00."))
print("\033[1;93m{0}\033[00m".format(
"Outside scheduled timetables. Please try again at 08:00."))
sys.exit()
for k, v in times.items():
if k == time:
@@ -59,12 +69,14 @@ class Now(object):
def get_status(self, options, room, status):
if options.available:
if len(status) <= 9:
print("\033[1;92m{0}\033[00m".format(room) + ": " +"\033[1;97m{0}\033[00m".format(status))
print("\033[1;92m{0}\033[00m".format(room) +
": " + "\033[1;97m{0}\033[00m".format(status))
elif len(status) > 9:
print("\033[1;91m{0}\033[00m".format(room) + ": " +"\033[1;90m{0}\033[00m".format(status))
print("\033[1;91m{0}\033[00m".format(room) +
": " + "\033[1;90m{0}\033[00m".format(status))
else:
print("\033[1;92m{0}\033[00m".format(room) + ": " +"\033[1;97m{0}\033[00m".format(status))
print("\033[1;92m{0}\033[00m".format(room) +
": " + "\033[1;97m{0}\033[00m".format(status))

def building_option(self, lst, options):
for room in lst:
@@ -81,8 +93,8 @@ class Now(object):
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/58.0.3029.110
Safari/537.36""")]
url = "https://www.dcu.ie/timetables/feed.php?room=GLA." + room + "&week1=" + self.week + "&hour=" + self.hour + "&day=" + self.day + "&template=location"
url = "https://www.dcu.ie/timetables/feed.php?room=GLA." + room + "&week1=" + \
self.week + "&hour=" + self.hour + "&day=" + self.day + "&template=location"
browser.open(url)
result = utils.check_room(url)
return result


+ 7
- 1
scripts/opts.py 查看文件

@@ -11,12 +11,15 @@ __version__ = '2.0.0'
__copyright__ = 'Copyright (c) 2018 theycallmemac'
__license__ = 'GPL-3.0'


def get_lab_credentials():
return utils.get_version_email()


def get_room_credentials():
return utils.get_version_form()


def booking_lab(info):
email, password, your_name, society = get_lab_credentials()
lab = LabBooking(email, password, your_name, society, info[1])
@@ -29,6 +32,7 @@ def booking_lab(info):
else:
"Booking cancelled."


def booking_room(info):
email, number, name, society = get_room_credentials()
room = RoomBooking(email, number, name, society, info[1])
@@ -40,6 +44,7 @@ def booking_room(info):
else:
"Booking cancelled."


def lookup_building(rooms, info, options):
lst = utils.get_lst(rooms[0], rooms[1], options)
week, day, hour = info[1]
@@ -48,6 +53,7 @@ def lookup_building(rooms, info, options):
look.check_time(info[0])
look.building_option(lst)


def lookup_room(info):
room, week, day, hour = info[1]
look = LookUp(week, day, hour)
@@ -55,6 +61,7 @@ def lookup_room(info):
look.check_time(info[0])
look.room_option(room)


def now(rooms, info, options):
week, day, hour, minute = utils.get_current_time(datetime.datetime.now())
now = Now(week, day, hour, minute)
@@ -63,4 +70,3 @@ def now(rooms, info, options):
now.check_time(info[0])
lst = utils.get_lst(rooms[0], rooms[1], options)
now.building_option(lst, options)


+ 6
- 2
scripts/room_booking.py 查看文件

@@ -12,9 +12,10 @@ __version__ = '2.0.0'
__copyright__ = 'Copyright (c) 2018 theycallmemac'
__license__ = 'GPL-3.0'


class RoomBooking(object):
arguments = []
email= ""
email = ""
number = ""
name = ""
society = ""
@@ -30,7 +31,10 @@ class RoomBooking(object):
browser = StatefulBrowser()
cookie_jar = LWPCookieJar()
browser.set_cookiejar(cookie_jar)
room, date, start, end = self.arguments[0], self.arguments[1].split("/"), self.arguments[2][:2] + ":" + self.arguments[2][2:], self.arguments[3][:2] + ":" + self.arguments[3][2:]
room, date, start, end = self.arguments[0], self.arguments[1].split(
"/"), self.arguments[2][:2] + ":" + \
self.arguments[2][2:], self.arguments[3][:2] + \
":" + self.arguments[3][2:]
day, month, year = date[0], date[1], date[2]
browser.open("http://www.dcu.ie/registry/booking.shtml")
browser.select_form(nr=4)


+ 19
- 7
scripts/utils.py 查看文件

@@ -9,11 +9,14 @@ __version__ = '2.0.0'
__copyright__ = 'Copyright (c) 2018 theycallmemac'
__license__ = 'GPL-3.0'


def confirm():
if int(sys.version[0]) < 3:
conf = raw_input("\033[1;93m{0}\033[00m".format("\nIs this information correct? (y/n): "))
conf = raw_input("\033[1;93m{0}\033[00m".format(
"\nIs this information correct? (y/n): "))
else:
conf = input("\033[1;93m{0}\033[00m".format("\nIs this information correct? (y/n): "))
conf = input("\033[1;93m{0}\033[00m".format(
"\nIs this information correct? (y/n): "))
if conf == "y":
return conf
else:
@@ -27,17 +30,23 @@ def check_args(week, day):
else:
pass


def check_room(timetable_url):
html = get(timetable_url)
soup = BeautifulSoup(html.text, "lxml")
tr = soup.select('tr')
return str(tr[12].getText().strip()) + " -> " + str(tr[14].getText().strip())
return str(tr[12].getText().strip()) + " -> " + \
str(tr[14].getText().strip())


def get_lst(c, g, options):
if options.computing == True: lst = c
elif options.grattan == True: lst = g
if options.computing:
lst = c
elif options.grattan:
lst = g
return lst


def get_current_time(date):
day = date.weekday()
hour = date.hour
@@ -51,16 +60,19 @@ def get_current_time(date):
week = week_no + offset - 1
return str(week), str(day + 1), str(hour), str(minute)


def get_version_email():
if int(sys.version[0]) < 3:
email = raw_input("\033[1;97m{0}\033[00m".format("Your gmail: "))
password = getpass.getpass("\033[1;97m{0}\033[00m".format("Your gmail password: "))
password = getpass.getpass(
"\033[1;97m{0}\033[00m".format("Your gmail password: "))
your_name = raw_input("\033[1;97m{0}\033[00m".format("Your name: "))
society = raw_input("\033[1;97m{0}\033[00m".format("Society name: "))
return email, password, your_name, society
else:
email = input("\033[1;97m{0}\033[00m".format("Your gmail: "))
password = getpass.getpass("\033[1;97m{0}\033[00m".format("Your gmail password: "))
password = getpass.getpass(
"\033[1;97m{0}\033[00m".format("Your gmail password: "))
your_name = input("\033[1;97m{0}\033[00m".format("Your name: "))
society = input("\033[1;97m{0}\033[00m".format("Society name: "))
return email, password, your_name, society


+ 10
- 10
setup.py 查看文件

@@ -7,17 +7,17 @@ setup(name='dcurooms',
author='theycallmemac',
url='https://github.com/theycallmemac/dcurooms',
license='GPL-3.0',
scripts=['scripts/dcurooms', 'scripts/main.py', 'scripts/utils.py', 'scripts/opts.py', 'scripts/lab_booking.py', 'scripts/room_booking.py', 'scripts/lookup.py', 'scripts/now.py'],
scripts=['scripts/dcurooms', 'scripts/main.py', 'scripts/utils.py', 'scripts/opts.py',
'scripts/lab_booking.py', 'scripts/room_booking.py', 'scripts/lookup.py', 'scripts/now.py'],
install_requires=[
'MechanicalSoup', 'bs4', 'requests'
],
classifiers=[
'Environment :: Console',
'Natural Language :: English',
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6'])

'Environment :: Console',
'Natural Language :: English',
'License :: OSI Approved :: GNU General Public License v3 (GPLv3)',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6'])

+ 3
- 2
tests/test_available_now_output.py 查看文件

@@ -4,9 +4,11 @@ import datetime
import os
sys.path.append('.')


class AvailableNowTestCase(unittest.TestCase):
def setUp(self):
available_result = os.system("""python scripts/dcurooms -anC > tests/available_output.txt""")
available_result = os.system(
"""python scripts/dcurooms -anC > tests/available_output.txt""")
self.assertTrue(available_result == 0)

def test_available_output(self):
@@ -27,4 +29,3 @@ class AvailableNowTestCase(unittest.TestCase):

if __name__ == '__main__':
unittest.main()


+ 7
- 3
tests/test_lookup_output.py 查看文件

@@ -3,10 +3,14 @@ import sys
import datetime
import os
sys.path.append('.')


class LookupTestCase(unittest.TestCase):
def setUp(self):
room_result = os.system("""python scripts/dcurooms -l CG12 21 1 1800 > tests/lookup_room_output.txt""")
building_result = os.system("""python scripts/dcurooms -lL 9 4 1500 > tests/lookup_building_output.txt""")
room_result = os.system(
"""python scripts/dcurooms -l CG12 21 1 1800 > tests/lookup_room_output.txt""")
building_result = os.system(
"""python scripts/dcurooms -lL 9 4 1500 > tests/lookup_building_output.txt""")
self.assertTrue(room_result == 0 and building_result == 0)

def test_lookup_output(self):
@@ -33,6 +37,6 @@ class LookupTestCase(unittest.TestCase):
del_result = os.system("""rm tests/*.txt""")
self.assertTrue(del_result == 0)


if __name__ == '__main__':
unittest.main()


+ 4
- 1
tests/test_now.py 查看文件

@@ -3,6 +3,8 @@ import sys
import datetime
sys.path.append('.')
from scripts.utils import get_current_time


class RequiredTestCase(unittest.TestCase):
def test_time(self):
now = get_current_time(datetime.datetime.now())
@@ -11,6 +13,7 @@ class RequiredTestCase(unittest.TestCase):
hr_d = datetime.datetime.now().hour
mn_d = datetime.datetime.now().minute
self.assertTrue(hr == hr_d and mn == mn_d)


if __name__ == '__main__':
unittest.main()


+ 6
- 2
tests/test_required.py 查看文件

@@ -3,10 +3,14 @@ import sys
import datetime
import os
from scripts.main import required


class RequiredTestCase(unittest.TestCase):
def test_required(self):
parser, (options, arguments), rooms, info = required()
self.assertNotIsInstance(rooms, basestring) and self.asserNotIsInstance(info, basestring)
self.assertNotIsInstance(
rooms, basestring) and self.asserNotIsInstance(info, basestring)


if __name__ == '__main__':
unittest.main()


Loading…
取消
儲存