Skip to content

Commit

Permalink
Merge pull request #25 from plone/python3
Browse files Browse the repository at this point in the history
More Python 3 fixes
  • Loading branch information
pbauer authored Sep 15, 2018
2 parents 1e34b68 + 6a97e02 commit e766c8b
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 129 deletions.
7 changes: 4 additions & 3 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Changelog
=========

2.0.3 (unreleased)
2.1.0 (unreleased)
------------------

Breaking changes:
Expand All @@ -14,15 +14,16 @@ New features:

Bug fixes:

- *add item here*
- Fix tests in py3.
[pbauer]


2.0.2 (2018-06-04)
------------------

Bug fixes:

- More Python 3 fixes
- More Python 3 fixes.
[ale, pbauer]


Expand Down
53 changes: 26 additions & 27 deletions plone/resource/directory.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from plone.resource.interfaces import IWritableResourceDirectory
from Products.BTreeFolder2.BTreeFolder2 import BTreeFolder2
from Products.CMFCore.utils import getToolByName
from six import StringIO
from zExceptions import Forbidden
from zExceptions import NotFound
from zope.event import notify
Expand Down Expand Up @@ -89,15 +88,16 @@ def __contains__(self, name):
return name in self.context

def openFile(self, path):
return StringIO(self.readFile(path))
return six.BytesIO(self.readFile(path))

def readFile(self, path):
try:
f = self.context.unrestrictedTraverse(path)
except Exception as e:
raise IOError(str(e))

return str(f.data)
if isinstance(f.data, six.binary_type):
return f.data
return f.data.__bytes__()

def listDirectory(self):
return [n for n in self.context.objectIds()
Expand Down Expand Up @@ -142,17 +142,20 @@ def write(dir, prefix, zf):
zf.close()

def makeDirectory(self, path):
if six.PY2:
path = path.encode('utf-8')

parent = self.context
names = path.strip('/').split('/')
for name in names:
if name not in parent:
if six.PY2 and isinstance(name, six.text_type):
name = name.encode('utf-8')
f = BTreeFolder2(name)
parent._setOb(name, f)
parent = parent[name]

def writeFile(self, path, data):
if isinstance(data, six.text_type):
data = data.encode('utf8')
basepath = '/'.join(path.split('/')[:-1])
if basepath:
self.makeDirectory(basepath)
Expand Down Expand Up @@ -245,13 +248,11 @@ def __getitem__(self, name):

def openFile(self, path):
filepath = self._resolveSubpath(path)
mode = u'r'
if six.PY2:
mode += u'b'
return open(filepath, mode)
return open(filepath, 'rb')

def readFile(self, path):
return self.openFile(path).read()
with self.openFile(path) as f:
return f.read()

def listDirectory(self):
names = os.listdir(self.directory)
Expand All @@ -265,23 +266,21 @@ def isFile(self, path):
return os.path.isfile(self._resolveSubpath(path))

def exportZip(self, out):
zf = zipfile.ZipFile(out, 'w')

toStrip = len(self.directory.replace(os.path.sep, '/')) + 1
with zipfile.ZipFile(out, 'w') as zf:
toStrip = len(self.directory.replace(os.path.sep, '/')) + 1

for (dirpath, dirnames, filenames) in os.walk(self.directory):
subpath = dirpath.replace(os.path.sep, '/')[toStrip:].strip('/')
for (dirpath, dirnames, filenames) in os.walk(self.directory):
subpath = dirpath.replace(os.path.sep, '/')[toStrip:].strip('/')

for filename in filenames:
path = '/'.join([subpath, filename]).strip('/')
for filename in filenames:
path = '/'.join([subpath, filename]).strip('/')

if any(any(filter.match(n) for filter in FILTERS)
for n in path.split('/')
):
continue
if any(any(filter.match(n) for filter in FILTERS)
for n in path.split('/')
):
continue

zf.writestr(
'/'.join([self.__name__, path, ]),
self.readFile(path)
)
zf.close()
zf.writestr(
'/'.join([self.__name__, path, ]),
self.readFile(path),
)
5 changes: 2 additions & 3 deletions plone/resource/download.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from six import StringIO
from io import BytesIO
from zope.publisher.browser import BrowserView


Expand All @@ -14,7 +14,7 @@ def __call__(self):
# the output stream, but this is awkward with the ZPublisher response
# interface. For now, we write the zipfile to a stream in memory.

out = StringIO()
out = BytesIO()
self.context.exportZip(out)

response.setHeader('Content-Type', 'application/zip')
Expand All @@ -23,5 +23,4 @@ def __call__(self):
'attachment; filename="%s.zip"' % name
)
response.setHeader('Content-Length', len(out.getvalue()))

response.write(out.getvalue())
2 changes: 1 addition & 1 deletion plone/resource/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from email.utils import formatdate
from z3c.caching.interfaces import ILastModified
from zope.component import adapter
from zope.component import queryUtility
from zope.filerepresentation.interfaces import IRawReadFile
from zope.interface import implementer
from ZPublisher.Iterators import filestream_iterator
from zope.component import queryUtility

import datetime
import mimetypes
Expand Down
13 changes: 10 additions & 3 deletions plone/resource/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
``bar``. ``title`` and ``description`` will be ``None`` if not found in the
manifest. ``bar`` will be ``baz`` if not found.
"""
import logging

from plone.resource.directory import FILTERS
from plone.resource.interfaces import IResourceDirectory
from plone.resource.utils import iterDirectoriesOfType
from six.moves.configparser import SafeConfigParser
from zope.component import getUtility

import logging
import six


MANIFEST_FILENAME = 'manifest.cfg'

Expand Down Expand Up @@ -79,7 +80,13 @@ def getManifest(fp, format, defaults=None):
defaults = format.defaults

parser = SafeConfigParser()
parser.readfp(fp)
if six.PY2:
parser.readfp(fp)
else:
data = fp.read()
if isinstance(data, six.binary_type):
data = data.decode()
parser.read_string(data)

results = {}
for key in format.keys:
Expand Down
Loading

0 comments on commit e766c8b

Please sign in to comment.