├── .gitignore
├── Backup.py
├── ChefeDeGabinete
├── Exoneracao.py
├── Nomeacao.py
├── Substituicao.py
├── __init__.py
└── main.py
├── Config
├── chefesdegabinete.xml
├── config.xml.template
└── prodam.xml
├── DiarioTools
├── Config.py
├── GMailer.py
├── Log.py
├── Parser.py
├── Process.py
├── ProdamMailer.py
├── Retriever.py
├── Search.py
└── __init__.py
├── DlSanity.py
├── LICENSE
├── Prodam
├── AdmIndireta.py
├── Common.py
├── GabineteDoPrefeito.py
├── Prodam.py
├── Suspensas.py
├── __init__.py
└── main.py
├── README.md
├── clean
└── main.py
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 | *.log
3 | *.pk
4 | *.swp
5 | *.orig
6 | *.htm
7 | *.html
8 | config*.xml
9 |
--------------------------------------------------------------------------------
/Backup.py:
--------------------------------------------------------------------------------
1 | import re
2 | import os
3 | import shutil
4 | import datetime
5 |
6 | def Backup():
7 | """Backup last execution pickles"""
8 | bkpFolder = "bkp"
9 | bkpName = os.path.join(bkpFolder,
10 | datetime.datetime.now().isoformat().replace("-", "").replace(":", "").replace(".", ""))
11 | if not os.path.exists(bkpFolder):
12 | os.mkdir(bkpFolder)
13 | os.mkdir(bkpName)
14 | files = os.listdir(".")
15 | for file in files:
16 | if re.search("\.pk", file):
17 | shutil.copy(file, bkpName)
18 |
--------------------------------------------------------------------------------
/ChefeDeGabinete/Exoneracao.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from DiarioTools.Parser import *
4 | from DiarioTools.Process import *
5 | from DiarioTools.Search import *
6 | import re
7 |
8 | class ParseExoneracaoChefeDeGabinete(GenericParser):
9 | def Initialize(self):
10 | self.AddExpression("^\s*Exonerar.{0,1000}?(senhora|senhor)([^,]+).{0,400}?Chefe de Gabinete.(.+)", [2,3,0], re.I|re.M)
11 |
12 | class SearchExoneracaoChefeDeGabinete(DlSearch):
13 | def SetOptions(self):
14 | self.options["sort"] = u"data desc"
15 | self.query = "exonerar \"chefe de gabinete\""
16 |
17 | class ProcessorExoneracaoChefeDeGabinete(ResponseProcessor):
18 | def __init__(self, configInstance, searchObject, parseObject, fileName, sessionName):
19 | super(ProcessorExoneracaoChefeDeGabinete, self).__init__(configInstance, searchObject, parseObject, sessionName)
20 | self.fileName = fileName
21 | self.records = []
22 |
23 | with open(self.fileName, "a") as fd:
24 | fd.write("*** Exonerações ***\r\n")
25 |
26 | def Persist(self, data):
27 | if len(data) > 0:
28 | strOut = """Em """ + self.ProcessDate(data) + """, """ + self.ProcessName(data) + """ foi exonerado do cargo Chefe de Gabinete """ + self.ProcessGabinete(data) + "\n"
29 | self.records.append(strOut.encode("utf-8"))
30 | with open(self.fileName, "a") as fd:
31 | fd.write(strOut.encode("utf-8"))
32 |
33 | def ProcessEnd(self):
34 | message = "*** Exonerações ***\r\n"
35 | if (len(self.records) == 0):
36 | message += """Nenhum Chefe de Gabinete exonerado neste período\r\n\r\n"""
37 | Log.Log("Sem Alterações")
38 | else:
39 | message += "\r\n".join(self.records)
40 | message += "\r\n"
41 | return message
42 |
43 | def ProcessName(self, data):
44 | return data[0]
45 |
46 | def ProcessGabinete(self, data):
47 | gabineteRe = re.search("(Funda..o|Controladoria|Secretaria|Subprefeitura|Superintend.ncia)\s*,?\s*(([^\.](?! constante))*)", data[1], re.I)
48 | if gabineteRe is not None:
49 | gabineteFromData = gabineteRe.group(0)
50 | gabineteFromData = "da " + gabineteFromData
51 | else:
52 | gabineteRe = re.search("(Instituto|Servi.o)\s*,?\s*([^,]*)", data[1], re.I)
53 | if gabineteRe is not None:
54 | gabineteFromData = gabineteRe.group(0)
55 | gabineteFromData = "do " + gabineteFromData
56 | else:
57 | gabineteRe = re.search("^([^,]*).\s*s.mbolo", data[1], re.I)
58 | if gabineteRe is not None:
59 | gabineteFromData = gabineteRe.group(1)
60 | else:
61 | gabineteFromData = data[1]
62 | gabineteFromData = re.sub("s.mbolo \w*,", "", gabineteFromData, re.I)
63 | gabineteFromData = re.sub(",?\s*da Chefia de Gabinete[^,]*x", "", gabineteFromData, re.I)
64 | gabineteFromData = re.sub(",?\s*constante.*$", "", gabineteFromData, re.I)
65 | return gabineteFromData
66 |
67 | def ProcessDate(self, data):
68 | date = self.GetDateFromId()
69 | dateRe = re.search("a partir de ([^,]*)", data[2], re.I)
70 | if dateRe is not None:
71 | date = dateRe.group(1)
72 | return date
73 |
--------------------------------------------------------------------------------
/ChefeDeGabinete/Nomeacao.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from DiarioTools.Parser import *
4 | from DiarioTools.Process import *
5 | from DiarioTools.Search import *
6 | import re
7 |
8 | class ParseNomeacaoChefeDeGabinete(GenericParser):
9 | def Initialize(self):
10 | self.AddExpression("^\s*Nomear.*?(senhora|senhor)\s*([^,]*).*?Chefe de Gabinete.(.*)", [2, 3, 0], re.I|re.M)
11 |
12 | class SearchNomeacaoChefeDeGabinete(DlSearch):
13 | def SetOptions(self):
14 | self.options["sort"] = u"data desc"
15 | self.query = "nomeação \"chefe de gabinete\""
16 |
17 | class ProcessorNomeacaoChefeDeGabinete(ResponseProcessor):
18 | def __init__(self, configInstance, searchObject, parseObject, fileName, sessionName):
19 | super(ProcessorNomeacaoChefeDeGabinete, self).__init__(configInstance, searchObject, parseObject, sessionName)
20 | self.fileName = fileName
21 | self.records = []
22 |
23 | with open(self.fileName, "a") as fd:
24 | fd.write("*** Nomeações ***\r\n")
25 |
26 | def Persist(self, data):
27 | if len(data) > 0:
28 | strOut = """Em """ + self.ProcessDate(data) + """, """ + self.ProcessName(data) + """ foi nomeado Chefe de Gabinete """ + self.ProcessGabinete(data) + "\n"
29 | self.records.append(strOut.encode("utf-8"))
30 | with open(self.fileName, "a") as fd:
31 | fd.write(strOut.encode("utf-8"))
32 |
33 | def ProcessEnd(self):
34 | message = "*** Nomeações ***\r\n"
35 | if (len(self.records) == 0):
36 | message += """Nenhum Chefe de Gabinete nomeado neste período\r\n\r\n"""
37 | Log.Log("Sem Alterações")
38 | else:
39 | message += "\r\n".join(self.records)
40 | message += "\r\n"
41 | return message
42 |
43 | def ProcessName(self, data):
44 | return data[0]
45 |
46 | def ProcessGabinete(self, data):
47 | gabineteRe = re.search("(Funda..o|Controladoria|Secretaria|Subprefeitura|Superintend.ncia)\s*,?\s*(([^\.](?! constante))*)", data[1], re.I)
48 | if gabineteRe is not None:
49 | gabineteFromData = gabineteRe.group(0)
50 | gabineteFromData = "da " + gabineteFromData
51 | else:
52 | gabineteRe = re.search("(Instituto|Servi.o)\s*,?\s*([^,]*)", data[1], re.I)
53 | if gabineteRe is not None:
54 | gabineteFromData = gabineteRe.group(0)
55 | gabineteFromData = "do " + gabineteFromData
56 | else:
57 | gabineteRe = re.search("^([^,]*).\s*s.mbolo", data[1], re.I)
58 | if gabineteRe is not None:
59 | gabineteFromData = gabineteRe.group(1)
60 | else:
61 | gabineteFromData = data[1]
62 | gabineteFromData = re.sub("s.mbolo \w*,", "", gabineteFromData, re.I)
63 | gabineteFromData = re.sub(",?\s*da Chefia de Gabinete[^,]*x", "", gabineteFromData, re.I)
64 | gabineteFromData = re.sub(",?\s*constante.*$", "", gabineteFromData, re.I)
65 | return gabineteFromData
66 |
67 | def ProcessDate(self, data):
68 | date = self.GetDateFromId()
69 | dateRe = re.search("a partir de ([^,]*)", data[2], re.I)
70 | if dateRe is not None:
71 | date = dateRe.group(1)
72 | return date
73 |
--------------------------------------------------------------------------------
/ChefeDeGabinete/Substituicao.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from DiarioTools.Parser import *
4 | from DiarioTools.Process import *
5 | from DiarioTools.Search import *
6 | import re
7 |
8 | class ParseSubstituicaoChefeDeGabinete(GenericParser):
9 | def Initialize(self):
10 | self.AddExpression("^.*?(senhora|senhor)([^,]+).{0,100}?Per.odo de ([^,]*).{0,100}?Substituir.{0,300}?(senhora|senhor)([^,]+).{0,300}?(chefe de gabinete.*)", [2,5,6,3], re.I|re.M)
11 |
12 | class SearchSubstituicaoChefeDeGabinete(DlSearch):
13 | def SetOptions(self):
14 | self.options["sort"] = u"data desc"
15 | self.query = "substituir \"chefe de gabinete\""
16 |
17 | class ProcessorSubstituicaoChefeDeGabinete(ResponseProcessor):
18 | def __init__(self, configInstance, searchObject, parseObject, fileName, sessionName):
19 | super(ProcessorSubstituicaoChefeDeGabinete, self).__init__(configInstance, searchObject, parseObject, sessionName)
20 | self.fileName = fileName
21 | self.records = []
22 |
23 | with open(self.fileName, "a") as fd:
24 | fd.write("*** Substituições ***\r\n")
25 |
26 | def Persist(self, data):
27 | if len(data) > 0:
28 | strOut = """Em """ + self.GetDateFromId() + """, """ + self.ProcessName1(data) + """ substitui """ + self.ProcessName2(data) + """, chefe de gabinete """ + self.ProcessGabinete(data) + """ de """ + self.ProcessPeriod(data)+ "\n"
29 | self.records.append(strOut.encode("utf-8"))
30 | with open(self.fileName, "a") as fd:
31 | fd.write(strOut.encode("utf-8"))
32 |
33 | def ProcessEnd(self):
34 | message = "*** Substituições ***\r\n"
35 | if (len(self.records) == 0):
36 | message += """Nenhum Chefe de Gabinete substituído neste período\r\n\r\n"""
37 | Log.Log("Sem Alterações")
38 | else:
39 | message += "\r\n".join(self.records)
40 | message += "\r\n"
41 | return message
42 |
43 | def ProcessName1(self, data):
44 | return data[0]
45 |
46 | def ProcessName2(self, data):
47 | return data[1]
48 |
49 | def ProcessPeriod(self, data):
50 | return data[3]
51 |
52 | def ProcessGabinete(self, data):
53 | gabineteRe = re.search("(Funda..o|Controladoria|Secretaria|Subprefeitura|Superintend.ncia)\s*,?\s*(([^\.](?! constante))*)", data[2], re.I)
54 | if gabineteRe is not None:
55 | gabineteFromData = gabineteRe.group(0)
56 | gabineteFromData = "da " + gabineteFromData
57 | else:
58 | gabineteRe = re.search("(Instituto|Servi.o)\s*,?\s*([^,]*)", data[2], re.I)
59 | if gabineteRe is not None:
60 | gabineteFromData = gabineteRe.group(0)
61 | gabineteFromData = "do " + gabineteFromData
62 | else:
63 | gabineteRe = re.search("^([^,]*).\s*s.mbolo", data[2], re.I)
64 | if gabineteRe is not None:
65 | gabineteFromData = gabineteRe.group(1)
66 | else:
67 | gabineteFromData = data[2]
68 | gabineteFromData = re.sub("s.mbolo \w*,", "", gabineteFromData, re.I)
69 | gabineteFromData = re.sub(",?\s*da Chefia de Gabinete[^,]*x", "", gabineteFromData, re.I)
70 | gabineteFromData = re.sub(",?\s*constante.*$", "", gabineteFromData, re.I)
71 | return gabineteFromData
72 |
--------------------------------------------------------------------------------
/ChefeDeGabinete/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LabProdam/LabDiario/76a81e366d0f41f500d86c9d0485c2ca2647c33b/ChefeDeGabinete/__init__.py
--------------------------------------------------------------------------------
/ChefeDeGabinete/main.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from Nomeacao import *
4 | from Exoneracao import *
5 | from Substituicao import *
6 | from DiarioTools.Config import Configuration
7 | from DiarioTools.GMailer import *
8 | from DiarioTools.Log import *
9 | import datetime
10 | import sys
11 | import os
12 |
13 | logName = "Default.log"
14 |
15 | def HandleNomeacao(configInstance):
16 | searcher = SearchNomeacaoChefeDeGabinete(configInstance, True)
17 | parser = ParseNomeacaoChefeDeGabinete()
18 | processor = ProcessorNomeacaoChefeDeGabinete(configInstance, searcher, parser, logName, "NomeacaoChefeDeGabinete")
19 | return processor.Process()
20 |
21 | def HandleExoneracao(configInstance):
22 | searcher = SearchExoneracaoChefeDeGabinete(configInstance, True)
23 | parser = ParseExoneracaoChefeDeGabinete()
24 | processor = ProcessorExoneracaoChefeDeGabinete(configInstance, searcher, parser, logName, "ExoneracaoChefeDeGabinete")
25 | return processor.Process()
26 |
27 | def HandleSubstituicao(configInstance):
28 | searcher = SearchSubstituicaoChefeDeGabinete(configInstance, True)
29 | parser = ParseSubstituicaoChefeDeGabinete()
30 | processor = ProcessorSubstituicaoChefeDeGabinete(configInstance, searcher, parser, logName, "SubstituicaoChefeDeGabinete")
31 | return processor.Process()
32 |
33 | def Run(localLogName = "Default.log"):
34 | global logName
35 | logName = localLogName
36 |
37 | try:
38 | config = Configuration(os.path.join("Config","config.xml"), sys.argv, logName)
39 | config.AppendConfigurationFile(os.path.join("Config","chefesdegabinete.xml"))
40 | Log.Log("Searching Nomeacoes")
41 | messages = HandleNomeacao(config)
42 | Log.Log("Searching Exoneracoes")
43 | messages += HandleExoneracao(config)
44 | Log.Log("Searching Substituicoes")
45 | messages += HandleSubstituicao(config)
46 |
47 | if (config.mode == "alert mode"):
48 | messages = "Relatório de " + datetime.datetime.now().strftime("%d/%m/%y %H:%M:%S") + "\r\n\r\n" + messages
49 | Log.Log("Enviando E-Mail")
50 | mailer = GMailerWrappper(config)
51 | mailer.Send(messages)
52 | except Exception as e:
53 | Log.Warning("Problemas encontrados durante a execução do script")
54 | Log.Warning(str(e))
55 |
--------------------------------------------------------------------------------
/Config/chefesdegabinete.xml:
--------------------------------------------------------------------------------
1 |
2 | LabProdam - Atualizações - Chefes de Gabinetes
3 | ChefesDeGabinete.log
4 | Overwrite
5 |
6 |
--------------------------------------------------------------------------------
/Config/config.xml.template:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | LabProdam Mailer Robot -
7 |
8 |
9 |
10 |
11 |
13 | 2014/11/27
15 |
16 |
17 |
18 |
19 |
20 | Overwrite
21 |
22 |
23 | 20
24 | 3
25 | 1800
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/Config/prodam.xml:
--------------------------------------------------------------------------------
1 |
2 | LabProdam - Atualizações - Prodam - Compras/Jurídico
3 | prodam.log
4 | Overwrite
5 |
6 |
--------------------------------------------------------------------------------
/DiarioTools/Config.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from Log import *
4 | from xml.etree.ElementTree import *
5 | import os
6 | import re
7 | import getopt
8 |
9 | manualTypes = ["Timeout",
10 | "Retries",
11 | "TimeBetweenRetries",
12 | "LogMode",
13 | "To",
14 | "Modules"]
15 |
16 | validConfig = False
17 | def IfValidConfig(func):
18 | def decorated(*args, **kwargs):
19 | if validConfig:
20 | return func(*args, **kwargs)
21 | return decorated
22 |
23 | class Configuration(object):
24 | """ Basic configuration. Read from xml and make available in config instance"""
25 | def __init__(self,configFileName, args, logName = None):
26 | self.logName = logName
27 | self.destination = []
28 | self.modules = []
29 | self._ProcessConfigFile(configFileName)
30 | self._ProcessArgs(args)
31 |
32 | def AppendConfigurationFile(self, configfileName):
33 | self._ProcessConfigFile(configfileName)
34 |
35 | def _ProcessArgs(self, args):
36 | self.startDate = None
37 | self.endDate = None
38 | opts = getopt.getopt(args[1:], "s:e:h")
39 | for opt, value in opts[0]:
40 | if opt == "-s":
41 | date = self._ParseDate(value)
42 | if date is not None:
43 | self.startDate = date
44 | if opt == "-e":
45 | date = self._ParseDate(value)
46 | if date is not None:
47 | self.endDate = date
48 | if opt == "-h":
49 | Log.Log("""Uso: """ + args[0] + """ [-s data -e data]
50 |
51 | Argumentos:
52 | -s Data inicial
53 | -e Data final""")
54 | exit(0)
55 |
56 | if self.startDate == None and self.endDate != None:
57 | Log.Warning("Data final especificada sem data initial")
58 | exit(1)
59 |
60 | if self.startDate != None and self.endDate == None:
61 | Log.Warning("Data inicial especificada sem data final")
62 | exit(1)
63 |
64 | if self.startDate is not None:
65 | self.mode = "local search"
66 | else:
67 | self.mode = "alert mode"
68 |
69 | def _ParseDate(self, date):
70 | retDate = None
71 | dateRe = re.search("(\d{2})/(\d{2})/(\d{4})", date)
72 | if dateRe is not None:
73 | retDate = dateRe.group(3) + "-" + dateRe.group(2) + "-" + dateRe.group(1) + "T00:00:00.000Z"
74 | return retDate
75 |
76 | def _ProcessConfigFile(self, configFileName):
77 | global validConfig
78 | if os.path.exists(configFileName):
79 | try:
80 | tree = parse(configFileName)
81 | types = tree.findall("./*")
82 |
83 | #automatically transform xml elements into attributes (lowering
84 | #first letter ex LogName becomes logName
85 | for configType in types:
86 | if configType.tag not in manualTypes:
87 | configName = configType.tag[0].lower() + configType.tag[1:]
88 | setattr(self, configName, configType.text)
89 |
90 | #manual types
91 | if tree.find("./Timeout") is not None:
92 | self.timeout = float(tree.find("./Timeout").text)
93 | if tree.find("./Retries") is not None:
94 | self.retries = int(tree.find("./Retries").text)
95 | if tree.find("./TimeBetweenRetries") is not None:
96 | self.timeBetweenRetries = float(tree.find("./TimeBetweenRetries").text)
97 | if tree.find("./LogMode") is not None:
98 | self._ProcessCleanLogs(tree.find("./LogMode").text)
99 |
100 | emails = tree.findall("./To/Email")
101 | for email in emails:
102 | self.AddDestination(email.text)
103 |
104 | modules = tree.findall("./Modules/Name")
105 | for module in modules:
106 | self.AddModule(module.text)
107 |
108 | validConfig = True
109 | except Exception as ex:
110 | Log.Warning("Erro de processamento do arquivo de configuração: " + str(ex))
111 | exit(1)
112 | else:
113 | Log.Warning("Arquivo de configuração não encontrado")
114 | exit(1)
115 |
116 | def _ProcessCleanLogs(self, logMode):
117 | if self.logName is not None and re.search("Overwrite", logMode, re.I) is not None:
118 | if os.path.exists(self.logName):
119 | os.remove(self.logName)
120 |
121 | def AddDestination(self, email):
122 | self.destination.append(email)
123 |
124 | def AddModule(self, modulesName):
125 | self.modules.append(modulesName)
126 |
--------------------------------------------------------------------------------
/DiarioTools/GMailer.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LabProdam/LabDiario/76a81e366d0f41f500d86c9d0485c2ca2647c33b/DiarioTools/GMailer.py
--------------------------------------------------------------------------------
/DiarioTools/Log.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 |
4 | import sys
5 | class Log(object):
6 | """Basic logging"""
7 | @staticmethod
8 | def Log(msg):
9 | print msg
10 | @staticmethod
11 | def Warning(msg):
12 | sys.stderr.write("WARNING: " + msg + "\n")
13 |
14 |
--------------------------------------------------------------------------------
/DiarioTools/Parser.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from Log import *
4 | import re
5 | class GenericParser(object):
6 | """ Given a set of regular expressions, evaluate them and convert groups
7 | of interest into array"""
8 | def __init__(self):
9 | self.expressions = []
10 | self.Initialize()
11 |
12 | def Initialize(self):
13 | """Override this method"""
14 | pass
15 |
16 | def AddExpression(self, reExpression, groupsOfInterest, flags = None, count = -1):
17 | self.expressions.append((reExpression, groupsOfInterest, flags, count))
18 |
19 |
20 | def Parse(self, content):
21 | for expression, groupsOfInterest, flags, count in self.expressions:
22 | if flags is not None:
23 | matches = re.finditer(expression, content, flags)
24 | else:
25 | matches = re.finditer(expression, content)
26 |
27 | yieldResult = False
28 | for num, match in enumerate(matches):
29 | if count >= 0 and num >= count:
30 | break
31 | matchGroups = []
32 | for group in groupsOfInterest:
33 | matchGroups.append(match.group(group))
34 | yield matchGroups
35 | yieldResult = True
36 |
37 | if not yieldResult:
38 | #yield empty response for keeping control when group is over
39 | yield []
40 |
41 |
--------------------------------------------------------------------------------
/DiarioTools/Process.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | import json
4 | import pickle
5 | import re
6 | from Log import *
7 | from ProdamMailer import *
8 |
9 | def IsNewer(id, other):
10 | newer = False
11 |
12 | if id is not None:
13 | pId = ProcessId(id)
14 | else:
15 | pId = 0
16 |
17 | if other is not None:
18 | other = ProcessId(other)
19 | else:
20 | other = 0
21 |
22 |
23 | if (pId > other):
24 | newer = True
25 |
26 | return newer
27 |
28 | def ProcessId(id):
29 | idRe = re.search("^(\d{4}).(\d{2}).(\d{2})", id)
30 | if idRe is not None:
31 | id = int(idRe.group(1) + idRe.group(2) + idRe.group(3))
32 | else:
33 | raise Exception("Invalid Id: " + id)
34 | return id
35 |
36 | class LastSearch(object):
37 | """To be persisted indicating where last search terminated"""
38 | def __init__(self):
39 | self.latest = None
40 | self.candidate = None
41 |
42 | def SetCandidate(self, id):
43 | if IsNewer(id, self.candidate):
44 | self.candidate = id
45 |
46 | def SetLatestFromCandidate(self):
47 | if IsNewer(self.candidate, self.latest):
48 | self.latest = self.candidate
49 | self.candidate = None
50 |
51 | def IsNewer(self, id):
52 | return IsNewer(id, self.latest)
53 |
54 | class ResponseProcessor(object):
55 | """Process received response"""
56 | def __init__(self, configInstance, searchObject, parseObject, sessionName):
57 | self.configuration = configInstance
58 | self.searchObject = searchObject
59 | self.parseObject = parseObject
60 | self.sessionName = sessionName
61 | self.lastSearch = None
62 | self.doc = None
63 |
64 | def _ProcessIterate(self):
65 | for val in self.searchObject.Search():
66 | Log.Log("Iterating")
67 |
68 | parsedVal = json.loads(val)
69 | docs = parsedVal["response"]["docs"]
70 | if len(parsedVal["response"]["docs"]) == 0:
71 | break
72 |
73 | for doc in docs:
74 | self.doc = doc
75 | if self.configuration.mode == "local search":#Meaning start/end dates were not passed
76 | for response in self.parseObject.Parse(doc['texto']):
77 | self.Persist(response)
78 | self.Iterate()
79 | elif (self.lastSearch is not None and
80 | self.lastSearch.IsNewer(doc['id']) and
81 | IsNewer(doc['id'], self.configuration.baseDate)):
82 |
83 | self.lastSearch.SetCandidate(doc['id'])
84 | for response in self.parseObject.Parse(doc['texto']):
85 | self.Persist(response)
86 | self.Iterate()
87 | else:
88 | Log.Log("Iteration Over")
89 | return
90 |
91 |
92 | def ProcessEnd(self):
93 | """To be implemented by subs"""
94 | pass
95 |
96 | def Process(self):
97 | pickleFileName = self.sessionName + ".pk"
98 | self._LoadPersistedFile(pickleFileName)
99 |
100 | self._ProcessIterate()
101 | self.lastSearch.SetLatestFromCandidate()
102 | retVal = self.ProcessEnd()
103 |
104 | self._SavePersistedFile(pickleFileName)
105 | return retVal
106 |
107 | def _LoadPersistedFile(self, pickleFileName):
108 | try:
109 | fd = open(pickleFileName)
110 | self.lastSearch = pickle.load(fd)
111 | fd.close()
112 | Log.Log("Reloading Last Session");
113 | except:
114 | self.lastSearch = LastSearch()
115 |
116 | def _SavePersistedFile(self, pickleFileName):
117 | with open(pickleFileName, "w") as fd:
118 | pickle.dump(self.lastSearch, fd)
119 |
120 | def GetDateFromId(self):
121 | idRe = re.search("^(\d{4}).(\d{2}).(\d{2})", self.doc["id"])
122 | if idRe is not None:
123 | dateFromId = idRe.group(3) + "/" + idRe.group(2) + "/" + idRe.group(1)
124 | else:
125 | dateFromId = self.doc["id"]
126 | return dateFromId
127 |
128 | def GetSecretary(self):
129 | return self.doc["secretaria"]
130 |
131 | def GetOrgan(self):
132 | return self.doc["orgao"]
133 |
134 | def GetType(self):
135 | return self.doc["tipo_conteudo"]
136 |
137 | def Persist(self, data):
138 | """To be implemented on child"""
139 | pass
140 |
141 | def Iterate(self):
142 | """To be implemented on child"""
143 | pass
144 |
--------------------------------------------------------------------------------
/DiarioTools/ProdamMailer.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from smtplib import *
4 | from Log import *
5 | from Config import Configuration, IfValidConfig
6 | from email.mime.text import MIMEText
7 | from email.mime.multipart import MIMEMultipart
8 | from email.header import Header
9 |
10 | import re
11 | import socket
12 | class ProxyException(Exception):
13 | def __repr__(self):
14 | return
15 | def __str__(self):
16 | return "Proxy Connection Error"
17 |
18 | class ProxiedSMTP(SMTP, object):
19 | """ Implement proxy layer above SMTP """
20 | def __init__(self, proxy = None, host="", port=0, local_hostname=None, timeout=socket._GLOBAL_DEFAULT_TIMEOUT):
21 | self.ProcessProxy(proxy)
22 | super(ProxiedSMTP, self).__init__(host, port, local_hostname, timeout)
23 |
24 | def ProcessProxy(self, proxy):
25 | self.proxyHost = None
26 | self.proxyPort = 0
27 |
28 | if proxy is not None:
29 | proxyRe = re.search("://([^:]*):(\d*)", proxy)
30 | if proxyRe is not None:
31 | self.proxyHost = socket.gethostbyname(proxyRe.group(1))
32 | self.proxyPort = int(proxyRe.group(2))
33 |
34 | def _get_socket(self, port, host, timeout):
35 | # This makes it simpler for SMTP_SSL to use the SMTP connect code
36 | # and just alter the socket connection bit.
37 | if self.proxyHost is None or len(self.proxyHost) <= 0:
38 | if self.debuglevel > 0:
39 | print>>stderr, 'connect:', (host, port)
40 | return socket.create_connection((port, host), timeout)
41 | else: #If proxy set
42 | # try:
43 | connSock = socket.create_connection((self.proxyHost, self.proxyPort), timeout)
44 | print "Connline: " + "CONNECT %s:%s HTTP/1.1\r\n\r\n" %(port, host)
45 | connSock.sendall("CONNECT %s:%s HTTP/1.1\r\n\r\n" %(port, host))
46 | rcv = connSock.recv(2048)
47 | print rcv
48 | result = re.search("\s(\d+)", rcv)
49 | if result:
50 | code = int(result.group(1))
51 | if code == 200:
52 | print "Connection successfull"
53 | return connSock
54 | else:
55 | raise ProxyException()
56 | # except:
57 | # raise ProxyException()
58 |
59 |
60 | class ProdamMailer(object):
61 | """ Sends e-mail through google server (only if valid configuration is found)"""
62 | def __init__(self, configInstance = None):
63 | if configInstance == None:
64 | Log.Warning("Configuração inválida. Não será possível mandar e-mails")
65 | else:
66 | self.config = configInstance
67 |
68 | @IfValidConfig
69 | def _PrepareMessage(self, messageText):
70 | multipart = MIMEMultipart('alternative')
71 | #multipart["From"] = Header(self.config.frommail.encode("utf-8"), "UTF-8").encode()
72 | #multipart["To"] = Header("; ".join(self.config.destination).encode("utf-8"), "UTF-8").encode()
73 | multipart["Subject"] = Header(self.config.subject.encode("utf-8"), "UTF-8").encode()
74 |
75 | body = self.config.header + "\r\n\r\n"
76 | body += messageText.decode("utf-8")
77 | body += "\r\n\r\n" + self.config.footer
78 | multipart.attach(MIMEText(body.encode("utf-8"), 'plain', 'UTF-8'))
79 | return multipart.as_string()
80 |
81 | @IfValidConfig
82 | def Send(self, messageText):
83 | message = self._PrepareMessage(messageText)
84 | try:
85 | server = ProxiedSMTP(self.config.proxy, self.config.serverAddr, self.config.serverPort)
86 | server.starttls()
87 | server.login(self.config.username, self.config.password)
88 | server.sendmail(self.config.frommail, self.config.destination, message)
89 | server.quit()
90 | Log.Log("E-mail enviado")
91 | except:
92 | Log.Warning("Não foi possível enviar e-mails")
93 |
94 |
--------------------------------------------------------------------------------
/DiarioTools/Retriever.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from Log import *
4 | import urllib
5 | import socket
6 | import re
7 | import time
8 |
9 | class Retriever(object):
10 | """ Retrieves html contents from URL"""
11 | def __init__(self, baseUrl, queryAddr, options, configuration):
12 | self.baseUrl = baseUrl
13 | self.queryAddr = queryAddr
14 | self.options = options
15 |
16 | timeout = configuration.timeout
17 | socket.setdefaulttimeout(timeout)
18 |
19 | self.retries = configuration.retries
20 | self.timeBetweenRetries = configuration.timeBetweenRetries
21 |
22 | proxy = configuration.proxy
23 | if proxy is not None and len(proxy) > 0:
24 | self.proxy = {"http" : proxy}
25 | else:
26 | self.proxy = {}
27 |
28 | def Retrieve(self, retries = None, timeBetweenRetries = None):
29 | """Tries to fetch information from provided url
30 | retries is the number of attempts to acquire contents if timeout occurs
31 | timeBetweenRetries is the number of seconds to wait for the next attempt if a request times out
32 | """
33 | if retries is None:
34 | retries = self.retries
35 | if timeBetweenRetries is None:
36 | timeBetweenRetries = self.timeBetweenRetries
37 |
38 | contents = None
39 | url = self.baseUrl + self.queryAddr + urllib.urlencode(self.options)
40 | Log.Log("Searching: " + url)
41 | sd = None
42 | try:
43 | sd = urllib.urlopen(url, proxies = self.proxy)
44 | contents = sd.read()
45 | except IOError:
46 | retries -= 1
47 | if retries > 0:
48 | Log.Warning("TimedOut. Retrying more " + str(retries) + " times in " + str(timeBetweenRetries) + "s")
49 | time.sleep(timeBetweenRetries)
50 | self.Retrieve(retries)
51 | else:
52 | raise
53 | finally:
54 | if sd:
55 | sd.close()
56 | return contents;
57 |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/DiarioTools/Search.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 |
4 | from DiarioTools.Log import *
5 | from DiarioTools.Retriever import *
6 | from DiarioTools.Parser import *
7 |
8 |
9 | class DlSearch(object):
10 | """Searcher module. Connects to colab and search the passed arguments"""
11 | def __init__(self, configuration, jsonFormat = False):
12 | self.configuration = configuration
13 | self.baseUrl = "http://devcolab.each.usp.br"
14 | self.options = {}
15 | self.query = None;
16 | self.queryAddr = "/do/?"
17 | if jsonFormat:
18 | self.queryAddr = "/do/catalog.json?"
19 |
20 | def SetDateOptions(self):
21 | if self.configuration.mode == "local search":
22 | self.options["date_range"] = "data:[" + self.configuration.startDate + " TO "+ self.configuration.endDate + "]"
23 | self.options["f[data][]"] = "date_range"
24 |
25 | def SetOptions(self):
26 | """ To be implemented by children"""
27 | pass
28 |
29 | def Search(self, query=None):
30 | """Searches accorgind to options set on SetOptions and query
31 | passed as argument or class attribute"""
32 | self.SetDateOptions()
33 | self.SetOptions()
34 | i = 0;
35 | while True:
36 | i += 1
37 | self.options["page"] = i
38 | if query is not None:
39 | self.options["q"] = query
40 | elif self.query is not None:
41 | self.options["q"] = self.query
42 | retriever = Retriever(self.baseUrl, self.queryAddr, self.options, self.configuration)
43 | contents = retriever.Retrieve()
44 | if contents is not None:
45 | yield contents
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/DiarioTools/__init__.py:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LabProdam/LabDiario/76a81e366d0f41f500d86c9d0485c2ca2647c33b/DiarioTools/__init__.py
--------------------------------------------------------------------------------
/DlSanity.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from DiarioTools.Parser import *
4 | from DiarioTools.Process import *
5 | from DiarioTools.Search import *
6 | import re
7 | import datetime
8 |
9 | class ParseSanity(GenericParser):
10 | def Initialize(self):
11 | self.AddExpression(".*", [0], re.I|re.M)
12 |
13 | class SearchSanity(DlSearch):
14 | def SetOptions(self):
15 | self.options["sort"] = u"data desc"
16 | self.options["f[data][]"] = u"date_range"
17 |
18 | #Search if there has been an update in the last 7 days
19 | now = datetime.datetime.now()
20 | delta = datetime.timedelta(days=7)
21 | ref = now - delta
22 | endDate = "%04d-%02d-%02dT00:00:00.000Z" %(now.year, now.month, now.day)
23 | startDate = "%04d-%02d-%02dT00:00:00.000Z" %(ref.year, ref.month, ref.day)
24 | self.options["date_range"] = "data:[" + startDate + " TO "+ endDate + "]"
25 | self.options["f[data][]"] = "date_range"
26 |
27 | class ProcessorSanity(ResponseProcessor):
28 | def __init__(self, configInstance, searchObject, parseObject, fileName, sessionName):
29 | super(ProcessorSanity, self).__init__(configInstance, searchObject, parseObject, sessionName)
30 | self.newData = False
31 |
32 | def _ProcessIterate(self):
33 | for val in self.searchObject.Search():
34 | Log.Log("Iterating")
35 |
36 | parsedVal = json.loads(val)
37 | docs = parsedVal["response"]["docs"]
38 | if len(parsedVal["response"]["docs"]) == 0:
39 | break
40 |
41 | for doc in docs:
42 | self.doc = doc
43 | if self.configuration.mode == "local search":#Meaning start/end dates were not passed
44 | for response in self.parseObject.Parse(doc['texto']):
45 | self.Persist(response)
46 | return
47 |
48 | elif (self.lastSearch is not None and
49 | self.lastSearch.IsNewer(doc['id']) and
50 | IsNewer(doc['id'], self.configuration.baseDate)):
51 |
52 | self.lastSearch.SetCandidate(doc['id'])
53 | for response in self.parseObject.Parse(doc['texto']):
54 | self.Persist(response)
55 | return
56 |
57 | else:
58 | Log.Log("Iteration Over")
59 | return
60 |
61 | def Persist(self, data):
62 | """We just want to know if there is new data"""
63 | self.newData = True
64 |
65 | def ProcessEnd(self):
66 | return self.newData
67 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 2, June 1991
3 |
4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6 | Everyone is permitted to copy and distribute verbatim copies
7 | of this license document, but changing it is not allowed.
8 |
9 | Preamble
10 |
11 | The licenses for most software are designed to take away your
12 | freedom to share and change it. By contrast, the GNU General Public
13 | License is intended to guarantee your freedom to share and change free
14 | software--to make sure the software is free for all its users. This
15 | General Public License applies to most of the Free Software
16 | Foundation's software and to any other program whose authors commit to
17 | using it. (Some other Free Software Foundation software is covered by
18 | the GNU Lesser General Public License instead.) You can apply it to
19 | your programs, too.
20 |
21 | When we speak of free software, we are referring to freedom, not
22 | price. Our General Public Licenses are designed to make sure that you
23 | have the freedom to distribute copies of free software (and charge for
24 | this service if you wish), that you receive source code or can get it
25 | if you want it, that you can change the software or use pieces of it
26 | in new free programs; and that you know you can do these things.
27 |
28 | To protect your rights, we need to make restrictions that forbid
29 | anyone to deny you these rights or to ask you to surrender the rights.
30 | These restrictions translate to certain responsibilities for you if you
31 | distribute copies of the software, or if you modify it.
32 |
33 | For example, if you distribute copies of such a program, whether
34 | gratis or for a fee, you must give the recipients all the rights that
35 | you have. You must make sure that they, too, receive or can get the
36 | source code. And you must show them these terms so they know their
37 | rights.
38 |
39 | We protect your rights with two steps: (1) copyright the software, and
40 | (2) offer you this license which gives you legal permission to copy,
41 | distribute and/or modify the software.
42 |
43 | Also, for each author's protection and ours, we want to make certain
44 | that everyone understands that there is no warranty for this free
45 | software. If the software is modified by someone else and passed on, we
46 | want its recipients to know that what they have is not the original, so
47 | that any problems introduced by others will not reflect on the original
48 | authors' reputations.
49 |
50 | Finally, any free program is threatened constantly by software
51 | patents. We wish to avoid the danger that redistributors of a free
52 | program will individually obtain patent licenses, in effect making the
53 | program proprietary. To prevent this, we have made it clear that any
54 | patent must be licensed for everyone's free use or not licensed at all.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | GNU GENERAL PUBLIC LICENSE
60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61 |
62 | 0. This License applies to any program or other work which contains
63 | a notice placed by the copyright holder saying it may be distributed
64 | under the terms of this General Public License. The "Program", below,
65 | refers to any such program or work, and a "work based on the Program"
66 | means either the Program or any derivative work under copyright law:
67 | that is to say, a work containing the Program or a portion of it,
68 | either verbatim or with modifications and/or translated into another
69 | language. (Hereinafter, translation is included without limitation in
70 | the term "modification".) Each licensee is addressed as "you".
71 |
72 | Activities other than copying, distribution and modification are not
73 | covered by this License; they are outside its scope. The act of
74 | running the Program is not restricted, and the output from the Program
75 | is covered only if its contents constitute a work based on the
76 | Program (independent of having been made by running the Program).
77 | Whether that is true depends on what the Program does.
78 |
79 | 1. You may copy and distribute verbatim copies of the Program's
80 | source code as you receive it, in any medium, provided that you
81 | conspicuously and appropriately publish on each copy an appropriate
82 | copyright notice and disclaimer of warranty; keep intact all the
83 | notices that refer to this License and to the absence of any warranty;
84 | and give any other recipients of the Program a copy of this License
85 | along with the Program.
86 |
87 | You may charge a fee for the physical act of transferring a copy, and
88 | you may at your option offer warranty protection in exchange for a fee.
89 |
90 | 2. You may modify your copy or copies of the Program or any portion
91 | of it, thus forming a work based on the Program, and copy and
92 | distribute such modifications or work under the terms of Section 1
93 | above, provided that you also meet all of these conditions:
94 |
95 | a) You must cause the modified files to carry prominent notices
96 | stating that you changed the files and the date of any change.
97 |
98 | b) You must cause any work that you distribute or publish, that in
99 | whole or in part contains or is derived from the Program or any
100 | part thereof, to be licensed as a whole at no charge to all third
101 | parties under the terms of this License.
102 |
103 | c) If the modified program normally reads commands interactively
104 | when run, you must cause it, when started running for such
105 | interactive use in the most ordinary way, to print or display an
106 | announcement including an appropriate copyright notice and a
107 | notice that there is no warranty (or else, saying that you provide
108 | a warranty) and that users may redistribute the program under
109 | these conditions, and telling the user how to view a copy of this
110 | License. (Exception: if the Program itself is interactive but
111 | does not normally print such an announcement, your work based on
112 | the Program is not required to print an announcement.)
113 |
114 | These requirements apply to the modified work as a whole. If
115 | identifiable sections of that work are not derived from the Program,
116 | and can be reasonably considered independent and separate works in
117 | themselves, then this License, and its terms, do not apply to those
118 | sections when you distribute them as separate works. But when you
119 | distribute the same sections as part of a whole which is a work based
120 | on the Program, the distribution of the whole must be on the terms of
121 | this License, whose permissions for other licensees extend to the
122 | entire whole, and thus to each and every part regardless of who wrote it.
123 |
124 | Thus, it is not the intent of this section to claim rights or contest
125 | your rights to work written entirely by you; rather, the intent is to
126 | exercise the right to control the distribution of derivative or
127 | collective works based on the Program.
128 |
129 | In addition, mere aggregation of another work not based on the Program
130 | with the Program (or with a work based on the Program) on a volume of
131 | a storage or distribution medium does not bring the other work under
132 | the scope of this License.
133 |
134 | 3. You may copy and distribute the Program (or a work based on it,
135 | under Section 2) in object code or executable form under the terms of
136 | Sections 1 and 2 above provided that you also do one of the following:
137 |
138 | a) Accompany it with the complete corresponding machine-readable
139 | source code, which must be distributed under the terms of Sections
140 | 1 and 2 above on a medium customarily used for software interchange; or,
141 |
142 | b) Accompany it with a written offer, valid for at least three
143 | years, to give any third party, for a charge no more than your
144 | cost of physically performing source distribution, a complete
145 | machine-readable copy of the corresponding source code, to be
146 | distributed under the terms of Sections 1 and 2 above on a medium
147 | customarily used for software interchange; or,
148 |
149 | c) Accompany it with the information you received as to the offer
150 | to distribute corresponding source code. (This alternative is
151 | allowed only for noncommercial distribution and only if you
152 | received the program in object code or executable form with such
153 | an offer, in accord with Subsection b above.)
154 |
155 | The source code for a work means the preferred form of the work for
156 | making modifications to it. For an executable work, complete source
157 | code means all the source code for all modules it contains, plus any
158 | associated interface definition files, plus the scripts used to
159 | control compilation and installation of the executable. However, as a
160 | special exception, the source code distributed need not include
161 | anything that is normally distributed (in either source or binary
162 | form) with the major components (compiler, kernel, and so on) of the
163 | operating system on which the executable runs, unless that component
164 | itself accompanies the executable.
165 |
166 | If distribution of executable or object code is made by offering
167 | access to copy from a designated place, then offering equivalent
168 | access to copy the source code from the same place counts as
169 | distribution of the source code, even though third parties are not
170 | compelled to copy the source along with the object code.
171 |
172 | 4. You may not copy, modify, sublicense, or distribute the Program
173 | except as expressly provided under this License. Any attempt
174 | otherwise to copy, modify, sublicense or distribute the Program is
175 | void, and will automatically terminate your rights under this License.
176 | However, parties who have received copies, or rights, from you under
177 | this License will not have their licenses terminated so long as such
178 | parties remain in full compliance.
179 |
180 | 5. You are not required to accept this License, since you have not
181 | signed it. However, nothing else grants you permission to modify or
182 | distribute the Program or its derivative works. These actions are
183 | prohibited by law if you do not accept this License. Therefore, by
184 | modifying or distributing the Program (or any work based on the
185 | Program), you indicate your acceptance of this License to do so, and
186 | all its terms and conditions for copying, distributing or modifying
187 | the Program or works based on it.
188 |
189 | 6. Each time you redistribute the Program (or any work based on the
190 | Program), the recipient automatically receives a license from the
191 | original licensor to copy, distribute or modify the Program subject to
192 | these terms and conditions. You may not impose any further
193 | restrictions on the recipients' exercise of the rights granted herein.
194 | You are not responsible for enforcing compliance by third parties to
195 | this License.
196 |
197 | 7. If, as a consequence of a court judgment or allegation of patent
198 | infringement or for any other reason (not limited to patent issues),
199 | conditions are imposed on you (whether by court order, agreement or
200 | otherwise) that contradict the conditions of this License, they do not
201 | excuse you from the conditions of this License. If you cannot
202 | distribute so as to satisfy simultaneously your obligations under this
203 | License and any other pertinent obligations, then as a consequence you
204 | may not distribute the Program at all. For example, if a patent
205 | license would not permit royalty-free redistribution of the Program by
206 | all those who receive copies directly or indirectly through you, then
207 | the only way you could satisfy both it and this License would be to
208 | refrain entirely from distribution of the Program.
209 |
210 | If any portion of this section is held invalid or unenforceable under
211 | any particular circumstance, the balance of the section is intended to
212 | apply and the section as a whole is intended to apply in other
213 | circumstances.
214 |
215 | It is not the purpose of this section to induce you to infringe any
216 | patents or other property right claims or to contest validity of any
217 | such claims; this section has the sole purpose of protecting the
218 | integrity of the free software distribution system, which is
219 | implemented by public license practices. Many people have made
220 | generous contributions to the wide range of software distributed
221 | through that system in reliance on consistent application of that
222 | system; it is up to the author/donor to decide if he or she is willing
223 | to distribute software through any other system and a licensee cannot
224 | impose that choice.
225 |
226 | This section is intended to make thoroughly clear what is believed to
227 | be a consequence of the rest of this License.
228 |
229 | 8. If the distribution and/or use of the Program is restricted in
230 | certain countries either by patents or by copyrighted interfaces, the
231 | original copyright holder who places the Program under this License
232 | may add an explicit geographical distribution limitation excluding
233 | those countries, so that distribution is permitted only in or among
234 | countries not thus excluded. In such case, this License incorporates
235 | the limitation as if written in the body of this License.
236 |
237 | 9. The Free Software Foundation may publish revised and/or new versions
238 | of the General Public License from time to time. Such new versions will
239 | be similar in spirit to the present version, but may differ in detail to
240 | address new problems or concerns.
241 |
242 | Each version is given a distinguishing version number. If the Program
243 | specifies a version number of this License which applies to it and "any
244 | later version", you have the option of following the terms and conditions
245 | either of that version or of any later version published by the Free
246 | Software Foundation. If the Program does not specify a version number of
247 | this License, you may choose any version ever published by the Free Software
248 | Foundation.
249 |
250 | 10. If you wish to incorporate parts of the Program into other free
251 | programs whose distribution conditions are different, write to the author
252 | to ask for permission. For software which is copyrighted by the Free
253 | Software Foundation, write to the Free Software Foundation; we sometimes
254 | make exceptions for this. Our decision will be guided by the two goals
255 | of preserving the free status of all derivatives of our free software and
256 | of promoting the sharing and reuse of software generally.
257 |
258 | NO WARRANTY
259 |
260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268 | REPAIR OR CORRECTION.
269 |
270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278 | POSSIBILITY OF SUCH DAMAGES.
279 |
280 | END OF TERMS AND CONDITIONS
281 |
282 | How to Apply These Terms to Your New Programs
283 |
284 | If you develop a new program, and you want it to be of the greatest
285 | possible use to the public, the best way to achieve this is to make it
286 | free software which everyone can redistribute and change under these terms.
287 |
288 | To do so, attach the following notices to the program. It is safest
289 | to attach them to the start of each source file to most effectively
290 | convey the exclusion of warranty; and each file should have at least
291 | the "copyright" line and a pointer to where the full notice is found.
292 |
293 | {description}
294 | Copyright (C) {year} {fullname}
295 |
296 | This program is free software; you can redistribute it and/or modify
297 | it under the terms of the GNU General Public License as published by
298 | the Free Software Foundation; either version 2 of the License, or
299 | (at your option) any later version.
300 |
301 | This program is distributed in the hope that it will be useful,
302 | but WITHOUT ANY WARRANTY; without even the implied warranty of
303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304 | GNU General Public License for more details.
305 |
306 | You should have received a copy of the GNU General Public License along
307 | with this program; if not, write to the Free Software Foundation, Inc.,
308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309 |
310 | Also add information on how to contact you by electronic and paper mail.
311 |
312 | If the program is interactive, make it output a short notice like this
313 | when it starts in an interactive mode:
314 |
315 | Gnomovision version 69, Copyright (C) year name of author
316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317 | This is free software, and you are welcome to redistribute it
318 | under certain conditions; type `show c' for details.
319 |
320 | The hypothetical commands `show w' and `show c' should show the appropriate
321 | parts of the General Public License. Of course, the commands you use may
322 | be called something other than `show w' and `show c'; they could even be
323 | mouse-clicks or menu items--whatever suits your program.
324 |
325 | You should also get your employer (if you work as a programmer) or your
326 | school, if any, to sign a "copyright disclaimer" for the program, if
327 | necessary. Here is a sample; alter the names:
328 |
329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330 | `Gnomovision' (which makes passes at compilers) written by James Hacker.
331 |
332 | {signature of Ty Coon}, 1 April 1989
333 | Ty Coon, President of Vice
334 |
335 | This General Public License does not permit incorporating your program into
336 | proprietary programs. If your program is a subroutine library, you may
337 | consider it more useful to permit linking proprietary applications with the
338 | library. If this is what you want to do, use the GNU Lesser General
339 | Public License instead of this License.
340 |
341 |
--------------------------------------------------------------------------------
/Prodam/AdmIndireta.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from Common import *
4 | from DiarioTools.Parser import *
5 | from DiarioTools.Process import *
6 | from DiarioTools.Search import *
7 | import re
8 |
9 | wordsOfInterest = ["Administração Indireta"]
10 | reOfInterest = ["Prodam", "Administra..o Indireta"]
11 |
12 | class ParseAdmIndireta(GenericParser):
13 | def Initialize(self):
14 | self.AddExpression(".+", [0], re.I|re.S)
15 |
16 | class SearchAdmIndireta(DlSearch):
17 | global wordsOfInterest
18 | def SetOptions(self):
19 | self.options["sort"] = u"data desc"
20 |
21 | query = ""
22 | for word in wordsOfInterest:
23 | query += "\"" + word + "\" "
24 | self.query = query
25 |
26 | self.options["f[tipo_conteudo_facet][]"] = "DESPACHO"
27 |
28 | class ProcessorAdmIndireta(ResponseProcessor):
29 | def __init__(self, configInstance, searchObject, parseObject, fileName, sessionName):
30 | super(ProcessorAdmIndireta, self).__init__(configInstance, searchObject, parseObject, sessionName)
31 | self.fileName = fileName
32 | self.data = ""
33 | self.atLeadOneFound = False
34 | self.dlProcessor = DlTagsProcessor(reOfInterest)
35 |
36 | with open(self.fileName, "a") as fd:
37 | fd.write("*** Administração indireta ***\r\n")
38 | self.data += """
39 |
40 |
41 |
42 |
43 |
44 |
92 | """
93 |
94 | def Persist(self, data):
95 | self.atLeadOneFound = True
96 | contents = data[0].encode("utf-8")
97 | with open(self.fileName, "a") as fd:
98 | fd.write(contents)
99 |
100 | self.data += ""
101 | self.data += """
102 |
103 |
[Esconder]
104 |
110 | """ + self.dlProcessor.Process(contents) + """
\n\n"""
111 | self.data += "
"
112 |
113 | def ProcessEnd(self):
114 | if not self.atLeadOneFound:
115 | return None
116 | else:
117 | self.data += ""
118 | return self.data
119 |
120 |
--------------------------------------------------------------------------------
/Prodam/Common.py:
--------------------------------------------------------------------------------
1 | #coding: utf-8
2 | import re
3 |
4 | class Tag(object):
5 | def __init__(self, startExpr, startReplace, endExpr, endReplace):
6 | self.se = "\(\(" + startExpr + "\)\)"
7 | self.sr = startReplace
8 | self.ee = "\(\(" + endExpr + "\)\)"
9 | self.er = endReplace
10 |
11 | def Apply(self, text):
12 | newText = text
13 | expressions = re.findall(self.se + ".*?" + self.ee, newText, re.I| re.S)
14 | for expression in expressions:
15 | newExpression = expression
16 | newExpression = re.sub(self.se, self.sr, newExpression, 1, re.I|re.S)
17 | newExpression = re.sub(self.ee, self.er, newExpression, 1, re.I|re.S)
18 | newText = newText.replace(expression, newExpression)
19 | return newText
20 |
21 | class DlTagsProcessor(object):
22 | def __init__(self, reOfInterest):
23 | bold = Tag("NG", "", "CL", "")
24 | self.reOfInterest = reOfInterest
25 | self.tags = [bold]
26 |
27 | def Process(self, text):
28 | parsedText = text
29 | for tag in self.tags:
30 | parsedText = tag.Apply(parsedText)
31 | parsedText = re.sub("\(\(T.TULO\)\)","", parsedText)
32 | parsedText = re.sub("\(\(TÍTULO\)\)","", parsedText)
33 | parsedText = re.sub("\(\(TEXTO\)\)","", parsedText)
34 | parsedText = re.sub("\(\(NG\)\)","", parsedText)
35 | parsedText = re.sub("\(\(CL\)\)","", parsedText)
36 | parsedText = re.sub("^\s*","", parsedText)
37 | parsedText = re.sub("^(.*)","\\1
", parsedText)
38 |
39 |
40 | for line in parsedText.splitlines():
41 | for word in self.reOfInterest:
42 | if re.search(word, line, re.I) is not None:
43 | expression = re.search("[^\>]*" + word + "[^\<]*", line, re.I).group(0)
44 | parsedText = parsedText.replace(expression, "" + expression + "")
45 | break
46 | return parsedText
47 |
--------------------------------------------------------------------------------
/Prodam/GabineteDoPrefeito.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from Common import *
4 | from DiarioTools.Parser import *
5 | from DiarioTools.Process import *
6 | from DiarioTools.Search import *
7 | import re
8 |
9 | wordsOfInterest = []
10 |
11 | reOfInterest = ["GabPrefeito",
12 | "Pro-dam",
13 | "EMPRESA DE TECNOLOGIA DA INFORMA..O E COMUNICA..O",
14 | "Emp. Tec. da Informa..o e Comunica..o",
15 | "CNPJ 43.076.702/0001-61",
16 | "CNPJ 43076702/0001-61"
17 | ]
18 |
19 | class ParseGabPrefeito(GenericParser):
20 | def Initialize(self):
21 | self.AddExpression(".+", [0], re.I|re.S)
22 |
23 | class SearchGabPrefeito(DlSearch):
24 | global wordsOfInterest
25 |
26 | def __init__(self, organ, *args):
27 | super(SearchGabPrefeito, self).__init__(*args)
28 | self.organ = organ
29 |
30 | def SetOptions(self):
31 | self.options["sort"] = u"data desc"
32 | self.options["f[orgao_facet][]"] = self.organ
33 | self.options["f[secretaria_facet][]"] = u"GABINETE DO PREFEITO"
34 |
35 | query = ""
36 | for word in wordsOfInterest:
37 | query += "\"" + word + "\" "
38 | self.query = query
39 |
40 | class ProcessorGabPrefeitoPartial(ResponseProcessor):
41 | def __init__(self, configInstance, searchObject, parseObject, fileName, sessionName):
42 | super(ProcessorGabPrefeitoPartial, self).__init__(configInstance, searchObject, parseObject, sessionName)
43 | self.fileName = fileName
44 | self.data = ""
45 | self.atLeadOneFound = False
46 | self.dlProcessor = DlTagsProcessor(reOfInterest)
47 |
48 | with open(self.fileName, "a") as fd:
49 | fd.write("*** GabPrefeito (" + sessionName + ") ***\r\n")
50 | self.data += ""
51 |
52 | def Persist(self, data):
53 | self.atLeadOneFound = True
54 | contents = data[0].encode("utf-8")
55 | with open(self.fileName, "a") as fd:
56 | fd.write(contents)
57 |
58 | self.data += ""
59 | self.data += """
60 |
61 |
[Esconder]
62 |
68 | """ + self.dlProcessor.Process(contents) + """
\n\n"""
69 | self.data += "
"
70 |
71 | def ProcessEnd(self):
72 | return self.data
73 |
74 |
75 | class ProcessorGabPrefeito(object):
76 | def __init__(self, configInstance, searchObjects, parseObject, fileName, sessionName):
77 | self.configInstance = configInstance
78 | self.parseObject = parseObject
79 | self.searchObjects = searchObjects
80 | self.fileName = fileName
81 | self.sessionName = sessionName
82 | self.data = ""
83 |
84 | with open(self.fileName, "a") as fd:
85 | fd.write("*** GabPrefeito ***\r\n")
86 | self.header = """
87 |
88 |
89 |
90 |
91 |
92 |
140 | """
141 |
142 | def Process(self):
143 | for searchObject in self.searchObjects:
144 | processor = ProcessorGabPrefeitoPartial(self.configInstance, searchObject, self.parseObject, self.fileName, self.sessionName + "_" + searchObject.organ)
145 | self.data += processor.Process()
146 | if len(self.data) <= 0:
147 | return None
148 | else:
149 | self.data = self.header + self.data + ""
150 | return self.data
151 |
152 |
153 |
--------------------------------------------------------------------------------
/Prodam/Prodam.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from Common import *
4 | from DiarioTools.Parser import *
5 | from DiarioTools.Process import *
6 | from DiarioTools.Search import *
7 | import re
8 |
9 | wordsOfInterest = ["Prodam",
10 | "Pro-dam",
11 | "EMPRESA DE TECNOLOGIA DA INFORMAÇÃO E COMUNICAÇÃO",
12 | "Emp. Tec. da Informação e Comunicação",
13 | "CNPJ 43.076.702/0001-61",
14 | "CNPJ 43076702/0001-61"
15 | ]
16 |
17 | reOfInterest = ["Prodam",
18 | "Pro-dam",
19 | "EMPRESA DE TECNOLOGIA DA INFORMA..O E COMUNICA..O",
20 | "Emp. Tec. da Informa..o e Comunica..o",
21 | "CNPJ 43.076.702/0001-61",
22 | "CNPJ 43076702/0001-61"
23 | ]
24 |
25 | class ParseProdam(GenericParser):
26 | def Initialize(self):
27 | self.AddExpression(".+", [0], re.I|re.S)
28 |
29 | class SearchProdam(DlSearch):
30 | global wordsOfInterest
31 | def SetOptions(self):
32 | self.options["sort"] = u"data desc"
33 |
34 | query = ""
35 | for word in wordsOfInterest:
36 | query += "\"" + word + "\" "
37 | self.query = query
38 |
39 | class ProcessorProdam(ResponseProcessor):
40 | def __init__(self, configInstance, searchObject, parseObject, fileName, sessionName):
41 | super(ProcessorProdam, self).__init__(configInstance, searchObject, parseObject, sessionName)
42 | self.fileName = fileName
43 | self.data = ""
44 | self.atLeadOneFound = False
45 | self.dlProcessor = DlTagsProcessor(reOfInterest)
46 |
47 | with open(self.fileName, "a") as fd:
48 | fd.write("*** Prodam ***\r\n")
49 | self.data += """
50 |
51 |
52 |
53 |
54 |
55 |
103 | """
104 |
105 | def Persist(self, data):
106 | self.atLeadOneFound = True
107 | contents = data[0].encode("utf-8")
108 | with open(self.fileName, "a") as fd:
109 | fd.write(contents)
110 |
111 | self.data += ""
112 | self.data += """
113 |
114 |
[Esconder]
115 |
121 | """ + self.dlProcessor.Process(contents) + """
\n\n"""
122 | self.data += "
"
123 |
124 | def ProcessEnd(self):
125 | if not self.atLeadOneFound:
126 | return None
127 | else:
128 | self.data += ""
129 | return self.data
130 |
131 |
--------------------------------------------------------------------------------
/Prodam/Suspensas.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | #coding: utf-8
3 | from Common import *
4 | from DiarioTools.Parser import *
5 | from DiarioTools.Process import *
6 | from DiarioTools.Search import *
7 | import re
8 |
9 | wordsOfInterest = ["SUSPENSAS DE PARTICIPAÇÃO EM LICITAÇÃO E IMPEDIDAS DE CONTRATAR COM A ADMINISTRAÇÃO"
10 | ]
11 |
12 | reOfInterest = ["Prodam", "SUSPENSAS DE PARTICIPA..O EM LICITA..O E IMPEDIDAS DE CONTRATAR COM A ADMINISTRA..O"
13 | ]
14 |
15 | class ParseSuspensas(GenericParser):
16 | def Initialize(self):
17 | self.AddExpression(".+", [0], re.I|re.S)
18 |
19 | class SearchSuspensas(DlSearch):
20 | global wordsOfInterest
21 | def SetOptions(self):
22 | self.options["sort"] = u"data desc"
23 |
24 | query = ""
25 | for word in wordsOfInterest:
26 | query += "\"" + word + "\" "
27 | self.query = query
28 |
29 | class ProcessorSuspensas(ResponseProcessor):
30 | def __init__(self, configInstance, searchObject, parseObject, fileName, sessionName):
31 | super(ProcessorSuspensas, self).__init__(configInstance, searchObject, parseObject, sessionName)
32 | self.fileName = fileName
33 | self.data = ""
34 | self.atLeadOneFound = False
35 | self.dlProcessor = DlTagsProcessor(reOfInterest)
36 |
37 | with open(self.fileName, "a") as fd:
38 | fd.write("*** Suspensas ***\r\n")
39 | self.data += """
40 |
41 |
42 |
43 |
44 |
45 |
93 | """
94 |
95 | def Persist(self, data):
96 | self.atLeadOneFound = True
97 | contents =data[0].encode("utf-8")
98 | with open(self.fileName, "a") as fd:
99 | fd.write(contents)
100 |
101 | self.data += ""
102 | self.data += """
103 |
104 |
[Esconder]
105 |
111 | """ + self.dlProcessor.Process(contents) + """
\n\n"""
112 | self.data += "
"
113 |
114 | def ProcessEnd(self):
115 | self.data += ""
116 | return self.data
117 |
118 | def ProcessEnd(self):
119 | if not self.atLeadOneFound:
120 | return None
121 | else:
122 | self.data += "