mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Refactor pelican_open using contextmanager
This commit is contained in:
parent
4ffa34544e
commit
41970cb2c6
1 changed files with 17 additions and 21 deletions
|
|
@ -2,20 +2,21 @@
|
||||||
from __future__ import unicode_literals, print_function
|
from __future__ import unicode_literals, print_function
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
import codecs
|
||||||
|
import errno
|
||||||
|
import fnmatch
|
||||||
|
import locale
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
|
||||||
import pytz
|
import pytz
|
||||||
|
import re
|
||||||
import shutil
|
import shutil
|
||||||
import traceback
|
import traceback
|
||||||
import logging
|
|
||||||
import errno
|
|
||||||
import locale
|
|
||||||
import fnmatch
|
|
||||||
from collections import Hashable
|
|
||||||
from functools import partial
|
|
||||||
|
|
||||||
from codecs import open, BOM_UTF8
|
from collections import Hashable
|
||||||
|
from contextlib import contextmanager
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from functools import partial
|
||||||
from itertools import groupby
|
from itertools import groupby
|
||||||
from jinja2 import Markup
|
from jinja2 import Markup
|
||||||
from operator import attrgetter
|
from operator import attrgetter
|
||||||
|
|
@ -215,20 +216,15 @@ def get_date(string):
|
||||||
raise ValueError('{0!r} is not a valid date'.format(string))
|
raise ValueError('{0!r} is not a valid date'.format(string))
|
||||||
|
|
||||||
|
|
||||||
class pelican_open(object):
|
@contextmanager
|
||||||
|
def pelican_open(filename):
|
||||||
"""Open a file and return its content"""
|
"""Open a file and return its content"""
|
||||||
def __init__(self, filename):
|
|
||||||
self.filename = filename
|
|
||||||
|
|
||||||
def __enter__(self):
|
with codecs.open(filename, encoding='utf-8') as infile:
|
||||||
with open(self.filename, encoding='utf-8') as infile:
|
content = infile.read()
|
||||||
content = infile.read()
|
if content[0] == codecs.BOM_UTF8.decode('utf8'):
|
||||||
if content[0] == BOM_UTF8.decode('utf8'):
|
content = content[1:]
|
||||||
content = content[1:]
|
yield content
|
||||||
return content
|
|
||||||
|
|
||||||
def __exit__(self, exc_type, exc_value, traceback):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def slugify(value, substitutions=()):
|
def slugify(value, substitutions=()):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue