# Path: vilya/libs/git.py
# class Repository(object):
# 
#     def __init__(self, path):
#         self.path = path
#         self.repository = _Repository(path)
# 
#     @property
#     def is_empty(self):
#         return self.repository.is_empty
# 
#     @property
#     def head(self):
#         return self.repository.head
# 
#     def get_file(self, reference, path):
#         blob = self.repository.resolve_blob("%s:%s" % (reference,
#                                                        path))
#         if not blob:
#             return None
#         return blob.data
# 
#     def list_entries(self, reference='HEAD', path=None):
#         return self.repository.list_entries(reference,
#                                             path=path)
# 
#     def list_branches(self):
#         return self.repository.list_branches()
# 
#     def list_tags(self):
#         return self.repository.list_tags()
# 
#     def list_commits(self, reference='HEAD', from_reference=None, path=None, max_count=25):
#         return self.repository.list_commits(reference,
#                                             from_ref=from_reference,
#                                             path=path,
#                                             max_count=max_count)
# 
#     def resolve_commit(self, reference):
#         return self.repository.resolve_commit(reference)
# 
#     def diff(self, reference, from_reference=None):
#         raw_diff = self.repository.diff(reference, from_reference=from_reference)
#         if raw_diff:
#             return Diff(raw_diff)
#         return raw_diff
# 
#     def list_remotes(self):
#         return self.repository.list_remotes()
# 
#     def create_remote(self, name, url):
#         return self.repository.create_remote(name, url)
# 
#     @classmethod
#     def clone(cls, *k, **kw):
#         return _Repository.clone(*k, **kw)
# 
#     def clone_to(self, path, **kw):
#         self.repository.clone_to(path, **kw)
#         return Repository(path)
# 
#     @classmethod
#     def init(cls, path, bare=True):
#         return _Repository.init(path, bare=bare)
# 
#     def merge(self, *k, **kw):
#         return self.repository.merge(*k, **kw)
# 
#     def fetch(self, name=None):
#         return self.repository.fetch(name)
# 
#     def resolve_merge_base(self, reference, from_reference):
#         return self.repository.merge_base(reference, from_reference)
# 
#     def update_reference(self, *k):
#         return self.repository.update_reference(*k)
# 
#     def resolve_type(self, reference):
#         return self.repository.resolve_type(reference)
# 
# Path: vilya/libs/text.py
# def format_md_or_rst(path, content, project_name=None):
#     m = Mikoto(content)
#     if path.endswith('.md') or path.endswith('.markdown'):
#         # FIXME with project or gist
#         return m.markdown
# 
#     if RST_RE.match(path):
#         try:
#             return m.restructuredtext
#         except docutils.ApplicationError:
#             pass
# 
#     return m.highlight_code(path)
# 
# Path: vilya/settings.py
# REPO_PATH = './repos'
# 
# Path: vilya/projects/commit.py
# class Commit(object):
# 
#     def __init__(self, commit):
#         self.commit = commit
# 
#     @property
#     def author(self):
#         from ..services import users
#         email = self.commit.author.email
#         user = users.first(email=email)
#         if not user:
#             return None
#         return user
# 
#     @property
#     def author_time(self):
#         return datetime.fromtimestamp(self.commit.author.time,
#                                       FixedOffset(self.commit.author.offset))
# 
#     @property
#     def author_name(self):
#         author = self.author
#         if not author:
#             return self.commit.author.name
#         if not author.name:
#             return self.commit.author.name
#         return author.name
# 
#     @property
#     def author_url(self):
#         author = self.author
#         if not author:
#             return None
#         return author.url
# 
#     @property
#     def author_avatar_url(self):
#         author = self.author
#         if not author:
#             return get_gravatar_url(self.commit.author.email)
#         return author.avatar_url
# 
#     @property
#     def hex(self):
#         return self.commit.hex
# 
#     @property
#     def parents(self):
#         return [Commit(p) for p in self.commit.parents]
# 
#     @property
#     def message(self):
#         return self.commit.message
# 
#     @property
#     def message_title(self):
#         title, _, _ = self.message.strip().partition('\n\n')
#         return title
# 
#     @property
#     def message_content(self):
#         _, _, content = self.message.strip().partition('\n\n')
#         return content


import os
from ..libs.git import Repository
from ..libs.text import format_md_or_rst
from ..settings import REPO_PATH
from .commit import Commit


class ProjectRepository(Repository):

    def __init__(self, project):
        path = os.path.join(REPO_ROOT_PATH, project.path)
        self.project = project
        super(ProjectRepository, self).__init__(path)

    @classmethod
    def init(self, path, bare=True):
        path = os.path.join(REPO_ROOT_PATH, path)
        super(ProjectRepository, self).init(path, bare=bare)

    def get_commit(self, reference):
        pass

    def get_readme(self, reference='HEAD', path=None):
        entries = self.repository.list_entries(reference=reference,
                                               path=path)
        for path in entries:
            entry = entries[path]
            if (entry.is_blob and entry.name == 'README' or entry.name.startswith('README.')):
                readme_content = self.get_file(reference, path)
                return format_md_or_rst(path, readme_content)
        return ''

    def get_rendered_file(self, reference='HEAD', path=None):
        content = self.get_file(reference=reference, path=path)
        if content:

# Path: vilya/libs/git.py
# class Repository(object):
# 
#     def __init__(self, path):
#         self.path = path
#         self.repository = _Repository(path)
# 
#     @property
#     def is_empty(self):
#         return self.repository.is_empty
# 
#     @property
#     def head(self):
#         return self.repository.head
# 
#     def get_file(self, reference, path):
#         blob = self.repository.resolve_blob("%s:%s" % (reference,
#                                                        path))
#         if not blob:
#             return None
#         return blob.data
# 
#     def list_entries(self, reference='HEAD', path=None):
#         return self.repository.list_entries(reference,
#                                             path=path)
# 
#     def list_branches(self):
#         return self.repository.list_branches()
# 
#     def list_tags(self):
#         return self.repository.list_tags()
# 
#     def list_commits(self, reference='HEAD', from_reference=None, path=None, max_count=25):
#         return self.repository.list_commits(reference,
#                                             from_ref=from_reference,
#                                             path=path,
#                                             max_count=max_count)
# 
#     def resolve_commit(self, reference):
#         return self.repository.resolve_commit(reference)
# 
#     def diff(self, reference, from_reference=None):
#         raw_diff = self.repository.diff(reference, from_reference=from_reference)
#         if raw_diff:
#             return Diff(raw_diff)
#         return raw_diff
# 
#     def list_remotes(self):
#         return self.repository.list_remotes()
# 
#     def create_remote(self, name, url):
#         return self.repository.create_remote(name, url)
# 
#     @classmethod
#     def clone(cls, *k, **kw):
#         return _Repository.clone(*k, **kw)
# 
#     def clone_to(self, path, **kw):
#         self.repository.clone_to(path, **kw)
#         return Repository(path)
# 
#     @classmethod
#     def init(cls, path, bare=True):
#         return _Repository.init(path, bare=bare)
# 
#     def merge(self, *k, **kw):
#         return self.repository.merge(*k, **kw)
# 
#     def fetch(self, name=None):
#         return self.repository.fetch(name)
# 
#     def resolve_merge_base(self, reference, from_reference):
#         return self.repository.merge_base(reference, from_reference)
# 
#     def update_reference(self, *k):
#         return self.repository.update_reference(*k)
# 
#     def resolve_type(self, reference):
#         return self.repository.resolve_type(reference)
# 
# Path: vilya/libs/text.py
# def format_md_or_rst(path, content, project_name=None):
#     m = Mikoto(content)
#     if path.endswith('.md') or path.endswith('.markdown'):
#         # FIXME with project or gist
#         return m.markdown
# 
#     if RST_RE.match(path):
#         try:
#             return m.restructuredtext
#         except docutils.ApplicationError:
#             pass
# 
#     return m.highlight_code(path)
# 
# Path: vilya/settings.py
# REPO_PATH = './repos'
# 
# Path: vilya/projects/commit.py
# class Commit(object):
# 
#     def __init__(self, commit):
#         self.commit = commit
# 
#     @property
#     def author(self):
#         from ..services import users
#         email = self.commit.author.email
#         user = users.first(email=email)
#         if not user:
#             return None
#         return user
# 
#     @property
#     def author_time(self):
#         return datetime.fromtimestamp(self.commit.author.time,
#                                       FixedOffset(self.commit.author.offset))
# 
#     @property
#     def author_name(self):
#         author = self.author
#         if not author:
#             return self.commit.author.name
#         if not author.name:
#             return self.commit.author.name
#         return author.name
# 
#     @property
#     def author_url(self):
#         author = self.author
#         if not author:
#             return None
#         return author.url
# 
#     @property
#     def author_avatar_url(self):
#         author = self.author
#         if not author:
#             return get_gravatar_url(self.commit.author.email)
#         return author.avatar_url
# 
#     @property
#     def hex(self):
#         return self.commit.hex
# 
#     @property
#     def parents(self):
#         return [Commit(p) for p in self.commit.parents]
# 
#     @property
#     def message(self):
#         return self.commit.message
# 
#     @property
#     def message_title(self):
#         title, _, _ = self.message.strip().partition('\n\n')
#         return title
# 
#     @property
#     def message_content(self):
#         _, _, content = self.message.strip().partition('\n\n')
#         return content


# Path: vilya/projects/repository.py
import os
from ..libs.git import Repository
from ..libs.text import format_md_or_rst
from ..settings import REPO_PATH
from .commit import Commit



class ProjectRepository(Repository):

    def __init__(self, project):
        path = os.path.join(REPO_ROOT_PATH, project.path)
        self.project = project
        super(ProjectRepository, self).__init__(path)

    @classmethod
    def init(self, path, bare=True):
        path = os.path.join(REPO_ROOT_PATH, path)
        super(ProjectRepository, self).init(path, bare=bare)

    def get_commit(self, reference):
        pass

    def get_readme(self, reference='HEAD', path=None):
        entries = self.repository.list_entries(reference=reference,
                                               path=path)
        for path in entries:
            entry = entries[path]
            if (entry.is_blob and entry.name == 'README' or entry.name.startswith('README.')):
                readme_content = self.get_file(reference, path)
                return format_md_or_rst(path, readme_content)
        return ''

    def get_rendered_file(self, reference='HEAD', path=None):
        content = self.get_file(reference=reference, path=path)
        if content:

            return format_md_or_rst(path, content)