a8cf1e8ccb2f3e0110e05fb51377a9a1f68a9d83
[sitecorepy.git] / sitecore / alt_tags.py
1 #!/usr/bin/env python
2 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 #
4 # This file is part of SiteCorePy.
5 #
6 # SiteCorePy is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation, either version 2 of the License, or (at your
9 # option) any later version.
10 #
11 # SiteCorePy is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with SiteCorePy.  If not, see <http://www.gnu.org/licenses/>.
18
19 """Add 'alt' tags to all images in headshots.
20
21 The images affected are:
22   Media Library -> Images -> physics -> headshots -> *
23 """
24
25 from selenium.common.exceptions import NoSuchElementException
26
27 from . import SiteCoreConnection
28
29
30 def add_headshot_alt_tags(s):
31     s.expand_nav_section('Media Library')
32     s.expand_nav_section('Images')
33     s.expand_nav_section('physics')
34     s.expand_nav_section('headshots')
35     div,img,link = s.find_nav_section('headshots')
36     for child_link in div.find_elements_by_tag_name('a'):
37         if child_link.get_text() == link.get_text():
38             continue
39         raw_name = child_link.get_text()
40         name = ' '.join([x.capitalize() for x in raw_name.split('_')])
41         s.logger.info('setting alt tag "%s" for %s' % (name, raw_name))
42         s.open_nav_section(raw_name)
43         try:
44             s.lock_section()
45         except NoSuchElementException, e:
46             s.logger.info("can't lock %s (already locked?), skipping" % raw_name)
47             continue
48         alt_granddad,alt_tag = s.find_field('Alt:')
49         alt_tag.clear()
50         alt_tag.send_keys(name)
51         s.publish_section('Added alt tag for %s' % name)
52
53
54 def main(argv)
55     import optparse
56     usage = """%prog [options]
57
58 Example setup before running:
59   Xvfb :99 > /dev/null 2>&1 &
60   export DISPLAY=:99
61   java -jar selenium-server-1.0.2-SNAPSHOT-standalone.jar > /dev/null 2>&1 &
62   ./sc alt_tags
63 """
64     p = optparse.OptionParser(usage)
65     p.add_option('-u', '--url', metavar='URL', dest='url',
66                  help='set the website URL (%default)',
67                  default='https://webedit.drexel.edu/sitecore/login')
68     p.add_option('-v', '--verbose', dest='verbose', action='count',
69                  help='increment verbosity (%default)',
70                  default=0)
71
72     options,args = p.parse_args()
73
74     s = SiteCoreConnection(options.url, options.verbose)
75     s.start()
76     try:
77         s.login()
78         add_headshot_alt_tags(s)
79     finally:
80         s.stop()