bmc/libbmc/tools.py

91 lines
2.7 KiB
Python
Raw Permalink Normal View History

2014-05-01 01:37:47 +02:00
# -*- coding: utf8 -*-
2014-05-26 16:12:21 +02:00
# -----------------------------------------------------------------------------
# "THE NO-ALCOHOL BEER-WARE LICENSE" (Revision 42):
# Phyks (webmaster@phyks.me) wrote this file. As long as you retain this notice
# you can do whatever you want with this stuff (and you can also do whatever
# you want with this stuff without retaining it, but that's not cool...). If we
# meet some day, and you think this stuff is worth it, you can buy me a
# <del>beer</del> soda in return.
# Phyks
# -----------------------------------------------------------------------------
2014-05-01 01:37:47 +02:00
2014-08-03 12:38:40 +02:00
from __future__ import print_function, unicode_literals
2014-05-01 01:37:47 +02:00
import os
import re
import sys
2015-06-09 12:02:54 +02:00
if os.name == "posix":
from termios import tcflush, TCIOFLUSH
2014-05-01 01:37:47 +02:00
2014-08-03 23:34:17 +02:00
try:
input = raw_input
except NameError:
pass
2014-05-01 01:37:47 +02:00
_slugify_strip_re = re.compile(r'[^\w\s-]')
_slugify_hyphenate_re = re.compile(r'[\s]+')
def slugify(value):
"""Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens to have nice filenames.
From Django's "django/template/defaultfilters.py".
"""
import unicodedata
2014-08-03 00:40:37 +02:00
try:
unicode_type = unicode
except NameError:
unicode_type = str
if not isinstance(value, unicode_type):
value = unicode_type(value)
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = unicode_type(_slugify_strip_re.sub('', value).strip())
2014-05-01 01:37:47 +02:00
return _slugify_hyphenate_re.sub('_', value)
def parsed2Bibtex(parsed):
"""Convert a single bibtex entry dict to bibtex string"""
2015-08-31 16:04:41 +02:00
bibtex = '@'+parsed['ENTRYTYPE']+'{'+parsed['ID']+",\n"
2015-08-31 16:04:41 +02:00
for field in [i for i in sorted(parsed) if i not in ['ENTRYTYPE', 'ID']]:
bibtex += "\t"+field+"={"+parsed[field]+"},\n"
bibtex += "}\n\n"
return bibtex
2014-05-01 01:37:47 +02:00
def getExtension(filename):
"""Get the extension of filename"""
return filename[filename.rfind('.'):]
def replaceAll(text, dic):
"""Replace all the dic keys by the associated item in text"""
2014-08-03 00:40:37 +02:00
for i, j in dic.items():
2014-05-01 01:37:47 +02:00
text = text.replace(i, j)
return text
def rawInput(string):
"""Flush stdin and then prompt the user for something"""
2015-06-09 12:02:54 +02:00
if os.name == "posix":
tcflush(sys.stdin, TCIOFLUSH)
2014-08-03 23:34:17 +02:00
return input(string)
2014-05-01 01:37:47 +02:00
def warning(*objs):
"""Write warnings to stderr"""
2014-08-03 12:38:40 +02:00
print("WARNING: ", *objs, file=sys.stderr)
2014-05-01 01:37:47 +02:00
def listDir(path):
"""List all files in path directory, works recursively
Return files list
"""
filenames = []
for root, dirs, files in os.walk(path):
for i in files:
filenames.append(os.path.join(root, i))
return filenames