Source code for nginx.config.api.options

from .base import Base

[docs]class KeyOption(Base): """ A KeyOption represents a directive with no value. For example: """ def __init__(self, name): = self.value = name def __repr__(self): return self._render( '{name};'.format(, ) )
[docs]class KeyValueOption(Base): """ A key/value directive. This covers most directives available for Nginx """ def __init__(self, name, value=''): = name if isinstance(value, bool): self.value = 'off' if value is False else 'on' elif isinstance(value, int): self.value = str(value) elif isinstance(value, list): self.value = [str(e) for e in value] else: self.value = value def __repr__(self): return self._render( '{name} {value};'.format(, value=self.value ) )
[docs]class KeyMultiValueOption(KeyValueOption): """ A key with multiple space delimited options. For example: Example:: >>> from nginx.config.api.options import KeyMultiValueOption >>> a_log = KeyMultiValueOption('access_log', ['/path/to/log.gz', 'combined', 'gzip', 'flush=5m']) >>> print(a_log) access_log /path/to/log.gz combined gzip flush=5m; """ def __repr__(self): return self._render( '{name} {value};'.format(, value=' '.join(self.value) ) )
class KeyValuesMultiLines(Base): def __init__(self, name, values=[]): = name self.lines = [] for value in values: if isinstance(value, list): self.lines.append(' '.join([str(v) for v in value])) else: self.lines.append(str(value)) def __repr__(self): return ''.join( [self._render('{name} {value};'.format(, value=line)) for line in self.lines] )
[docs]class Comment(Base): """ A simple comment object. """ _offset = '' _comment = '' def __init__(self, offset='', comment='', **kwargs): self._offset = offset self._comment = comment super(Comment, self).__init__(**kwargs) def __repr__(self): return self._render( '{offset}# {comment}'.format( offset=self._offset, comment=self._comment, ) )
class AttrDict(dict): """ A dictionary that exposes it's values as attributes. """ def __init__(self, owner): self.__dict__ = self self._owner = owner def __setitem__(self, key, val): if hasattr(val, '_parent'): val._parent = self._owner return super(AttrDict, self).__setitem__(key, val) def __repr__(self): owner = self.pop('_owner') ret = super(AttrDict, self).__repr__() self._owner = owner return ret class AttrList(AttrDict): """ A dictionary/list hybrid that exposes values as attributes. """ def __iter__(self): return iter(self.values()) def append(self, item): if hasattr(item, '_parent'): item._parent = self._owner if hasattr(item, 'name'): self[] = item else: self[hash(item)] = item def add(self, *items): for item in items: self.append(item) # alias for backwards compatibility KeyValuesMultilines = KeyValuesMultiLines