From 7ead7646c4d00e09ca334bc9638c429f089c070a Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 30 Jun 2010 17:18:59 -0400 Subject: [PATCH] Use regexps instead of string matching for fields --- sitecore/__init__.py | 8 +++--- sitecore/alt_tags.py | 4 ++- sitecore/prof/import.py | 63 ++++++++++++++++++++++++++++------------- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/sitecore/__init__.py b/sitecore/__init__.py index 5a24530..91c8e77 100755 --- a/sitecore/__init__.py +++ b/sitecore/__init__.py @@ -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) diff --git a/sitecore/alt_tags.py b/sitecore/alt_tags.py index 0bc6472..11aa9d8 100644 --- a/sitecore/alt_tags.py +++ b/sitecore/alt_tags.py @@ -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) diff --git a/sitecore/prof/import.py b/sitecore/prof/import.py index f434f84..9144b5e 100644 --- a/sitecore/prof/import.py +++ b/sitecore/prof/import.py @@ -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) -- 2.26.2