Skip to content

Commit

Permalink
Remove unused code. Coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
theotherbjorn committed Aug 7, 2020
1 parent 2b8c1ba commit b40da3d
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 98 deletions.
7 changes: 0 additions & 7 deletions dk/apps.py

This file was deleted.

149 changes: 62 additions & 87 deletions dk/utils.py
Expand Up @@ -2,7 +2,7 @@

"""FIXME: many of these really should go in their own modules...
"""
from typing import Any
from typing import Any, Optional, Union

from past.builtins import basestring
from builtins import str as text
Expand All @@ -19,7 +19,7 @@ def identity(x): # XXX: replace any usages of this function with lambda x:x!
return x


def srcpath(base, pth):
def srcpath(base, pth): # pragma: nocover
"""Return an absolute path based on the relative path `pth`.
Useful in tests, where we don't know what the absolute path is,
and we can't use relative paths since we don't know which folder
Expand All @@ -32,36 +32,16 @@ def srcpath(base, pth):
"""
raise EnvironmentError("dk.utils.srcpath no longer does anything useful.")
# # FIXME: __file__ is not srcroot when this file has been moved!
# # XXX: this no longer works as intended!
#
# srcroot = __file__.replace('\\', '/').rsplit('/', 1)[0]
#
# if not base:
# base = '/'
# base = base.replace('\\', '/')
# if base[0] != '/':
# base = '/' + base
# if base[-1] != '/':
# base += '/'
#
# return srcroot + base + pth.replace('\\', '/')


# def root(): # FIXME: this is fubar (__file__ isn't near the root of the source tree when utils is here...)
# "Return the root of the source tree."
# return __file__.replace('\\', '/').rsplit('/', 1)[0]
def root():


def root(): # pragma: nocover
"""Return the root of the source tree.
"""
raise EnvironmentError("dk.utils.root no longer does anything useful.")
# srcroot = __file__.replace('\\', '/').rsplit('/', 1)[0]
# srcroot = srcroot.replace("lib/dk/dk", "src/datakortet")
# return srcroot


# FIXME: this doesn't work since srcpath doesn't work!
def dkpath(pth=None):
def dkpath(pth=None): # pragma: nocover
"""Usage
::
Expand All @@ -72,11 +52,6 @@ def dkpath(pth=None):
"""
raise EnvironmentError("dk.utils.dkpath no longer does anything useful.")
# if not pth:
# pth = ''
# if pth.startswith('/'):
# pth = pth[1:]
# return os.path.normcase(os.path.normpath(srcpath('', pth)))


def hour_minute(v): # XXX: move to ttcal?
Expand Down Expand Up @@ -114,7 +89,7 @@ def lower_case(s, encoding='u8'):
return s.encode(encoding)


def ulower_case(val): # type: (Optional[text]) -> text
def ulower_case(val): # type: (Any) -> text
"""Call val.lower(). Return '' if val is None.
"""
if val is None:
Expand Down Expand Up @@ -146,14 +121,14 @@ def utitle_case(val):
_mcmac = re.compile(u'(Mc)|(Mac)|(Van)|(Von)')


def title_case_lastname(s, encoding='u8'):
def title_case_lastname(s, encoding='u8'): # type: (Any, str) -> bytes
"""Return a title cased version of ``s`` encoded in ``encoding``.
If it looks like ``s`` is already title cased, then leave it alone
(in case of manual override and complex capitalization rules for
last names).
"""
if not s:
return ''
return b''
us = unicode_repr(s)
m = _mixedcase.match(us)
if m:
Expand Down Expand Up @@ -259,7 +234,7 @@ def normalize(v): # type: (Any) -> bytes
return v.encode('u8')
if v in (None, ''):
return b''
return bytes(v)
return text(v).encode('u8')


def nlat(v):
Expand Down Expand Up @@ -304,55 +279,55 @@ def kr_ore(n):
return kronestring(kr) + ',' + orestring(ore)


def mk_post(model):
"""Encode ``model`` (a dict-like object) into a dict where:
- all values are strings
- None values are removed
- date values are expanded into year/month/day parts
Note:: this function is superceeded by maint.client._encode_date
which does this transparently for unit tests.
"""
res = {}
for key, val in model.items():
if isinstance(val, datetime.date):
res[key + '_year'] = str(val.year)
res[key + '_month'] = str(val.month)
res[key + '_day'] = str(val.day)
elif val is None:
pass # do nothing
else:
res[key] = str(val)
return res


class Ordered(dict): # FIXME: should be removed asap.
"""
Mapping that maintains insertion order.
(Should be removed and the adt versions should be used).
"""
def __init__(self):
super(Ordered, self).__init__()
self._ordered = []

def __setitem__(self, key, val):
super(Ordered, self).__setitem__(key, val)
self._ordered.append(key)

def __getitem__(self, key):
if key not in self._ordered:
return ''
return super(Ordered, self).__getitem__(key)

def keys(self):
return self._ordered

def values(self):
for key in self._ordered:
yield self[key]

