Skip to content

Commit

Permalink
Add renewal config stucture. Integrate with account objects
Browse files Browse the repository at this point in the history
  • Loading branch information
nocarryr committed Apr 7, 2016
1 parent aea45a6 commit 6a85889
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
1 change: 1 addition & 0 deletions letssync/structures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from letssync.structures import base
from letssync.structures import account
from letssync.structures import renewal

def build_tree(root_path):
return base.Directory(path=root_path)
17 changes: 15 additions & 2 deletions letssync/structures/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,32 @@ class Account(Directory):
Data is read from the files within the account directory:
'meta.json', 'private_key.json' and 'regr.json'
"""
serialize_attrs = ['meta', 'private_key', 'regr']
serialize_attrs = ['meta', 'private_key', 'regr', 'domains']
@classmethod
def _child_class_override(cls, child_class, **kwargs):
parent = kwargs.get('parent')
if parent.id == 'directory' and parent.__class__ is Accounts:
return Account
def read(self, **kwargs):
super(Account, self).read(**kwargs)
self.domains = kwargs.get('domains', [])
def add_child(self, cls, **kwargs):
cls = AccountFile
obj = super(Account, self).add_child(cls, **kwargs)
attr = os.path.splitext(obj.id)[0]
setattr(self, attr, obj.data)
return obj

def on_tree_built(self):
renewal = self.root.children['renewal']
for key in renewal.accounts[self.id].keys():
if key in self.domains:
continue
self.domains.append(key)
def __eq__(self, other):
r = super(Account, self).__eq__(other)
if not r:
return False
return set(self.domains) == set(other.domains)

class AccountFile(FileObjBase):
"""A file used to read and store data used in :class:`Account`
Expand Down
60 changes: 60 additions & 0 deletions letssync/structures/renewal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import sys
import os
import io

from configobj import ConfigObj

from letssync.structures.base import Directory, FileObj

PY2 = sys.version_info.major == 2

class Renewals(Directory):
def __init__(self, **kwargs):
self.domains = {}
self.accounts = {}
super(Renewals, self).__init__(**kwargs)
@classmethod
def _child_class_override(cls, child_class, **kwargs):
parent = kwargs.get('parent')
if kwargs.get('id') == 'renewal' and parent.parent is None:
return Renewals
def add_child(self, cls, **kwargs):
cls = RenewalConf
obj = super(Renewals, self).add_child(cls, **kwargs)
self.domains[obj.domain] = obj
if obj.account_id not in self.accounts:
self.accounts[obj.account_id] = {}
self.accounts[obj.account_id][obj.domain] = obj
return obj


class RenewalConf(FileObj):
serialize_attrs = ['account_id', 'domain']
def read(self, **kwargs):
super(RenewalConf, self).read(**kwargs)
if PY2:
b = io.BytesIO(self.content)
else:
b = io.StringIO(self.content)
self.config = ConfigObj(b)
self.account_id = kwargs.get('account_id')
if self.account_id is None:
try:
self.account_id = self.config['renewalparams']['account']
except TypeError:
print(self.config['renewalparams'], type(self.config['renewalparams']))
raise
self.domain = kwargs.get('domain')
if self.domain is None:
self.domain = self.id.rstrip('.conf')
def on_tree_built(self):
accounts = self.root.children['accounts']
self.account = accounts.accounts.get(self.account_id)
def _write(self, overwrite=False):
p = self.path
if os.path.exists(p) and overwrite is False:
return
with open(self.path, 'wb') as f:
self.config.write(f)
def __eq__(self, other):
return self.config.dict() == other.config.dict()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PyOpenSSL
configobj

0 comments on commit 6a85889

Please sign in to comment.