Skip to content

Commit

Permalink
added faker script. Close joke2k#65
Browse files Browse the repository at this point in the history
  • Loading branch information
fcurella committed Mar 12, 2014
1 parent 824ee58 commit fdacd92
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
9 changes: 9 additions & 0 deletions bin/faker
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python

import sys

from faker.__main__ import command


if __name__ == '__main__':
command(*sys.argv[1:])
27 changes: 26 additions & 1 deletion faker/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from __future__ import unicode_literals
from __future__ import print_function

from faker import Faker, Factory, documentor, AVAILABLE_LOCALES, DEFAULT_LOCALE

import os
import sys

if sys.version < '3':
Expand Down Expand Up @@ -40,7 +44,6 @@ def print_provider(doc, provider, formatters, excludes=None):


def main(provider_or_field=None, *args):
from faker import Faker, Factory, documentor, DEFAULT_LOCALE, AVAILABLE_LOCALES
fake = Faker(locale=DEFAULT_LOCALE)

from faker.providers import BaseProvider
Expand Down Expand Up @@ -82,6 +85,28 @@ def main(provider_or_field=None, *args):
print_provider(d, p, fs)


def command(*args):
try:
f = args[0]
except IndexError:
f = '--help'

if f in ["--help", "-h"]:
main()
sys.exit()

locale = os.environ['LANG'].split('.')[0]

if locale not in AVAILABLE_LOCALES:
locale = DEFAULT_LOCALE
fake = Faker(locale=locale)

try:
print(fake.format(f, *args[1:]))
except AttributeError:
print('No faker found for "{0}"'.format(f))


if __name__ == "__main__":
if sys.stdout.encoding is None:
print("please set python env PYTHONIOENCODING=UTF-8, example: "
Expand Down
4 changes: 4 additions & 0 deletions faker/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ def test_documentor(self):
main('faker.providers.it_IT.person')
self.assertRaises(AttributeError, self.generator.get_formatter, 'barFormatter')

def test_command(self):
from faker.__main__ import command
command('address')


if __name__ == '__main__':
unittest.main()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
version=version,
description="Faker is a Python package that generates fake data for you.",
long_description=README + '\n\n' + NEWS,
scripts=['bin/faker'],
classifiers=[
# Get strings from http://pypi.python.org/pypi?%3Aaction=list_classifiers
'Development Status :: 3 - Alpha',
Expand Down

0 comments on commit fdacd92

Please sign in to comment.