<commit_before>#!/usr/bin/env python3
"""
    Client library generator for SciGraph REST api.
"""

import requests

class State:
    def __init__(self, api_url):
        self.shebang = "#!/usr/bin/env python3\n"
        self.imports = "import requests\n\n"
        self.api_url = api_url
        self.current_path = self.api_url
        self.exten_mapping = {}
        self.paths = {}
        self.globs = {}
        self.tab = '    '
        self.gencode()

    @property
    def code(self):
        return self.make_main()

    def make_main(self):
        code = ""
        code += self.shebang
        code += self.make_doc()
        code += self.imports
        code += "exten_mapping = %s\n\n" % repr(self.exten_mapping)
        code += self.make_baseclass()
        code += self._code
        
        return code
        
    def make_doc(self):
        code = '""" Swagger Version: {swaggerVersion}, API Version: {apiVersion}\n{t}generated for {api_url}\n{t}by scigraph.py\n"""\n'
        swaggerVersion = self.globs['swaggerVersion']
        apiVersion = self.globs['apiVersion']
        return code.format(swaggerVersion=swaggerVersion, apiVersion=apiVersion, api_url=self.api_url, t=self.tab)

    def make_baseclass(self):
        code = (
            'class restService:\n'
            '{t}""" Base class for SciGraph rest services. """\n\n'
            '{t}def _get(self, method, url):\n'
            '{t}{t}print(url)\n'
            '{t}{t}s = requests.Session()\n'
            '{t}{t}req = requests.Request(method=method, url=url)\n'
            '{t}{t}prep = req.prepare()\n'
            '{t}{t}resp = s.send(prep)\n'
            '{t}{t}return resp\n'
            '\n'
        )
        return code.format(t=self.tab)

    def make_class(self, dict_):
        code = (
            '\n'
            'class {classname}(restService):\n'
            '{t}""" {docstring} """\n\n'
            '{t}def __init__(self, basePath=\'{basePath}\'):\n'
            '{t}{t}self.basePath = basePath\n\n'
        )
        classname = dict_['resourcePath'].strip('/').capitalize()
        docstring = dict_['docstring']
        _, basePath = self.basePath_(dict_['basePath'])
        return code.format(classname=classname, docstring=docstring, basePath=basePath, t=self.tab)

    def make_param_parts(self, dict_):
        if dict_['required']:
            param_args = '{name}'
            param_args = param_args.format(name=dict_['name'])
        else:
            param_args = "{name}='{defaultValue}'"
            param_args = param_args.format(name=dict_['name'], defaultValue=dict_.get('defaultValue',''))

        param_rest = '{name}'
        param_doc = '{t}{t}{t}{name}: {description}'

        param_rest = param_rest.format(name=dict_['name'])
        desc = dict_.get('description','')
        if len(desc) > 60:
            tmp = desc.split(' ')
            part = len(desc) // 60
            size = len(tmp) // part
            lines = []
            for i in range(part + 1):
                lines.append(' '.join(tmp[i*size:(i+1) * size]))
            desc = '\n{t}{t}{t}'.format(t=self.tab).join([l for l in lines if l])
        param_doc = param_doc.format(name=dict_['name'], description=desc, t=self.tab)
    
        return param_args, param_rest, param_doc

    def make_params(self, list_):
        pargs, prests, pdocs = [], [], []
        for param in list_:
            parg, prest, pdoc = self.make_param_parts(param)
            pargs.append(parg)
            prests.append(prest)
            pdocs.append(pdoc)

        if pargs:
            pargs = ', '.join(pargs) + ', '  # if there are args add a comma
        else:
            pargs = ''
        if prests:
            prests = '?' + '&'.join([pr + '={%s}'%pr for pr in prests]) + "'.format(%s)" % ', '.join([pr + '=' + pr for pr in prests]) 
        else:
            prests = "'"
        pdocs = '\n'.join(pdocs)
        return pargs, prests, pdocs

    def apiVersion(self, value):
        self.globs['apiVersion'] = value
        return None, ''

    def swaggerVersion(self, value):
        self.globs['swaggerVersion'] = value
        return None, ''

    def operation(self, api_dict):
        code = (
            '{t}def {nickname}(self, {params}output=\'application/json\'):\n'
            '{t}{t}""" {docstring}\n{t}{t}"""\n\n'
            '{t}{t}url = self.basePath + \'{path}{param_rest}\n'
            '{t}{t}return self._get(\'{method}\', url)\n'
        )


        params, param_rest, param_docs = self.make_params(api_dict['parameters'])
        nickname = api_dict['nickname']
        path = self.paths[nickname]
        docstring = api_dict['summary'] + ' from: ' + path + '\n\n{t}{t}{t}Arguments:\n'.format(t=self.tab) + param_docs
        method = api_dict['method']
        if len(path.split('{')) > 1:
            if '&' in param_rest:  # FIXME this is obscure and could be explicit at a lower level
                param_rest = '?' + param_rest.split('&',1)[1]  # remove required args from rest args
            else:
                param_rest = ''
        formatted = code.format(path=path, nickname=nickname, params=params, param_rest=param_rest, method=method, docstring=docstring, t=self.tab)
        self.dodict(api_dict)  # catch any stateful things we need, but we arent generating code from it
        return formatted


    def description(self, value):
        return None, ''

    def resourcePath(self, value):
        return None, ''

    def top_path(self, extension):
        newpath = self.api_url + extension
        json = requests.get(newpath).json()
        return json

    def path(self, value):
        # if anything do substitution here
        # need something extra here?
        return None, ''

    def apis(self, list_):<commit_msg>fixed bug where args only would not have propper formatting<commit_after>#!/usr/bin/env python3
