Example #1
0
 def test_creating_existing_file(self):
     file_path = os.path.join(self.parent, b'test')
     path.get_or_create_file(file_path)
     created = path.get_or_create_file(file_path)
     self.assert_(os.path.exists(file_path))
     self.assert_(os.path.isfile(file_path))
     self.assertEqual(created, file_path)
Example #2
0
 def test_creating_existing_file(self):
     file_path = os.path.join(self.parent, b'test')
     path.get_or_create_file(file_path)
     created = path.get_or_create_file(file_path)
     self.assert_(os.path.exists(file_path))
     self.assert_(os.path.isfile(file_path))
     self.assertEqual(created, file_path)
Example #3
0
    def test_creating_existing_file(self):
        file_path = self.parent / "test"
        path.get_or_create_file(str(file_path))

        created = path.get_or_create_file(str(file_path))

        assert file_path.is_file()
        assert created == file_path
Example #4
0
def create_initial_config_file(args, extensions_data):
    """Initialize whatever the last config file is with defaults"""

    config_file = args.config_files[-1]

    if os.path.exists(path.expand_path(config_file)):
        return

    try:
        default = config_lib.format_initial(extensions_data)
        path.get_or_create_file(config_file, mkdir=False, content=default)
        logger.info("Initialized %s with default config", config_file)
    except IOError as error:
        logger.warning("Unable to initialize %s with default config: %s", config_file, encoding.locale_decode(error))
Example #5
0
def create_initial_config_file(args, extensions_data):
    """Initialize whatever the last config file is with defaults"""

    config_file = args.config_files[-1]

    if os.path.exists(path.expand_path(config_file)):
        return

    try:
        default = config_lib.format_initial(extensions_data)
        path.get_or_create_file(config_file, mkdir=False, content=default)
        logger.info('Initialized %s with default config', config_file)
    except IOError as error:
        logger.warning('Unable to initialize %s with default config: %s',
                       config_file, encoding.locale_decode(error))
Example #6
0
def create_file_structures_and_config(args, extensions_data):
    path.get_or_create_dir(b'$XDG_DATA_DIR/mopidy')
    path.get_or_create_dir(b'$XDG_CONFIG_DIR/mopidy')

    # Initialize whatever the last config file is with defaults
    config_file = args.config_files[-1]
    if os.path.exists(path.expand_path(config_file)):
        return

    try:
        default = config_lib.format_initial(extensions_data)
        path.get_or_create_file(config_file, mkdir=False, content=default)
        logger.info('Initialized %s with default config', config_file)
    except IOError as error:
        logger.warning(
            'Unable to initialize %s with default config: %s',
            config_file, encoding.locale_decode(error))
Example #7
0
def create_initial_config_file(args, extensions_data):
    """Initialize whatever the last config file is with defaults"""

    config_file = path.expand_path(args.config_files[-1])

    if config_file.exists():
        return

    try:
        default = config_lib.format_initial(extensions_data)
        path.get_or_create_file(
            config_file,
            mkdir=False,
            content=default.encode(errors="surrogateescape"),
        )
        logger.info(f"Initialized {config_file.as_uri()} with default config")
    except OSError as exc:
        logger.warning(
            f"Unable to initialize {config_file.as_uri()} with default config: {exc}"
        )
Example #8
0
 def test_creating_nested_file(self):
     level2_dir = os.path.join(self.parent, b'test')
     file_path = os.path.join(self.parent, b'test', b'test')
     self.assert_(not os.path.exists(level2_dir))
     self.assert_(not os.path.exists(file_path))
     created = path.get_or_create_file(file_path)
     self.assert_(os.path.exists(level2_dir))
     self.assert_(os.path.isdir(level2_dir))
     self.assert_(os.path.exists(file_path))
     self.assert_(os.path.isfile(file_path))
     self.assertEqual(created, file_path)
Example #9
0
    def test_creating_nested_file(self):
        level2_dir = self.parent / "test"
        file_path = self.parent / "test" / "test"
        assert not level2_dir.exists()
        assert not file_path.exists()

        created = path.get_or_create_file(str(file_path))

        assert level2_dir.is_dir()
        assert file_path.is_file()
        assert created == file_path
