utils.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. '''
  2. Botadero, una aplicacion para compartir archivos libremente.
  3. Copyright (C) 2016 Rodrigo Garcia <strysg@riseup.net>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU Affero General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. '''
  15. from botadero.Estadisticas_Archivos import *
  16. from botadero.datos_archivo import *
  17. import random
  18. EstadisticaArchivos = EstadisticaArchivos('parametros.txt', False)
  19. ParametrosServer = EstadisticaArchivos.Parametros
  20. # Develve el nombre de un esquema de colores al azar
  21. def esquema_colores_random():
  22. '''
  23. devuelve un esquema de colores random de los definidos en static/
  24. '''
  25. esquemas = ('gris1', 'neutral','verde1','azul1','amarillo1', 'rojo1','cafe1')
  26. return esquemas[random.randint(0, len(esquemas)-1)]
  27. def ls_archivos(categoria=""):
  28. '''
  29. Devuelve una lista con nombre_archivo, tamanyo y dias_restantes
  30. para eliminacion del directorio de subidas.
  31. '''
  32. l_archivos = []
  33. upload_folder = ParametrosServer.UploadFolder
  34. pila_archivos = EstadisticaArchivos.PilaArchivos
  35. nombres = []
  36. for ra in pila_archivos:
  37. if ra.categoria == categoria:
  38. nombres.append(ra.Nombre)
  39. # coloca cada archivo en la pantalla
  40. for arch in nombres:
  41. # TODO: controlar excepcion
  42. size_long = EstadisticaArchivos.GetDatosArchivo(arch).Tam
  43. unidades = "B"
  44. if size_long > 1000 and size_long < 1000000:
  45. tam = round(size_long/float(1000), 2)
  46. unidades = "KB"
  47. elif size_long > 1000000 and size_long < 1000000000:
  48. tam = round(size_long/float(1000000), 2)
  49. unidades = "MB"
  50. elif size_long > 1000000000:
  51. tam = round(size_long/float(1000000000), 2)
  52. unidades = "GB"
  53. else:
  54. tam = float(size_long)
  55. # lista a devolver
  56. l_archivos.append([upload_folder, arch, str(tam)+" "+unidades, \
  57. str(EstadisticaArchivos.GetDatosArchivo(arch).DiasRestantes)])
  58. return l_archivos
  59. def categorias():
  60. '''
  61. Devuelve la lista categorias (carpetas) dentro el directorio almacen/
  62. No realiza recursion solo devuelve carpetas en el nivel 1
  63. '''
  64. upload_folder = ParametrosServer.UploadFolder
  65. pathf = os.path.abspath(upload_folder)
  66. #print "[DIRS] - abs path: %s" %pathf
  67. categorias = []
  68. ow = os.walk(pathf) # apuntando a /alamacen
  69. # por el momento solo se hace la comprobacion de un nivel
  70. directorios = ow.next()[1]
  71. for d in directorios:
  72. categorias.append(d)
  73. print "[DIRS] - folders found: %s" %str(directorios)
  74. return categorias
  75. #print "[DIRS] - List of folders: %s" %filter(os.path.isdir, os.listdir(pathf))
  76. #return filter(os.path.isdir, os.listdir(pathf))
  77. def num_archivos_por_categoria(categoria):
  78. '''Devuelve el numero de archivos segun la categoria
  79. dada'''
  80. num = 0
  81. for da in EstadisticaArchivos.PilaArchivos:
  82. if da.categoria == categoria:
  83. num += 1
  84. return num
  85. def categorias_y_nums_archivos():
  86. '''Devuelve una lista con (categria, num_archivos)
  87. de cada categoria de archivos'''
  88. cats = []
  89. cats.append("") # para el directorio principal
  90. cats = cats + categorias()
  91. cat_y_nums = []
  92. for cat in cats:
  93. cat_y_nums.append((cat, num_archivos_por_categoria(cat)))
  94. return cat_y_nums