build_docs.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # coding=utf-8
  2. from __future__ import print_function
  3. from __future__ import unicode_literals
  4. import os
  5. import pprint
  6. import sys
  7. if sys.version < '3':
  8. text_type = unicode
  9. binary_type = str
  10. else:
  11. text_type = str
  12. binary_type = bytes
  13. DOCS_ROOT = os.path.abspath(os.path.join('..', 'docs'))
  14. def write(fh, s):
  15. return fh.write(s.encode('utf-8'))
  16. def write_provider(fh, doc, provider, formatters, excludes=None):
  17. if excludes is None:
  18. excludes = []
  19. write(fh, '\n')
  20. title = "``{0}``".format(doc.get_provider_name(provider))
  21. write(fh, '%s\n' % title)
  22. write(fh, "-" * len(title))
  23. write(fh, '\n\n::\n')
  24. for signature, example in formatters.items():
  25. if signature in excludes:
  26. continue
  27. try:
  28. # `pprint` can't format sets of heterogenous types.
  29. if not isinstance(example, set):
  30. example = pprint.pformat(example, indent=4)
  31. lines = text_type(example).expandtabs().splitlines()
  32. except UnicodeEncodeError:
  33. msg = 'error on "{0}" with value "{1}"'.format(signature, example)
  34. raise Exception(msg)
  35. write(fh, '\n')
  36. write(fh, "\t{fake}\n{example}\n".format(
  37. fake=signature,
  38. example='\n'.join(['\t# ' + line for line in lines]),
  39. ))
  40. def write_docs(*args, **kwargs):
  41. from faker import Faker, documentor
  42. from faker.config import DEFAULT_LOCALE, AVAILABLE_LOCALES
  43. fake = Faker(locale=DEFAULT_LOCALE)
  44. from faker.providers import BaseProvider
  45. base_provider_formatters = [f for f in dir(BaseProvider)]
  46. doc = documentor.Documentor(fake)
  47. formatters = doc.get_formatters(with_args=True, with_defaults=True)
  48. for provider, fakers in formatters:
  49. provider_name = doc.get_provider_name(provider)
  50. fname = os.path.join(DOCS_ROOT, 'providers', '%s.rst' % provider_name)
  51. with open(fname, 'wb') as fh:
  52. write_provider(fh, doc, provider, fakers)
  53. with open(os.path.join(DOCS_ROOT, 'providers.rst'), 'wb') as fh:
  54. write(fh, 'Providers\n')
  55. write(fh, '=========\n')
  56. write(fh, '.. toctree::\n')
  57. write(fh, ' :maxdepth: 2\n\n')
  58. [write(fh, ' providers/%s\n' % doc.get_provider_name(provider))
  59. for provider, fakers in formatters]
  60. AVAILABLE_LOCALES = sorted(AVAILABLE_LOCALES)
  61. for lang in AVAILABLE_LOCALES:
  62. fname = os.path.join(DOCS_ROOT, 'locales', '%s.rst' % lang)
  63. with open(fname, 'wb') as fh:
  64. write(fh, '\n')
  65. title = 'Language {0}\n'.format(lang)
  66. write(fh, title)
  67. write(fh, '=' * len(title))
  68. write(fh, '\n')
  69. fake = Faker(locale=lang)
  70. d = documentor.Documentor(fake)
  71. for p, fs in d.get_formatters(with_args=True, with_defaults=True,
  72. locale=lang,
  73. excludes=base_provider_formatters):
  74. write_provider(fh, d, p, fs)
  75. with open(os.path.join(DOCS_ROOT, 'locales.rst'), 'wb') as fh:
  76. write(fh, 'Locales\n')
  77. write(fh, '=======\n')
  78. write(fh, '.. toctree::\n')
  79. write(fh, ' :maxdepth: 2\n\n')
  80. [write(fh, ' locales/%s\n' % lang) for lang in AVAILABLE_LOCALES]
  81. # wrappers for sphinx
  82. def _main(app, *args, **kwargs):
  83. return write_docs(*args, **kwargs)
  84. def setup(app):
  85. app.connect(str('builder-inited'), _main)
  86. if __name__ == "__main__":
  87. write_docs(*sys.argv[1:])