Skip to content

Commit

Permalink
Merge pull request #1 from JoshuaOndieki/develop
Browse files Browse the repository at this point in the history
Functional v0.0
  • Loading branch information
JoshuaOndieki authored Apr 11, 2017
2 parents d0a5e0e + f27bb94 commit 96c07ab
Show file tree
Hide file tree
Showing 21 changed files with 374 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__
venv
.pyc
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: python
python:
- "3.6"
# command to install dependencies
install:
- pip install -r requirements.txt

# command to run tests
script: pytest

after-success:
- coveralls
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
[![Coverage Status](https://coveralls.io/repos/github/JoshuaOndieki/dojo/badge.svg?branch=master)](https://coveralls.io/github/JoshuaOndieki/dojo?branch=master)
[![Build Status](https://travis-ci.org/JoshuaOndieki/dojo.svg?branch=master)](https://travis-ci.org/JoshuaOndieki/dojo)
# dojo
An utterly fantastic CLI application for office Space Allocation


## Usage:
`create_room <room_type> <name>...`
`add_person <firstname> <surname> <person_type> [<wants_accomodation>]`

## How to Use

To use this project, follow these steps:

1. Create your working environment.
2. Git clone this project (`$ git clone https://github.com/JoshuaOndieki/dojo.git`)
3. (`$ cd dojo`)
4. Install project dependencies (`$ pip install -r requirements.txt`)
5. `python app.py`
6. (`DOJO$$$`)

## Tests
Run tests with `nosetests`

## License: [![CocoaPods](https://img.shields.io/cocoapods/l/AFNetworking.svg?style=plastic)]()

## Further Reading

- [Docopt](https://github.com/docopt/docopt)
85 changes: 85 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
"""
Usage:
create_room <type_room> <name>...
add_person <firstname> <surname> <person_type> [<wants_accomodation>]
dojo (-i | --interactive)
dojo (-h | --help | --version)
Options:
-i, --interactive Interactive Mode
-h, --help Show this screen and exit.
"""


from docopt import docopt, DocoptExit
import cmd
import os
import sys
from models.dojo import Dojo
from termcolor import colored
from insta import instance


def docopt_cmd(func):
"""
This decorator is used to simplify the try/except block and pass the result
of the docopt parsing to the called action
"""

def fn(self, arg):
try:
opt = docopt(fn.__doc__, arg)

except DocoptExit as e:
# The DocoptExit is thrown when the args do not match
# We print a message to the user and the usage block
print('Invalid Command!')
print(e)
return

except SystemExit:
# The SystemExit exception prints the usage for --help
# We do not need to do the print here
return

return func(self, opt)

fn.__name__ = func.__name__
fn.__doc__ = func.__doc__
fn.__dict__.update(func.__dict__)
return fn


def intro():
os.system("clear")
print(__doc__)


class DOJO(cmd.Cmd):

prompt = colored('DOJO$$$', 'magenta', attrs=['blink','bold'])

@docopt_cmd
def do_create_room(self, arg):
"""Usage: create_room <type_room> <name>..."""
for room in arg['<name>']:
print(instance.create_room(room,arg['<type_room>']))

@docopt_cmd
def do_add_person(self, arg):
"""Usage: add_person <firstname> <surname> <person_type> [<wants_accomodation>]"""
print(instance.add_person(arg['<firstname>'],arg['<surname>'],arg['<person_type>'],arg['<wants_accomodation>']))

@docopt_cmd
def do_quit(self, arg):
"""Usage: quit"""
os.system('clear')
print ('Dojo Exiting')
exit()

if __name__ == "__main__":
try:
intro()
DOJO().cmdloop()
except KeyboardInterrupt:
os.system("clear")
print('Dojo Exiting')
3 changes: 3 additions & 0 deletions insta.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from models.dojo import Dojo

instance=Dojo()
Empty file added models/__init__.py
Empty file.
68 changes: 68 additions & 0 deletions models/dojo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from models.fellow import Fellow
from models.staff import Staff
from models.livingspace import LivingSpace
from models.office import Office
import random

class Dojo():
def __init__(self):
self.offices={}
self.livingspaces={}
self.fellows=[]
self.staff=[]
self.all_rooms={}
self.all_people=[]

def create_room(self, name,room_type):
if room_type.lower() not in ['office','livingspace']:
return('Only offices and livingspaces allowed!')
if not isinstance(name,str):
return('Room names can only be strings!')
if name in self.all_rooms:
return('Room %s exists!'%(name))
else:
if room_type.lower()=='office'.lower():
self.room=Office(name)
self.offices[name]=self.room.members
self.all_rooms[name]=[room_type,self.room.members]
return('An office called %s has been created successfully!'%(name))
elif room_type.lower()=='livingspace'.lower():
self.room=LivingSpace(name)
self.livingspaces[name]=self.room.members
self.all_rooms[name]=[room_type,self.room.members]
return('A Livingspace called %s has been created successfully!'%(name))

def add_person(self, firstname,surname,person_type,wants_accomodation='N'):
if person_type.lower() not in ['fellow','staff']:
return('Only fellow and staff allowed!')
if not isinstance(firstname,str) or not isinstance(surname,str):
return('People names can only be strings!')
if firstname+' '+surname in self.all_people:
return('%s %s exists!'%(firstname,surname))
else:
if person_type.lower()=='fellow':
fellow=Fellow(firstname,surname)
self.fellows.append(firstname +' '+ surname)
self.all_people.append(firstname +' '+ surname)
if self.offices:
office=random.choice(list(self.offices))
self.offices[office].append(firstname +' '+ surname)
print('Fellow %s %s has been assigned office %s!'%(firstname,surname,office))
else:
print('No office to assign!')
if wants_accomodation=='Y' and self.livingspaces:
room=random.choice(list(self.livingspaces))
self.livingspaces[room].append(firstname +' '+ surname)
print('Fellow %s %s has been assigned livingspace %s!'%(firstname,surname,room))
return('Fellow %s %s has been added successfully!'%(firstname,surname))
elif person_type.lower()=='staff':
staff=Staff(firstname,surname)
self.staff.append(firstname +' '+ surname)
self.all_people.append(firstname +' '+ surname)
if self.offices:
office=random.choice(list(self.offices))
self.offices[office].append(firstname +' '+ surname)
print('Staff %s %s has been assigned office %s!'%(firstname,surname,office))
else:
print('No office to assign!')
return('Staff %s %s has been added successfully!'%(firstname,surname))
7 changes: 7 additions & 0 deletions models/fellow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from models.person import Person

class Fellow(Person):
def __init__(self,firstname,surname):
# super().__init__(self,firstname,surname)
self.firstname=firstname
self.surname=surname
8 changes: 8 additions & 0 deletions models/livingspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from models.room import Room

class LivingSpace(Room):
def __init__(self,name):
# super().__init__(self,name)
self.name=name
self.capacity=4
self.members=[]
8 changes: 8 additions & 0 deletions models/office.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from models.room import Room

class Office(Room):
def __init__(self,name):
# super().__init__(self,name)
self.name=name
self.capacity=6
self.members=[]
4 changes: 4 additions & 0 deletions models/person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Person():
def __init__(self, firstname,surname):
self.firstname = firstname
self.surname = surname
4 changes: 4 additions & 0 deletions models/room.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

class Room():
def __init__(self,name):
self.name=name
7 changes: 7 additions & 0 deletions models/staff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from models.person import Person

class Staff(Person):
def __init__(self,firstname,surname):
# super().__init__(self,firstname,surname)
self.firstname=firstname
self.surname=surname
13 changes: 13 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
appdirs==1.4.3
coverage==4.3.4
coveralls==1.1
docopt==0.6.2
nose==1.3.7
packaging==16.8
py==1.4.33
pyparsing==2.2.0
pytest==3.0.7
requests==2.13.0
six==1.10.0
SQLAlchemy==1.1.9
termcolor==1.1.0
Empty file added tests/__init__.py
Empty file.
11 changes: 11 additions & 0 deletions tests/test_add_fellow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import unittest
from models.fellow import Fellow

class TestFellow(unittest.TestCase):
def test_creates_fellow_instance(self):
self.fellow=Fellow('Joshua','Ondieki')
self.assertTrue(self.fellow)
self.assertTrue('Joshua'==self.fellow.firstname and 'Ondieki'==self.fellow.surname)

if __name__ == '__main__':
unittest.main()
39 changes: 39 additions & 0 deletions tests/test_add_person.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
from models.dojo import Dojo

class TestAddPerson(unittest.TestCase):
def setUp(self):
self.dojo=Dojo()
self.initial_people = len(self.dojo.all_people)

def test_create_people_successfully(self):
self.fellow = self.dojo.add_person('Joshua','Ondieki', 'Fellow')
self.staff = self.dojo.add_person('Annette','Kamau', 'Staff')
# self.assertTrue(self.fellow and self.staff)
new_people = len(self.dojo.all_people)
self.assertEqual(new_people - self.initial_people, 2)
def test_it_fails_with_existing_person(self):
exist_person = self.dojo.add_person('Joshua','Ondieki', 'Fellow')
try_overwrite_f = self.dojo.add_person('Joshua','Ondieki', 'Fellow')
exist_person = self.dojo.add_person('Evans','Musomi', 'Staff')
try_overwrite_s = self.dojo.add_person('Evans','Musomi', 'Staff')
self.assertTrue(try_overwrite_f == 'Joshua Ondieki exists!' and try_overwrite_s == 'Evans Musomi exists!')

def test_it_fails_with_invalid_person_type(self):
invalid_type = self.dojo.add_person('Loice','Andia','BFA')
self.assertEqual('Only fellow and staff allowed!',invalid_type)

def test_fails_with_person_name_not_string(self):
invalid_person = self.dojo.add_person(['Oj'],'Josh','Fellow')
invalid_person1 = self.dojo.add_person({'Oj':'Josh'},{},'Fellow')
invalid_person2 = self.dojo.add_person(['Oj'],['Josh'],'Staff')
invalid_person3 = self.dojo.add_person({'Oj':'Josh'},{},'Staff')
self.assertEqual(invalid_person,'People names can only be strings!')
self.assertEqual(invalid_person1,'People names can only be strings!')
self.assertEqual(invalid_person2,'People names can only be strings!')
self.assertEqual(invalid_person3,'People names can only be strings!')



if __name__ == '__main__':
unittest.main()
12 changes: 12 additions & 0 deletions tests/test_add_staff.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest
from models.staff import Staff

class TestStaff(unittest.TestCase):
def test_creates_staff_instance(self):
self.staff=Staff('James','Ndiga')
self.assertTrue(self.staff)
self.assertTrue('James'==self.staff.firstname and 'Ndiga'==self.staff.surname)


if __name__ == '__main__':
unittest.main()
12 changes: 12 additions & 0 deletions tests/test_create_livingspace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest
from models.livingspace import LivingSpace

class TestLivingSpace(unittest.TestCase):
def test_creates_livingspace_instance(self):
self.livingspace=LivingSpace('Amity')
self.assertTrue(self.livingspace)
self.assertEqual(self.livingspace.capacity,4)
self.assertEqual(self.livingspace.members,[])

if __name__ == '__main__':
unittest.main()
12 changes: 12 additions & 0 deletions tests/test_create_office.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import unittest
from models.office import Office

class TestOffice(unittest.TestCase):
def test_creates_office_instance(self):
self.office=Office('Spire')
self.assertTrue(self.office)
self.assertEqual(self.office.capacity,6)
self.assertEqual(self.office.members,[])

if __name__ == '__main__':
unittest.main()
39 changes: 39 additions & 0 deletions tests/test_create_room.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
from models.dojo import Dojo

class TestCreateRoom(unittest.TestCase):
def setUp(self):
self.dojo=Dojo()
self.initial_rooms = len(self.dojo.all_rooms)

def test_create_rooms_successfully(self):
amity_livingspace = self.dojo.create_room('Amity', 'livingspace')
spire_office = self.dojo.create_room('Spire','office')
self.new_rooms = len(self.dojo.all_rooms)
self.assertEqual(self.new_rooms - self.initial_rooms, 2)
self.assertEqual('An office called Spire has been created successfully!',spire_office)
self.assertEqual('A Livingspace called Amity has been created successfully!',amity_livingspace)

def test_it_fails_with_existing_room(self):
exist_room = self.dojo.create_room('Amhere','office')
try_overwrite_o = self.dojo.create_room('Amhere','office')
exist_room = self.dojo.create_room('Amhere','livingspace')
try_overwrite_l = self.dojo.create_room('Amhere','livingspace')
self.assertTrue(try_overwrite_o == 'Room Amhere exists!' and try_overwrite_l == 'Room Amhere exists!')

def test_it_fails_with_invalid_room_type(self):
invalid_type = self.dojo.create_room('Amhere','Kitchen')
self.assertEqual('Only offices and livingspaces allowed!',invalid_type)

def test_fails_with_room_name_not_string(self):
invalid_room = self.dojo.create_room(['Amhere'],'office')
invalid_room1 = self.dojo.create_room({'Amhere':'room'},'office')
invalid_room2 = self.dojo.create_room(['Amhere'],'livingspace')
invalid_room3 = self.dojo.create_room({'Amhere':'room'},'livingspace')
self.assertEqual(invalid_room,'Room names can only be strings!')
self.assertEqual(invalid_room1,'Room names can only be strings!')
self.assertEqual(invalid_room2,'Room names can only be strings!')
self.assertEqual(invalid_room3,'Room names can only be strings!')

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

0 comments on commit 96c07ab

Please sign in to comment.