Skip to content

Commit

Permalink
More python linting
Browse files Browse the repository at this point in the history
When I opened some of these files, my linter threw a party.

These are only be cosmetic changes and should have no functional impact.
  • Loading branch information
jeffwidman committed Jan 12, 2019
1 parent 1d9f3d3 commit fb1e331
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 47 deletions.
46 changes: 25 additions & 21 deletions tarsnapper/config.py
@@ -1,5 +1,7 @@
"""Deal with jobs defined in a config file. The format is YAML that looks
like this:
"""
Deal with jobs defined in a config file.
The format is YAML that looks like this:
# Global values, valid for all jobs unless overridden:
deltas: 1d 7d 30d
Expand Down Expand Up @@ -36,13 +38,15 @@
source: /important-2/
delta: important
"""
from __future__ import print_function

from datetime import timedelta
from string import Template
import glob
import yaml
from string import Template
import os

import yaml


__all__ = ('Job', 'load_config', 'load_config_from_file', 'ConfigError',)

Expand All @@ -52,8 +56,7 @@ class ConfigError(Exception):


class Job(object):
"""Represent a single backup job.
"""
"""Represent a single backup job."""

def __init__(self, **initial):
self.name = initial.get('name')
Expand All @@ -69,12 +72,13 @@ def __init__(self, **initial):


def require_placeholders(text, placeholders, what):
"""Ensure that ``text`` contains the given placeholders.
"""
Ensure that ``text`` contains the given placeholders.
Raises a ``ConfigError`` using ``what`` in the message, or returns
the unmodified text.
"""
if not text is None:
if text is not None:
for var in placeholders:
if Template(text).safe_substitute({var: 'foo'}) == text:
raise ConfigError(('%s must make use of the following '
Expand All @@ -84,8 +88,7 @@ def require_placeholders(text, placeholders, what):


def str_to_timedelta(text):
"""Parse a string to a timedelta value.
"""
"""Parse a string to a timedelta value."""
if text.endswith('s'):
return timedelta(seconds=int(text[:-1]))
elif text.endswith('h'):
Expand All @@ -96,8 +99,7 @@ def str_to_timedelta(text):


def parse_deltas(delta_string):
"""Parse the given string into a list of ``timedelta`` instances.
"""
"""Parse the given string into a list of ``timedelta`` instances."""
if delta_string is None:
return None

Expand All @@ -116,6 +118,7 @@ def parse_deltas(delta_string):

return deltas


def parse_named_deltas(named_delta_dict):
named_deltas = {}
for name, deltas in named_delta_dict.items():
Expand All @@ -124,6 +127,7 @@ def parse_named_deltas(named_delta_dict):
named_deltas[name] = parse_deltas(deltas)
return named_deltas


def load_config(text):
"""Load the config file and return a dict of jobs, with the local
and global configurations merged.
Expand All @@ -147,37 +151,37 @@ def load_job(job_name, job_dict):
job_dict = job_dict or {}
# sources
if 'sources' in job_dict and 'source' in job_dict:
raise ConfigError(('%s: Use either the "source" or "sources" '+
'option, not both') % job_name)
raise ConfigError(('%s: Use either the "source" or "sources" ' +
'option, not both') % job_name)
if 'source' in job_dict:
sources = [job_dict.pop('source')]
else:
sources = job_dict.pop('sources', None)
# aliases
if 'aliases' in job_dict and 'alias' in job_dict:
raise ConfigError(('%s: Use either the "alias" or "aliases" '+
'option, not both') % job_name)
raise ConfigError(('%s: Use either the "alias" or "aliases" ' +
'option, not both') % job_name)
if 'alias' in job_dict:
aliases = [job_dict.pop('alias')]
else:
aliases = job_dict.pop('aliases', None)
# excludes
if 'excludes' in job_dict and 'exclude' in job_dict:
raise ConfigError(('%s: Use either the "excludes" or "exclude" '+
'option, not both') % job_name)
raise ConfigError(('%s: Use either the "excludes" or "exclude" ' +
'option, not both') % job_name)
if 'exclude' in job_dict:
excludes = [job_dict.pop('exclude')]
else:
excludes = job_dict.pop('excludes', [])
# deltas
if 'deltas' in job_dict and 'delta' in job_dict:
raise ConfigError(('%s: Use either the "deltas" or "delta" '+
'option, not both') % job_name)
raise ConfigError(('%s: Use either the "deltas" or "delta" ' +
'option, not both') % job_name)
if 'delta' in job_dict:
delta_name = job_dict.pop('delta', None)
if delta_name not in named_deltas:
raise ConfigError(('%s: Named delta "%s" not defined')
% (job_name,delta_name))
% (job_name, delta_name))
deltas = named_deltas[delta_name]
else:
deltas = parse_deltas(job_dict.pop('deltas', None)) or default_deltas
Expand Down
1 change: 0 additions & 1 deletion tarsnapper/expire.py
@@ -1,5 +1,4 @@
import operator
from datetime import datetime, timedelta


__all__ = ('expire',)
Expand Down
19 changes: 11 additions & 8 deletions tarsnapper/script.py
@@ -1,18 +1,20 @@
from __future__ import print_function

import argparse
import dateutil.parser
from datetime import datetime
import getpass
from io import StringIO
import logging
import os
import pexpect
import re
from os import path
from string import Template
import subprocess
import re
import sys
import uuid

from datetime import datetime
from io import StringIO
from os import path
from string import Template
import dateutil.parser
import pexpect

from . import config, expire
from .config import Job
Expand All @@ -27,7 +29,8 @@ class TarsnapError(Exception):


class TarsnapBackend(object):
"""The code that calls the tarsnap executable.
"""
The code that calls the tarsnap executable.
One of the reasons this is designed as a class is to allow the backend
to mimimize the calls to "tarsnap --list-archives" by caching the result.
Expand Down
15 changes: 7 additions & 8 deletions tarsnapper/test.py
@@ -1,18 +1,17 @@
from datetime import datetime
from .expire import expire as default_expire_func
from .config import parse_deltas
from .script import parse_date


__all__ = ('BackupSimulator',)


try:
from collections import OrderedDict # Python 2.7
except ImportError:
# Install from: http://pypi.python.org/pypi/ordereddict
from ordereddict import OrderedDict

from .config import parse_deltas
from .expire import expire as default_expire_func
from .script import parse_date


__all__ = ('BackupSimulator',)


class BackupSimulator(object):
"""Helper to simulate making backups, and expire old ones, at
Expand Down
9 changes: 8 additions & 1 deletion tests/test_config.py
@@ -1,6 +1,7 @@
from tarsnapper.config import load_config, ConfigError
from nose.tools import assert_raises

from tarsnapper.config import load_config, ConfigError


def test_empty_config():
assert_raises(ConfigError, load_config, """
Expand Down Expand Up @@ -190,6 +191,7 @@ def test_source_and_sources():
/var
""")


def test_alias_and_aliases():
"""You can't use both options at the same time."""
assert_raises(ConfigError, load_config, """
Expand All @@ -203,6 +205,7 @@ def test_alias_and_aliases():
moo
""")


def test_exclude_and_excludes():
"""You can't use both options at the same time."""
assert_raises(ConfigError, load_config, """
Expand All @@ -216,6 +219,7 @@ def test_exclude_and_excludes():
moo
""")


def test_named_delta():
c = load_config("""
target: $name-$date
Expand All @@ -237,6 +241,7 @@ def test_named_delta():
assert len(c[0]['foo'].deltas) == 3
assert len(c[0]['bar'].deltas) == 4


def test_unspecified_named_delta():
assert_raises(ConfigError, load_config, """
target: $name-$date
Expand All @@ -248,6 +253,7 @@ def test_unspecified_named_delta():
delta: myDelta
""")


def test_undefined_named_delta():
assert_raises(ConfigError, load_config, """
target: $name-$date
Expand All @@ -259,6 +265,7 @@ def test_undefined_named_delta():
delta: importantDelta
""")


def test_named_delta_and_deltas():
"""You can't use both named delta and deltas at the same time."""
assert_raises(ConfigError, load_config, """
Expand Down
3 changes: 2 additions & 1 deletion tests/test_expire.py
Expand Up @@ -8,6 +8,7 @@

from tarsnapper.test import BackupSimulator


def test_failing_keep():
"""This used to delete backup B, because we were first looking
for a seven day old backup, finding A, then looking for a six day
Expand All @@ -22,4 +23,4 @@ def test_failing_keep():
'20100620-000000', # C
])
delete, keep = s.expire()
assert not delete
assert not delete
13 changes: 6 additions & 7 deletions tests/test_script.py
@@ -1,15 +1,15 @@
from io import StringIO
import argparse
from datetime import datetime
import logging
from os import path
import re
import shutil
import tempfile
import logging
import argparse
from datetime import datetime

from tarsnapper.config import Job, parse_deltas, str_to_timedelta
from tarsnapper.script import (
TarsnapBackend, MakeCommand, ListCommand, ExpireCommand, parse_args,
DEFAULT_DATEFORMAT)
from tarsnapper.config import Job, parse_deltas, str_to_timedelta


class FakeBackend(TarsnapBackend):
Expand Down Expand Up @@ -176,7 +176,6 @@ def test_no_deltas(self):
])
assert cmd.backend.match([])


def test_something_to_expire(self):
cmd = self.run(self.job(deltas='1d 2d'), [
self.filename('1d'),
Expand Down Expand Up @@ -222,4 +221,4 @@ def test(self):
# necessary.
assert cmd.backend.match([
('--list-archives',)
])
])

0 comments on commit fb1e331

Please sign in to comment.