def items(self):
for key in self._ordered:
yield key, self[key]
# def mk_post(model):
# """Encode ``model`` (a dict-like object) into a dict where:
#
# - all values are strings
# - None values are removed
# - date values are expanded into year/month/day parts
#
# Note:: this function is superceeded by maint.client._encode_date
# which does this transparently for unit tests.
#
# """
# res = {}
# for key, val in model.items():
# if isinstance(val, datetime.date):
# res[key + '_year'] = str(val.year)
# res[key + '_month'] = str(val.month)
# res[key + '_day'] = str(val.day)
# elif val is None:
# pass # do nothing
# else:
# res[key] = str(val)
# return res
#
#
# class Ordered(dict): # FIXME: should be removed asap.
# """
# Mapping that maintains insertion order.
# (Should be removed and the adt versions should be used).
# """
# def __init__(self):
# super(Ordered, self).__init__()
# self._ordered = []
#
# def __setitem__(self, key, val):
# super(Ordered, self).__setitem__(key, val)
# self._ordered.append(key)
#
# def __getitem__(self, key):
# if key not in self._ordered:
# return ''
# return super(Ordered, self).__getitem__(key)
#
# def keys(self):
# return self._ordered
#
# def values(self):
# for key in self._ordered:
# yield self[key]
#
# def items(self):
# for key in self._ordered:
# yield key, self[key]
54 changes: 50 additions & 4 deletions tests/test_utils.py
@@ -1,7 +1,12 @@
# -*- coding: utf-8 -*-
import pytest

from dk.utils import *


def test_identity():
assert identity(identity(42)) == 42

# def test_srcpath():
# assert srcpath(base=None, pth='foo/bar/things.py')[-28:] == '/lib/dk/dk/foo/bar/things.py'
# assert srcpath(base='foo', pth='bar/things.py')[-28:] == '/lib/dk/dk/foo/bar/things.py'
Expand Down Expand Up @@ -46,44 +51,74 @@ def test_single_line():


def test_lower_case():
lower_case('This is JUST a test!!') == b'this is just a test!!'
lower_case('Gåsa flyr over Høybukta') == u'gåsa flyr over høybukta'.encode('u8')
lower_case('Gåsa flyr over Høybukta', 'l1') == u'gåsa flyr over høybukta'.encode('l1')
assert lower_case('This is JUST a test!!') == b'this is just a test!!'
assert lower_case('Gåsa flyr over Høybukta') == u'gåsa flyr over høybukta'.encode('u8')
assert lower_case('Gåsa flyr over Høybukta', 'l1') == u'gåsa flyr over høybukta'.encode('l1')


def test_ulower_case():
assert ulower_case(None) == u''
assert lower_case('Tømmer og biller') == u'tømmer og biller'.encode('u8')
assert ulower_case(u'Tømmer og biller') == u'tømmer og biller'


def test_title_case():
assert title_case('høna verper egg') == u'Høna Verper Egg'.encode('u8')


def test_utitle_case():
assert utitle_case(None) == u''
with pytest.raises(ValueError):
utitle_case(42)
assert utitle_case(u'hanen stend på stabburshella') == u'Hanen Stend På Stabburshella'


def test_title_case_lastname():
assert title_case_lastname(None) == b''
assert title_case_lastname(u'OlSen') == b'OlSen'
assert title_case_lastname('ole olsen') == b'Ole Olsen'
assert title_case_lastname(u'jan jönson') == b'Jan J\xc3\xb6nson'
assert title_case_lastname(u'jan jönson', 'l1') == b'Jan J\xf6nson'


def test_utitle_case_lastname():
assert utitle_case_lastname(None) == ''
assert utitle_case_lastname(u"McDonald") == 'McDonald'
assert utitle_case_lastname('ole olsen') == 'Ole Olsen'
assert utitle_case_lastname(u'jan jönson') == u'Jan Jönson'


def test_unicode_repr():
assert unicode_repr('Laksen springer i håven') == u'Laksen springer i håven'
assert unicode_repr(object())[:17] == '<object object at'
assert unicode_repr(u'Bjørn'.encode('iso-8859-1')) == u'Bjørn'


def test_normalize():
assert normalize(42) == b'42'
assert normalize(None) == b''
assert normalize(u'bjørn') == u'bjørn'.encode('u8')


def test_nlat():
assert nlat(42) == b'42'
assert nlat(None) == b''
assert nlat(u'bjørn') == u'bjørn'.encode('l1')


def test_utf8():
assert utf8(42) == b'42'


def test_latin1():
assert latin1(42) == b'42'


def test_unhtml():
assert unhtml(42) == 42
string = u'&nbsp;&Aring;&AElig;&Oslash;&aring;&aelig;&oslash;&eacute;'
result = u' ÅÆØåæøé'
assert unhtml(string) == result
assert unhtml(string.encode('u8')) == result


def test_html2u8():
Expand All @@ -93,5 +128,16 @@ def test_html2u8():


def test_kronestring():
assert kronestring(0) == '0'
assert kronestring(1054) == '1 054'
assert kronestring(-1054) == '-1 054'


def test_orestring():
assert orestring(0) == '-'
assert orestring(2) == '02'
assert orestring(42) == '42'


def test_kr_ore():
assert kr_ore(1054) == '10,54'

0 comments on commit b40da3d

Please sign in to comment.