Implement Steve Main template processing suggestion.

This commit is contained in:
Ross Scroggs
2016-06-29 23:06:56 -07:00
parent cab068357a
commit b648ddbfdf
3 changed files with 41 additions and 21 deletions

View File

@ -44,6 +44,7 @@ If an item contains spaces, it should be surrounded by " or '.
<Time> ::= <Year>-<Month>-<Day>(<Space>|T)<Hour>:<Minute>[:<Second>[.<MilliSeconds>[Z]]] <Time> ::= <Year>-<Month>-<Day>(<Space>|T)<Hour>:<Minute>[:<Second>[.<MilliSeconds>[Z]]]
<PrintJobAge> ::= <Number>(m|h|d) <PrintJobAge> ::= <Number>(m|h|d)
<REPattern> ::= <Python Regular Expression, see: https://docs.python.org/2/library/re.html> <REPattern> ::= <Python Regular Expression, see: https://docs.python.org/2/library/re.html>
<Tag> ::= <String>
<UniqueID> ::= uid:<String> <UniqueID> ::= uid:<String>
# Named items # Named items
@ -723,7 +724,7 @@ gam <UserTypeEntity> show sendas
gam <UserTypeEntity> shortcuts <Boolean> gam <UserTypeEntity> shortcuts <Boolean>
gam <UserTypeEntity> signature|sig <String>|(file <FileName> [charset <Charset>]) (replace <REPattern> <String>)* gam <UserTypeEntity> signature|sig <String>|(file <FileName> [charset <Charset>]) (replace <Tag> <String>)*
gam <UserTypeEntity> show signature|sig [format] gam <UserTypeEntity> show signature|sig [format]
gam <UserTypeEntity> snippets <Boolean> gam <UserTypeEntity> snippets <Boolean>
@ -731,7 +732,7 @@ gam <UserTypeEntity> snippets <Boolean>
gam <UserTypeEntity> utf|utf8|utf-8|unicode <Boolean> gam <UserTypeEntity> utf|utf8|utf-8|unicode <Boolean>
gam <UserTypeEntity> vacation <FalseValues> gam <UserTypeEntity> vacation <FalseValues>
gam <UserTypeEntity> vacation <TrueValues> subject <String> (message <String>)|(file <FileName> [charset <CharSet>]) (replace <REPattern> <String>)* gam <UserTypeEntity> vacation <TrueValues> subject <String> (message <String>)|(file <FileName> [charset <CharSet>]) (replace <Tag> <String>)*
[contactsonly] [domainonly] [startdate <Date>] [enddate <Date>] [contactsonly] [domainonly] [startdate <Date>] [enddate <Date>]
gam <UserTypeEntity> show vacation gam <UserTypeEntity> show vacation

View File

