Skip to content

Commit

Permalink
Made scandir an optional dependency to support PyPy
Browse files Browse the repository at this point in the history
  • Loading branch information
titusz committed Jul 21, 2015
1 parent 9f8b597 commit ebba1af
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
13 changes: 8 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
from __future__ import absolute_import, print_function

import io
import os
import re
import platform
from glob import glob
from os.path import basename
from os.path import dirname
from os.path import join
from os.path import relpath
from os.path import splitext

from setuptools import find_packages
from setuptools import setup


def read(*names, **kwargs):
return io.open(
join(dirname(__file__), *names),
encoding=kwargs.get('encoding', 'utf8')
).read()


dependencies = ['click', 'lxml', 'defusedxml']
if platform.python_implementation() == 'PyPy':
dependencies.append('scandir')


setup(
name='onixcheck',
version='0.4.0',
Expand Down Expand Up @@ -57,9 +62,7 @@ def read(*names, **kwargs):
keywords=[
'ONIX', 'validation', 'EDItEUR', 'XML', 'RelaxNG', 'XMLSchema'
],
install_requires=[
'click', 'lxml', 'defusedxml', 'scandir'
],
install_requires=dependencies,
extras_require={
# eg:
# 'rst': ['docutils>=0.11'],
Expand Down
23 changes: 19 additions & 4 deletions src/onixcheck/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@
"""Generic or common utility functions"""
from __future__ import print_function, unicode_literals
from os.path import splitext, join
from scandir import scandir, walk

has_scandir = True

try:
from scandir import scandir, walk
except ImportError:
from os import walk
from os import listdir as scandir
from os.path import isfile
has_scandir = False


def iter_files(root, exts=None, recursive=False):
Expand All @@ -22,9 +31,15 @@ def iter_files(root, exts=None, recursive=False):

if recursive is False:
for entry in scandir(root):
ext = splitext(entry.name)[-1].lstrip('.').lower()
if entry.is_file() and matches(ext):
yield entry.path
if has_scandir:
ext = splitext(entry.name)[-1].lstrip('.').lower()
if entry.is_file() and matches(ext):
yield entry.path
else:
ext = splitext(entry)[-1].lstrip('.').lower()
if isfile(entry) and matches(ext):
yield entry

else:
for root, folders, files in walk(root):
for f in files:
Expand Down

0 comments on commit ebba1af

Please sign in to comment.