Skip to content

versioncontrol

Classes and functions to change names of corpus files.

GIT

Bases: VersionController

Implement basic git functionality.

Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
class GIT(VersionController):
    """Implement basic git functionality."""

    def __init__(self, gitrepo):
        """Initialise the GIT class.

        Args:
            gitrepo (git.Repo): client to control the git repo
        """
        super().__init__()
        self.gitrepo = gitrepo
        self.config = self.gitrepo.config_reader()

    def add(self, path):
        """Add path to the repo.

        Args:
            path (str): path that should be added to the git repo.
        """
        self.gitrepo.git.add(path)

    def move(self, oldpath, newpath):
        """Move a file within the repo.

        Args:
            oldpath (src): path of the file that should be moved
            newpath (scr): new path of the file to be moved
        """
        self.gitrepo.git.mv(oldpath, newpath)

    def remove(self, path):
        """Remove a file from the repo.

        Args:
            path (src): path of the file that should be removed.
        """
        self.gitrepo.git.rm(path)

__init__(gitrepo)

Initialise the GIT class.

Parameters:

Name Type Description Default
gitrepo git.Repo

client to control the git repo

required
Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
77
78
79
80
81
82
83
84
85
def __init__(self, gitrepo):
    """Initialise the GIT class.

    Args:
        gitrepo (git.Repo): client to control the git repo
    """
    super().__init__()
    self.gitrepo = gitrepo
    self.config = self.gitrepo.config_reader()

add(path)

Add path to the repo.

Parameters:

Name Type Description Default
path str

path that should be added to the git repo.

required
Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
87
88
89
90
91
92
93
def add(self, path):
    """Add path to the repo.

    Args:
        path (str): path that should be added to the git repo.
    """
    self.gitrepo.git.add(path)

move(oldpath, newpath)

Move a file within the repo.

Parameters:

Name Type Description Default
oldpath src

path of the file that should be moved

required
newpath scr

new path of the file to be moved

required
Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
 95
 96
 97
 98
 99
100
101
102
def move(self, oldpath, newpath):
    """Move a file within the repo.

    Args:
        oldpath (src): path of the file that should be moved
        newpath (scr): new path of the file to be moved
    """
    self.gitrepo.git.mv(oldpath, newpath)

remove(path)

Remove a file from the repo.

Parameters:

Name Type Description Default
path src

path of the file that should be removed.

required
Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
104
105
106
107
108
109
110
def remove(self, path):
    """Remove a file from the repo.

    Args:
        path (src): path of the file that should be removed.
    """
    self.gitrepo.git.rm(path)

VersionControlError

Bases: Exception

Raise this exception when errors arise in this module.

Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
29
30
class VersionControlError(Exception):
    """Raise this exception when errors arise in this module."""

VersionController

A very basic version control class.

Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class VersionController:
    """A very basic version control class."""

    def __init__(self):
        """Initialise the VersionController class."""
        # non-repo config to get at global values
        self.config = git.GitConfigParser(
            [os.path.normpath(os.path.expanduser("~/.gitconfig"))], read_only=True
        )

    def add(self, path):
        """Meta function."""
        raise NotImplementedError("You have to subclass and override add")

    def move(self, oldpath, newpath):
        """Meta function."""
        raise NotImplementedError("You have to subclass and override move")

    def remove(self, path):
        """Meta function."""
        raise NotImplementedError("You have to subclass and override remove")

    def user_name(self):
        """Try to get a username."""
        if self.config.has_option("user", "name"):
            return self.config.get("user", "name")
        else:
            pwnam = pwd.getpwnam(getpass.getuser()).pw_gecos
            if pwnam is not None:
                return pwnam
            else:
                return ""

    def user_email(self):
        """Try to get the users email."""
        if self.config.has_option("user", "email"):
            return self.config.get("user", "email")
        else:
            return ""

__init__()

Initialise the VersionController class.

Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
36
37
38
39
40
41
def __init__(self):
    """Initialise the VersionController class."""
    # non-repo config to get at global values
    self.config = git.GitConfigParser(
        [os.path.normpath(os.path.expanduser("~/.gitconfig"))], read_only=True
    )

add(path)

Meta function.

Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
43
44
45
def add(self, path):
    """Meta function."""
    raise NotImplementedError("You have to subclass and override add")

move(oldpath, newpath)

Meta function.

Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
47
48
49
def move(self, oldpath, newpath):
    """Meta function."""
    raise NotImplementedError("You have to subclass and override move")

remove(path)

Meta function.

Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
51
52
53
def remove(self, path):
    """Meta function."""
    raise NotImplementedError("You have to subclass and override remove")

user_email()

Try to get the users email.

Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
66
67
68
69
70
71
def user_email(self):
    """Try to get the users email."""
    if self.config.has_option("user", "email"):
        return self.config.get("user", "email")
    else:
        return ""

user_name()

Try to get a username.

Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
55
56
57
58
59
60
61
62
63
64
def user_name(self):
    """Try to get a username."""
    if self.config.has_option("user", "name"):
        return self.config.get("user", "name")
    else:
        pwnam = pwd.getpwnam(getpass.getuser()).pw_gecos
        if pwnam is not None:
            return pwnam
        else:
            return ""

vcs(directory)

Make a version control client.

Parameters:

Name Type Description Default
directory str

the directory where the working copy is found.

required

Returns:

Type Description
GIT

A GIT class instance.

Raises:

Type Description
VersionControlError

If the given directory is not a git repository

Source code in /home/anders/projects/CorpusTools/corpustools/versioncontrol.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
def vcs(directory):
    """Make a version control client.

    Args:
        directory (str): the directory where the working copy is found.

    Returns:
        (GIT): A GIT class instance.

    Raises:
        VersionControlError: If the given directory is not a git repository
    """
    try:
        git_repo = git.Repo(directory)
        return GIT(git_repo)
    except git.exc.InvalidGitRepositoryError:
        raise VersionControlError(
            f"{directory} is not a Git repo. Files can only be added to a git repo."
        )