-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create interface for manipulating objects in github files
* Create new interface for manipulating objects in files * Update README with new interface * Add comment explaining YAML representer * Add test for BaseFile * Make load/dump methods internal
- Loading branch information
Showing
13 changed files
with
158 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .base_file import BaseFile | ||
from .yaml_file import YamlFile | ||
from .json_file import JsonFile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class BaseFile: | ||
|
||
def __init__(self, github_file, repo, start=0): | ||
self.github_file = github_file | ||
self.repo = repo | ||
self.num = start | ||
self.file_contents = github_file.decoded_content | ||
self.objects = self._load_objects() | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __next__(self): | ||
if self.num == len(self.objects): | ||
raise StopIteration | ||
|
||
num = self.num | ||
self.num += 1 | ||
return self.objects[num] | ||
|
||
def save(self, message, dry_run): | ||
self.repo.update_file(self.github_file, self._dump(), message, dry_run) | ||
|
||
def _load_objects(self): | ||
raise NotImplementedError | ||
|
||
def _dump(self): | ||
raise NotImplementedError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from . import BaseFile | ||
import json | ||
|
||
class JsonFile(BaseFile): | ||
|
||
def __init__(self, github_file, repo): | ||
super().__init__(github_file, repo) | ||
|
||
def _load_objects(self): | ||
return json.loads(self.file_contents) | ||
|
||
def _dump(self): | ||
return json.dumps(self.objects, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from . import BaseFile | ||
import yaml | ||
|
||
class YamlFile(BaseFile): | ||
|
||
def __init__(self, github_file, repo): | ||
super().__init__(github_file, repo) | ||
yaml.add_representer(type(None), represent_none) | ||
|
||
def _load_objects(self): | ||
return list(yaml.safe_load_all(self.file_contents)) | ||
|
||
def _dump(self): | ||
return yaml.dump_all(self.objects, default_flow_style=False, explicit_start=True) | ||
|
||
def represent_none(self, _): | ||
# Disable dumping 'null' string for null values | ||
# Taken from here: https://stackoverflow.com/a/41786451 | ||
return self.represent_scalar('tag:yaml.org,2002:null', '') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import unittest | ||
from unittest.mock import MagicMock | ||
from gordian.repo import Repo | ||
from gordian.files import YamlFile | ||
from .utils import Utils | ||
|
||
class TestBaseFile(unittest.TestCase): | ||
|
||
def setUp(self): | ||
self.github_file = Utils.create_github_content_file() | ||
self.mock_git = MagicMock() | ||
self.repo = Repo('test', git=self.mock_git) | ||
self.base_file = YamlFile(self.github_file, self.repo) | ||
|
||
def test_iterable(self): | ||
assert(iter(self.base_file)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import base64 | ||
from github import ContentFile | ||
|
||
class Utils: | ||
|
||
def create_github_content_file(): | ||
f = open('./tests/fixtures/content.yaml', 'r') | ||
contents = str(base64.b64encode(bytearray(f.read(), 'utf-8')), 'utf-8') | ||
attributes = {'name':'content.yaml', 'path':'/content.yaml','encoding':'base64','content': contents} | ||
return ContentFile.ContentFile(None, {}, attributes, completed=True) |