Skip to content

Commit 15bbd8a

Browse files
committed
added complete test cases for config setting
1 parent e70511e commit 15bbd8a

5 files changed

Lines changed: 42 additions & 37 deletions

File tree

newspaper/api.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,34 @@
1313
from .configuration import Configuration
1414
from .mthreading import NewsPool
1515
from .configuration import Configuration
16-
from .utils import print_available_languages
16+
from .utils import print_available_languages, extend_config
1717

18-
def build(url=u'', config=None):
18+
def build(url=u'', dry=False, config=None, **kwargs):
1919
"""
2020
Returns a constructed source object without
2121
downloading or parsing the articles.
2222
"""
2323
config = config or Configuration() # Order matters
24-
url = url or '' # Empty string precedence over None
25-
valid_href = ('://' in url) and (url[:4] == 'http')
24+
config = extend_config(config, kwargs)
2625

27-
if not valid_href:
28-
print 'ERR: provide a valid url'
29-
return None
26+
url = url or ''
27+
s = Source(url, config=config)
3028

31-
s = Source(url, config)
32-
s.build()
29+
# dry means we are just testing, don't actually build source
30+
if not dry:
31+
s.build()
3332
return s
3433

35-
def build_article(url=u''):
34+
def build_article(url=u'', config=None, **kwargs):
3635
"""
3736
Returns a constructed article object without
3837
downloading or parsing.
3938
"""
40-
url = url or '' # empty string precedence over None
41-
a = Article(url)
39+
config = config or Configuration() # Order matters
40+
config = extend_config(config, kwargs)
41+
42+
url = url or ''
43+
a = Article(url, config=config)
4244
return a
4345

4446
def languages():

newspaper/article.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from . import settings
1919
from .configuration import Configuration
2020
from .extractors import StandardContentExtractor
21-
from .utils import URLHelper, encodeValue, RawHelper
21+
from .utils import URLHelper, encodeValue, RawHelper, extend_config
2222
from .cleaners import StandardDocumentCleaner
2323
from .outputformatters import StandardOutputFormatter
2424
from .videos.extractors import VideoExtractor
@@ -33,23 +33,13 @@ class ArticleException(Exception):
3333
class Article(object):
3434
"""
3535
"""
36-
def extend_config(self, config_items):
37-
"""
38-
We are handling config value setting like this for a cleaner api.
39-
Users just need to pass in a named param to this article and we
40-
can dynamically set a config object for it.
41-
"""
42-
for key, val in config_items.items():
43-
if hasattr(self.config, key):
44-
setattr(self.config, key, val)
45-
4636
def __init__(self, url, title=u'', source_url=u'', config=None, **kwargs):
4737
"""
4838
The **kwargs arguement can be filled with config values which we then
4939
push in.
5040
"""
5141
self.config = config or Configuration()
52-
self.extend_config(kwargs)
42+
self.config = extend_config(self.config, kwargs)
5343

5444
self.parser = self.config.get_parser()
5545
self.extractor = StandardContentExtractor(config=self.config)

newspaper/source.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from .urls import (
2121
get_domain, get_scheme, prepare_url)
2222
from .utils import (
23-
memoize_articles, cache_disk, clear_memo_cache, encodeValue)
23+
memoize_articles, cache_disk, clear_memo_cache, encodeValue, extend_config)
2424

2525
log = logging.getLogger(__name__)
2626

@@ -52,16 +52,6 @@ class Source(object):
5252
articles = [<article obj>, <article obj>, ..]
5353
brand = 'cnn'
5454
"""
55-
def extend_config(self, config_items):
56-
"""
57-
We are handling config value setting like this for a cleaner api.
58-
Users just need to pass in a named param to this source and we can
59-
dynamically generate a config object for it.
60-
"""
61-
for key, val in config_items.items():
62-
if hasattr(self.config, key):
63-
setattr(self.config, key, val)
64-
6555
def __init__(self, url, config=None, **kwargs):
6656
"""
6757
**The config object for this source will be passed into all of this
@@ -71,7 +61,7 @@ def __init__(self, url, config=None, **kwargs):
7161
raise Exception('Input url is bad!')
7262

7363
self.config = config or Configuration() # Order matters
74-
self.extend_config(kwargs)
64+
self.config = extend_config(self.config, kwargs)
7565

7666
self.parser = self.config.get_parser()
7767
self.extractor = StandardContentExtractor(config=self.config)

newspaper/utils/__init__.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,15 @@ def print_available_languages():
369369
print ' %s\t\t\t %s' % (code, language_dict[code])
370370
print
371371

372+
def extend_config(config, config_items):
373+
"""
374+
We are handling config value setting like this for a cleaner api.
375+
Users just need to pass in a named param to this source and we can
376+
dynamically generate a config object for it.
377+
"""
378+
for key, val in config_items.items():
379+
if hasattr(config, key):
380+
setattr(config, key, val)
381+
382+
return config
372383

tests/unit_tests.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def runTest(self):
232232

233233
@print_test
234234
def test_source_build(self):
235-
huff_paper = newspaper.build('http://www.huffingtonpost.com/')
235+
huff_paper = newspaper.build('http://www.huffingtonpost.com/', dry=True)
236236
assert isinstance(huff_paper, Source) == True
237237

238238
@print_test
@@ -346,6 +346,18 @@ def test_config_build(self):
346346
assert s.config.language == 'en'
347347
assert s.config.use_meta_language == False
348348

349+
s = newspaper.build('http://cnn.com', dry=True)
350+
assert s.config.language == 'en'
351+
assert s.config.MAX_FILE_MEMO == 20000
352+
assert s.config.memoize_articles == True
353+
assert s.config.use_meta_language == True
354+
355+
s = newspaper.build('http://cnn.com', dry=True, memoize_articles=False,
356+
MAX_FILE_MEMO=10000, language='zh')
357+
assert s.config.language == 'zh'
358+
assert s.config.MAX_FILE_MEMO == 10000
359+
assert s.config.memoize_articles == False
360+
assert s.config.use_meta_language == False
349361

350362
class MultiLanguageTestCase(unittest.TestCase):
351363
def runTest(self):

0 commit comments

Comments
 (0)