"""
    Client library generator for SciGraph REST api.
"""

import requests

class State:
    def __init__(self, api_url):
        self.shebang = "#!/usr/bin/env python3\n"
        self.imports = "import requests\n\n"
        self.api_url = api_url
        self.current_path = self.api_url
        self.exten_mapping = {}
        self.paths = {}
        self.globs = {}
        self.tab = '    '
        self.gencode()

    @property
    def code(self):
        return self.make_main()

    def make_main(self):
        code = ""
        code += self.shebang
        code += self.make_doc()
        code += self.imports
        code += "exten_mapping = %s\n\n" % repr(self.exten_mapping)
        code += self.make_baseclass()
        code += self._code
        
        return code
        
    def make_doc(self):
        code = '""" Swagger Version: {swaggerVersion}, API Version: {apiVersion}\n{t}generated for {api_url}\n{t}by scigraph.py\n"""\n'
        swaggerVersion = self.globs['swaggerVersion']
        apiVersion = self.globs['apiVersion']
        return code.format(swaggerVersion=swaggerVersion, apiVersion=apiVersion, api_url=self.api_url, t=self.tab)

    def make_baseclass(self):
        code = (
            'class restService:\n'
            '{t}""" Base class for SciGraph rest services. """\n\n'
            '{t}def _get(self, method, url):\n'
            '{t}{t}print(url)\n'
            '{t}{t}s = requests.Session()\n'
            '{t}{t}req = requests.Request(method=method, url=url)\n'
            '{t}{t}prep = req.prepare()\n'
            '{t}{t}resp = s.send(prep)\n'
            '{t}{t}return resp\n'
            '\n'
        )
        return code.format(t=self.tab)

    def make_class(self, dict_):
        code = (
            '\n'
            'class {classname}(restService):\n'
            '{t}""" {docstring} """\n\n'
            '{t}def __init__(self, basePath=\'{basePath}\'):\n'
            '{t}{t}self.basePath = basePath\n\n'
        )
        classname = dict_['resourcePath'].strip('/').capitalize()
        docstring = dict_['docstring']
        _, basePath = self.basePath_(dict_['basePath'])
        return code.format(classname=classname, docstring=docstring, basePath=basePath, t=self.tab)

    def make_param_parts(self, dict_):
        if dict_['required']:
            param_args = '{name}'
            param_args = param_args.format(name=dict_['name'])
        else:
            param_args = "{name}='{defaultValue}'"
            param_args = param_args.format(name=dict_['name'], defaultValue=dict_.get('defaultValue',''))

        param_rest = '{name}'
        param_doc = '{t}{t}{t}{name}: {description}'

        param_rest = param_rest.format(name=dict_['name'])
        desc = dict_.get('description','')
        if len(desc) > 60:
            tmp = desc.split(' ')
            part = len(desc) // 60
            size = len(tmp) // part
            lines = []
            for i in range(part + 1):
                lines.append(' '.join(tmp[i*size:(i+1) * size]))
            desc = '\n{t}{t}{t}'.format(t=self.tab).join([l for l in lines if l])
        param_doc = param_doc.format(name=dict_['name'], description=desc, t=self.tab)
    
        return param_args, param_rest, param_doc

    def make_params(self, list_):
        pargs_list, prests, pdocs = [], [], []
        for param in list_:
            parg, prest, pdoc = self.make_param_parts(param)
            pargs_list.append(parg)
            prests.append(prest)
            pdocs.append(pdoc)

        if pargs_list:
            pargs = ', '.join(pargs_list) + ', '  # if there are args add a comma
        else:
            pargs = ''

        if prests and pargs_list != prests:  # if prests isn't just a copy of the arg
            prests = '?' + '&'.join([pr + '={%s}'%pr for pr in prests]) + "'.format(%s)" % ', '.join([pr + '=' + pr for pr in prests]) 
        elif pargs:
            prests = "'.format(%s)" % ', '.join([pr + '=' + pr for pr in prests]) 
        else:
            prests = "'"

        pdocs = '\n'.join(pdocs)
        return pargs, prests, pdocs

    def apiVersion(self, value):
        self.globs['apiVersion'] = value
        return None, ''

    def swaggerVersion(self, value):
        self.globs['swaggerVersion'] = value
        return None, ''

    def operation(self, api_dict):
        code = (
            '{t}def {nickname}(self, {params}output=\'application/json\'):\n'
            '{t}{t}""" {docstring}\n{t}{t}"""\n\n'
            '{t}{t}url = self.basePath + \'{path}{param_rest}\n'
            '{t}{t}return self._get(\'{method}\', url)\n'
        )


        params, param_rest, param_docs = self.make_params(api_dict['parameters'])
        nickname = api_dict['nickname']
        path = self.paths[nickname]
        docstring = api_dict['summary'] + ' from: ' + path + '\n\n{t}{t}{t}Arguments:\n'.format(t=self.tab) + param_docs
        method = api_dict['method']
        if len(path.split('{')) > 1:
            if '&' in param_rest:  # FIXME this is obscure and could be explicit at a lower level
                param_rest = '?' + param_rest.split('&',1)[1]  # remove required args from rest args
                
        formatted = code.format(path=path, nickname=nickname, params=params, param_rest=param_rest, method=method, docstring=docstring, t=self.tab)
        self.dodict(api_dict)  # catch any stateful things we need, but we arent generating code from it
        return formatted


    def description(self, value):
        return None, ''

    def resourcePath(self, value):
        return None, ''

    def top_path(self, extension):
        newpath = self.api_url + extension
        json = requests.get(newpath).json()
        return json

    def path(self, value):
        # if anything do substitution here
        # need something extra here?
        return None, ''

    def apis(self, list_):
{'fraction_non_alphanumeric': 0.1536956706642851, 'fraction_numerical': 0.0018712256528025857, 'mean_word_length': 2.1312916111850866, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5671124', 'n_tokens_mistral': 3795, 'n_tokens_neox': 3737, 'n_words': 792}