enable genspider command outside projects

This commit is contained in:
Valdir Stumm Junior 2016-06-11 20:44:08 -03:00
parent 759a555d28
commit 1779f5feca
2 changed files with 18 additions and 6 deletions

View File

@ -25,7 +25,7 @@ def sanitize_module_name(module_name):
class Command(ScrapyCommand):
requires_project = True
requires_project = False
default_settings = {'LOG_ENABLED': False}
def syntax(self):
@ -94,14 +94,19 @@ class Command(ScrapyCommand):
'classname': '%sSpider' % ''.join(s.capitalize() \
for s in module.split('_'))
}
spiders_module = import_module(self.settings['NEWSPIDER_MODULE'])
spiders_dir = abspath(dirname(spiders_module.__file__))
if self.settings.get('NEWSPIDER_MODULE'):
spiders_module = import_module(self.settings['NEWSPIDER_MODULE'])
spiders_dir = abspath(dirname(spiders_module.__file__))
else:
spiders_module = None
spiders_dir = "."
spider_file = "%s.py" % join(spiders_dir, module)
shutil.copyfile(template_file, spider_file)
render_templatefile(spider_file, **tvars)
print("Created spider %r using template %r in module:" % (name, \
template_name))
print(" %s.%s" % (spiders_module.__name__, module))
print("Created spider %r using template %r " % (name, \
template_name), end=('' if spiders_module else '\n'))
if spiders_module:
print("in module:\n %s.%s" % (spiders_module.__name__, module))
def _find_template(self, template):
template_file = join(self.templates_dir, '%s.tmpl' % template)

View File

@ -146,6 +146,13 @@ class GenspiderCommandTest(CommandTest):
assert not exists(join(self.proj_mod_path, 'spiders', '%s.py' % self.project_name))
class GenspiderStandaloneCommandTest(ProjectTest):
def test_generate_standalone_spider(self):
self.call('genspider', 'example', 'example.com')
assert exists(join(self.temp_path, 'example.py'))
class MiscCommandsTest(CommandTest):
def test_list(self):