Skip to content

Commit

Permalink
Serialize options
Browse files Browse the repository at this point in the history
  • Loading branch information
AgarFu authored May 12, 2022
1 parent 40a2ee0 commit 0f15a7b
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 50 deletions.
10 changes: 6 additions & 4 deletions gordian/files/base_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

logger = logging.getLogger(__name__)

class BaseFile:

class BaseFile:
def __init__(self, github_file, repo, start=0):
self.github_file = github_file
self.repo = repo
Expand All @@ -22,11 +22,13 @@ def __next__(self):
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 save(self, message, dry_run, serialize_options={}):
self.repo.update_file(
self.github_file, self._dump(serialize_options), message, dry_run
)

def _load_objects(self):
raise NotImplementedError

def _dump(self):
def _dump(self, serialize_options={}):
raise NotImplementedError
40 changes: 20 additions & 20 deletions gordian/files/changelog_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import re
from datetime import datetime

CHANGELOG_ENTRY_REGEX = '.*\[([0-9]*\.[0-9]*\.[0-9]*)\] - [0-9]{4}-[0-9]{1,2}-[0-9]+'
CHANGELOG_ENTRY_REGEX = ".*\[([0-9]*\.[0-9]*\.[0-9]*)\] - [0-9]{4}-[0-9]{1,2}-[0-9]+"

class ChangelogFile(MarkdownFile):

class ChangelogFile(MarkdownFile):
def __init__(self, github_file, repo):
self.changelog_entry_types = [ 'added', 'updated', 'removed' ]
self.changelog_entry_types = ["added", "updated", "removed"]
self._generate_interface_methods()
super().__init__(github_file, repo)

def _load_objects(self):
string_lines = [line.decode('utf-8') for line in super()._load_objects()]
string_lines = [line.decode("utf-8") for line in super()._load_objects()]
self.header = []
changelog_entries = []
changelog_entry_block = []
Expand Down Expand Up @@ -52,27 +52,27 @@ def _load_objects(self):

changelog_entries.append(changelog_entry_block)
if footer_start:
self.footer = string_lines[footer_start:len(string_lines) - 1]
self.footer = string_lines[footer_start : len(string_lines) - 1]
else:
self.footer = []
self.footer = []

return changelog_entries

def _dump(self):
def _dump(self, serialize_options={}):
blocks = []
for o in self.objects:
blocks.append('\n'.join(o))
blocks.append("\n".join(o))

entries = self._format_new_changelog_entry()

lines = ['\n'.join(self.header)]
lines = ["\n".join(self.header)]
if entries:
lines.append('\n'.join(entries))
lines.append('')
lines.append('\n'.join(blocks))
lines.append('\n'.join(self.footer))
lines.append("\n".join(entries))
lines.append("")
lines.append("\n".join(blocks))
lines.append("\n".join(self.footer))

return '\n'.join(lines)
return "\n".join(lines)

def _format_new_changelog_entry(self):
lines = []
Expand All @@ -86,22 +86,22 @@ def _format_new_changelog_entry(self):

def _format_list_entry(self, list_type):
lines = []
entries = getattr(self, f'_{list_type}')
entries = getattr(self, f"_{list_type}")
for entry, ticket in entries:
if ticket:
line = f'{entry} [{ticket}]'
line = f"{entry} [{ticket}]"
else:
line = entry
lines.append(f'- {line}')
lines.append(f"- {line}")

if lines:
header = f'### {list_type.capitalize()}'
header = f"### {list_type.capitalize()}"
lines.insert(0, header)
return lines

def _generate_interface_methods(self):
for changelog_entry_type in self.changelog_entry_types:
internal_store = f'_{changelog_entry_type}'
internal_store = f"_{changelog_entry_type}"

def fn(self, entry, ticket=None, store=internal_store):
getattr(self, store).append((entry, ticket))
Expand All @@ -110,4 +110,4 @@ def fn(self, entry, ticket=None, store=internal_store):
setattr(ChangelogFile, internal_store, [])

def _format_date(self):
return datetime.now().strftime('%Y-%m-%d')
return datetime.now().strftime("%Y-%m-%d")
7 changes: 4 additions & 3 deletions gordian/files/json_file.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from . import BaseFile
import json

