4444from textwrap import indent
4545
4646
47- class ColorFallback () :
48- __getattr__ = lambda self , name : '' # noqa: E731
47+ class ColorFallback :
48+ __getattr__ = lambda self , name : "" # noqa: E731
4949
5050
5151try :
5252 from colorama import Fore , Style , init
53+
5354 init () # needed for Windows
5455except ImportError : # fallback so that the imported classes always exist
5556 Fore = Style = ColorFallback ()
5657
5758
58- Comment = namedtuple ('Comment' , ('uri' , 'id' , 'parent' , 'created' , 'text' , 'author' , 'email' , 'website' , 'likes' , 'dislikes' , 'replies' ))
59+ Comment = namedtuple (
60+ "Comment" ,
61+ (
62+ "uri" ,
63+ "id" ,
64+ "parent" ,
65+ "created" ,
66+ "text" ,
67+ "author" ,
68+ "email" ,
69+ "website" ,
70+ "likes" ,
71+ "dislikes" ,
72+ "replies" ,
73+ ),
74+ )
5975
60- INDENT = ' '
61- QUERY = ' SELECT uri, comments.id, parent, created, text, author, email, website, likes, dislikes FROM comments INNER JOIN threads on comments.tid = threads.id'
76+ INDENT = " "
77+ QUERY = " SELECT uri, comments.id, parent, created, text, author, email, website, likes, dislikes FROM comments INNER JOIN threads on comments.tid = threads.id"
6278
6379
6480def main ():
@@ -78,14 +94,18 @@ def main():
7894 for comment in comments :
7995 if comment .parent : # == this is a "reply" comment
8096 comments_per_id [comment .parent ].replies .append (comment )
81- if args .sort_by_last_reply and (sort_date is None or comment .created > sort_date ):
97+ if args .sort_by_last_reply and (
98+ sort_date is None or comment .created > sort_date
99+ ):
82100 sort_date = comment .created
83101 else :
84102 root_comments .append (comment )
85103 if sort_date is None or comment .created > sort_date :
86104 sort_date = comment .created
87105 root_comments_per_sort_date [sort_date ] = root_comments
88- for _ , root_comments in sorted (root_comments_per_sort_date .items (), key = lambda pair : pair [0 ]):
106+ for _ , root_comments in sorted (
107+ root_comments_per_sort_date .items (), key = lambda pair : pair [0 ]
108+ ):
89109 print (Fore .MAGENTA + args .url_prefix + root_comments [0 ].uri + Fore .RESET )
90110 for comment in root_comments :
91111 print_comment (INDENT , comment )
@@ -95,34 +115,55 @@ def main():
95115
96116
97117def print_comment (prefix , comment ):
98- author = comment .author or ' Anonymous'
99- email = comment .email or ''
100- website = comment .website or ''
118+ author = comment .author or " Anonymous"
119+ email = comment .email or ""
120+ website = comment .website or ""
101121 when = date .fromtimestamp (comment .created )
102- popularity = ''
122+ popularity = ""
103123 if comment .likes :
104- popularity = ' +{.likes}' .format (comment )
124+ popularity = " +{.likes}" .format (comment )
105125 if comment .dislikes :
106126 if popularity :
107- popularity += '/'
108- popularity = '-{.dislikes}' .format (comment )
109- print (prefix + '{Style.BRIGHT}{author}{Style.RESET_ALL} {Style.DIM}- {email} {website}{Style.RESET_ALL} {when} {Style.DIM}{popularity}{Style.RESET_ALL}' .format (Style = Style , ** locals ()))
127+ popularity += "/"
128+ popularity = "-{.dislikes}" .format (comment )
129+ print (
130+ prefix
131+ + "{Style.BRIGHT}{author}{Style.RESET_ALL} {Style.DIM}- {email} {website}{Style.RESET_ALL} {when} {Style.DIM}{popularity}{Style.RESET_ALL}" .format (
132+ Style = Style , ** locals ()
133+ )
134+ )
110135 print (indent (comment .text , prefix ))
111136
112137
113138def parse_args ():
114- parser = argparse .ArgumentParser (description = 'Dump all Isso comments in chronological order, grouped by replies' ,
115- formatter_class = ArgparseHelpFormatter )
116- parser .add_argument ('db_path' , help = 'File path to Isso Sqlite DB' )
117- parser .add_argument ('--sort-by-last-reply' , action = 'store_true' , help = 'By default comments are sorted by "parent" comment date, this sort comments based on the last replies' )
118- parser .add_argument ('--url-prefix' , default = '' , help = 'Optional domain name to prefix to pages URLs' )
119- parser .add_argument ('--no-colors' , action = 'store_false' , dest = 'colors' , default = True , help = 'Disabled colored output' )
139+ parser = argparse .ArgumentParser (
140+ description = "Dump all Isso comments in chronological order, grouped by replies" ,
141+ formatter_class = ArgparseHelpFormatter ,
142+ )
143+ parser .add_argument ("db_path" , help = "File path to Isso Sqlite DB" )
144+ parser .add_argument (
145+ "--sort-by-last-reply" ,
146+ action = "store_true" ,
147+ help = 'By default comments are sorted by "parent" comment date, this sort comments based on the last replies' ,
148+ )
149+ parser .add_argument (
150+ "--url-prefix" , default = "" , help = "Optional domain name to prefix to pages URLs"
151+ )
152+ parser .add_argument (
153+ "--no-colors" ,
154+ action = "store_false" ,
155+ dest = "colors" ,
156+ default = True ,
157+ help = "Disabled colored output" ,
158+ )
120159 return parser .parse_args ()
121160
122161
123- class ArgparseHelpFormatter (argparse .RawTextHelpFormatter , argparse .ArgumentDefaultsHelpFormatter ):
162+ class ArgparseHelpFormatter (
163+ argparse .RawTextHelpFormatter , argparse .ArgumentDefaultsHelpFormatter
164+ ):
124165 pass
125166
126167
127- if __name__ == ' __main__' :
168+ if __name__ == " __main__" :
128169 main ()
0 commit comments