Commits

Brian Curtin committed bd74615

Rearrange a few things and make CSV the output type.

Comments (0)

Files changed (1)

 # at the top of the list anyway. Only check the first 1 MB of the file.
 MAX_SIZE = 1024 * 1000 # 1 MB
 
+VERSIONS = ("1.5", "2.0", "2.1", "2.2", "2.3", "2.4",
+            "2.5", "2.6", "2.7", "3.0", "3.1", "3.2")
+
 pattern = ("^(?P<count>\d*)" # Hit count
            ".*" # A bunch of percentages we don't care about
            "/ftp/python//?" # The start of the download path
     return None
 
 def main():
+    # TODO: Add argparse handling of different options.
+    csv(get_stats())
+
+def get_stats():
     # Database starts at 200601, and we want to go through the last full month
     year, month = 2006, 1
     dates = []
         os.mkdir(CACHE_DIR)
     except WindowsError:
         pass
-
+    
+    all_stats = {}
     for date in dates:
         cached_file = os.path.join(CACHE_DIR, "url_{}.html".format(date))
         if os.path.exists(cached_file):
                     val = file.read(size)
                     if not val:
                         break
-                    #yield val.decode("latin1", "replace")
                     yield val
                     bytes_read += len(val)
                     if bytes_read > MAX_SIZE:
         end -= len("</PRE>")
 
         major_stats = {}
+        for release in VERSIONS:
+            major_stats[release] = 0
+
         for line in text[start:end].split("\n"):
             stats = parse(line)
             if not stats:
                 continue
 
-            majorminor = "{}.{}".format(stats.version.major, stats.version.minor)
-            if majorminor == "None.None":
-                print(line)
+            majorminor = "{}.{}".format(stats.version.major,
+                                        stats.version.minor)
             if not majorminor in major_stats:
                 major_stats[majorminor] = 0
             major_stats[majorminor] += stats.hits
 
-        print(date)
-        print(major_stats)
-        print("*"*50)
+        all_stats[date] = major_stats
+
+    return all_stats
+
+
+def csv(all_stats, header=True):
+    if header:
+        print("Month", end="")
+        for version in VERSIONS:
+            print(", {}".format(version), end="")
+        print()
+
+    dates = sorted(all_stats.keys())
+    for date in dates:
+        stats = all_stats[date]
+        print("{}".format(date), end="")
+        for key in sorted(stats.keys()):
+            print(", {}".format(stats[key]), end="")
+        print("")
 
 if __name__ == "__main__":
     try:
         exit(main())
     except KeyboardInterrupt:
         pass
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.