Scrapy, a fast high-level web crawling & scraping framework for Python.
Creates an usable project structure on startproject. closes #58 and closes #54
Daniel Granaf7dac0e449ab
diff --git a/scrapy/trunk/scrapy/bin/scrapy-admin.py b/scrapy/trunk/scrapy/bin/scrapy-admin.pyindex 5804a0c63..a64228008 100755--- a/scrapy/trunk/scrapy/bin/scrapy-admin.py+++ b/scrapy/trunk/scrapy/bin/scrapy-admin.py@@ -3,10 +3,11 @@tasks"""import osimport shutil+import stringfrom optparse import OptionParserimport scrapy-from scrapy.utils.misc import render_templatefile+from scrapy.utils.misc import render_templatefile, string_camelcaseusage = """scrapy-admin.py [options] [command]@@ -17,14 +18,21 @@ Available commands:Starts a new project with name 'project_name'"""+PROJECT_TEMPLATES_PATH = os.path.join(scrapy.__path__[0], 'templates/project')++# This is the list of templatefile's path that are rendered *after copying* to+# project directory.TEMPLATES = (- 'scrapy_settings.py',+ 'scrapy-ctl.py',+ '${project_name}/settings.py',+ '${project_name}/items.py',+ '${project_name}/pipelines.py',)def main():parser = OptionParser(usage=usage)opts, args = parser.parse_args()-+if not args:parser.print_help()return@@ -33,10 +41,20 @@ def main():if cmd == "startproject":if len(args) >= 2:project_name = args[1]- project_tplpath = os.path.join(scrapy.__path__[0], "conf", "project_template")- shutil.copytree(project_tplpath, project_name)+ project_root_path = project_name+ project_module_path = '%s/%s' % (project_name, project_name)++ roottpl = os.path.join(PROJECT_TEMPLATES_PATH, 'root')+ shutil.copytree(roottpl, project_name)++ moduletpl = os.path.join(PROJECT_TEMPLATES_PATH, 'module')+ shutil.copytree(moduletpl, '%s/%s' % (project_name, project_name))+for path in TEMPLATES:- render_templatefile(os.path.join(project_name, path), project_name=project_name)+ tplfile = os.path.join(project_root_path,+ string.Template(path).substitute(project_name=project_name))+ render_templatefile(tplfile, project_name=project_name,+ ProjectName=string_camelcase(project_name))else:print "scrapy-admin.py: missing project name"else:diff --git a/scrapy/trunk/scrapy/command/commands/genspider.py b/scrapy/trunk/scrapy/command/commands/genspider.pyindex 249a4a30e..3e954908a 100644--- a/scrapy/trunk/scrapy/command/commands/genspider.py+++ b/scrapy/trunk/scrapy/command/commands/genspider.py@@ -7,7 +7,8 @@ import shutilfrom scrapy.spider import spidersfrom scrapy.command import ScrapyCommandfrom scrapy.conf import settings-from scrapy.utils.misc import render_templatefile+from scrapy.utils.misc import render_templatefile, string_camelcase+class Command(ScrapyCommand):""" Childs can define custom tvars """@@ -54,7 +55,8 @@ class Command(ScrapyCommand):def _genspider(self, name, site, template_file):""" Generate spider """tvars = {- 'project_name': settings.get('BOT_NAME'),+ 'project_name': settings.get('PROJECT_NAME'),+ 'ProjectName': string_camelcase(settings.get('PROJECT_NAME')),'name': name,'site': site,'classname': '%sSpider' % ''.join([s.capitalize() for s in name.split('_')])diff --git a/scrapy/trunk/scrapy/conf/project_template/scrapy-ctl.py b/scrapy/trunk/scrapy/conf/project_template/scrapy-ctl.pydeleted file mode 100755index e8e56eaf2..000000000--- a/scrapy/trunk/scrapy/conf/project_template/scrapy-ctl.py+++ /dev/null@@ -1,4 +0,0 @@-#!/usr/bin/env python--from scrapy.command.cmdline import execute-execute()diff --git a/scrapy/trunk/scrapy/conf/project_template/__init__.py b/scrapy/trunk/scrapy/templates/project/module/__init__.pysimilarity index 100%rename from scrapy/trunk/scrapy/conf/project_template/__init__.pyrename to scrapy/trunk/scrapy/templates/project/module/__init__.pydiff --git a/scrapy/trunk/scrapy/conf/project_template/items.py b/scrapy/trunk/scrapy/templates/project/module/items.pysimilarity index 70%rename from scrapy/trunk/scrapy/conf/project_template/items.pyrename to scrapy/trunk/scrapy/templates/project/module/items.pyindex d78c4d149..bf93a82e2 100644--- a/scrapy/trunk/scrapy/conf/project_template/items.py+++ b/scrapy/trunk/scrapy/templates/project/module/items.py@@ -2,5 +2,5 @@from scrapy.item import ScrapedItem-class MyItem(ScrapedItem):+class ${ProjectName}Item(ScrapedItem):passdiff --git a/scrapy/trunk/scrapy/templates/project/module/pipelines.py b/scrapy/trunk/scrapy/templates/project/module/pipelines.pynew file mode 100644index 000000000..61ceea1fa--- /dev/null+++ b/scrapy/trunk/scrapy/templates/project/module/pipelines.py@@ -0,0 +1,5 @@+# Define yours item pipelines here++class ${ProjectName}Pipeline(object):+ def process_item(self, domain, item):+ return itemdiff --git a/scrapy/trunk/scrapy/conf/project_template/scrapy_settings.py b/scrapy/trunk/scrapy/templates/project/module/settings.pysimilarity index 96%rename from scrapy/trunk/scrapy/conf/project_template/scrapy_settings.pyrename to scrapy/trunk/scrapy/templates/project/module/settings.pyindex 4b88158f4..dde506c40 100644--- a/scrapy/trunk/scrapy/conf/project_template/scrapy_settings.py+++ b/scrapy/trunk/scrapy/templates/project/module/settings.py@@ -3,8 +3,9 @@ import $project_name# ---------------------------------------------------------------------------# - Scrapy settings for $project_name -# ---------------------------------------------------------------------------+PROJECT_NAME = '$project_name'-BOT_NAME = '$project_name'+BOT_NAME = PROJECT_NAMEBOT_VERSION = '1.0'SPIDER_MODULES = ['$project_name.spiders']@@ -79,9 +80,9 @@ SPIDER_MIDDLEWARES = (# Spider side)-# Item pipelines are usually configured by commands (see conf/commands)-#ITEM_PIPELINES = (-#)+ITEM_PIPELINES = (+ '${project_name}.pipelines.${ProjectName}Pipeline',+)#DEPTH_LIMIT = 10 # limit the maximum link depth to follow#DEPTH_STATS = 1 # enable depth statsdiff --git a/scrapy/trunk/scrapy/conf/project_template/spiders/__init__.py b/scrapy/trunk/scrapy/templates/project/module/spiders/__init__.pysimilarity index 100%rename from scrapy/trunk/scrapy/conf/project_template/spiders/__init__.pyrename to scrapy/trunk/scrapy/templates/project/module/spiders/__init__.pydiff --git a/scrapy/trunk/scrapy/conf/project_template/templates/spider_basic.tmpl b/scrapy/trunk/scrapy/templates/project/module/templates/spider_basic.tmplsimilarity index 100%rename from scrapy/trunk/scrapy/conf/project_template/templates/spider_basic.tmplrename to scrapy/trunk/scrapy/templates/project/module/templates/spider_basic.tmpldiff --git a/scrapy/trunk/scrapy/conf/project_template/templates/spider_crawl.tmpl b/scrapy/trunk/scrapy/templates/project/module/templates/spider_crawl.tmplsimilarity index 89%rename from scrapy/trunk/scrapy/conf/project_template/templates/spider_crawl.tmplrename to scrapy/trunk/scrapy/templates/project/module/templates/spider_crawl.tmplindex a45ffdf42..9f777b231 100644--- a/scrapy/trunk/scrapy/conf/project_template/templates/spider_crawl.tmpl+++ b/scrapy/trunk/scrapy/templates/project/module/templates/spider_crawl.tmpl@@ -4,7 +4,7 @@ import refrom scrapy.xpath import HtmlXPathSelectorfrom scrapy.link.extractors import RegexLinkExtractorfrom scrapy.contrib.spiders import CrawlSpider, Rule-from $project_name.items import MyItem+from $project_name.items import ${ProjectName}Itemclass $classname(CrawlSpider):domain_name = '$site'@@ -15,7 +15,7 @@ class $classname(CrawlSpider):)def parse_item(self, response):- i = MyItem()+ i = ${ProjectName}Item()#xs = HtmlXPathSelector(response)#i.attribute('site_id', xs.x('//input[@id="sid"]/@value'))#i.attribute('name', xs.x('//div[@id="name"]'))diff --git a/scrapy/trunk/scrapy/conf/project_template/templates/spider_csvfeed.tmpl b/scrapy/trunk/scrapy/templates/project/module/templates/spider_csvfeed.tmplsimilarity index 87%rename from scrapy/trunk/scrapy/conf/project_template/templates/spider_csvfeed.tmplrename to scrapy/trunk/scrapy/templates/project/module/templates/spider_csvfeed.tmplindex bc96a3de9..2d6ec5991 100644--- a/scrapy/trunk/scrapy/conf/project_template/templates/spider_csvfeed.tmpl+++ b/scrapy/trunk/scrapy/templates/project/module/templates/spider_csvfeed.tmpl@@ -1,6 +1,6 @@# -*- coding: utf8 -*-from scrapy.contrib.spiders import CSVFeedSpider-from $project_name.items import MyItem+from $project_name.items import ${ProjectName}Itemclass $classname(CSVFeedSpider):domain_name = '$site'@@ -13,7 +13,7 @@ class $classname(CSVFeedSpider):# return responsedef parse_row(self, response, row):- i = MyItem()+ i = ${ProjectName}Item()#i.attribute('url', row['url'])#i.attribute('name', row['name'])#i.attribute('description', row['description'])diff --git a/scrapy/trunk/scrapy/conf/project_template/templates/spider_xmlfeed.tmpl b/scrapy/trunk/scrapy/templates/project/module/templates/spider_xmlfeed.tmplsimilarity index 82%rename from scrapy/trunk/scrapy/conf/project_template/templates/spider_xmlfeed.tmplrename to scrapy/trunk/scrapy/templates/project/module/templates/spider_xmlfeed.tmplindex 7ea43375e..395204cf5 100644--- a/scrapy/trunk/scrapy/conf/project_template/templates/spider_xmlfeed.tmpl+++ b/scrapy/trunk/scrapy/templates/project/module/templates/spider_xmlfeed.tmpl@@ -1,13 +1,13 @@# -*- coding: utf8 -*-from scrapy.contrib.spiders import XMLFeedSpider-from $project_name.items import MyItem+from $project_name.items import ${ProjectName}Itemclass $classname(XMLFeedSpider):domain_name = '$site'start_urls = ['http://www.$site/feed.xml']def parse_item(self, response, xSel):- i = MyItem()+ i = ${ProjectName}Item()#i.attribute('url', xSel('url'))#i.attribute('name', xSel('name'))#i.attribute('description', xSel('description'))diff --git a/scrapy/trunk/scrapy/templates/project/root/scrapy-ctl.py b/scrapy/trunk/scrapy/templates/project/root/scrapy-ctl.pynew file mode 100755index 000000000..2d2ed44e9--- /dev/null+++ b/scrapy/trunk/scrapy/templates/project/root/scrapy-ctl.py@@ -0,0 +1,7 @@+#!/usr/bin/env python++import os+os.environ.setdefault('SCRAPYSETTINGS_MODULE', '${project_name}.settings')++from scrapy.command.cmdline import execute+execute()diff --git a/scrapy/trunk/scrapy/utils/misc.py b/scrapy/trunk/scrapy/utils/misc.pyindex ea926cdde..c6352fc3e 100644--- a/scrapy/trunk/scrapy/utils/misc.py+++ b/scrapy/trunk/scrapy/utils/misc.py@@ -153,3 +153,8 @@ def items_to_csv(file, items, delimiter=';', headers=None):value = unicode_to_str(value) if isinstance(value, basestring) else valuerow.append(value)csv_file.writerow(row)+++CAMELCASE_INVALID_CHARS = re.compile('[^a-zA-Z]')+def string_camelcase(string):+ return CAMELCASE_INVALID_CHARS.sub('', string.title())