@ -382,19 +382,6 @@ def getCharSet(i):
return (i, GC_Values.get(GC_CHARSET, GM_Globals[GM_SYS_ENCODING])) return (i, GC_Values.get(GC_CHARSET, GM_Globals[GM_SYS_ENCODING]))
return (i+2, sys.argv[i+1]) return (i+2, sys.argv[i+1])
def getREPattern(i):
if i < len(sys.argv):
patstr = sys.argv[i]
if patstr:
try:
pattern = re.compile(patstr)
return pattern
except re.error as e:
print u'ERROR: "{0}" is not a valid RE pattern: {1}'.format(patstr, e)
sys.exit(2)
print u'ERROR: expected an <REPattern>'
sys.exit(2)
def getString(i, item, emptyOK=False, optional=False): def getString(i, item, emptyOK=False, optional=False):
if i < len(sys.argv): if i < len(sys.argv):
argstr = sys.argv[i] argstr = sys.argv[i]
@ -5090,7 +5077,31 @@ def getForward(users):
except TypeError: except TypeError:
pass pass
RT_PATTERN = re.compile(r'(?s){RT}.*?{(.*?)}.*?{/RT}')
RT_OPEN_PATTERN = re.compile(r'{RT}')
RT_CLOSE_PATTERN = re.compile(r'{/RT}')
RT_STRIP_PATTERN = re.compile(r'(?s){RT}.*?{/RT}')
RT_TAG_REPLACE_PATTERN = re.compile(r'{(.*?)}')
def processTags(tagReplacements, message):
while True:
match = RT_PATTERN.search(message)
if not match:
break
if tagReplacements.get(match.group(1)):
message = RT_OPEN_PATTERN.sub(u'', message, count=1)
message = RT_CLOSE_PATTERN.sub(u'', message, count=1)
else:
message = RT_STRIP_PATTERN.sub(u'', message, count=1)
while True:
match = RT_TAG_REPLACE_PATTERN.search(message)
if not match:
break
message = re.sub(match.group(0), tagReplacements.get(match.group(1), u''), message)
return message
def doSignature(users): def doSignature(users):
tagReplacements = {}
i = 4 i = 4
if sys.argv[i].lower() == u'file': if sys.argv[i].lower() == u'file':
filename = sys.argv[i+1] filename = sys.argv[i+1]
@ -5102,13 +5113,15 @@ def doSignature(users):
i += 1 i += 1
while i < len(sys.argv): while i < len(sys.argv):
if sys.argv[i].lower() == u'replace': if sys.argv[i].lower() == u'replace':
matchPattern = getREPattern(i+1) matchTag = getString(i+1, u'Tag')
matchReplacement = getString(i+2, u'String', emptyOK=True) matchReplacement = getString(i+2, u'String', emptyOK=True)
signature = matchPattern.sub(matchReplacement, signature) tagReplacements[matchTag] = matchReplacement
i += 3 i += 3
else: else:
print u'ERROR: %s is not a valid argument for "gam <users> signature"' % sys.argv[i] print u'ERROR: %s is not a valid argument for "gam <users> signature"' % sys.argv[i]
sys.exit(2) sys.exit(2)
if tagReplacements:
signature = processTags(tagReplacements, signature)
emailsettings = getEmailSettingsObject() emailsettings = getEmailSettingsObject()
count = len(users) count = len(users)
i = 1 i = 1
@ -5172,6 +5185,7 @@ def doWebClips(users):
def doVacation(users): def doVacation(users):
subject = message = u'' subject = message = u''
tagReplacements = {}
if sys.argv[4].lower() in true_values: if sys.argv[4].lower() in true_values:
enable = True enable = True
elif sys.argv[4].lower() in false_values: elif sys.argv[4].lower() in false_values:
@ -5207,9 +5221,9 @@ def doVacation(users):
i, encoding = getCharSet(i) i, encoding = getCharSet(i)
message = readFile(filename, encoding=encoding) message = readFile(filename, encoding=encoding)
elif sys.argv[i].lower() == u'replace': elif sys.argv[i].lower() == u'replace':
matchPattern = getREPattern(i+1) matchTag = getString(i+1, u'Tag')
matchReplacement = getString(i+2, u'String', emptyOK=True) matchReplacement = getString(i+2, u'String', emptyOK=True)
message = matchPattern.sub(matchReplacement, message) tagReplacements[matchTag] = matchReplacement
i += 3 i += 3
else: else:
print u'ERROR: %s is not a valid argument for "gam <users> vacation"' % sys.argv[i] print u'ERROR: %s is not a valid argument for "gam <users> vacation"' % sys.argv[i]
@ -5218,6 +5232,8 @@ def doVacation(users):
count = len(users) count = len(users)
emailsettings = getEmailSettingsObject() emailsettings = getEmailSettingsObject()
message = message.replace(u'\\n', u'\n') message = message.replace(u'\\n', u'\n')
if tagReplacements:
message = processTags(tagReplacements, message)
for user in users: for user in users:
if user.find(u'@') > 0: if user.find(u'@') > 0:
emailsettings.domain = user[user.find(u'@')+1:] emailsettings.domain = user[user.find(u'@')+1:]

View File

@ -8,10 +8,13 @@ Added argument to gam print cros to limit number of activeTimeRanges and recentU
`gam print cros ... [listlimit <Number>]` `gam print cros ... [listlimit <Number>]`
Added argument to `gam <Users> signature` and `gam <Users> vacation` to allow specification of file character set so that extended characters can be read. Added argument to `gam <Users> signature` and `gam <Users> vacation` to allow specification of file character set so that extended characters can be read.
Credit to Steve Main suggesting the following enhancement.
Added argument to `gam <Users> signature` and `gam <Users> vacation` to allow pattern substitution in the signature and vacation message. Added argument to `gam <Users> signature` and `gam <Users> vacation` to allow pattern substitution in the signature and vacation message.
`gam <Users> signature <String>|(file <FileName> [charset <Charset>]) (replace <REPattern> <String>)*` `gam <Users> signature <String>|(file <FileName> [charset <Charset>]) (replace <Tag> <String>)*`
`gam <Users> vacation <TrueValues> subject <String> (message <String>)|(file <FileName> [charset <CharSet>]) (replace <REPattern> <String>)* `gam <Users> vacation <TrueValues> subject <String> (message <String>)|(file <FileName> [charset <CharSet>]) (replace <Tag> <String>)*
[contactsonly] [domainonly] [startdate <Date>] [enddate <Date>]` [contactsonly] [domainonly] [startdate <Date>] [enddate <Date>]`
Every instance of `{<Tag>}` in the signature/message will be replaced by `<String>`. Instances of the form `{RT}...{Text}...{/RT}` will be eliminated
if there was no `<Tag>` specified that matches `Text` or if a `<Tag>` matching `Text` was specified but the matching `<String>` is empty.
This is especially useful with CSV files. This is especially useful with CSV files.
`gam csv Users.csv gam user "~User" signature file SignatureTemplate.txt replace "#User#" "~User" replace "#Title#" "~Title"` `gam csv Users.csv gam user "~User" signature file SignatureTemplate.txt replace "#User#" "~User" replace "#Title#" "~Title"`