From 824ee589fbed24f4e2b1911c6b780ffefa38cd51 Mon Sep 17 00:00:00 2001 From: Flavio Curella Date: Wed, 12 Mar 2014 09:33:14 -0500 Subject: [PATCH 1/2] removed 'bin' from gitignore --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index b32b2f450a..22c4a3d37d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,6 @@ dist build eggs parts -bin var sdist develop-eggs From fdacd92df2c4246da8c37e564788728a3335b199 Mon Sep 17 00:00:00 2001 From: Flavio Curella Date: Wed, 12 Mar 2014 09:39:50 -0500 Subject: [PATCH 2/2] added faker script. Close #65 --- bin/faker | 9 +++++++++ faker/__main__.py | 27 ++++++++++++++++++++++++++- faker/tests.py | 4 ++++ setup.py | 1 + 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100755 bin/faker diff --git a/bin/faker b/bin/faker new file mode 100755 index 0000000000..6400aed327 --- /dev/null +++ b/bin/faker @@ -0,0 +1,9 @@ +#!/usr/bin/env python + +import sys + +from faker.__main__ import command + + +if __name__ == '__main__': + command(*sys.argv[1:]) diff --git a/faker/__main__.py b/faker/__main__.py index 6dd31495ab..6c62aae877 100644 --- a/faker/__main__.py +++ b/faker/__main__.py @@ -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': @@ -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 @@ -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: " diff --git a/faker/tests.py b/faker/tests.py index 36b8859151..4201e33489 100644 --- a/faker/tests.py +++ b/faker/tests.py @@ -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() diff --git a/setup.py b/setup.py index add6e5ec86..fc3d60e938 100644 --- a/setup.py +++ b/setup.py @@ -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',