class JsonFile(BaseFile):

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)
def _dump(self, serialize_options={}):
indent = serialize_options.get("indent", 4)
return json.dumps(self.objects, indent=indent)
8 changes: 4 additions & 4 deletions gordian/files/markdown_file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from . import BaseFile

class MarkdownFile(BaseFile):

class MarkdownFile(BaseFile):
def __init__(self, github_file, repo):
super().__init__(github_file, repo)

def _load_objects(self):
return self.file_contents.split(b'\n')
return self.file_contents.split(b"\n")

def _dump(self):
return b'\n'.join(self.file_contents)
def _dump(self, serialize_options={}):
return b"\n".join(self.file_contents)
4 changes: 2 additions & 2 deletions gordian/files/plaintext_file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from . import BaseFile

class PlainTextFile(BaseFile):

class PlainTextFile(BaseFile):
def __init__(self, github_file, repo):
super().__init__(github_file, repo)

def _load_objects(self):
return self.file_contents

def _dump(self):
def _dump(self, serialize_options={}):
return self.file_contents
15 changes: 11 additions & 4 deletions gordian/files/yaml_file.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
from . import BaseFile
import yaml

class YamlFile(BaseFile):

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 _dump(self, serialize_options={}):
default_flow_style = serialize_options.get("default_flow_style", False)
explicit_start = serialize_options.get("explicit_start", True)
return yaml.dump_all(
self.objects,
default_flow_style=default_flow_style,
explicit_start=explicit_start,
)


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', '')
return self.represent_scalar("tag:yaml.org,2002:null", "")
18 changes: 8 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import setuptools

setup_reqs = ['pytest', 'pytest-cov', 'pytest-runner', 'flake8']
setup_reqs = ["pytest", "pytest-cov", "pytest-runner", "flake8"]
setuptools.setup(
name="gordian",
version="3.2.0",
version="3.3.0",
author="Intuit",
author_email="cg-sre@intuit.com",
description="A tool to search and replace files in a Git repo",
url="https://github.com/argoproj-labs/gordian",
install_requires=['pygithub', 'pyyaml', 'jsonpatch', 'deepdiff', 'retry'],
install_requires=["pygithub", "pyyaml", "jsonpatch", "deepdiff", "retry"],
setup_requires=setup_reqs,
extras_require={
'test': setup_reqs
},
extras_require={"test": setup_reqs},
tests_require=setup_reqs,
packages=['gordian', 'gordian.files'],
packages=["gordian", "gordian.files"],
entry_points={
'console_scripts': [
'gordian = gordian.gordian:main',
'pulpo = gordian.pulpo:main',
"console_scripts": [
"gordian = gordian.gordian:main",
"pulpo = gordian.pulpo:main",
],
},
)
28 changes: 25 additions & 3 deletions tests/test_base_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,36 @@
from gordian.files import YamlFile
from .utils import Utils

class TestBaseFile(unittest.TestCase):

class TestBaseFile(unittest.TestCase):
def setUp(self):
self.github_file = Utils.create_github_content_file()
self.mock_git = MagicMock()
self.repo = Repo('test', github=self.mock_git)
self.repo = Repo("test", github=self.mock_git)
self.base_file = YamlFile(self.github_file, self.repo)

def test_iterable(self):
assert(iter(self.base_file))
assert iter(self.base_file)


class TestYamlFile(unittest.TestCase):
def setUp(self):
self.github_file = Utils.create_github_content_file()
self.mock_git = MagicMock()
self.repo = Repo("test", github=self.mock_git)
self.base_file = YamlFile(self.github_file, self.mock_git)

def test_save_options(self):
self.base_file.save("Test message", False, {"explicit_start": False})
self.mock_git.update_file.assert_called_once_with(
self.github_file, "test:\n foo: bar\n iam: blah\n", "Test message", False
)

def test_save_default_options(self):
self.base_file.save("Test message", False)
self.mock_git.update_file.assert_called_once_with(
self.github_file,
"---\ntest:\n foo: bar\n iam: blah\n",
"Test message",
False,
)

0 comments on commit 0f15a7b

Please sign in to comment.