Skip to content

Commit

Permalink
feat: adding python type hint support in class and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lpmatos committed Jul 28, 2020
1 parent 4e07f70 commit 8a6e7f7
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 41 deletions.
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
GitLabRC
================================================
4 changes: 3 additions & 1 deletion gitlabrc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

__version__ = ".".join(map(str, (0, 0, 6)))
VERSION = (0, 0, 9)

__version__ = ".".join(map(str, VERSION))
66 changes: 37 additions & 29 deletions gitlabrc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
from .constants import CLI
from .settings import Config
from .method import CloneMethod
from typing import NoReturn, Text
from argparse import ArgumentParser, RawTextHelpFormatter

Expand All @@ -23,38 +24,45 @@ def _create_parser_object(self) -> ArgumentParser:

def _adding_arguments(self) -> NoReturn:
self._parser.add_argument("-u", "--url",
type=str,
dest="url",
default="https://gitlab.com",
metavar="<url>",
help="base URL of GitLab instance")
type = str,
dest = "url",
default = "https://gitlab.com",
metavar = "<url>",
help = "base URL of GitLab instance")
self._parser.add_argument("-t", "--token",
type=str,
dest="token",
default=self._config.get_env("GITLAB_TOKEN"),
metavar="<token>",
help="token GitLab API")
type = str,
dest = "token",
default = self._config.get_env("GITLAB_TOKEN"),
metavar = "<token>",
help = "token GitLab API")
self._parser.add_argument("-n", "--namespace",
type=str,
dest="namespace",
default="",
metavar="<namespace>",
help="namespace in GitLab to clone all projects")
type = str,
dest = "namespace",
default = "",
metavar = "<namespace>",
help = "namespace in GitLab to clone all projects")
self._parser.add_argument("-p", "--path",
dest="path",
default=self._config.get_env("PWD"),
metavar="<path>",
help="destination path for cloned projects")
dest = "path",
default = self._config.get_env("PWD"),
metavar = "<path>",
help = "destination path to cloned projects")
self._parser.add_argument("-m", "--method",
type = CloneMethod.parse,
dest = "method",
default = self._config.get_env("GITLAB_CLONE_METHOD", "http"),
metavar = "<method>",
choices = list(CloneMethod),
help = "method used in GitLabRC to cloning repositories (either <http> or <ssh>)")
self._parser.add_argument("--disable-root",
action="store_true",
dest="noroot",
default=False,
help="do not create root namepace folder in path")
action ="store_true",
dest = "noroot",
default = False,
help = "don't create root namepace folder in path")
self._parser.add_argument("--dry-run",
action="store_true",
dest="dryrun",
default=False,
help="list the repositories without clone/fetch")
action = "store_true",
dest = "dryrun",
default = False,
help = "list all repositories without clone/fetch")
self._parser.add_argument("--version",
action="store_true",
help="show version")
action = "store_true",
help = "show version")
10 changes: 7 additions & 3 deletions gitlabrc/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import optparse
import subprocess
from .cli import Arguments
from .method import CloneMethod
from . import __version__ as VERSION

from art import *
Expand Down Expand Up @@ -110,15 +111,18 @@ def perform(options):
clone_path = options.path + "/" + "/".join(str(x) for x in folders)
clone_path = re.sub("/+", "/", clone_path)
print(pname() + " folder " + clone_path)

project_url = project.http_url_to_repo if options.method is CloneMethod.HTTP else project.ssh_url_to_repo

if not os.path.isdir(clone_path):
print(pname() + " cloning " + project.http_url_to_repo)
print(pname() + " cloning " + project_url)
try:
subprocess.run(["git", "clone", project.http_url_to_repo, clone_path])
subprocess.run(["git", "clone", project_url, clone_path])
except:
sys.stderr.write("Unexpected error while cloning: terminating\n")
exit(2)
else:
print(pname() + " fetching " + project.http_url_to_repo)
print(pname() + " fetching " + project_url)
try:
subprocess.run(["git", "-C", clone_path, "fetch", "--all"])
except:
Expand Down
2 changes: 2 additions & 0 deletions gitlabrc/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
Clone all projects inside specific namespace in specific directory:
gitlabcr -u $GITLAB_URL -t $GITLAB_TOKEN -n msp/charts -p /home/ubuntu
Getting projects with specific git clone method:
gitlabcr -u $GITLAB_URL -t $GITLAB_TOKEN -n msp/charts -p /home/ubuntu -m http
"""
14 changes: 14 additions & 0 deletions gitlabrc/method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from enum import Enum
from typing import Text, Type

"""
https://stackoverflow.com/questions/36932/how-can-i-represent-an-enum-in-python
"""

class CloneMethod(Enum):
HTTP = 1
SSH = 2

@staticmethod
def parse(method: Type[Text]) -> Enum:
return CloneMethod[method.upper()]
9 changes: 3 additions & 6 deletions gitlabrc/settings.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
# -*- coding: utf-8 -*-

from os import environ
from typing import Text
from typing import Text, Type, Optional

class Config:

@staticmethod
def get_env(env: Text) -> Text:
try:
return environ.get(env)
except KeyError as error:
print(f"Key Error: {error}")
def get_env(env: Type[Text], default: Optional[Type[Text]] = None) -> Text:
return environ.get(env, default)

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
# Import the README and use it as the long-description.
# Note: this will only work if 'README.md' is present in your MANIFEST.in file!
try:
with io.open(os.path.join(here, "README.md"), encoding="utf-8") as file:
LONG_DESCRIPTION = "\n" + file.read()
with io.open(os.path.join(here, "README.md"), encoding="utf-8") as longdesc:
LONG_DESCRIPTION = "\n" + longdesc.read()
except FileNotFoundError:
LONG_DESCRIPTION = DESCRIPTION

Expand Down

0 comments on commit 8a6e7f7

Please sign in to comment.