[cpif] r242 - in trunk: backend frontend-web frontend-web/smileys

svn at argo.es svn at argo.es
Wed Jul 4 13:56:59 CEST 2007


Author: jcea
Date: Wed Jul  4 13:56:57 2007
New Revision: 242

Log:
Primeros pasos para el soporte de smileys

Added:
   trunk/frontend-web/load_smileys.py
      - copied, changed from r241, /trunk/frontend-web/url_stop.py
   trunk/frontend-web/smileys/
   trunk/frontend-web/smileys/buey_smiley.gif   (contents, props changed)
   trunk/frontend-web/smileys/cabezazo1.gif   (contents, props changed)
   trunk/frontend-web/smileys/cabezazo2.gif   (contents, props changed)
   trunk/frontend-web/smileys/smile.gif   (contents, props changed)
   trunk/frontend-web/smileys/yuhu.gif   (contents, props changed)
Modified:
   trunk/backend/database.py
   trunk/backend/upgrade.py

Modified: trunk/backend/database.py
==============================================================================
--- trunk/backend/database.py	(original)
+++ trunk/backend/database.py	Wed Jul  4 13:56:57 2007
@@ -1,7 +1,7 @@
 # $Id$
 
 
-VERSION_DB="2007070401"
+VERSION_DB="2007070402"
 
 from globales import thread_len
 
@@ -29,6 +29,7 @@
 
     from durus.btree import BTree
     from durus.persistent_dict import PersistentDict
+    from durus.persistent_list import PersistentList
 
     usuarios=PersistentDict()
     usuarios["usuarios"]=BTree()
@@ -59,6 +60,8 @@
 
     root["static"]=PersistentDict()
     root["static"]["avatars"]=BTree()
+    root["static"]["smileys"]=BTree()
+    root["smileys"]=PersistentList()
 
   init()
 
@@ -103,37 +106,73 @@
   if metadata : metadata=metadata[0]
   return metadata
 
-def save_static(conn,section,data,metadata,allow_duplicates) :
+def save_static(conn,section,data,metadata,update=False) :
   from durus.persistent_list import PersistentList
   root=conn.get_root()
   import md5
   hash=md5.new(data).hexdigest()
   d=root["static"][section]
-  if (not allow_duplicates) and (hash in d) : return None
-  d[hash]=(metadata,PersistentList([data]))
+  if (hash not in d) or update : 
+    d[hash]=(metadata,PersistentList([data]))
   return hash
 
-def avatar_update(conn,nick,avatar) :
-  avatar_metadata={}
-  if len(avatar)>125*1024 :
-    return "El avatar mide m´s de 125Kbytes"
+def check_image(img) :
+  metadata={}
   from PIL import Image
   from cStringIO import StringIO
-  f=StringIO(avatar)
+  f=StringIO(img)
   try :
     im=Image.open(f)
-    avatar_metadata["size"]=im.size
-    if im.size[0]>150 or im.size[1]>150 :
-      return "Solo admitimos imágenes de un máximo de 150*150 pixels"
+    metadata["size"]=im.size
     format=im.format
     if format not in ("PNG","JPEG","GIF") :
-      return "Solo admitimos imágenes en formato PNG, JPEG o GIF"
-    avatar_metadata["content-type"]="image/%s" %format.lower()
+      return ("Solo admitimos imágenes en formato PNG, JPEG o GIF",None)
+    metadata["content-type"]="image/%s" %format.lower()
     im.verify()
   except :
-    return "El fichero parece corrupto o no se corresponde a una imagen"
+    return ("El fichero parece corrupto o no se corresponde a una imagen",None)
+
+  return (None,metadata)
+
+def smileys_update(conn,smileys,directory) :
+  import os.path
+  root=conn.get_root()
+
+  files={}
+  for i in smileys :
+    files[i[1]]=None # Eliminate duplicates
+
+  for i in files.keys() :
+    img=open(os.path.join(directory,i)).read()
+    result,img_metadata=check_image(img)
+    if result : raise RuntimeError("ERROR al cargar los iconos:",result)
+    files[i]=save_static(conn,"smileys",img,img_metadata,update=False)
+
+  from durus.persistent_list import PersistentList
+  if len(set(smileys))!=len(smileys) : raise RuntimeError("ERROR al cargar los iconos: duplicados")
+  smiley_list=PersistentList()
+  smileys.sort(cmp=lambda x,y: cmp((len(y[0]),y),(len(x[0]),x))) # Longest prefixes first
+  for i in smileys :
+    smiley_list.append((i[0],files[i[1]]))
+
+  root["smileys"]=smiley_list
+
+# The list given is "longest prefixes first"
+def get_smiley_list(conn) :
+  root=conn.get_root()
+  return list(root["smileys"])
+
+def avatar_update(conn,nick,avatar) :
+  if len(avatar)>125*1024 :
+    return "El avatar mide m´s de 125Kbytes"
+  result,avatar_metadata=check_image(avatar)
+  if result : return result
+
+  x,y=avatar_metadata["size"]
+  if x>150 or y>150 :
+    return "Solo admitimos imágenes de un máximo de 150*150 pixels"
 
