branch : pmacs2
This commit is contained in:
moculus 2007-03-29 22:37:34 +00:00
parent 8d33c8b0e2
commit 33dc4ff2e3
5 changed files with 108 additions and 69 deletions

View File

@ -131,37 +131,37 @@ sub get_reporting_aliases {
my $self = shift; my $self = shift;
my $rdmr = TBB::DataManager::Relational->new( my $rdmr = TBB::DataManager::Relational->new(
host => "reports.v2.tbb", host => "reports.v2.tbb",
port => 5432, port => 5432,
db => "reporting", db => "reporting",
user => "tbbv2db", user => "tbbv2db",
password => "reports", password => "reports",
); );
my $aliases_aref = $rdmr->select(select => "alias, question, type", my $aliases_aref = $rdmr->select(select => "alias, question, type",
from => "user_data_alias"); from => "user_data_alias");
$self->{aliases} = []; $self->{aliases} = [];
# make sure each alias is valid; undefined formulas or conditions will give # make sure each alias is valid; undefined formulas or conditions will give
# us problems down the road # us problems down the road
foreach my $alias (@$aliases_aref) { foreach my $alias (@$aliases_aref) {
eval { eval {
my $id = TBB::ID::normalize($alias->{question}); my $id = TBB::ID::normalize($alias->{question});
$alias->{question} = $id; $alias->{question} = $id;
if(TBB::ID::is_a($id, "question")) { if(TBB::ID::is_a($id, "question")) {
push(@{$self->{aliases}}, $alias); push(@{$self->{aliases}}, $alias);
} elsif($TBB::BenefitDelivery::ResourceManager->get_component($id)) { } elsif($TBB::BenefitDelivery::ResourceManager->get_component($id)) {
push(@{$self->{aliases}}, $alias); push(@{$self->{aliases}}, $alias);
} else { } else {
write_log("error", "Alias \"$alias->{question}\" does not exist"); write_log("error", "Alias \"$alias->{question}\" does not exist");
}
};
if($@) {
write_log("error", "There was a problem with \"$alias->{question}\": $@");
} }
};
if($@) {
write_log("error", "There was a problem with \"$alias->{question}\": $@");
} }
}
# this will help to make sure that the exports for each user are proceeding # this will help to make sure that the exports for each user are proceeding
# correctly # correctly
write_log("notice", "We are reporting on " . scalar(@{$self->{aliases}}) . " aliases."); write_log("notice", "We are reporting on " . scalar(@{$self->{aliases}}) . " aliases.");
@ -173,11 +173,11 @@ sub get_reporting_aliases {
# cleaner # cleaner
sub data_by_user_sql { sub data_by_user_sql {
my $self = shift; my $self = shift;
my $sql = "CREATE TABLE data_by_user (id_user integer PRIMARY KEY, "; my $sql = "CREATE TABLE data_by_user (id_user integer PRIMARY KEY, ";
$sql .= join(", ", map { "$_->{alias} $_->{type}" } @{$self->{aliases}}); $sql .= join(", ", map { "$_->{alias} $_->{type}" } @{$self->{aliases}});
$sql .= ");"; $sql .= ");";
return $sql; return $sql;
} }
@ -194,14 +194,14 @@ sub populate {
'', '',
{RaiseError => 1}); {RaiseError => 1});
eval { $rdbh->do("DROP TABLE data_by_user;"); }; eval { $rdbh->do("DROP TABLE data_by_user;"); };
# build new data_by_user table # build new data_by_user table
my $sql = $self->data_by_user_sql(); my $sql = $self->data_by_user_sql();
print STDERR "FFF: $sql\n"; print STDERR "FFF: $sql\n";
$rdbh->do($sql); $rdbh->do($sql);
print STDERR "GGG: we are ok\n"; print STDERR "GGG: we are ok\n";
#exit(1); #exit(1);
my @report_fields = map {$_->{alias}} @{$self->{aliases}}; my @report_fields = map {$_->{alias}} @{$self->{aliases}};
push(@report_fields, "id_user"); push(@report_fields, "id_user");
@report_fields = sort(@report_fields); @report_fields = sort(@report_fields);
@ -210,7 +210,7 @@ sub populate {
#print STDERR "JJJ: $sql\n"; #print STDERR "JJJ: $sql\n";
my $sth = $rdbh->prepare($sql); my $sth = $rdbh->prepare($sql);
#exit(1); #exit(1);
# for each client user, grab their system_data_id (which is # for each client user, grab their system_data_id (which is
# a session ID); we get an array of session_id references. # a session ID); we get an array of session_id references.
my $fields = "id_user, system_data_id"; my $fields = "id_user, system_data_id";
@ -219,17 +219,17 @@ sub populate {
my $session_ids = $self->{dmr}->select(select => $fields, my $session_ids = $self->{dmr}->select(select => $fields,
from => $table, from => $table,
where => $where); where => $where);
# for each hash in the array we made (each session ID) # for each hash in the array we made (each session ID)
my $processed = 0; my $processed = 0;
my $total = scalar(@$session_ids); my $total = scalar(@$session_ids);
foreach my $session_id (@$session_ids) { foreach my $session_id (@$session_ids) {
$processed++; $processed++;
my $system_id = $session_id->{system_data_id}; my $system_id = $session_id->{system_data_id};
my $client_id = $session_id->{id_user}; my $client_id = $session_id->{id_user};
write_log('info', "Exporting $system_id (user: $client_id) ($processed/$total)"); write_log('info', "Exporting $system_id (user: $client_id) ($processed/$total)");
# we need to see if there is a session ID or not in the # we need to see if there is a session ID or not in the
# sessions table. there almost certainly won't be more # sessions table. there almost certainly won't be more
# than one but we test anyway. unless there is exactly # than one but we test anyway. unless there is exactly
@ -246,33 +246,33 @@ sub populate {
write_log('warn', "Session $system_id is not unique ($count found)"); write_log('warn', "Session $system_id is not unique ($count found)");
next; next;
} }
my $csd = TBB::ClientSystemData->new($system_id, "reporter"); my $csd = TBB::ClientSystemData->new($system_id, "reporter");
my $dms = $csd->retrieve_data_manager_session(); my $dms = $csd->retrieve_data_manager_session();
my $user_data = TBB::UserData::New->new(data_manager_relational => $self->{dmr}, my $user_data = TBB::UserData::New->new(data_manager_relational => $self->{dmr},
data_manager_session => $dms, data_manager_session => $dms,
client_system_data => $csd, client_system_data => $csd,
current_client_id => $client_id); current_client_id => $client_id);
my $user_data_obj = {'id_user' => $client_id}; my $user_data_obj = {'id_user' => $client_id};
foreach my $alias_href (@{$self->{aliases}}) { foreach my $alias_href (@{$self->{aliases}}) {
my $alias = $alias_href->{alias}; my $alias = $alias_href->{alias};
my $id = $alias_href->{question}; my $id = $alias_href->{question};
my $type = $alias_href->{type}; my $type = $alias_href->{type};
my $value = $user_data->retrieve_value(base => $id, my $value = $user_data->retrieve_value(base => $id,
this_id_user => $client_id); this_id_user => $client_id);
$value ||= ""; $value ||= "";
$user_data_obj->{$alias} = $value; $user_data_obj->{$alias} = $value;
} }
my @values = (); my @values = ();
foreach my $name (@report_fields) { foreach my $name (@report_fields) {
push(@values, $user_data_obj->{$name}); push(@values, $user_data_obj->{$name});
} }
$sth->execute(@values); $sth->execute(@values);
write_log('debug', " Saving " . scalar(keys(%$user_data_obj)) . " components") if TBB::LogManager::writes_at('debug'); write_log('debug', " Saving " . scalar(keys(%$user_data_obj)) . " components") if TBB::LogManager::writes_at('debug');
#write_log('debug', " User Data Obj: " . Dumper($user_data_obj)) if TBB::LogManager::writes_at('debug'); #write_log('debug', " User Data Obj: " . Dumper($user_data_obj)) if TBB::LogManager::writes_at('debug');

View File

@ -1,14 +1,35 @@
import sys
color_list = []
color_list.extend(['\033[3%dm' % x for x in range(0, 8)])
color_list.extend(['\033[3%d;1m' % x for x in range(0, 8)])
color_list.append('\033[0m')
color_names = [
'black', 'dred', 'dgreen', 'brown', 'dblue', 'dpurple', 'dcyan', 'lgrey',
'dgrey', 'lred', 'lgreen', 'yellow', 'lblue', 'lpurple', 'lcyan', 'white',
'unset',
]
color_dict ={}
for i in range(0, len(color_list)):
color_dict[color_names[i]] = color_list[i]
class Highlighter: class Highlighter:
def __init__(self, lexer): def __init__(self, lexer):
self.lexer = lexer self.lexer = lexer
self.tokens = [] self.tokens = []
def display(self): def display(self, token_colors={}, debug=False):
for group in self.tokens: for group in self.tokens:
for token in group: for token in group:
if token.name in token_colors:
color_name = token_colors[token.name]
sys.stdout.write(color_dict[color_name])
elif debug:
raise Exception, "no highlighting for %r" % token.name
sys.stdout.write(token.string) sys.stdout.write(token.string)
sys.stdout.write('\n') sys.stdout.write('\n')
sys.stdout.write('\n')
def highlight(self, lines): def highlight(self, lines):
self.tokens = [[] for l in lines] self.tokens = [[] for l in lines]

View File

@ -221,6 +221,10 @@ class DualRegionRule(Rule):
class Grammar: class Grammar:
rules = [] rules = []
def __init__(self):
for rule in self.rules:
if hasattr(rule, 'grammar') and rule.grammar is None:
rule.grammar = self
class Lexer: class Lexer:
def __init__(self, name, grammar): def __init__(self, name, grammar):

View File

@ -115,11 +115,11 @@ class PerlGrammar(Grammar):
pattern=r"(?<!->)(?:STDIN|STDERR|STDOUT|and|cmp|continue|do|else|elsif|eq|eval|foreach|for|if|last|my|next|ne|not|or|our|package|require|return|sub|undef|unless|until|use|while)(?![a-zA-Z0-9_])", pattern=r"(?<!->)(?:STDIN|STDERR|STDOUT|and|cmp|continue|do|else|elsif|eq|eval|foreach|for|if|last|my|next|ne|not|or|our|package|require|return|sub|undef|unless|until|use|while)(?![a-zA-Z0-9_])",
), ),
PatternRule( PatternRule(
name=r'hash_bareword_index', name=r'bareword_hash_index',
pattern=r'(?<={) *[A-Za-z0-9_]+(?=})', pattern=r'(?<={) *[A-Za-z0-9_]+(?=})',
), ),
PatternRule( PatternRule(
name=r'literal_hash_bareword_index', name=r'bareword_hash_key',
pattern=r'[A-Za-z0-9_]+(?= *=>)', pattern=r'[A-Za-z0-9_]+(?= *=>)',
), ),
PatternRule( PatternRule(
@ -226,7 +226,7 @@ class PerlGrammar(Grammar):
), ),
PatternRule( PatternRule(
name=r'sub', name=r'sub',
pattern=r"(?<=sub )[a-zA-Z_][a-zA-Z_0-9]*(?=[ \n]*{)", pattern=r"(?<=sub )[a-zA-Z_][a-zA-Z_0-9]*(?= *{)",
), ),
PatternRule( PatternRule(
name=r'use', name=r'use',
@ -258,11 +258,33 @@ class PerlGrammar(Grammar):
), ),
PatternRule( PatternRule(
name=r'bareword_method', name=r'bareword_method',
pattern=r"(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]* *\(", pattern=r"(?:[a-zA-Z_][a-zA-Z_0-9]*::)*[a-zA-Z_][a-zA-Z_0-9]*(?= *\()",
),
#PatternRule(
# name=r'delimiter',
# pattern=r"\(|\)|\[|\]|{|}|,|;|->|=>|=|\?|(?<!:):(?!=:)",
#),
RegionRule(
name=r'paren',
start=r'\(',
grammar=None,
end=r'\)',
),
RegionRule(
name=r'brace',
start=r'{',
grammar=None,
end=r'}',
),
RegionRule(
name=r'bracket',
start=r'\[',
grammar=None,
end=r'\]',
), ),
PatternRule( PatternRule(
name=r'delimiter', name=r'delimiter',
pattern=r"\(|\)|\[|\]|{|}|,|;|->|=>|=|\?|(?<!:):(?!=:)", pattern=r",|;|->|=>|=|\?|(?<!:):(?!=:)",
), ),
PatternRule( PatternRule(
name=r'unary_operator', name=r'unary_operator',

View File

@ -1,5 +1,5 @@
import sys import sys
import lex2, lex2_perl import lex2, lex2_perl, highlight2
color_list = [] color_list = []
color_list.extend(['\033[3%dm' % x for x in range(0, 8)]) color_list.extend(['\033[3%dm' % x for x in range(0, 8)])
@ -87,15 +87,7 @@ for path in paths:
grammar = lex2_perl.PerlGrammar() grammar = lex2_perl.PerlGrammar()
lexer = lex2.Lexer('lexer', grammar) lexer = lex2.Lexer('lexer', grammar)
highlighter = highlight2.Highlighter(lexer)
lexer.lex(lines) highlighter.highlight(lines)
y = 0 highlighter.display(token_colors)
for token in lexer:
while token.y > y:
sys.stdout.write('\n')
y += 1
#color_name = token_colors.get(token.name, 'white')
color_name = token_colors[token.name]
sys.stdout.write(color_dict[color_name])
sys.stdout.write(token.string)
sys.stdout.write('\n')