Use regexps instead of string matching for fields
authorW. Trevor King <wking@drexel.edu>
Wed, 30 Jun 2010 21:18:59 +0000 (17:18 -0400)
committerW. Trevor King <wking@drexel.edu>
Wed, 30 Jun 2010 21:20:54 +0000 (17:20 -0400)
sitecore/__init__.py
sitecore/alt_tags.py
sitecore/prof/import.py

index 5a2453043dc8dbade6396bb5cb4faf569102bb7c..91c8e7781e74b95113867ede1ef94b4a3af3fbb6 100755 (executable)
@@ -150,8 +150,8 @@ class SiteCoreConnection (object):
         a = self.w.switch_to_active_element()
         a.send_keys(message+'\n')
 
-    def find_field(self, field):
-        self.logger.info('finding %s field' % field)
+    def find_field(self, field_regexp, name=None):
+        self.logger.info('finding %s field' % field_regexp)
         # found id start by manually focusing in the element and running
         #   >>> a = s.w.switch_to_active_element()
         #   >>> a.get_attribute('id')
@@ -163,9 +163,9 @@ class SiteCoreConnection (object):
         match = False
         for f in fields:
             granddad = f.find_element_by_xpath('/../..')
-            if granddad.get_text().startswith(field):
+            if field_regexp.match(granddad.get_text()) != None:
                 match = True
                 break
         if match != True:
-            raise KeyError(field)
+            raise KeyError(name)
         return (granddad, f)
index 0bc6472262b1fb47a8fc43a9f14daa7dc0beb1fb..11aa9d8ed689d3ffbd2b5a4d60bba8d39f3f1f64 100644 (file)
@@ -22,6 +22,8 @@ The images affected are:
   Media Library -> Images -> physics -> headshots -> *
 """
 
+import re
+
 from selenium.common.exceptions import NoSuchElementException
 
 from . import SiteCoreConnection
@@ -45,7 +47,7 @@ def add_headshot_alt_tags(s):
         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_granddad,alt_tag = s.find_field(re.compile('.*Alt:.*'), 'Alt')
         alt_tag.clear()
         alt_tag.send_keys(name)
         s.publish_section('Added alt tag for %s' % name)
index f434f84f74792efee9fc9c4e5595354f5798f452..9144b5e8e9e58315f3690e2974eb24c179115bf0 100644 (file)
@@ -22,6 +22,7 @@ Professors will be created in:
   Content -> Home -> physics -> contact -> facultyDirectory
 """
 
+import re
 import time
 
 from selenium.common.exceptions import NoSuchElementException
@@ -36,6 +37,27 @@ class ProfessorAdder (object):
     """
     def __init__(self, sitecore_connection):
         self.s = sitecore_connection
+        self.regexps = {
+            'Degrees':re.compile('Degrees:.*'),
+            'College':re.compile('College:.*'),
+            'Program':re.compile('Program:.*'),
+            'Office':re.compile('Office:.*'),
+            'Specialization':re.compile('.*Specialization:.*'),
+            'Research Interests':re.compile('.*Research Interests:.*'),
+            'Personal Site':re.compile('.*Personal Site:.*'),
+            'Title':re.compile('.*Title:.*'),
+            'First Name':re.compile('.*First Name:.*'),
+            'Last Name':re.compile('.*Last Name:.*'),
+            'Bio':re.compile('.*Bio:.*'),
+            'Headshot':re.compile('.*Headshot:.*'),
+            'Email':re.compile('.*Email:.*'),
+            'Phone':re.compile('.*Phone:.*'),
+            'Fax':re.compile('.*Fax:.*'),
+            'Page Title':re.compile('.*Page Title -.*'),
+            'Menu Title':re.compile('.*Menu Title -.*'),
+            'Breadcrumb Title':re.compile('.*Breadcrumb Title -.*'),
+            'See Also Title':re.compile('.*See Also title -.*'),
+            }
 
     def setup(self):
         #self.s.expand_nav_section('sitecore')
@@ -54,31 +76,32 @@ class ProfessorAdder (object):
         self.create_prof_page(name)
         self.lock_section(name)
         settings = [
-            ('Degrees:', prof.degrees()),
-            ('College:', prof.colleges()),
-            ('Program:', prof.program()),
-            ('Office:', prof.contact.office),
-            ('Specialization:', prof.bio.specialization),
-            ('Research Interests:', ''),
-            ('Personal Site:', prof.sites()),
-            ('Title:', prof.title),
-            ('First Name:', prof.name.first_middle),
-            ('Last Name:', prof.name.last),
-            ('Bio:', prof.profile()),
-            ('Headshot:', ('/Images/physics/headshots/%s'
+            ('Degrees', prof.degrees()),
+            ('College', prof.colleges()),
+            ('Program', prof.program()),
+            ('Office', prof.contact.office),
+            ('Specialization', prof.bio.specialization),
+            ('Research Interests', ''),
+            ('Personal Site', prof.sites()),
+            ('Title', prof.title),
+            ('First Name', prof.name.first_middle),
+            ('Last Name', prof.name.last),
+            ('Bio', prof.profile()),
+            ('Headshot', ('/Images/physics/headshots/%s'
                            % name.lower().replace('.','').replace(' ','_'))),
-            ('Email:', prof.contact.email),
-            ('Phone:', prof.contact.phone),
-            ('Fax:', prof.lab_contact()),
-            ('Page Title -', name),
-            ('Menu Title -', name),
-            ('Breadcrumb Title -', name),
-            ('See Also title -', name),
+            ('Email', prof.contact.email),
+            ('Phone', prof.contact.phone),
+            ('Fax', prof.lab_contact()),
+            ('Page Title', name),
+            ('Menu Title', name),
+            ('Breadcrumb Title', name),
+            ('See Also title', name),
             ]
         for field_name,value in settings:
             if value == None:
                 value = ''
-            granddad,field = self.s.find_field(field_name)
+            granddad,field = self.s.find_field(self.regexps[field_name],
+                                               field_name)
             try:
                 editor_link = granddad.find_element_by_link_text('Edit Html')
                 self.set_editor_field(editor_link, value)