Selaa lähdekoodia

Ensure backwards compatibility with Python 2

master
Pavel Šimerda 8 vuotta sitten
vanhempi
commit
be12d78710
2 muutettua tiedostoa jossa 34 lisäystä ja 32 poistoa
  1. +17
    -15
      lib/invoice/cli.py
  2. +17
    -17
      lib/invoice/db/base.py

+ 17
- 15
lib/invoice/cli.py Näytä tiedosto

@@ -1,4 +1,6 @@
#!/usr/bin/python3
# encoding: utf-8
from __future__ import print_function

import os, sys, argparse, datetime, subprocess
import invoice.db
@@ -71,17 +73,17 @@ class Application:

self.args = parser.parse_args()
log.setLevel(self.args.__dict__.pop("log_level"))
log.debug("Arguments: {}".format(self.args))
log.debug("Arguments: {0}".format(self.args))

def run(self):
try:
self.method(**vars(self.args))
except (SanityCheckError) as error:
print("Error: {} Use '--force' to suppress this check.".format(error), file=sys.stderr)
print("Error: {0} Use '--force' to suppress this check.".format(error), file=sys.stderr)
if log.isEnabledFor(logging.DEBUG):
raise
except invoice.db.DatabaseError as error:
print("Error: {}".format(error), file=sys.stderr)
print("Error: {0}".format(error), file=sys.stderr)
if log.isEnabledFor(logging.DEBUG):
raise

@@ -121,7 +123,7 @@ class Application:
self._edit(self.db.invoices[selector]._path)

def _edit(self, path):
log.debug("Editing file: {}".format(path))
log.debug("Editing file: {0}".format(path))
assert os.path.exists(path)
subprocess.call((self.editor, path))

@@ -144,12 +146,12 @@ class Application:

tmp_path = self.tmp_path.format(year=self.year)
output_path = self.output_path.format(year=self.year)
log.debug("tmp_path={}".format(tmp_path))
log.debug("tmp_path={0}".format(tmp_path))

tex_template = os.path.join(self.template_path, "invoice.tex")
tex_file = os.path.join(tmp_path, "{}.tex".format(invoice._name))
tmp_pdf_file = os.path.join(tmp_path, "{}.pdf".format(invoice._name))
pdf_file = os.path.join(output_path, "{}.pdf".format(invoice._name))
tex_file = os.path.join(tmp_path, "{0}.tex".format(invoice._name))
tmp_pdf_file = os.path.join(tmp_path, "{0}.pdf".format(invoice._name))
pdf_file = os.path.join(output_path, "{0}.pdf".format(invoice._name))

if generate:
#if(not os.path.exists(pdf_file) or
@@ -161,9 +163,9 @@ class Application:
issuer_data = issuer.data()
customer_data = customer.data()

log.debug("Invoice: {}".format(invoice_data._data))
log.debug("Issuer: {}".format(issuer_data._data))
log.debug("Customer: {}".format(customer_data._data))
log.debug("Invoice: {0}".format(invoice_data._data))
log.debug("Issuer: {0}".format(issuer_data._data))
log.debug("Customer: {0}".format(customer_data._data))

log.debug("Creating TeX invoice...")
self._check_path(self.tmp_path)
@@ -173,7 +175,7 @@ class Application:
assert(os.path.exists(tex_file))

log.debug("Creating PDF invoice...")
if subprocess.call((self.tex_program, "{}.tex".format(invoice._name)), cwd=tmp_path) != 0:
if subprocess.call((self.tex_program, "{0}.tex".format(invoice._name)), cwd=tmp_path) != 0:
raise GenerationError("PDF generation failed.")
assert(os.path.exists(tmp_pdf_file))

@@ -187,7 +189,7 @@ class Application:

def _check_path(self, path):
if not os.path.exists(path):
raise LookupError("Directory doesn't exist: {}".format(path))
raise LookupError("Directory doesn't exist: {0}".format(path))

def do_delete(self, selector, force):
"""List invoices."""
@@ -228,7 +230,7 @@ class Application:
self._show(item._path)

def _show(self, path):
log.debug("Viewing file: {}".format(path))
log.debug("Viewing file: {0}".format(path))
assert os.path.exists(path)
subprocess.call((self.viewer, path))

@@ -239,6 +241,6 @@ class Application:
invoices = self.db.invoices.select({"company_name": company._name})
if invoices:
for invoice in invoices:
log.info("Dependent invoice: {}".format(invoice))
log.info("Dependent invoice: {0}".format(invoice))
raise SanityCheckError("This company is used by some invoices. You should not delete it.")
company.delete()

