Skip to content

Commit

Permalink
chore: add .pre-commit-config; reformat with black (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roguelazer authored May 18, 2024
1 parent d412f4a commit 64c912c
Show file tree
Hide file tree
Showing 10 changed files with 296 additions and 238 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ jobs:
pip install -r requirements-tests.txt -e .
- name: Run pytest
run: py.test -vs tests/
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@f43a0e5ff2bd294095638e18286ca9a3d1956744
- uses: actions/setup-python@61a6322f88396a6271a6ee3565807d608ecaddd1
- uses: pre-commit/action@646c83fcd040023954eafda54b4db0192ce70507
27 changes: 27 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- id: fix-byte-order-marker
- id: check-symlinks
- id: check-shebang-scripts-are-executable
- id: check-yaml
- id: check-json
- id: check-toml
- id: check-merge-conflict
- id: check-ast
- repo: https://github.com/psf/black
rev: 24.1.1
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
4 changes: 2 additions & 2 deletions muttdown/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version_info = (0, 3, 5)
__version__ = '.'.join(map(str, version_info))
__author__ = 'James Brown <Roguelazer@gmail.com>'
__version__ = ".".join(map(str, version_info))
__author__ = "James Brown <Roguelazer@gmail.com>"
1 change: 0 additions & 1 deletion muttdown/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

from muttdown.main import main


sys.exit(main())
72 changes: 36 additions & 36 deletions muttdown/config.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import copy
import yaml
import subprocess
import os.path
from subprocess import check_output

import six
import yaml

# largely copied from my earlier work in fakemtpd


if hasattr(subprocess, 'check_output'):
check_output = subprocess.check_output
else:
def check_output(*args, **kwargs):
kwargs['stdout'] = subprocess.PIPE
p = subprocess.Popen(*args, **kwargs)
stdout, _ = p.communicate()
assert p.returncode == 0
return stdout


def _param_getter_factory(parameter):
def f(self):
return self._config[parameter]

f.__name__ = parameter
return f

Expand All @@ -32,6 +22,7 @@ class _ParamsAsProps(type):
Cool fact: you can override any of these properties by just defining
your own with the same name. Just like if they were statically defined!"""

def __new__(clsarg, name, bases, d):
cls = super(_ParamsAsProps, clsarg).__new__(clsarg, name, bases, d)
for parameter in cls._parameters.keys():
Expand All @@ -46,24 +37,25 @@ def __init__(self, message):
self.message = message

def __repr__(self):
return '%s(%r)' % (self.__class__.__name__, self.message)
return "%s(%r)" % (self.__class__.__name__, self.message)

def __str__(self):
return '%s(%r)' % (self.__class__.__name__, self.message)
return "%s(%r)" % (self.__class__.__name__, self.message)


@six.add_metaclass(_ParamsAsProps)
class Config(object):
_parameters = {
'smtp_host': '127.0.0.1',
'smtp_port': 25,
'smtp_ssl': True, # if false, do STARTTLS
'smtp_username': '',
'smtp_password': None,
'smtp_password_command': None,
'smtp_timeout': 10,
'css_file': None,
'sendmail': '/usr/sbin/sendmail',
"smtp_host": "127.0.0.1",
"smtp_port": 25,
"smtp_ssl": True, # if false, do STARTTLS
"smtp_username": "",
"smtp_password": None,
"smtp_password_command": None,
"smtp_timeout": 10,
"css_file": None,
"sendmail": "/usr/sbin/sendmail",
"assume_markdown": False,
}

def __init__(self):
Expand All @@ -73,17 +65,21 @@ def __init__(self):
def merge_config(self, d):
invalid_keys = set(d.keys()) - set(self._config.keys())
if invalid_keys:
raise ConfigError('Unexpected config keys: %s' % ', '.join(sorted(invalid_keys)))
raise ConfigError(
"Unexpected config keys: %s" % ", ".join(sorted(invalid_keys))
)
for key in self._config:
if key in d:
self._config[key] = d[key]
if self._config['smtp_password'] and self._config['smtp_password_command']:
raise ConfigError('Cannot set smtp_password *and* smtp_password_command')
if self._config['css_file']:
if self._config["smtp_password"] and self._config["smtp_password_command"]:
raise ConfigError("Cannot set smtp_password *and* smtp_password_command")
if self._config["css_file"]:
self._css = None
self._config['css_file'] = os.path.expanduser(self._config['css_file'])
if not os.path.exists(self._config['css_file']):
raise ConfigError('CSS file %s does not exist' % self._config['css_file'])
self._config["css_file"] = os.path.expanduser(self._config["css_file"])
if not os.path.exists(self._config["css_file"]):
raise ConfigError(
"CSS file %s does not exist" % self._config["css_file"]
)

def load(self, fobj):
d = yaml.safe_load(fobj)
Expand All @@ -93,15 +89,19 @@ def load(self, fobj):
def css(self):
if self._css is None:
if self.css_file is not None:
with open(os.path.expanduser(self.css_file), 'r') as f:
with open(os.path.expanduser(self.css_file), "r") as f:
self._css = f.read()
else:
self._css = ''
self._css = ""
return self._css

@property
def smtp_password(self):
if self._config['smtp_password_command']:
return check_output(self._config['smtp_password_command'], shell=True, universal_newlines=True).rstrip('\n')
if self._config["smtp_password_command"]:
return check_output(
self._config["smtp_password_command"],
shell=True,
universal_newlines=True,
).rstrip("\n")
else:
return self._config['smtp_password']
return self._config["smtp_password"]
3 changes: 1 addition & 2 deletions muttdown/debug.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
import email.iterators

import sys

email.iterators._structure(email.message_from_file(sys.stdin))
Loading

0 comments on commit 64c912c

Please sign in to comment.