def parse_meditation(lines): lines = [l for l in lines if l.strip() if l.strip()] title_line = lines[0] rest = lines[1:] slug, title = title_line.replace("//", "").strip().split("|") koans = OrderedDict() current = None comment_bracket_cnt = 0 for line in rest: if line.strip().startswith("//"): current = line.replace("//", "").strip() koans[current] = {"hint": [], "code": [], "slug": current.split("|")[0], "name": current.split("|")[1]} elif line.strip().startswith("/*"): koans[current]["hint"].append(line) comment_bracket_cnt += 1 elif line.strip().startswith("*/"): koans[current]["hint"].append(line) comment_bracket_cnt -= 1 else: if comment_bracket_cnt != 0: koans[current]["hint"].append(line) else: koans[current]["code"].append(line) for koan in koans.values(): koan["hint"] = "".join(koan["hint"]) koan["code"] = "".join(koan["code"]) ks = [Koan(k["slug"], k["name"], k["hint"], k["code"]) for k in koans.values()] m = Meditation(slug, title) for k in ks: m.add_koan(k) return m
def retrieve(self): meditations = list(Meditation.collection().values()) if len(meditations) > 0: s = sorted(meditations, key=lambda m: m.slug) return [marshal(m, meditation_list_fields) for m in s] else: return {} #FIXME
def run(self, slug): matches = filter(lambda m: m.slug.startswith(slug), Meditation.collection().values()) if len(matches) == 0: print "No meditation matches given name" elif len(matches) > 1: print "Multiple matches:" for m in matches: print "\t{0}".format(m.slug) else: matches[0].delete()
def retrieve(self, slug): meditation = Meditation.get(slug) if not meditation: return {"detail": "Not found"}, 404 user = get_user_from_request(request) if not user: self.extend_with_null_solutions(meditation.koans) else: self.extend_with_user_solutions(meditation.koans, user) return marshal(meditation, meditation_detail_fields)
def parse_meditation(lines): lines = [l for l in lines if l.strip() if l.strip()] title_line = lines[0] rest = lines[1:] slug, title = title_line.replace('//', '').strip().split('|') koans = OrderedDict() current = None comment_bracket_cnt = 0 for line in rest: if line.strip().startswith('//'): current = line.replace('//', '').strip() koans[current] = { 'hint': [], 'code': [], 'slug': current.split('|')[0], 'name': current.split('|')[1] } elif line.strip().startswith('/*'): koans[current]['hint'].append(line) comment_bracket_cnt += 1 elif line.strip().startswith('*/'): koans[current]['hint'].append(line) comment_bracket_cnt -= 1 else: if comment_bracket_cnt != 0: koans[current]['hint'].append(line) else: koans[current]['code'].append(line) for koan in koans.values(): koan['hint'] = "".join(koan['hint']) koan['code'] = "".join(koan['code']) ks = [ Koan(k['slug'], k['name'], k['hint'], k['code']) for k in koans.values() ] m = Meditation(slug, title) for k in ks: m.add_koan(k) return m
def create(self, meditation_slug, koan_slug): meditation = Meditation.get(meditation_slug) if not meditation: return {"detail": "Not found"}, 404 koans = filter(lambda k: k.slug == koan_slug, meditation.koans) try: koan = koans[0] except IndexError: return {"detail": "Not found"}, 404 answer = answer_parser.parse_args() # @UndefinedVariable new_answer = Answer(koan, answer['answer']) g.user.add_answer(new_answer) return {}, 201
def run(self, filepath): try: with open(filepath, 'r') as fp: source_lines = fp.readlines() except IOError: print "Could not read contents of file" return try: meditation = parse_meditation(source_lines) except Exception: print "Could not parse meditation." return if Meditation.get(meditation.name): print "Meditation `{0}` already exists.".format(meditation.slug) meditation.save()