Commits

Michele Orrù committed 0be1c73

using optparse

Comments (0)

Files changed (1)

 
 if __name__ == '__main__':
     import optparse
+    import os
 
     optparser = OptionParser(usage='usage: %(prog) [-x] *-meta-history.xml',
                        description=('A simple tool for extractiong and plotting'
                                    ' fations in wikipedia talks'))
     # add optionparser argument
-    optparser.add_option('-i', dest='xmlfile', default='foo.xml',
-                         help='Extract data from an xml file')
-    optparser.add_option('-o', dest='output', default='foo.csv')
     optparser.add_option('-x', dest='extract_data', action='store_true',
                          help='Extract or graphize data')
+    optparser.add_option('-v', dest='verbose', action='store_false',
+                         help='Verbose information during processing')
 
     opts, files = optparser.parse_args()
 
-    opts.loglevel = logging.DEBUG
-    # XXX: uncomment
-    #if len(files) != 2:
-    #    optp.error('Invalid file arguments. Use --help option for details')
-    #else:
-    #    optp, metahist = files
-
-    outcsv = 'foo.csv'
-    metahist = 'foo.xml'
+    # set loglevel
+    if opts.verbose:
+        loglevel = logging.DEBUG
+    else:
+        loglevel = logging.ERROR
     # start logging configuration
-    logging.basicConfig(stream=sys.stderr, level=opts.loglevel,
+    logging.basicConfig(stream=sys.stderr, level=loglevel,
                         format='%(levelname)s - %(message)s')
 
-    # start processing
-    with open(metahist, 'r') as src:
-        tag = get_tags(src,
-                       tags='page,redirect,title,text,contributor,username,ip')
-        with open(outcsv, 'a') as out:
-            processor = TalkProcessor(output=out, tag=tag)
-            processor.start(src)
+    if opts.extract_data:
+        if len(files) != 2:
+            optparser.error('Invalid file arguments. Use --help option for details')
+        else:
+            xmlfile, outcsv = files
 
-    # plot!
-    # plot(file)
+        # if the csv file exists, remove it!
+        if os.path.exists(outcsv):
+            os.remove(outcsv)
+
+        # start processing
+        with open(xmlfile, 'r') as src:
+            tag = get_tags(src,
+                           tags='page,redirect,title,text,contributor,username,ip')
+            with open(outcsv, 'a') as out:
+                processor = TalkProcessor(output=out, tag=tag)
+                processor.start(src)
+    else:
+        if len(files) != 1:
+            optparser.error('Invalid file arguments. Use --help option for'
+                            'details')
+        else:
+            outgraph, = files
+
+        # plot!
+        # plot(file)