-  name=save_static(conn,"avatars",avatar,avatar_metadata,allow_duplicates=True)
+  name=save_static(conn,"avatars",avatar,avatar_metadata,update=False)
   assert name
 
   root=conn.get_root()

Modified: trunk/backend/upgrade.py
==============================================================================
--- trunk/backend/upgrade.py	(original)
+++ trunk/backend/upgrade.py	Wed Jul  4 13:56:57 2007
@@ -4,6 +4,7 @@
   from durus.btree import BTree
   from durus.persistent_dict import PersistentDict
   from durus.persistent_set import PersistentSet
+  from durus.persistent_list import PersistentList
 
   root=conn.get_root()
 
@@ -129,5 +130,12 @@
     root["version del foro"]="2007070401"
     for metatag in root["metatags"]["metatags"].values() :
       metatag["moderadores"]=[]
+    conn.commit()
+
+  if root["version del foro"]=="2007070401" :
+    print "Actualizando la base de datos: 2007070401 -> 2007070402"
+    root["version del foro"]="2007070402"
+    root["static"]["smileys"]=BTree()
+    root["smileys"]=PersistentList()
     conn.commit()
 

Copied: trunk/frontend-web/load_smileys.py (from r241, /trunk/frontend-web/url_stop.py)
==============================================================================
--- /trunk/frontend-web/url_stop.py	(original)
+++ trunk/frontend-web/load_smileys.py	Wed Jul  4 13:56:57 2007
@@ -1,18 +1,31 @@
 # $Id$
 
-from globales import monitor
+def main() :
+  smileys=[
+    (":buey:","buey_smiley.gif"),
+    (":cabezazo:","cabezazo2.gif"),
+    (":yuhu:","yuhu.gif"),
+    (":-)","smile.gif"),
+  ]
 
-def gestiona_url(handler,path,usuario) :
-  if len(path)!=1 : return None
+  import os.path,sys
+  directorio_padre=os.path.split(os.getcwd())[0]  # Directorio padre
+  sys.path.append(os.path.join(directorio_padre,"backend"))
 
-  import skins
-  pagina = skins.Skin(path,usuario)
-  pagina.load_dict({"page_title": "cpif - parando el servicio"})
-
-  if usuario!="master" :
-    pagina.load_dict({"generic_message": "<h2>Solo el usuario 'master' puede parar el servicio</h2>"})
-  else:
-    handler.must_stop=True
-    pagina.load_dict({"generic_message": "<h2>PARAMOS EL SERVICIO!!</h2>"})
+  import storage
+  dummy,dummy,monitor=storage.storage_and_monitor(os.path.join(os.path.pardir,"backend","db"))
+
+  import database
+  database.init_database(monitor)
+
+  @monitor
+  def load(conn,smileys) :
+    database.smileys_update(conn,smileys,"smileys")
+    
+  load(smileys)
+  print "Hemos cargado %d smileys" %len(smileys)
+
+
+if __name__=="__main__" :
+  main()
 
-  return(pagina.web())

Added: trunk/frontend-web/smileys/buey_smiley.gif
==============================================================================
Binary file. No diff available.

Added: trunk/frontend-web/smileys/cabezazo1.gif
==============================================================================
Binary file. No diff available.

Added: trunk/frontend-web/smileys/cabezazo2.gif
==============================================================================
Binary file. No diff available.

Added: trunk/frontend-web/smileys/smile.gif
==============================================================================
Binary file. No diff available.

Added: trunk/frontend-web/smileys/yuhu.gif
==============================================================================
Binary file. No diff available.



More information about the cpif mailing list