Ejemplo n.º 1
0
    def write_page(self, name, content, message=None, create=False, username=None, email=None):
        """Write page to git repo

        :param name: Name of page.
        :param content: Content of page.
        :param message: Commit message.
        :param create: Perform git add operation?
        :param username: Commit Name.
        :param email: Commit Email.
        :return: Git commit sha1.
        """

        cname = to_canonical(name)
        filename = cname_to_filename(cname)

        with open(self.path + "/" + filename, 'w') as f:
            f.write(content)

        if create:
            self.gittle.add(filename)

        if not message:
            message = "Updated %s" % name

        username, email = self._get_user(username, email)

        ret = self.gittle.commit(name=username,
                                 email=email,
                                 message=message,
                                 files=[filename])

        cache.delete(cname)

        return ret
Ejemplo n.º 2
0
    def write_page(self, name, content, message=None, create=False, username=None, email=None):
        """Write page to git repo

        :param name: Name of page.
        :param content: Content of page.
        :param message: Commit message.
        :param create: Perform git add operation?
        :param username: Commit Name.
        :param email: Commit Email.
        :return: Git commit sha1.
        """

        cname = to_canonical(name)
        filename = cname_to_filename(cname)

        with open(self.path + "/" + filename, 'w') as f:
            f.write(content)

        if create:
            self.gittle.add(filename)

        if not message:
            message = "Updated %s" % name

        username, email = self._get_user(username, email)

        ret = self.gittle.commit(name=username,
                                 email=email,
                                 message=message,
                                 files=[filename])

        cache.delete(cname)

        return ret
Ejemplo n.º 3
0
    def get_page(self, name, sha='HEAD'):
        """Get page data, partials, commit info.

        :param name: Name of page.
        :param sha: Commit sha.
        :return: dict

        """
        cached = cache.get(self._cache_key(name, sha))
        if cached:
            return cached

        # commit = gittle.utils.git.commit_info(self.repo[sha])
        filename = cname_to_filename(name).encode('utf8')
        sha = sha.encode('latin-1')

        try:
            data = self.gittle.get_commit_files(sha,
                                                paths=[filename]).get(filename)
            if not data:
                return None
            partials = {}
            if data.get('data'):
                meta = self.get_meta(data['data'])
                if meta and 'import' in meta:
                    for partial_name in meta['import']:
                        partials[partial_name] = self.get_page(partial_name)
            data['partials'] = partials
            data['info'] = self.get_history(name, limit=1)[0]
            cache.set(self._cache_key(name, sha), data)
            return data

        except KeyError:
            # HEAD doesn't exist yet
            return None
Ejemplo n.º 4
0
    def get_history(self, name, limit=100):
        """Get page history.

        :param name: Name of page.
        :param limit: Limit history size.
        :return: list -- List of dicts

        """
        if not len(self.repo.open_index()):
            # Index is empty, no commits
            return []

        file_path = cname_to_filename(name)
        versions = []

        walker = self.repo.get_walker(paths=[file_path], max_entries=limit)
        for entry in walker:
            change_type = None
            for change in entry.changes():
                if change.old.path == file_path:
                    change_type = change.type
                elif change.new.path == file_path:
                    change_type = change.type
            author_name, author_email = entry.commit.author.rstrip('>').split(
                '<')
            versions.append(
                dict(author=author_name.strip(),
                     author_email=author_email,
                     time=entry.commit.author_time,
                     message=entry.commit.message,
                     sha=entry.commit.id,
                     type=change_type))

        return versions
Ejemplo n.º 5
0
    def delete_page(self, name, username=None, email=None, message=None):
        """Delete page.
        :param name: Page that will be deleted
        :param username: Committer name
        :param email: Committer email
        :return: str -- Commit sha1

        """

        username, email = self._get_user(username, email)

        if not message:
            message = "Deleted %s" % name

        filename = cname_to_filename(name)

        # gittle.rm won't actually remove the file, have to do it ourselves
        os.remove(os.path.join(self.path, filename))
        self.gittle.rm(filename)
        commit = self.gittle.commit(name=username,
                                    email=email,
                                    message=message,
                                    files=[filename])
        cache.delete_many(name)
        return commit
Ejemplo n.º 6
0
    def delete_page(self, name, username=None, email=None, message=None):
        """Delete page.
        :param name: Page that will be deleted
        :param username: Committer name
        :param email: Committer email
        :return: str -- Commit sha1

        """

        username, email = self._get_user(username, email)

        if not message:
            message = "Deleted %s" % name

        filename = cname_to_filename(name)

        # gittle.rm won't actually remove the file, have to do it ourselves
        os.remove(os.path.join(self.path, filename))
        self.gittle.rm(filename)
        commit = self.gittle.commit(name=username,
                                    email=email,
                                    message=message,
                                    files=[filename])
        cache.delete_many(self._cache_key(name))
        return commit
