Use re.DOTALL to also match newlines with .
[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 import re
26
27 from selenium.common.exceptions import NoSuchElementException
28
29 from . import SiteCoreConnection
30
31
32 def add_headshot_alt_tags(s):
33     s.expand_nav_section('Media Library')
34     s.expand_nav_section('Images')
35     s.expand_nav_section('physics')
36     s.expand_nav_section('headshots')
37     div,img,link = s.find_nav_section('headshots')
38     for child_link in div.find_elements_by_tag_name('a'):
39         if child_link.get_text() == link.get_text():
40             continue
41         raw_name = child_link.get_text()
42         name = ' '.join([x.capitalize() for x in raw_name.split('_')])
43         s.logger.info('setting alt tag "%s" for %s' % (name, raw_name))
44         s.open_nav_section(raw_name)
45         try:
46             s.lock_section()
47         except NoSuchElementException, e:
48             s.logger.info("can't lock %s (already locked?), skipping" % raw_name)
49             continue
50         alt_granddad,alt_tag = s.find_field(
51             re.compile('.*Alt:.*', re.DOTALL), 'Alt')
52         alt_tag.clear()
53         alt_tag.send_keys(name)
54         s.publish_section('Added alt tag for %s' % name)
55
56
57 def main(argv)
58     import optparse
59     usage = """%prog [options]
60
61 Example setup before running:
62   Xvfb :99 > /dev/null 2>&1 &
63   export DISPLAY=:99
64   java -jar selenium-server-1.0.2-SNAPSHOT-standalone.jar > /dev/null 2>&1 &
65   ./sc.py alt_tags
66 """
67     p = optparse.OptionParser(usage)
68     p.add_option('-u', '--url', metavar='URL', dest='url',
69                  help='set the website URL (%default)',
70                  default='https://webedit.drexel.edu/sitecore/login')
71     p.add_option('-v', '--verbose', dest='verbose', action='count',
72                  help='increment verbosity (%default)',
73                  default=0)
74
75     options,args = p.parse_args(argv[1:])
76
77     s = SiteCoreConnection(options.url, options.verbose)
78     s.start()
79     try:
80         s.login()
81         add_headshot_alt_tags(s)
82     finally:
83         s.stop()