Skip to content
Snippets Groups Projects
Commit b8b5c4fe authored by Bruno Nocera Zanette's avatar Bruno Nocera Zanette
Browse files

Modify Support functions into a Class definition


This commit modifies the support functions for each version (xml and json)
into a class definition. This change was made to simplify the code.
For example, there is no need to pass the database and output file
as arguments anymore.

Also, it was implemented a function to update the indentation every time a
tag is opened or closed, so that there is no need to specify the indentation
every time a write tag functions is called.

Signed-off-by: default avatarBruno Nocera Zanette <brunonzanette@gmail.com>
parent 1422ec36
Branches
No related tags found
No related merge requests found
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of participatorio/opendata
#
# participatorio/opendata is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
import MySQLdb
import codecs
import datetime
import base64
import string_functions as strf
import queries_definition as qry
######################################################################
# Support functions:
#--------------------------------------------------------------------#
def open_json_file (json_filename):
json_file = codecs.open(json_filename,'w',encoding='utf-8')
return json_file
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_open_tag (xml, l, tag_name, sep):
if len(tag_name) > 0:
xml.write(strf.lvl(l)+"\""+tag_name+"\""+":"+sep+"\n")
else:
xml.write(strf.lvl(l)+sep+"\n")
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_close_tag (xml, l, sep, comma_flag):
if comma_flag == True:
xml.write(strf.lvl(l)+sep+","+"\n")
else:
xml.write(strf.lvl(l)+sep+"\n")
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_tag (xml, l, tag_name, info_str, comma):
name="\""+tag_name+"\""
info="\""+strf.substbadc(info_str)+"\""
xml.write(strf.lvl(l)+name+":"+info+comma+"\n")
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_comments (db, xml, post_guid):
post_comments = db.cursor()
post_comments.execute(qry.qry_post_comments, (post_guid,))
write_open_tag(xml,4,"comentarios","[")
row=0
for (user_id, user_name, user_username, string, time)\
in post_comments:
row=row+1
write_open_tag(xml,5,"","{")
prefix='profile/'
user_attr=strf.urlparticipa(prefix,user_username)
write_open_tag(xml,6,"usuario","{")
write_tag(xml,7,"uid",user_attr,",")
write_tag(xml,7,"nome",user_name,"")
write_close_tag(xml,6,"}",True)
write_tag(xml,6,"data",strf.datestr(time),",")
write_tag(xml,6,"mensagem",string,"")
write_close_tag(xml,5,"}",(row < post_comments.rowcount))
write_close_tag(xml,4,"]",False)
post_comments.close()
#--------------------------------------------------------------------#
######################################################################
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of participatorio/opendata
#
# participatorio/opendata is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
import MySQLdb
import codecs
import datetime
import base64
import opendata_string_functions as strf
import opendata_queries_definition as qry
class OpendataJSON:
database = None
indentation = None
level = None
dir_results = None
filename = None
out_file = None
#--------------------------------------------------------------------#
def __init__ (self, database, dir_results, filename):
self.database = database
self.indentation = 0
self.level = strf.lvl(self.indentation)
self.dir_results = dir_results
self.filename = filename
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def update_indentation(self, increment):
self.indentation=self.indentation+(increment)
self.level = strf.lvl(self.indentation)
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def open_file (self):
self.out_file=codecs.open(self.out_filename,'w',encoding='utf-8')
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def close_file (self):
self.out_file.close()
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_open_tag (self, tag_name, sep):
if len(tag_name) > 0:
self.out_file.write(self.level+"\""+tag_name+"\""+":"+sep+"\n")
else:
self.out_file.write(self.level+sep+"\n")
self.update_indentation(+1)
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_close_tag (self, sep, comma_flag):
self.update_indentation(-1)
if comma_flag == True:
self.out_file.write(self.level+sep+","+"\n")
else:
print(self.level+sep+"\n")
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_tag (self, tag_name, info_str, comma):
name="\""+tag_name+"\""
info="\""+strf.substbadc(info_str)+"\""
print(self.level+name+":"+info+comma+"\n")
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_comments (self, post_guid):
post_comments = self.database.cursor()
post_comments.execute(qry.qry_post_comments, (post_guid,))
self.write_open_tag("comentarios","[")
row=0
for (user_id, user_name, user_username, string, time)\
in post_comments:
row=row+1
self.write_open_tag("","{")
prefix='profile/'
user_attr=strf.urlparticipa(prefix,user_username)
self.write_open_tag("usuario","{")
self.write_tag("uid",user_attr,",")
self.write_tag("nome",user_name,"")
self.write_close_tag("}",True)
self.write_tag("data",strf.datestr(time),",")
self.write_tag("mensagem",string,"")
self.write_close_tag("}",(row < post_comments.rowcount))
self.write_close_tag("]",False)
post_comments.close()
#--------------------------------------------------------------------#
File moved
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA. # USA.
import codecs
import datetime import datetime
#--------------------------------------------------------------------# #--------------------------------------------------------------------#
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of participatorio/opendata
#
# participatorio/opendata is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
import MySQLdb
import codecs
import datetime
import opendata_string_functions as strf
import opendata_queries_definition as qry
class OpendataXML:
database = None
indentation = None
level = None
dir_results = None
filename = None
out_file = None
#--------------------------------------------------------------------#
def __init__ (self, database, dir_results, filename):
self.database = database
self.indentation = 0
self.level = strf.lvl(self.indentation)
self.dir_results = dir_results
self.filename = filename
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def update_indentation(self, increment):
self.indentation=self.indentation+(increment)
self.level = strf.lvl(self.indentation)
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def open_file (xml_filename):
self.out_file = codecs.open(xml_filename,'w',encoding='utf-8')
self.out_file.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def close_file (self):
self.out_file.close()
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_open_tag (self, tag_name, attr_str):
self.out_file.write(self.level+"<"+tag_name+attr_str+">"+"\n")
self.update_indentation(+1)
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_close_tag (self, tag_name):
self.update_indentation(-1)
self.out_file.write(self.level+"</"+tag_name+">"+"\n")
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_tag (self, tag_name, info_str, attr_str):
if len(info_str) > 0:
tag_begin=("<"+tag_name+attr_str+">")
tag_end=("</"+tag_name+">")
self.out_file.write(self.level+tag_begin+info_str+tag_end+"\n")
else:
self.out_file.write(self.level+"<"+tag_name+attr_str+"/>"+"\n")
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_comments (self, post_guid):
post_comments = db.cursor()
post_comments.execute(qry.qry_post_comments, (post_guid,))
self.write_open_tag("comentarios",'')
for (user_id, user_name, user_username, string, time)\
in post_comments:
self.write_open_tag("comentario",'')
prefix='profile/'
user_attr=strf.uidstr(strf.urlparticipa(prefix,user_username))
self.write_tag("usuario",user_name,user_attr)
self.write_tag("data",strf.datestr(time),'')
self.write_tag("mensagem",strf.cdata(string),'')
self.write_close_tag("comentario")
self.write_close_tag("comentarios")
post_comments.close()
#--------------------------------------------------------------------#
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2004-2008 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana - C3SL/UFPR
#
# This file is part of participatorio/opendata
#
# participatorio/opendata is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
import MySQLdb
import codecs
import datetime
import string_functions as strf
import queries_definition as qry
######################################################################
# Support functions:
#--------------------------------------------------------------------#
def open_xml_file (xml_filename):
xml_file = codecs.open(xml_filename,'w',encoding='utf-8')
xml_file.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
return xml_file
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_open_tag (xml, l, tag_name, attr_str):
xml.write(strf.lvl(l)+"<"+tag_name+attr_str+">"+"\n")
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_close_tag (xml, l, tag_name):
xml.write(strf.lvl(l)+"</"+tag_name+">"+"\n")
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_tag (xml, l, tag_name, info_str, attr_str):
if len(info_str) > 0:
tag_begin=("<"+tag_name+attr_str+">")
tag_end=("</"+tag_name+">")
xml.write(strf.lvl(l)+tag_begin+info_str+tag_end+"\n")
else:
xml.write(strf.lvl(l)+"<"+tag_name+attr_str+"/>"+"\n")
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
def write_comments (db, xml, post_guid):
post_comments = db.cursor()
post_comments.execute(qry.qry_post_comments, (post_guid,))
write_open_tag(xml,4,"comentarios",'')
for (user_id, user_name, user_username, string, time) in post_comments:
write_open_tag(xml,5,"comentario",'')
prefix='profile/'
user_attr=strf.uidstr(strf.urlparticipa(prefix,user_username))
write_tag(xml,6,"usuario",user_name,user_attr)
write_tag(xml,6,"data",strf.datestr(time),'')
write_tag(xml,6,"mensagem",strf.cdata(string),'')
write_close_tag(xml,5,"comentario")
write_close_tag(xml,4,"comentarios")
post_comments.close()
#--------------------------------------------------------------------#
######################################################################
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment