Advertisement
TorroesPrime

Untitled

Dec 1st, 2021
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. import re
  2.  
  3. class Adventure:
  4.     def __init__(self, filepath):
  5.         self.sections = re.compile(r'(?:===\n)(.*?):\n(.*?)(?=\n===)', re.DOTALL)
  6.         self.section_contents = re.compile(r'\n(?:---)?\n?', re.DOTALL)
  7.  
  8.         with open(filepath) as f:
  9.             self.file_contents = f.read()
  10.         self.read_adventure_metadata()
  11.         self.assemble_adventure()
  12.  
  13.     def read_adventure_metadata(self):
  14.         name, description, version, adventure = self.file_contents.split('\n', maxsplit=3)
  15.         self.name = name
  16.         self.description = description.replace('description: ', '')
  17.         self.version = version.replace('Version:', '')
  18.         self.adventure = adventure
  19.  
  20.     def assemble_adventure(self):
  21.         constructors = {'Rooms' : self.rooms,
  22.                         'Exits' : self.exits}
  23.         for section_type, section_contents in self.sections.findall(self.adventure):
  24.             section_contents = self.section_contents.split(section_contents)
  25.             constructor = constructors[section_type]
  26.             constructor(section_contents)
  27.  
  28.     def rooms(self, section_contents):
  29.         # do something with Rooms text
  30.  
  31.     def exits(self, section_contents):
  32.         # do something with Exits text
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement