Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

Commit

Permalink
Split section out into it's own file
Browse files Browse the repository at this point in the history
  • Loading branch information
obmarg committed May 30, 2012
1 parent 0b4ccee commit f87ba06
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 79 deletions.
5 changes: 5 additions & 0 deletions db/__init__.py
@@ -0,0 +1,5 @@

from doc import Document
from section import Section

__all__ = [ 'Document', 'Section' ]
4 changes: 4 additions & 0 deletions db/constants.py
@@ -0,0 +1,4 @@

MASTER_REF = 'refs/heads/master'
SECTION_REF_PREFIX = 'refs/heads/sections/'

82 changes: 10 additions & 72 deletions db/doc.py
@@ -1,80 +1,18 @@

import os
import time
import pygit2
from pygit2 import Repository, init_repository
from gitutils import CommitBlob
from .section import Section
from .constants import MASTER_REF, SECTION_REF_PREFIX

rootPath = 'data'

class Section(object):
'''
Represents a Section in a document
'''

def __init__( self, name, headCommit, repo ):
'''
Constructor
Args:
name The name of the section
headCommit The head commit of the section
repo The repository object
'''
self.name = name
self.headCommit = headCommit
self.repo = repo

def CurrentContent( self ):
'''
Returns the current content of the section
'''
oid = self.headCommit.tree[ 0 ].oid
blob = self.repo[ oid ]
return blob.data

def ContentHistory( self ):
'''
Generator function that returns the history of this section
This niavely assumes there's only one parent commit
on each commit, which will do for now.
'''
current = self.headCommit
while True:
oid = current.tree[ 0 ].oid
yield self.repo[ oid ].data
if current.parents:
# current has at least one parent
current = current.parents[ 0 ]
else:
break

def SetContent( self, newContent ):
'''
Adds a new version of the section
Args:
newContent The new content of the section
'''
newId = CommitBlob(
self.repo,
newContent,
self.name,
'Updating section',
[ self.headCommit ],
Document.SECTION_REF_PREFIX + self.name
)
self.headCommit = self.repo[ newId ]


class Document(object):
'''
Class representing a document, interacts with the git
database
'''
MASTER_REF = 'refs/heads/master'
SECTION_REF_PREFIX = 'refs/heads/sections/'

def __init__( self, name, create=False ):
'''
Expand All @@ -100,10 +38,10 @@ def _CreateMasterBranch( self ):
commitId = CommitBlob(
self.repo, '', 'layout', 'Initial commit'
)
self.repo.create_reference( self.MASTER_REF, commitId )
self.repo.create_reference( MASTER_REF, commitId )

@classmethod
def _IsSectionRef( cls, refName ):
@staticmethod
def _IsSectionRef( refName ):
'''
Checks if a refererence name refers to a section
Expand All @@ -112,17 +50,17 @@ def _IsSectionRef( cls, refName ):
Returns:
A boolean
'''
return refName.startswith( cls.SECTION_REF_PREFIX )
return refName.startswith( SECTION_REF_PREFIX )

@classmethod
def _RefNameToSectionName( cls, refName ):
@staticmethod
def _RefNameToSectionName( refName ):
'''
Converts a reference name to a section name
Args:
ref: The reference name
'''
return refName[ len(cls.SECTION_REF_PREFIX) : ]
return refName[ len(SECTION_REF_PREFIX) : ]

def _SectionRefs( self ):
'''
Expand Down Expand Up @@ -165,7 +103,7 @@ def AddSection( self, name, content='' ):
'Created section ' + name
)
ref = self.repo.create_reference(
self.SECTION_REF_PREFIX + name,
SECTION_REF_PREFIX + name,
commitId
)
return Section( name, self.repo[ ref.oid ], self.repo )
65 changes: 65 additions & 0 deletions db/section.py
@@ -0,0 +1,65 @@
import pygit2
from gitutils import CommitBlob
from .constants import SECTION_REF_PREFIX

class Section(object):
'''
Represents a Section in a document
'''

def __init__( self, name, headCommit, repo ):
'''
Constructor
Args:
name The name of the section
headCommit The head commit of the section
repo The repository object
'''
self.name = name
self.headCommit = headCommit
self.repo = repo

def CurrentContent( self ):
'''
Returns the current content of the section
'''
oid = self.headCommit.tree[ 0 ].oid
blob = self.repo[ oid ]
return blob.data

def ContentHistory( self ):
'''
Generator function that returns the history of this section
This niavely assumes there's only one parent commit
on each commit, which will do for now.
'''
current = self.headCommit
while True:
oid = current.tree[ 0 ].oid
yield self.repo[ oid ].data
if current.parents:
# current has at least one parent
current = current.parents[ 0 ]
else:
break

def SetContent( self, newContent ):
'''
Adds a new version of the section
Args:
newContent The new content of the section
'''
newId = CommitBlob(
self.repo,
newContent,
self.name,
'Updating section',
[ self.headCommit ],
SECTION_REF_PREFIX + self.name
)
self.headCommit = self.repo[ newId ]


15 changes: 8 additions & 7 deletions db/tests.py
Expand Up @@ -5,6 +5,7 @@
import pygit2
import gitutils
import doc
import section
from collections import namedtuple

class BaseTest(unittest.TestCase):
Expand Down Expand Up @@ -261,7 +262,7 @@ class SectionTests(BaseTest):

def setUp( self ):
super( SectionTests, self ).setUp()
self.mox.StubOutWithMock(doc, 'CommitBlob')
self.mox.StubOutWithMock(section, 'CommitBlob')

def testCurrentContent( self ):
'''
Expand All @@ -284,8 +285,8 @@ def testCurrentContent( self ):

self.mox.ReplayAll()

section = doc.Section( 'name', mockHead, mockRepo )
c = section.CurrentContent()
s = section.Section( 'name', mockHead, mockRepo )
c = s.CurrentContent()

self.mox.VerifyAll()
self.assertEqual( 'blobData', c )
Expand All @@ -297,7 +298,7 @@ def testSetContent( self ):
mockHead = self.mox.CreateMock( pygit2.Commit )
mockRepo = self.mox.CreateMock( pygit2.Repository )

doc.CommitBlob(
section.CommitBlob(
mockRepo, 'content', 'name', 'Updating section',
[ mockHead ], 'refs/heads/sections/name'
).AndReturn( 'newId' )
Expand All @@ -306,9 +307,9 @@ def testSetContent( self ):

self.mox.ReplayAll()

section = doc.Section( 'name', mockHead, mockRepo )
section.SetContent( 'content' )
s= section.Section( 'name', mockHead, mockRepo )
s.SetContent( 'content' )

self.mox.VerifyAll()
self.assertEqual( section.headCommit, 'newCommit' )
self.assertEqual( s.headCommit, 'newCommit' )

0 comments on commit f87ba06

Please sign in to comment.