<commit_before>from ConfigParser import ConfigParser
from osmapi import OsmApi
from pprint import pprint
import requests
import errortypes
import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
    "-d",
    "--dry",
    help="Do not actually make changed, only a dry run",
    action="store_true"
)
args = parser.parse_args()
if args.dry:
    print "### Dry run: ###"

config = ConfigParser()
config.read('setup.cfg')

# OSM config
osm_user = config.get('Osm', 'username')
osm_pass = config.get('Osm', 'password')
osm_api = config.get('Osm', 'api')
osm_app = config.get('Osm', 'appid')

# Kort config
kort_api = config.get('Kort', 'completed_fix_api')
db_api = config.get('Kort', 'db_api')
db_api_key = config.get('Kort', 'db_api_key')

osm = OsmApi(
    api=osm_api,
    appid=osm_app,
    username=osm_user,
    password=<PASSWORD>
)


def osm_type_get_factory(type, id):
    if type == 'node':
        return osm.NodeGet(id)
    if type == 'way':
        return osm.WayGet(id)
    if type == 'relation':
        return osm.RelationGet(id)


def osm_type_update_factory(type, new_values):
    if type == 'node':
        return osm.NodeUpdate(new_values)
    if type == 'way':
        return osm.WayUpdate(new_values)
    if type == 'relation':
        return osm.RelationUpdate(new_values)


def mark_fix(fix_id):
    table_name = 'kort.fix'
    column = 'in_osm'
    where_clause = 'fix_id = %s' % fix_id
    params = {'where': where_clause, 'key': db_api_key}
    payload = {column: 't'}

    # make request
    url = db_api + '/' + table_name + '/' + column
    r = requests.put(
        url,
        params=params,
        data=payload
    )
    if r.status_code == requests.codes.ok:
        print "Successfully marked fix as 'in_osm'"
    else:
        print "Error while marking fix as 'in_osm':"
        pprint(r.text)


# read a solution (from database or API)
r = requests.get(kort_api)
for kort_fix in r.json()[0:1]:
    try:
        print "===="
        pprint(kort_fix)

        osm_type = osm_type_get_factory(
            kort_fix['osm_type'],
            kort_fix['osm_id']
        )

        print "----"
        pprint(osm_type['tag'])

        error_type = errortypes.Error(kort_fix['error_type'], osm_type)
        fixed_osm_type, description = error_type.apply_fix(kort_fix)

        print "----"
        pprint(fixed_osm_type['tag'])
        print "===="
    except (errortypes.ErrorTypeError, ValueError), e:
        print e
        fixed_osm_type = None
    if not args.dry:<commit_msg>Add 'count' argument to define amount of fixes to process<commit_after>from ConfigParser import ConfigParser
from osmapi import OsmApi
from pprint import pprint
import requests
import errortypes
import argparse

parser = argparse.ArgumentParser()
parser.add_argument(
    "-d",
    "--dry",
    help="do not actually make changed, only a dry run",
    action="store_true"
)
parser.add_argument(
    "-c",
    "--count",
    help="count of fixes to run through from kort to OSM",
    type=int
)
args = parser.parse_args()
if args.dry:
    print "### Dry run: ###"

config = ConfigParser()
config.read('setup.cfg')

# OSM config
osm_user = config.get('Osm', 'username')
osm_pass = config.get('Osm', 'password')
osm_api = config.get('Osm', 'api')
osm_app = config.get('Osm', 'appid')

# Kort config
kort_api = config.get('Kort', 'completed_fix_api')
db_api = config.get('Kort', 'db_api')
db_api_key = config.get('Kort', 'db_api_key')

osm = OsmApi(
    api=osm_api,
    appid=osm_app,
    username=osm_user,
    password=<PASSWORD>
)


def osm_type_get_factory(type, id):
    if type == 'node':
        return osm.NodeGet(id)
    if type == 'way':
        return osm.WayGet(id)
    if type == 'relation':
        return osm.RelationGet(id)


def osm_type_update_factory(type, new_values):
    if type == 'node':
        return osm.NodeUpdate(new_values)
    if type == 'way':
        return osm.WayUpdate(new_values)
    if type == 'relation':
        return osm.RelationUpdate(new_values)


def mark_fix(fix_id):
    table_name = 'kort.fix'
    column = 'in_osm'
    where_clause = 'fix_id = %s' % fix_id
    params = {'where': where_clause, 'key': db_api_key}
    payload = {column: 't'}

    # make request
    url = db_api + '/' + table_name + '/' + column
    r = requests.put(
        url,
        params=params,
        data=payload
    )
    if r.status_code == requests.codes.ok:
        print "Successfully marked fix as 'in_osm'"
    else:
        print "Error while marking fix as 'in_osm':"
        pprint(r.text)


# read a solution (from database or API)
r = requests.get(kort_api)
limit = args.count if args.count is not None else 1
for kort_fix in r.json()[0:limit]:
    try:
        print "===="
        pprint(kort_fix)

        osm_type = osm_type_get_factory(
            kort_fix['osm_type'],
            kort_fix['osm_id']
        )

        print "----"
        pprint(osm_type['tag'])

        error_type = errortypes.Error(kort_fix['error_type'], osm_type)
        fixed_osm_type, description = error_type.apply_fix(kort_fix)

        print "----"
        pprint(fixed_osm_type['tag'])
        print "===="
    except (errortypes.ErrorTypeError, ValueError), e:
        print e
        fixed_osm_type = None
    if not args.dry:
{'fraction_non_alphanumeric': 0.13404013631200304, 'fraction_numerical': 0.0007572889057175312, 'mean_word_length': 2.881704628949302, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3247087', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1861, 'n_words': 429}