コード例 #1
0
ファイル: unbloat.py プロジェクト: sqram/Unbloat
	def go(self, e):
		self.liststore.clear()
		css_filename = self.filechooser_css.get_filename()
		html_dirname = self.filechooser_dir.get_filename()
		
		parser = Parser()
		
		# Scan css for selectors
		css = open(css_filename)		
		css_list = parser.parse_css(css.read())

		css_dict = {'class' : [], 'id' : []}
		for element in css_list:
			# First we search the string. If a '.' was found, it's a class, otherwise a '#' was found and it's an 'id'.
			# We then split the string at .|# and we'll have a list with 2 elements. we put them in tuples inside the
			# class dict ie. div#footer becomes ('div', footer') and we put that inside css_dict['id']
			if re.search('\.', element):
				type = 'class'
			else:
				type = 'id'
			
			split = re.split('\.|\#', element)
			css_dict[type].append( (split[0], split[1]) )
								
			


		# scan HTML files for tags/match/w.e
		# Since we're already traversing directories, search and store all images found in imgs[]
		imgs = []	# These are the actual image files found (ie, header.jpg, logo.png, etc)
		img_srcs = [] # The name of images found in the html <img src tag.
		# Types of files to scan
		filetypes = ('php', 'html', 'xhtml') # Add more to suit your needs :)
		
		# Image files extensions
		imgtypes = ('jpg', 'png', 'jpeg', 'gif', 'bmp', 'tiff')
		
		html_dict = {'class' : [], 'id' : []}
		for dirname, dirnames, filenames in walk(html_dirname):
			for filename in filenames:
				ext = filename.split('.')[-1].lower()
				if ext in filetypes:
					html = open(path.join(dirname, filename))
					
					# parse_html returns a tuple. first element is a list of all matched tags.
					# second element is a list of all images found in <img src=...' />
					# we loop through the elements/image names found in that list and and append it to
					# img_srcs[] so we can later compare img_srcs against imgs
					html_parse_result = parser.parse_html(html.read(), filename)
					html_list = html_parse_result[0]
					for image_name in html_parse_result[1]:
						img_srcs.append(image_name)

					# html_list is now a list, which holds list(s). example:
					# [ [2, hi.php, div, id, contact_form], [5, hi.php, div, class, wrapper] ].
					# those were all the elements found in filename. We now check the 4th element of each list
					# inside html_list, which will be either 'id' or 'class' and insert the list into the appropiate
					# dictionary key
	
					
					for i in html_list:
						type = i[3] # either 'class' or 'id'
						html_dict[type].append(i)

				elif ext in imgtypes:
					# File is an image. add it to imgs[]
					imgs.append(filename)


					
		self.compare(css_dict, html_dict, css_filename, imgs, img_srcs)
コード例 #2
0
ファイル: __init__.py プロジェクト: maebert/cnoms
def import_website(user=None, path_to_site=None):
    """import a website

        * create templates
        * add content to database
        * copy static files
    """
    print 'import_website'
    if not (user and path_to_site):
        path_to_site = request.args['path_to_site']
        user = request.args['user']
    sitename = os.path.basename(os.path.normpath(path_to_site))
    static_path = os.path.join(app.config['STATIC_MEDIA'], user, sitename)
    if not os.path.exists(static_path):
        os.makedirs(static_path)

    # copy static files
    # path = os.path.dirname(__file__)
    # new_static_path = os.path.join(path, '..', 'static', user, sitename)
    # if os.path.exists(new_static_path):
    #     shutil.rmtree(new_static_path)
    # if os.path.exists(os.path.join(path_to_site, 'static')):
    #     shutil.copytree(os.path.join(path_to_site, 'static'), new_static_path)
    # else:
    #     os.makedirs(os.path.join(new_static_path, 'static'))

    # if os.path.exists(os.path.join(path_to_site, '__icon.png')):
    #     shutil.copyfile(os.path.join(path_to_site, '__icon.png'), os.path.join(new_static_path, '__icon.png'))

    # create templates and add parsed stuff to db
    new_templates_path = os.path.join(app.config['TEMPLATE_PATH'], user, sitename)
    if not os.path.exists(new_templates_path):
        os.makedirs(new_templates_path)

    parser = Parser(user, sitename)
    for filename in os.listdir(path_to_site):
        if any([filename.endswith(ext) for ext in app.config['HTML_EXT']]):
            template = parser.parse_html(os.path.join(path_to_site, filename))
            save_path = os.path.join(new_templates_path, os.path.basename(filename))
            with open(save_path, 'w') as f:
                f.write(str(template))

    for entry in parser.fields:
        Entry.get_or_create(user=user, site=sitename, **entry)

    for resource in set(parser.resources):
        if any([resource.endswith(ext) for ext in app.config["STYLESHEET_EXT"]]):
            parser.parse_css(path_to_site, resource)

    for resource in set(parser.resources):
        source = os.path.join(path_to_site, resource)
        destination = os.path.join(static_path, resource)
        if not os.path.exists(source):
            print "WARNING", source, "does not exist."
        else:
            if not os.path.exists(os.path.dirname(destination)):
                os.makedirs(os.path.dirname(destination))
            shutil.copyfile(source, destination)
            print "Copying", resource
        
        #shutil.copyfile(os.path.join(path_to_site, '__icon.png'), os.path.join(new_static_path, '__icon.png'))
    return ''