Example #10
0
 def test_creating_nested_file(self):
     level2_dir = os.path.join(self.parent, b'test')
     file_path = os.path.join(self.parent, b'test', b'test')
     self.assert_(not os.path.exists(level2_dir))
     self.assert_(not os.path.exists(file_path))
     created = path.get_or_create_file(file_path)
     self.assert_(os.path.exists(level2_dir))
     self.assert_(os.path.isdir(level2_dir))
     self.assert_(os.path.exists(file_path))
     self.assert_(os.path.isfile(file_path))
     self.assertEqual(created, file_path)
Example #11
0
 def test_create_dir_without_mkdir(self):
     file_path = os.path.join(self.parent, b'foo', b'bar')
     with self.assertRaises(IOError):
         path.get_or_create_file(file_path, mkdir=False)
Example #12
0
 def test_create_file_with_none_filename_throws_value_error(self):
     with self.assertRaises(ValueError):
         path.get_or_create_file(None)
Example #13
0
 def test_create_dir_with_unicode_filename_throws_value_error(self):
     with self.assertRaises(ValueError):
         file_path = compat.text_type(os.path.join(self.parent, b'test'))
         path.get_or_create_file(file_path)
Example #14
0
 def test_create_file_with_name_of_existing_dir_throws_ioerror(self):
     conflicting_dir = os.path.join(self.parent)
     with self.assertRaises(IOError):
         path.get_or_create_file(conflicting_dir)
Example #15
0
 def test_create_dir_with_unicode_filename_throws_value_error(self):
     with self.assertRaises(ValueError):
         file_path = compat.text_type(os.path.join(self.parent, b'test'))
         path.get_or_create_file(file_path)
Example #16
0
 def test_create_file_with_name_of_existing_dir_throws_ioerror(self):
     conflicting_dir = os.path.join(self.parent)
     with self.assertRaises(IOError):
         path.get_or_create_file(conflicting_dir)
Example #17
0
 def test_create_dir_without_mkdir(self):
     file_path = os.path.join(self.parent, b'foo', b'bar')
     with self.assertRaises(IOError):
         path.get_or_create_file(file_path, mkdir=False)
Example #18
0
    def test_create_dir_with_bytes_content(self):
        file_path = self.parent / "test"

        created = path.get_or_create_file(str(file_path), content=b"foobar")

        assert created.read_bytes() == b"foobar"
Example #19
0
    def test_create_dir_without_mkdir(self):
        file_path = self.parent / "foo" / "bar"

        with pytest.raises(OSError):
            path.get_or_create_file(file_path, mkdir=False)
Example #20
0
 def test_create_file_with_none_filename_throws_type_error(self):
     with pytest.raises(TypeError):
         path.get_or_create_file(None)
Example #21
0
 def test_create_dir_with_bytes_content(self):
     file_path = os.path.join(self.parent, b'test')
     created = path.get_or_create_file(file_path, content=b'foobar')
     with open(created) as fh:
         self.assertEqual(fh.read(), b'foobar')
Example #22
0
 def test_create_dir_with_unicode_content(self):
     file_path = os.path.join(self.parent, b'test')
     created = path.get_or_create_file(file_path, content='foobaræøå')
     with open(created) as fh:
         self.assertEqual(fh.read(), b'foobaræøå')
Example #23
0
 def test_create_file_with_none_filename_throws_value_error(self):
     with self.assertRaises(ValueError):
         path.get_or_create_file(None)
Example #24
0
 def test_create_file_with_name_of_existing_dir_throws_error(self):
     with pytest.raises(OSError):
         path.get_or_create_file(self.parent)
Example #25
0
    def test_create_dir_with_unicode_content(self):
        file_path = self.parent / "test"

        created = path.get_or_create_file(str(file_path), content="foobaræøå")
        assert created.read_bytes() == b"foobar\xc3\xa6\xc3\xb8\xc3\xa5"
Example #26
0
 def test_create_dir_with_unicode_content(self):
     file_path = os.path.join(self.parent, b'test')
     created = path.get_or_create_file(file_path, content='foobaræøå')
     with open(created) as fh:
         self.assertEqual(fh.read(), b'foobar\xc3\xa6\xc3\xb8\xc3\xa5')