def test__create_w_filetype_symlink(self):
        filetype = FO.typestr_to_type("symlink")
        fo = Factory.create(self.path, filetype=filetype)

        self.assertTrue(isinstance(fo, FO.SymlinkObject))
        self.assertEquals(fo.type(), G.TYPE_SYMLINK)
        self.assertEquals(fo.path, self.path)
        self.assertEquals(fo.mode, fo.defaults.mode)
        self.assertEquals(fo.uid, fo.defaults.uid)
        self.assertEquals(fo.gid, fo.defaults.gid)
        self.assertEquals(fo.checksum, fo.defaults.checksum)
    def test__create_w_filetype_dir(self):
        filetype = FO.typestr_to_type("dir")
        fo = Factory.create(self.path, filetype=filetype)

        self.assertTrue(isinstance(fo, FO.DirObject))
        self.assertEquals(fo.type(), G.TYPE_DIR)
        self.assertEquals(fo.path, self.path)
        self.assertEquals(fo.mode, fo.defaults.mode)
        self.assertEquals(fo.uid, fo.defaults.uid)
        self.assertEquals(fo.gid, fo.defaults.gid)
        self.assertEquals(fo.checksum, fo.defaults.checksum)
def create(path, use_rpmdb=False, **attrs):
    """
    A kind of factory method to create an appropriate type of FileObjects'
    instance from path.

    :param path:  Path of target object.
    :param attrs: A dict holding metadata other than path such as mode, gid,
                  uid, checksum, create, filetype, src, linkto, etc.
    """
    fo = B.Bunch(path=path, **attrs)

    if "create" in fo and fo.create:
        assert to_be_created(fo), \
            "Missing info to create: path=%s, attrs=%s" % (path, str(attrs))
    else:
        # $path exists or not exist but it's a symlink, link to
        # non-existent-obj to be linked.
        if os.path.exists(path) or os.path.islink(path):
            return create_from_real_object(fo, use_rpmdb)
        else:
            fo.create = True if to_be_created(fo) else False

    if "filetype" in fo:
        filetype = FO.typestr_to_type(fo.filetype)
    else:
        filetype = G.TYPE_UNKNOWN

        if ("content" in fo and fo.content) or \
                ("src" in fo and fo.src != fo.path):
            filetype = G.TYPE_FILE

        elif "linkto" in fo:
            filetype = G.TYPE_SYMLINK

        else:  # TODO: Is there any specific features in dirs?
            filetype = G.TYPE_DIR

    #logging.debug("xo=" + str(fo))

    cls = FO.FILEOBJECTS.get(filetype, None)
    assert cls is not None

    return cls(**fo)