import praw
import textwrap
import time
import codecs

wrapper = textwrap.TextWrapper()
def format_submission(submission):
    """return formatted submission and all comments as [String]"""
    text = []
    text.append(submission.title)
    text.append(get_info_str(submission))

    text = '\n'.join(text)
    text = [text] + ['\n'.join(format_comment(c)) for c in submission.comments]
    return '\n'.join(text)

def get_info_str(comsub):
    d = {}
    d['author'] = comsub.author if comsub.author else "DELETED"
    d['time'] = time.ctime(comsub.created)
    d['score'] = comsub.score
    d['ups'] = comsub.ups
    d['downs'] = comsub.downs
    d['id'] = comsub.id
    return "%(author)s %(score)d points %(time)s ago"\
    " (%(ups)d|%(downs)d) id:%(id)s" % d

def format_comment(comment, depth=0):
    """returns formatted comment + children as a [String]""" 
    indent = 2
    base_ind=4
    indent += depth * base_ind
    if isinstance(comment, praw.objects.MoreComments):
        return [' '*indent + "More..."]
    text = ['\n\n',get_comment_header(comment, indent),''] 
    text += get_comment_body(comment,indent)
    for child in comment.replies:
        text += format_comment(child, depth+1)
    return text  


def get_comment_header(comment, indent):
    """return formatted header of post"""
    return indent * '-' + get_info_str(comment)

def get_comment_body(comment, indent):
    """returns formatted body of comment as [String]"""
    indent = indent * ' '
    wrapper = textwrap.TextWrapper(initial_indent=indent, subsequent_indent=indent,
            width=79)
    return wrapper.wrap(comment.body)

def get_top_10(subreddit):
    r = praw.Reddit('test!')
    sub = r.get_subreddit(subreddit)
    return [post for post in sub.get_top(limit=10)]    

    
#testing code
if __name__=='__main__':
    r = praw.Reddit('test!')
    sub = r.get_subreddit('iama')
    posts = [post for post in sub.get_top(limit=1)]   
    with codecs.open('out.txt', mode='w',encoding='utf-8') as f:
        for post in posts:
            lines = format_submission(post) 
            f.write(lines)

@@ -235,28 +235,20 @@
 le)%0A    
-text.append(
+d = 
 get_info
@@ -248,19 +248,20 @@
 et_info_
-str
+dict
 (submiss
@@ -264,18 +264,146 @@
 mission)
-)
 %0A
+    formatted = %22%25(author)s %25(time)s ago%5Cn%22%5C%0A    +%22%25(score)d points (%25(ups)d%7C%25(downs)d) id:%25(id)s%22%0A    text.append(formatted %25 d)
 %0A    tex
@@ -539,27 +539,28 @@
 ef get_info_
-str
+dict
 (comsub):%0A  
@@ -800,51 +800,9 @@
 urn 
-%22%25(author)s %25(score)d points %25(time)s ago%22%5C
+d
 %0A   
@@ -1389,22 +1389,76 @@
 %22%22%22%0A    
-return
+formatted = indent * '-'+ %22%25(author)s %25(time)s ago%5Cn%22%5C%0A    +
  indent 
@@ -1464,12 +1464,69 @@
  * '
--
+ 
 ' +
+ %22%25(score)d points (%25(ups)d%7C%25(downs)d) id:%25(id)s%22%0A    d =
  get
@@ -1535,11 +1535,12 @@
 nfo_
-str
+dict
 (com
@@ -1544,16 +1544,42 @@
 comment)
+%0A    return formatted %25 d 
 %0A%0Adef ge

refactor out code common to subject and comments