Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- class Adventure:
- def __init__(self, filepath):
- self.sections = re.compile(r'(?:===\n)(.*?):\n(.*?)(?=\n===)', re.DOTALL)
- self.section_contents = re.compile(r'\n(?:---)?\n?', re.DOTALL)
- with open(filepath) as f:
- self.file_contents = f.read()
- self.read_adventure_metadata()
- self.assemble_adventure()
- def read_adventure_metadata(self):
- name, description, version, adventure = self.file_contents.split('\n', maxsplit=3)
- self.name = name
- self.description = description.replace('description: ', '')
- self.version = version.replace('Version:', '')
- self.adventure = adventure
- def assemble_adventure(self):
- constructors = {'Rooms' : self.rooms,
- 'Exits' : self.exits}
- for section_type, section_contents in self.sections.findall(self.adventure):
- section_contents = self.section_contents.split(section_contents)
- constructor = constructors[section_type]
- constructor(section_contents)
- def rooms(self, section_contents):
- # do something with Rooms text
- def exits(self, section_contents):
- # do something with Exits text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement