<commit_before>#!/usr/bin/env python
"""
This script generates documentation files that get used when building the
libsaas API documentation.

It crawls docstrings from modules in the libsaas.services package.
"""
import inspect
import os
import pkgutil
import sys

sys.path.insert(0, os.path.abspath('..'))

from libsaas import services


def _title(title, char):
    return '\n{0}\n{1}\n\n'.format(title, char * len(title))


def method_path(resource, method):
    return '.'.join((resource.__module__,
                     resource.__name__,
                     method.__name__))


def walk_resource(resource, rst):
    for child_name in resource.list_resources():
        method = getattr(resource, child_name)
        path = method_path(resource, method)
        rst.write(_title(', '.join([p.__name__ for p in method.produces]), '-'))
        rst.write('.. automethod:: {0}\n'.format(path))

        for child_resource in method.produces:
            walk_resource(child_resource, rst)

    for child_name in resource.list_methods():
        method = getattr(resource, child_name)
        path = method_path(resource, method)
        rst.write('.. automethod:: {0}\n'.format(path))


def process_package(importer, modname):
    module = importer.find_module(modname).load_module(modname)
    klass = inspect.getmembers(module, inspect.isclass)[0][1]

    rst = open(os.path.join('generated', '{0}.rst'.format(modname)), 'w')

    rst.write(_title(klass.__name__, '='))<commit_msg>doc: take care to only crawl Resources when generating documentation<commit_after>#!/usr/bin/env python
"""
This script generates documentation files that get used when building the
libsaas API documentation.

It crawls docstrings from modules in the libsaas.services package.
"""
import inspect
import os
import pkgutil
import sys

sys.path.insert(0, os.path.abspath('..'))

from libsaas import services
from libsaas.services import base


def _title(title, char):
    return '\n{0}\n{1}\n\n'.format(title, char * len(title))


def method_path(resource, method):
    return '.'.join((resource.__module__,
                     resource.__name__,
                     method.__name__))


def walk_resource(resource, rst):
    for child_name in resource.list_resources():
        method = getattr(resource, child_name)
        path = method_path(resource, method)
        rst.write(_title(', '.join([p.__name__ for p in method.produces]), '-'))
        rst.write('.. automethod:: {0}\n'.format(path))

        for child_resource in method.produces:
            walk_resource(child_resource, rst)

    for child_name in resource.list_methods():
        method = getattr(resource, child_name)
        path = method_path(resource, method)
        rst.write('.. automethod:: {0}\n'.format(path))


def is_resource(klass):
    return inspect.isclass(klass) and issubclass(klass, base.Resource)


def process_package(importer, modname):
    module = importer.find_module(modname).load_module(modname)
    klass = inspect.getmembers(module, is_resource)[0][1]

    rst = open(os.path.join('generated', '{0}.rst'.format(modname)), 'w')

    rst.write(_title(klass.__name__, '='))
{'fraction_non_alphanumeric': 0.1314955640050697, 'fraction_numerical': 0.005069708491761723, 'mean_word_length': 3.5424460431654676, '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': '3666371', 'n_tokens_mistral': 973, 'n_tokens_neox': 941, 'n_words': 234}