Ejemplo n.º 7
0
    def get_history(self, name, limit=100):
        """Get page history.

        :param name: Name of page.
        :param limit: Limit history size.
        :return: list -- List of dicts

        """
        if not len(self.repo.open_index()):
            # Index is empty, no commits
            return []

        file_path = cname_to_filename(name)
        versions = []

        walker = self.repo.get_walker(paths=[file_path], max_entries=limit)
        for entry in walker:
            change_type = None
            for change in entry.changes():
                if change.old.path == file_path:
                    change_type = change.type
                elif change.new.path == file_path:
                    change_type = change.type
            author_name, author_email = entry.commit.author.split('<')
            versions.append(dict(
                author=author_name.strip(),
                time=entry.commit.author_time,
                message=entry.commit.message,
                sha=entry.commit.id,
                type=change_type))

        return versions
Ejemplo n.º 8
0
    def get_page(self, name, sha='HEAD'):
        """Get page data, partials, commit info.

        :param name: Name of page.
        :param sha: Commit sha.
        :return: dict

        """
        cached = cache.get(name)
        if cached:
            return cached

        # commit = gittle.utils.git.commit_info(self.repo[sha])
        filename = cname_to_filename(name).encode('utf8')
        sha = sha.encode('latin-1')

        try:
            data = self.gittle.get_commit_files(sha, paths=[filename]).get(filename)
            if not data:
                return None
            partials = {}
            if data.get('data'):
                meta = self.get_meta(data['data'])
                if meta and 'import' in meta:
                    for partial_name in meta['import']:
                        partials[partial_name] = self.get_page(partial_name)
            data['partials'] = partials
            data['info'] = self.get_history(name, limit=1)[0]
            return data

        except KeyError:
            # HEAD doesn't exist yet
            return None
Ejemplo n.º 9
0
    def get_page(self, name, sha='HEAD'):
        """Get page data, partials, commit info.

        :param name: Name of page.
        :param sha: Commit sha.
        :return: dict

        """
        cached = cache.get(name)
        if cached:
            return cached

        # commit = gittle.utils.git.commit_info(self.repo[sha])
        filename = cname_to_filename(name).encode('latin-1')
        sha = sha.encode('latin-1')

        namespace_path = os.path.join(self.path, os.path.splitext(filename)[0])
        namespace_cname = to_canonical(os.path.splitext(filename)[0])
        if not os.path.exists(os.path.join(
                self.path, filename)) and os.path.isdir(namespace_path):
            files = [
                "[%s](%s_%s)" % (x, namespace_cname, filename_to_cname(x))
                for x in os.listdir(namespace_path)
            ]
            print(files)
            return {
                'data':
                "# Namespace %s \n\n This is an automatically generated list of pages in this namespace.\n\n %s"
                % (os.path.splitext(filename)[0], '\n'.join(files))
            }

        try:
            data = self.gittle.get_commit_files(sha,
                                                paths=[filename]).get(filename)
            if not data:
                return None
            partials = {}
            if data.get('data'):
                meta = self.get_meta(data['data'])
                if meta and 'import' in meta:
                    for partial_name in meta['import']:
                        partials[partial_name] = self.get_page(partial_name)
            data['partials'] = partials
            data['info'] = self.get_history(name, limit=1)[0]

            return data

        except KeyError:
            # HEAD doesn't exist yet
            return None
Ejemplo n.º 10
0
    def rename(self, new_name, username=None, email=None, message=None):
        """Rename page.

        :param new_name: New name of page.
        :param username: Committer name
        :param email: Committer email
        :return: str -- Commit sha1

        """
        assert self.sha == 'HEAD'
        old_filename, new_filename = self.filename, cname_to_filename(new_name)
        if old_filename not in self.wiki.gittle.index:
            # old doesn't exist
            return None
        elif old_filename == new_filename:
            return None
        else:
            # file is being overwritten, but that is ok, it's git!
            pass

        username, email = self._get_user(username, email)

        if not message:
            message = "Moved %s to %s" % (self.name, new_name)

        os.rename(os.path.join(self.wiki.path, old_filename), os.path.join(self.wiki.path, new_filename))

        self.wiki.gittle.add(new_filename)
        self.wiki.gittle.rm(old_filename)

        commit = self.wiki.commit(name=username,
                                  email=email,
                                  message=message,
                                  files=[old_filename, new_filename])

        old_history = cache.get(self._cache_key('history'))
        self._invalidate_cache()
        self.name = new_name
        self.filename = new_filename
        # We need to clear the cache for the new name as well as the old
        self._invalidate_cache(save_history=old_history)

        return commit