+ 17
- 17
lib/invoice/db/base.py Näytä tiedosto

@@ -17,7 +17,7 @@ class ItemNameCheckError(DatabaseError, ValueError):
class ItemExistsError(DatabaseError):
pass

class List:
class List(object):
"""Base class for database lists.
This class provides a real-time view to a file-based database. It can be
@@ -29,7 +29,7 @@ class List:
self._path = os.path.expanduser(data_path.format(
year=year, directory=self._directory))
self._db = db
log.debug("{}: {}".format(self.__class__.__name__, self._path))
log.debug("{0}: {1}".format(self.__class__.__name__, self._path))

def _item_class(self):
"""Returns class object used to instantiate items.
@@ -57,9 +57,9 @@ class List:
items = self.select(selector)
assert(len(items) < 2)
if not items:
raise ItemNotFoundError("{} '{}' not found.".format(self._item_class().__name__, selector))
raise ItemNotFoundError("{0} '{1}' not found.".format(self._item_class().__name__, selector))
item = items[0]
log.debug("Found matching item: {}".format(item))
log.debug("Found matching item: {0}".format(item))
return item

def select(self, selector=None):
@@ -84,7 +84,7 @@ class List:
selector = {"name": selector}
elif isinstance(selector, int):
selector = {"number": selector}
log.debug("Selecting: {}".format(selector))
log.debug("Selecting: {0}".format(selector))
assert isinstance(selector, dict)
return [item for item in self
if all(getattr(item, key) == selector[key] for key in selector)]
@@ -98,21 +98,21 @@ class List:
Returns
"""
log.info("Creating {}: {}".format(self._item_name(), name))
log.info("Creating {0}: {1}".format(self._item_name(), name))
if not self._regex.match(name):
raise ItemNameCheckError("Name {} doesn't match {} regex.".format(name, self._item_name()))
raise ItemNameCheckError("Name {0} doesn't match {1} regex.".format(name, self._item_name()))
if name in self:
raise ItemExistsError("Item {} of type {} already exists.".format(name, self._item_name()))
raise ItemExistsError("Item {0} of type {1} already exists.".format(name, self._item_name()))
self._new(os.path.join(self._path, name))
return self[name]

def _new(self, path):
log.debug("Creating {} file: {}".format(self._item_name(), path))
log.debug("Creating {0} file: {1}".format(self._item_name(), path))
stream = os.fdopen(os.open(path, os.O_WRONLY|os.O_EXCL|os.O_CREAT, 0o644), "w")
stream.write(self.data_template)
stream.close()

class Item:
class Item(object):
"""Base class for database list items."""
def __init__(self, list_, **selector):
self._list = list_
@@ -120,7 +120,7 @@ class Item:
self._postprocess()
self._name = self._list._template.format(**selector)
self._path = os.path.join(list_._path, self._name)
log.debug("{!r}".format(self))
log.debug("{0!r}".format(self))

def _postprocess(self):
"""Postprocess the _selector attribute.
@@ -132,7 +132,7 @@ class Item:
return self._name < other._name

def __repr__(self):
return "{}({!r}, **{})".format(self.__class__.__name__, self._name, self._selector)
return "{0}({1!r}, **{2})".format(self.__class__.__name__, self._name, self._selector)

def __str__(self):
return self._name
@@ -141,10 +141,10 @@ class Item:
return self._selector[key]

def delete(self):
log.info("Deleting: {}".format(self))
log.info("Deleting: {0}".format(self))
path = self._path
newpath = path + "~"
log.debug("Renaming file {} to {}.".format(path, newpath))
log.debug("Renaming file {0} to {1}.".format(path, newpath))
assert os.path.exists(path)
os.rename(path, newpath)

@@ -158,7 +158,7 @@ class Item:
def _data_class(self):
return Data

class Data:
class Data(object):
"""Base class for database list item data objects."""
_fields = []
_multivalue_fields = []
@@ -181,7 +181,7 @@ class Data:
for n, line in enumerate(stream):
match = self._line_regex.match(line)
if not match:
log.warning("Ignoring {}:{}: {}".format(n, self._item._name, line))
log.warning("Ignoring {0}:{1}: {2}".format(n, self._item._name, line))
continue
key, value = match.groups()
key = key.lower().replace("-", "_")
@@ -190,7 +190,7 @@ class Data:
elif key in self._multivalue_fields:
self._data[key].append(value)
else:
log.warning("Key ignored: {}".format(key))
log.warning("Key ignored: {0}".format(key))

def _postprocess(self):
"""Postprocess item data.


Loading…
Peruuta
Tallenna