Exemple #1
0
def compile_init_py():
    import codecs
    import os
    import sys
    current_path = os.path.dirname(__file__)
    current_path_appended = False
    if current_path not in sys.path:
        sys.path.append(current_path)
        current_path_appended = True
    from coolname.loader import load_config
    if current_path_appended:
        sys.path.remove(current_path)
    config_path = os.path.join(current_path, 'coolname', 'data')
    config = load_config(config_path)

    # Eliminate u'' literals if setup.py is executed from Python 2
    def _to_str(obj):
        if isinstance(obj, dict):
            return {str(x): _to_str(y) for x, y in obj.items()}
        elif isinstance(obj, list):
            return [_to_str(x) for x in obj]
        elif isinstance(obj, tuple):
            return tuple(str(x) for x in obj)
        elif obj.__class__.__name__ == 'unicode':
            return str(obj)
        else:
            return obj

    config = _to_str(config)
    # Write to data/__init__.py to be used from .egg
    with codecs.open(os.path.join(config_path, '__init__.py'),
                     'w',
                     encoding='utf-8') as file:
        file.write(_INIT_TEMPLATE.format(config))
Exemple #2
0
def compile_init_py():
    import codecs
    import os
    import sys
    print(sys.argv[1])
    current_path = os.path.dirname(__file__)
    current_path_appended = False
    if current_path not in sys.path:
        sys.path.append(current_path)
        current_path_appended = True
    from coolname.loader import load_config
    if current_path_appended:
        sys.path.remove(current_path)
    config_path = os.path.join(current_path, 'coolname', 'data')
    config = load_config(config_path)
    # Eliminate u'' literals if setup.py is executed from Python 2
    def _to_str(obj):
        if isinstance(obj, dict):
            return {str(x): _to_str(y) for x, y in obj.items()}
        elif isinstance(obj, list):
            return [str(x) for x in obj]
        else:
            return str(obj)
    config = _to_str(config)
    # Write to data/__init__.py to be used from .egg
    with codecs.open(os.path.join(config_path, '__init__.py'), 'w', encoding='utf-8') as file:
        file.write(_INIT_TEMPLATE.format(config))
 def test_create_from_file(self, load_config_mock, *args):
     load_config_mock.return_value = {
         'all': {
             'type': 'cartesian',
             'lists': ['number', 'number']
         },
         'number': {
             'type': 'words',
             'words': [str(x) for x in range(0, 10)]
         }
     }
     generator = RandomGenerator(load_config('dummy'))
     with patch.object(generator, '_randrange', return_value=35):
         self.assertEqual(generator.generate_slug(), '3-5')
 def test_create_from_file(self, load_config_mock, *args):
     load_config_mock.return_value = {
         'all': {
             'type': 'cartesian',
             'lists': ['number', 'number']
         },
         'number': {
             'type': 'words',
             'words': [str(x) for x in range(0, 10)]
         }
     }
     generator = RandomNameGenerator(load_config('dummy'))
     with patch('coolname.impl.randrange', return_value=35):
         self.assertEqual(generator.generate_slug(), '3-5')
Exemple #5
0
def _create_default_generator():
    data_dir = os.getenv('COOLNAME_DATA_DIR')
    data_module = os.getenv('COOLNAME_DATA_MODULE')
    if not data_dir and not data_module:
        data_dir = op.join(op.dirname(op.abspath(__file__)), 'data')
        data_module = 'coolname.data'  # used when imported from egg; consumes more memory
    if data_dir and op.isdir(data_dir):
        from coolname.loader import load_config
        config = load_config(data_dir)
    elif data_module:
        import importlib
        config = importlib.import_module(data_module).config
    else:
        raise ImportError(
            'Configure valid COOLNAME_DATA_DIR and/or COOLNAME_DATA_MODULE')
    config['all']['__nocheck'] = True
    return RandomGenerator(config)
 def test_create_from_directory_conflict(self, load_data_mock, *args):
     load_data_mock.return_value = (
         {
             'all': {
                 'type': 'cartesian',
                 'lists': ['mywords']
             },
             'mywords': {
                 'type': 'words',
                 'words': ['this', 'is', 'a', 'conflict']
             }
         },
         {'mywords': ['a', 'b']})
     with self.assertRaisesRegex(InitializationError,
                                 r"^Conflict: list 'mywords' is defined both in config "
                                 "and in \*\.txt file. If it's a 'words' list, "
                                 "you should remove it from config\.$"):
         RandomNameGenerator(load_config('dummy'))
 def test_create_from_directory_conflict(self, load_data_mock, *args):
     load_data_mock.return_value = ({
         'all': {
             'type': 'cartesian',
             'lists': ['mywords']
         },
         'mywords': {
             'type': 'words',
             'words': ['this', 'is', 'a', 'conflict']
         }
     }, {
         'mywords': ['a', 'b']
     })
     with self.assertRaisesRegex(
             InitializationError,
             r"^Conflict: list 'mywords' is defined both in config "
             "and in \*\.txt file. If it's a 'words' list, "
             "you should remove it from config\.$"):
         RandomGenerator(load_config('dummy'))
 def test_create_from_file_not_found(self, *args):
     with self.assertRaisesRegex(InitializationError,
                                 r'File or directory not found: .*dummy'):
         RandomGenerator(load_config('dummy'))
 def test_create_from_file_not_found(self, *args):
     with self.assertRaisesRegex(InitializationError,
                                 r'File or directory not found: .*dummy'):
         RandomNameGenerator(load_config('dummy'))