Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edickinson release candidate #3

Merged
merged 6 commits into from
Jul 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[flake8]
ignore = F401
exclude = .git,.github,examples,__pycache__,docs
max-complexity = 10
ignore = F401,C901,F811,E203,W503,E501
exclude = .git,.github,examples,__pycache__,docs,venv,env,.venv,build,examples
max-complexity = 10
max-line-length = 120
builtins = unicode
tee = True
11 changes: 11 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# To get started with Dependabot version updates, you'll need to specify which
# package ecosystems to update and where the package manifests are located.
# Please see the documentation for all configuration options:
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates

version: 2
updates:
- package-ecosystem: "pip"
directory: "/requirements.txt" # Location of package manifests
schedule:
interval: "daily"
20 changes: 19 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
examples/
examples/

build/

dist/

pyterraformer.egg-info/

venv/

.idea/

__pycache__/

.terraform/

.terraform.lock.hcl

/scripts/local_execution.py
2 changes: 2 additions & 0 deletions pyterraformer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from .terraform.backends import LocalBackend
from .terraform.terraform import Terraform

__version__ = "0.0.1-rc.1"

__all__ = [
"HumanSerializer",
"Config",
Expand Down
8 changes: 3 additions & 5 deletions pyterraformer/config.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import atexit
import shutil
from dataclasses import dataclass
from tempfile import mkdtemp
from typing import Optional

from pyterraformer.settings import get_default_terraform_location

tempdir = mkdtemp()

atexit.register(shutil.rmtree, tempdir)
# tempdir = mkdtemp()
#
# atexit.register(shutil.rmtree, tempdir)


@dataclass
Expand Down
41 changes: 41 additions & 0 deletions pyterraformer/core/generics/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,44 @@
from .terraform import TerraformConfig
from .terraform_block import BlockList, BlockSet
from .variables import Variable

__all__ = [
"Variable",
"BlockList",
"BlockSet",
"TerraformConfig",
"Output",
"Local",
"Literal",
"Metadata",
"DependsOn",
"Provider",
"ForEach",
"Count",
"Lifecycle",
"Backend",
"Comment",
"Data",
"Interpolation",
"DictLookup",
"PropertyLookup",
"StringLit",
"String",
"Expression",
"Conditional",
"BinaryOp",
"BinaryOperator",
"BinaryTerm",
"Parenthetical",
"File",
"Boolean",
"Merge",
"Concat",
"Replace",
"Types",
"ArrayLookup",
"GenericFunction",
"Symlink",
"LegacySplat",
"ToSet",
]
6 changes: 2 additions & 4 deletions pyterraformer/core/generics/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@


class Backend(TerraformObject):
def __init__(self, name:str,
metadata: Optional[ObjectMetadata] = None,
**kwargs,):
TerraformObject.__init__(self, "backend", metadata=metadata, **kwargs)
def __init__(self, name: str, _metadata: Optional[ObjectMetadata] = None, **kwargs):
TerraformObject.__init__(self, "backend", _metadata=_metadata, **kwargs)
self.name = str(name).replace('"', "")

# def render(self, variables=None):
Expand Down
23 changes: 14 additions & 9 deletions pyterraformer/core/generics/comment.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
from pyterraformer.core.objects import TerraformObject
from typing import Optional

from pyterraformer.core.objects import TerraformObject, ObjectMetadata


class Comment(TerraformObject):
def __init__(self, text, multiline=False):
def __init__(
self,
text: str,
multiline: bool = False,
_metadata: Optional[ObjectMetadata] = None,
):
self.multiline = multiline
if self.multiline:
text = f"/*{text}*/"
TerraformObject.__init__(self, "comment", text, None)

def __repr__(self):
return f"{self._type}({self._original_text})"
else:
text = text.strip()
TerraformObject.__init__(
self, "comment", tf_id=None, text=text, _metadata=_metadata
)

@property
def has_content(self):
return bool(self.text.strip())

def render(self, variables=None):
return self.template.render(multiline=self.multiline, text=self._original_text)
11 changes: 5 additions & 6 deletions pyterraformer/core/generics/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
from dataclasses import dataclass
from typing import Any, Optional, List, TYPE_CHECKING


if TYPE_CHECKING:
from pyterraformer.core.namespace import TerraformFile, TerraformNamespace
from pyterraformer.core.namespace import TerraformFile
from pyterraformer.core.workspace import TerraformWorkspace


Expand Down Expand Up @@ -275,12 +274,12 @@ def resolve(


class StringLit(Resolvable):
def __init__(self, contents:List):
def __init__(self, contents: List):
self.contents = contents

@property
def string(self)->str:
return ''.join([str(v) for v in self.contents])
def string(self) -> str:
return "".join([str(v) for v in self.contents])

def __add__(self, v):
return self.string.__add__(v)
Expand Down Expand Up @@ -603,7 +602,7 @@ def __init__(self, name, values):
key = str(attribute[0])
val = attribute[1]
if isinstance(val, Block):
base = getattr(self, key, Block())
base = getattr(self, key, Block)
base.append(val)
val = base
setattr(self, key, val)
Expand Down
7 changes: 2 additions & 5 deletions pyterraformer/core/generics/meta_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@


class Provider(TerraformObject):
def __init__(self,
type:str,
metadata: Optional[ObjectMetadata] = None,
**kwargs,):
def __init__(self, type: str, _metadata: Optional[ObjectMetadata] = None, **kwargs):
self.ptype = str(type).replace('"', "")
TerraformObject.__init__(self, type = "provider", metadata=metadata)
TerraformObject.__init__(self, type="provider", _metadata=_metadata)

def __repr__(self):
return (
Expand Down
11 changes: 2 additions & 9 deletions pyterraformer/core/generics/terraform.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,9 @@


class TerraformConfig(TerraformObject):
def __init__(self,
metadata: Optional[ObjectMetadata] = None,
**kwargs, ):
def __init__(self, _metadata: Optional[ObjectMetadata] = None, **kwargs):
# self.backends = [obj for obj in attributes if isinstance(obj, Backend)]
TerraformObject.__init__(
self,
"terraform",
metadata=metadata,
**kwargs
)
TerraformObject.__init__(self, "terraform", _metadata=_metadata, **kwargs)

# def render(self, variables=None):
# from pyterraformer.core.generics import Block, Literal
Expand Down
1 change: 1 addition & 0 deletions pyterraformer/core/generics/terraform_block.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class BlockList(list):
pass


class BlockSet(set):
pass
20 changes: 12 additions & 8 deletions pyterraformer/core/generics/variables.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from pyterraformer.core.objects import TerraformObject
from typing import TYPE_CHECKING

if TYPE_CHECKING:
from pyterraformer.core.generics import Literal


class Variable(TerraformObject):
Expand All @@ -22,28 +26,28 @@ def __getitem__(self, val):
return item
raise KeyError(val)

def render_lookup(self, item):
def render_lookup(self, item) -> "Literal":
from pyterraformer.core.generics import Literal

return Literal(f'var.{self.name}["{item}"]')

def render_attribute(self, item):
def render_attribute(self, item) -> "Literal":
from pyterraformer.core.generics import Literal

return Literal(f"var.{self.name}.{item}")

def render_basic(self):
def render_basic(self) -> "Literal":
from pyterraformer.core.generics import Literal

return Literal(f"var.{self.name}")

def get(self, val, fallback=None):
def get(self, val, fallback: str = None):
for key, item in self.default.items():
if val == key:
return item
return fallback

def get_type(self, val):
def get_type(self, val) -> "Literal":
from pyterraformer.core.generics.interpolation import String
from pyterraformer.core.generics import Literal, StringLit

Expand All @@ -55,9 +59,9 @@ def get_type(self, val):
out = set()
for item in val.values():
out.add(self.get_type(item).value)
out = list(out)
if len(out) == 1:
return Literal(f"map({out[0]})")
outlist = list(out)
if len(outlist) == 1:
return Literal(f"map({outlist[0]})")
return Literal("map(any)")
elif isinstance(val, set):
return Literal(f"set({self.get_type(next(iter(val))).value})")
Expand Down
8 changes: 5 additions & 3 deletions pyterraformer/core/objects.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Dict, Any, List, Optional, TYPE_CHECKING
from typing import Dict, Optional, TYPE_CHECKING

from pyterraformer.exceptions import ValidationError

Expand All @@ -12,18 +12,20 @@ class ObjectMetadata:
source_file: Optional[str] = None
orig_text: Optional[str] = None
row_num: Optional[int] = None
start_pos: Optional[int] = None
end_pos: Optional[int] = None


class TerraformObject(object):
def __init__(
self,
type,
tf_id: Optional[str] = None,
metadata: Optional[ObjectMetadata] = None,
_metadata: Optional[ObjectMetadata] = None,
**kwargs,
):

self.metadata = metadata or ObjectMetadata()
self._metadata = _metadata or ObjectMetadata()
self.id = tf_id
arguments = kwargs or {}
self.render_variables: Dict[str, str] = {
Expand Down
7 changes: 2 additions & 5 deletions pyterraformer/core/resources/resource_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ class ResourceObject(TerraformObject):
_type = "generic_resource_object"

def __init__(
self,
tf_id: str,
metadata: Optional[ObjectMetadata] = None,
**kwargs,
self, tf_id: str, _metadata: Optional[ObjectMetadata] = None, **kwargs
):
TerraformObject.__init__(self, self._type, tf_id, metadata=metadata, **kwargs)
TerraformObject.__init__(self, self._type, tf_id, _metadata=_metadata, **kwargs)

def render_attribute(self, item):
return f"${{{self._type}.{self.id}.{item}}}"
Expand Down
Loading