Ejemplo n.º 11
0
    def get_page(self, name, sha='HEAD'):
        """Get page data, partials, commit info.

        :param name: Name of page.
        :param sha: Commit sha.
        :return: dict

        """
        cached = cache.get(name)
        if cached:
            return cached

        # commit = gittle.utils.git.commit_info(self.repo[sha])
        filename = cname_to_filename(name).encode('latin-1')
        sha = sha.encode('latin-1')

        namespace_path = os.path.join(self.path, os.path.splitext(filename)[0])
        namespace_cname = to_canonical(os.path.splitext(filename)[0])
        if not os.path.exists(os.path.join(self.path, filename)) and os.path.isdir(namespace_path):
            files = ["[%s](%s_%s)" % (x, namespace_cname, filename_to_cname(x)) for x in os.listdir(namespace_path)]
            print(files)
            return {'data': "# Namespace %s \n\n This is an automatically generated list of pages in this namespace.\n\n %s" % (os.path.splitext(filename)[0], '\n'.join(files))}

        try:
            data = self.gittle.get_commit_files(sha, paths=[filename]).get(filename)
            if not data:
                return None
            partials = {}
            if data.get('data'):
                meta = self.get_meta(data['data'])
                if meta and 'import' in meta:
                    for partial_name in meta['import']:
                        partials[partial_name] = self.get_page(partial_name)
            data['partials'] = partials
            data['info'] = self.get_history(name, limit=1)[0]

            return data

        except KeyError:
            # HEAD doesn't exist yet
            return None
Ejemplo n.º 12
0
    def rename(self, new_name, username=None, email=None, message=None):
        """Rename page.

        :param new_name: New name of page.
        :param username: Committer name
        :param email: Committer email
        :return: str -- Commit sha1

        """
        assert self.sha == 'HEAD'
        old_filename, new_filename = self.filename, cname_to_filename(new_name)
        if old_filename not in self.wiki.repo.open_index():
            # old doesn't exist
            return None
        elif old_filename == new_filename:
            return None
        else:
            # file is being overwritten, but that is ok, it's git!
            pass

        username, email = self._get_user(username, email)

        if not message:
            message = "Moved %s to %s" % (self.name, new_name)

        os.rename(os.path.join(self.wiki.path, old_filename),
                  os.path.join(self.wiki.path, new_filename))
        commit = self.wiki.commit(name=username,
                                  email=email,
                                  message=message,
                                  files=[old_filename, new_filename])

        old_history = cache.get(self._cache_key('history'))
        self._invalidate_cache()
        self.name = new_name
        self.filename = new_filename
        # We need to clear the cache for the new name as well as the old
        self._invalidate_cache(save_history=old_history)

        return commit
Ejemplo n.º 13
0
    def delete_page(self, name, username=None, email=None, message=None):
        """Delete page.
        :param name: Page that will be deleted
        :param username: Committer name
        :param email: Committer email
        :return: str -- Commit sha1

        """

        username, email = self._get_user(username, email)

        if not message:
            message = "Deleted %s" % name

        filename = cname_to_filename(name)
        self.gittle.rm(filename)
        commit = self.gittle.commit(name=username,
                                    email=email,
                                    message=message,
                                    files=[str(filename)])
        cache.delete_many(name)
        return commit
Ejemplo n.º 14
0
    def delete_page(self, name, username=None, email=None, message=None):
        """Delete page.
        :param name: Page that will be deleted
        :param username: Committer name
        :param email: Committer email
        :return: str -- Commit sha1

        """

        username, email = self._get_user(username, email)

        if not message:
            message = "Deleted %s" % name

        filename = cname_to_filename(name)
        self.gittle.rm(filename)
        commit = self.gittle.commit(name=username,
                                    email=email,
                                    message=message,
                                    files=[str(filename)])
        cache.delete_many(name)
        return commit
Ejemplo n.º 15
0
 def test_cname_to_filename(self):
     eq_(cname_to_filename('test'), 'test.md')
Ejemplo n.º 16
0
 def __init__(self, name, wiki, sha='HEAD'):
     self.name = name
     self.filename = cname_to_filename(name)
     self.sha = sha.encode('latin-1')
     self.wiki = wiki
Ejemplo n.º 17
0
 def test_cname_to_filename(self):
     eq_(cname_to_filename('test'), 'test.md')
Ejemplo n.º 18
0
 def __init__(self, name, wiki, sha='HEAD'):
     self.name = name
     self.filename = cname_to_filename(name)
     self.sha = sha.encode('latin-1')
     self.wiki = wiki