view collect_issues @ 2:3e9f4a6803d1

replaced funtion with string.isdigit.
author sean
date Wed, 15 Apr 2015 11:40:28 +0200
parents 3f139db894f1
children
line wrap: on
line source
#!/usr/bin/env python3

""" Fetch issues from a roundup-tracker and save them in a databse.

author: Sascha L. Teichmann <sascha.teichmann@intevation.de>
author: Bernhard Reiter <bernhard@intevation.de>
author: Sean Engelhardt <sean.engelhardt@intevation.de>

(c) 2010,2015 by Intevation GmbH

This is Free Software unter the terms of the
GNU GENERAL PUBLIC LICENSE Version 3 or later.
See http://www.gnu.org/licenses/gpl-3.0.txt for details
"""

import http.cookiejar
import urllib.parse
import urllib.request
import csv
import io
import sqlite3 as db
import os
import roundup_content_data as rcd


# Types of URLS
BASE_URL = "http://10.42.7.199:8917/demo/"
SEARCH_URL = "issue?@action=export_csv&@columns=title,priority&@filter=status&@pagesize=50&@startwith=0&status=-1,1,2,3,4,5,6,7"
REPORT_URL = BASE_URL + SEARCH_URL


LOGIN_PARAMETERS = (
    ("__login_name", "demo"),
    ("__login_password", "demo"),
    ("@action", "Login"),
    )


# SQL-COMMANDS


def connect_to_server():
    enc_data = urllib.parse.urlencode(LOGIN_PARAMETERS).encode()
    cj = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
    req = urllib.request.Request(url=BASE_URL, data=enc_data)
    opener.open(req)
    return opener


def get_issues_as_csv(opener):
    csv_req = urllib.request.Request(url=REPORT_URL)
    f = opener.open(csv_req)

    reader = csv.DictReader(io.TextIOWrapper(f))

    return reader


def check_create_database(database_file):
    if not os.path.isfile(database_file):
        con = None
        cur = None
        try:
            con = db.connect(database_file)
            cur = con.cursor()
            try:
                cur.execute(rcd.CREATE_DB)
                con.commit()
                os.chmod(rcd.DATABASE_FILE, 0o744)
            except:
                con.rollback()
                raise
        finally:
            if cur:
                cur.close()
            if con:
                con.close()


def issues_to_quantities(rows):
    quantities = [0] * len(rcd.COLUMNS)
    for row in rows:
        quantities[int(row["priority"]) - 1] += 1
    return quantities


def save_issues_to_db(quantities, database_file):
    check_create_database(database_file)

    cur = None
    con = None

    try:
        con = db.connect(database_file)
        cur = con.cursor()
        try:
            cur.execute(rcd.INSERT_NEW, quantities)
            con.commit()
        except:
            con.rollback()
            raise
    finally:
        if cur:
            cur.close()
        if con:
            con.close()


def save_stats_in_db():
    try:
        opener = connect_to_server()
        current_issues_csv = get_issues_as_csv(opener)
        opener.close()

        quantities = issues_to_quantities(current_issues_csv)

        save_issues_to_db(quantities, rcd.DATABASE_FILE)
    except urllib.error.URLError:
        print("No Valid Connection to server : " + BASE_URL)


save_stats_in_db()
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)