|
1 # UploadFormat Parser -- test version |
|
2 # -- ideamonk |
|
3 |
|
4 class Submission(): |
|
5 """ |
|
6 A helper class for bulk uploads. It parses the given chunk of bulk |
|
7 upload and tries to submit them to PyKata |
|
8 """ |
|
9 def __init__(self, bulk_text): |
|
10 # initialize everything as default/blank as some things might not be |
|
11 # present in submission |
|
12 self.data_dict = dict() |
|
13 self.bulk_text = bulk_text |
|
14 self.multiline_fields = ('description', 'examples') |
|
15 self.data_dict['user_id'] = '' |
|
16 |
|
17 def __getitem__(self, key): |
|
18 try: |
|
19 return self.data_dict[key] |
|
20 except : |
|
21 return None |
|
22 |
|
23 def __setitem__(self, key, item): |
|
24 # mapping to match form submission for code reuse |
|
25 if key=='pubTests': |
|
26 key = 'tests' |
|
27 if key=='privTests': |
|
28 key='other_tests' |
|
29 if key=='userID': |
|
30 key='user_id' |
|
31 |
|
32 self.data_dict[key] = item |
|
33 |
|
34 def __str__(self): |
|
35 return str(self.bulk_text) |
|
36 |
|
37 def parse(self): |
|
38 """ Parses a problem submission into data fillable in the submission |
|
39 form |
|
40 """ |
|
41 chunks = self.bulk_text.split('#-') |
|
42 for chunk in chunks[1:]: |
|
43 # ^^ ignores the first comment line of the problem |
|
44 chunk_key, chunk_data = chunk.split(':',1) |
|
45 chunk_key = chunk_key.strip() |
|
46 if chunk_key in self.multiline_fields: |
|
47 # these are data surrounded in triple quotes, single or double |
|
48 chunk_data = eval(chunk_data).strip('\r\n') |
|
49 else: |
|
50 chunk_data = chunk_data.strip().strip('\r\n') |
|
51 self[chunk_key] = chunk_data |
|
52 |
|
53 def talk(self): |
|
54 """ Just a testing function, a submission talks about itself to everyone |
|
55 """ |
|
56 print "Hi I am a submission problem on PyKata," |
|
57 print "Let me tell you something about myself - " |
|
58 print "I was created by -", self['userID'] |
|
59 print "My name is -", self['name'] |
|
60 print "I belong to these categories -", self['categories'] |
|
61 print "Here, something more about myself --" |
|
62 print self['description'] |
|
63 print "---------------------------------------" |
|
64 print "My solution --" |
|
65 print self['solution'] |
|
66 print "----------------------------------------" |
|
67 print "My skeleton --" |
|
68 print self['skeleton'] |
|
69 print "----------------------------------------" |
|
70 print "Public Tests --" |
|
71 print self['pubTests'] |
|
72 print "----------------------------------------" |
|
73 print "Private Tests --" |
|
74 print self['privTests'] |
|
75 print "----------------------------------------" |
|
76 print "\n So tell me something about you too?" |
|
77 |
|
78 |
|
79 def test_submission_parse(): |
|
80 import urllib2 |
|
81 # example_file = urllib2.urlopen('http://ideamonk.in/code/foo.py').read() |
|
82 example_file = open('/home/amit/foo2.py').read() |
|
83 example_submissions = example_file.split('#---')[1:-1] |
|
84 # ^^ ignore the first null string and last __main__ test code |
|
85 print example_submissions |
|
86 example_submissions = [ Submission(x) for x in example_submissions ] |
|
87 |
|
88 for key, a_submission in enumerate(example_submissions): |
|
89 print "Problem #", key, "parsed as --" |
|
90 print "*************************************************************" |
|
91 a_submission.parse() |
|
92 a_submission.talk() |
|
93 |
|
94 if __name__=='__main__': |
|
95 # testing |
|
96 test_submission_parse() |
|
97 |