<commit_before>import glob
import json
import os

import yaml

from oonib.handlers import OONIBHandler

from oonib import config

class InputDescHandler(OONIBHandler):
    def get(self, inputID):
        #XXX return the input descriptor
        # see oonib.md in ooni-spec
        bn = os.path.basename(inputID) + ".desc"
        try:
            f = open(os.path.join(config.main.input_dir, bn))
            a = {}
            inputDesc = yaml.safe_load(f)
            a['id'] = inputID
            a['name'] = inputDesc['name']
            a['description'] = inputDesc['description']
            self.write(json.dumps(a))
        except Exception:
            log.err("No Input Descriptor found for id %s" % inputID) 

class InputListHandler(OONIBHandler):
    def get(self):
        path = os.path.abspath(config.main.input_dir) + "/*.desc"
        inputnames = map(os.path.basename, glob.iglob(path))
        inputList = []
        for inputname in inputnames:
            f = open(os.path.join(config.main.input_dir, inputname))
            d = yaml.safe_load(f)
            inputList.append({
                'id': inputname,
                'name': d['name'],
                'description': d['description']
            })
            f.close()
        self.write(json.dumps(inputList))
<commit_msg>Implement input API as spec'd in oonib.md<commit_after>import glob
import json
import os
import yaml

from oonib.handlers import OONIBHandler
from oonib import config, log

class InputDescHandler(OONIBHandler):
    def get(self, inputID):
        bn = os.path.basename(inputID) + ".desc"
        try:
            f = open(os.path.join(config.main.input_dir, bn))
            a = {}
            inputDesc = yaml.safe_load(f)
            for k in ['name', 'description', 'version', 'author', 'date']:
                a[k] = inputDesc[k]
            self.write(json.dumps(a))
        except IOError:
            log.err("No Input Descriptor found for id %s" % inputID) 
        except Exception, e:
            log.err("Invalid Input Descriptor found for id %s" % inputID) 

class InputListHandler(OONIBHandler):
    def get(self):
        path = os.path.abspath(config.main.input_dir) + "/*.desc"
        inputnames = map(os.path.basename, glob.iglob(path))
        inputList = []
        for inputname in inputnames:
            f = open(os.path.join(config.main.input_dir, inputname))
            d = yaml.safe_load(f)
            inputList.append({
                'id': inputname,
                'name': d['name'],
                'description': d['description']
            })
            f.close()
        self.write(json.dumps(inputList))

{'fraction_non_alphanumeric': 0.12184081478687288, 'fraction_numerical': 0.0, 'mean_word_length': 2.023945267958951, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5796049', 'n_tokens_mistral': 826, 'n_tokens_neox': 806, 'n_words': 185}