

def form_to_json(form):
    '\n\tTakes the form from the POST request in the web interface, and generates the JSON config\tfile \n\n\t:param form: The form from the POST request\n\t:return: None\n\n\t'
    config = dict()
    if (form['project_name'] == ''):
        raise Exception('Project name cannot be empty.')
    if (form['selector_type'] not in ['css', 'xpath']):
        raise Exception('Selector type has to css or xpath')
    config['project_name'] = form['project_name']
    config['selector_type'] = form['selector_type']
    config['scraping'] = dict()
    if (form['url'] == ''):
        raise Exception('URL cannot be empty')
    config['scraping']['url'] = form['url']
    config['scraping']['data'] = list()
    for i in itertools.count(start=1):
        try:
            data = {
                'field': form[('field_' + str(i))],
                'selector': form[('selector_' + str(i))],
                'attr': form[('attribute_' + str(i))],
                'default': f[('default_' + str(i))],
            }
            config['scraping']['data'].append(data)
        except KeyError:
            break
    with open(os.path.join(os.getcwd(), (form['project_name'] + '.json')), 'w') as f:
        json.dump(config, f)
    return

dataset/ETHPy150Open AlexMathew/scrapple/scrapple/utils/form.py form_to_json/VarMisuse@203/380 `form`->`f`