From a1a24122cd2108103fa54a5389f97bce77d5bf67 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Fri, 25 Jun 2010 15:08:33 -0400 Subject: [PATCH] Moved to submodule structure and split out alt_tags --- sitecore.py => sitecore/__init__.py | 57 -------------------- sitecore/alt_tags.py | 83 +++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 57 deletions(-) rename sitecore.py => sitecore/__init__.py (74%) create mode 100644 sitecore/alt_tags.py diff --git a/sitecore.py b/sitecore/__init__.py similarity index 74% rename from sitecore.py rename to sitecore/__init__.py index 6d864fd..e6e0ac1 100755 --- a/sitecore.py +++ b/sitecore/__init__.py @@ -33,7 +33,6 @@ import logging import time from selenium.firefox.webdriver import WebDriver -from selenium.common.exceptions import NoSuchElementException class SiteCoreConnection (object): @@ -162,59 +161,3 @@ class SiteCoreConnection (object): if match != True: raise KeyError(field) return (granddad, f) - - -def add_headshot_alt_tags(s): - s.expand_nav_section('Media Library') - s.expand_nav_section('Images') - s.expand_nav_section('physics') - s.expand_nav_section('headshots') - div,img,link = s.find_nav_section('headshots') - for child_link in div.find_elements_by_tag_name('a'): - if child_link.get_text() == link.get_text(): - continue - raw_name = child_link.get_text() - name = ' '.join([x.capitalize() for x in raw_name.split('_')]) - s.logger.info('setting alt tag "%s" for %s' % (name, raw_name)) - s.open_nav_section(raw_name) - try: - s.lock_section() - except NoSuchElementException, e: - s.logger.info("can't lock %s (already locked?), skipping" % raw_name) - continue - alt_granddad,alt_tag = s.find_field('Alt:') - alt_tag.clear() - alt_tag.send_keys(name) - s.publish_section('Added alt tag for %s' % name) - - -if __name__ == '__main__': - import optparse - usage = """%prog [options] - -Example setup before running: - Xvfb :99 > /dev/null 2>&1 & - export DISPLAY=:99 - java -jar selenium-server-1.0.2-SNAPSHOT-standalone.jar > /dev/null 2>&1 & - ./sitecore.py -""" - p = optparse.OptionParser(usage) - p.add_option('-u', '--url', metavar='URL', dest='url', - help='set the website URL (%default)', - default='https://webedit.drexel.edu/sitecore/login') - p.add_option('-r', '--reload', metavar='NUM', dest='reload', type='int', - help='if a page load fails, try again NUM times (%default)', - default=5) - p.add_option('-v', '--verbose', dest='verbose', action='count', - help='increment verbosity (%default)', - default=0) - - options,args = p.parse_args() - - s = SiteCoreConnection(options.url, options.verbose) - s.start() - try: - s.login() - add_headshot_alt_tags(s) - finally: - s.stop() diff --git a/sitecore/alt_tags.py b/sitecore/alt_tags.py new file mode 100644 index 0000000..19fac3c --- /dev/null +++ b/sitecore/alt_tags.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python +# Copyright (C) 2010 W. Trevor King +# +# This file is part of SiteCorePy. +# +# SiteCorePy is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation, either version 2 of the License, or (at your +# option) any later version. +# +# SiteCorePy is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with SiteCorePy. If not, see . + +"""Add 'alt' tags to all images in headshots. + +The images affected are: + Media Library -> Images -> physics -> headshots -> * +""" + +from selenium.common.exceptions import NoSuchElementException + +from . import SiteCoreConnection + + +def add_headshot_alt_tags(s): + s.expand_nav_section('Media Library') + s.expand_nav_section('Images') + s.expand_nav_section('physics') + s.expand_nav_section('headshots') + div,img,link = s.find_nav_section('headshots') + for child_link in div.find_elements_by_tag_name('a'): + if child_link.get_text() == link.get_text(): + continue + raw_name = child_link.get_text() + name = ' '.join([x.capitalize() for x in raw_name.split('_')]) + s.logger.info('setting alt tag "%s" for %s' % (name, raw_name)) + s.open_nav_section(raw_name) + try: + s.lock_section() + except NoSuchElementException, e: + s.logger.info("can't lock %s (already locked?), skipping" % raw_name) + continue + alt_granddad,alt_tag = s.find_field('Alt:') + alt_tag.clear() + alt_tag.send_keys(name) + s.publish_section('Added alt tag for %s' % name) + + +if __name__ == '__main__': + import optparse + usage = """%prog [options] + +Example setup before running: + Xvfb :99 > /dev/null 2>&1 & + export DISPLAY=:99 + java -jar selenium-server-1.0.2-SNAPSHOT-standalone.jar > /dev/null 2>&1 & + ./sitecore.py +""" + p = optparse.OptionParser(usage) + p.add_option('-u', '--url', metavar='URL', dest='url', + help='set the website URL (%default)', + default='https://webedit.drexel.edu/sitecore/login') + p.add_option('-r', '--reload', metavar='NUM', dest='reload', type='int', + help='if a page load fails, try again NUM times (%default)', + default=5) + p.add_option('-v', '--verbose', dest='verbose', action='count', + help='increment verbosity (%default)', + default=0) + + options,args = p.parse_args() + + s = SiteCoreConnection(options.url, options.verbose) + s.start() + try: + s.login() + add_headshot_alt_tags(s) + finally: + s.stop() -- 2.26.2