utils.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #coding: utf-8
  2. #修正输入的文件路径,输入路径的标准格式:abc,不需要前后置的路径符号
  3. def FixFilePath(OutputPath,instance=None):
  4. if callable(OutputPath):
  5. try:
  6. OutputPath=OutputPath(instance)
  7. except:
  8. OutputPath=""
  9. else:
  10. try:
  11. import datetime
  12. OutputPath=datetime.datetime.now().strftime(OutputPath)
  13. except:
  14. pass
  15. if len(OutputPath)>0:
  16. OutputPath="%s/" % OutputPath.strip("/")
  17. return OutputPath
  18. #在上传的文件名后面追加一个日期时间+随机,如abc.jpg--> abc_20120801202409.jpg
  19. def GenerateRndFilename(filename):
  20. import datetime
  21. import random
  22. from os.path import splitext
  23. f_name,f_ext=splitext(filename)
  24. return "%s_%s%s%s" % (f_name, datetime.datetime.now().strftime("%Y%m%d_%H%M%S_"),random.randrange(10,99),f_ext)
  25. #文件大小类
  26. class FileSize():
  27. SIZE_UNIT={"Byte":1,"KB":1024,"MB":1048576,"GB":1073741824,"TB":1099511627776L}
  28. def __init__(self,size):
  29. self.size=long(FileSize.Format(size))
  30. @staticmethod
  31. def Format(size):
  32. import re
  33. if isinstance(size,int) or isinstance(size,long):
  34. return size
  35. else:
  36. if not isinstance(size,str):
  37. return 0
  38. else:
  39. oSize=size.lstrip().upper().replace(" ","")
  40. pattern=re.compile(r"(\d*\.?(?=\d)\d*)(byte|kb|mb|gb|tb)",re.I)
  41. match=pattern.match(oSize)
  42. if match:
  43. m_size, m_unit=match.groups()
  44. if m_size.find(".")==-1:
  45. m_size=long(m_size)
  46. else:
  47. m_size=float(m_size)
  48. if m_unit!="BYTE":
  49. return m_size*FileSize.SIZE_UNIT[m_unit]
  50. else:
  51. return m_size
  52. else:
  53. return 0
  54. #返回字节为单位的值
  55. @property
  56. def size(self):
  57. return self.size
  58. @size.setter
  59. def size(self,newsize):
  60. try:
  61. self.size=long(newsize)
  62. except:
  63. self.size=0
  64. #返回带单位的自动值
  65. @property
  66. def FriendValue(self):
  67. if self.size<FileSize.SIZE_UNIT["KB"]:
  68. unit="Byte"
  69. elif self.size<FileSize.SIZE_UNIT["MB"]:
  70. unit="KB"
  71. elif self.size<FileSize.SIZE_UNIT["GB"]:
  72. unit="MB"
  73. elif self.size<FileSize.SIZE_UNIT["TB"]:
  74. unit="GB"
  75. else:
  76. unit="TB"
  77. if (self.size % FileSize.SIZE_UNIT[unit])==0:
  78. return "%s%s" % ((self.size / FileSize.SIZE_UNIT[unit]),unit)
  79. else:
  80. return "%0.2f%s" % (round(float(self.size) /float(FileSize.SIZE_UNIT[unit]) ,2),unit)
  81. def __str__(self):
  82. return self.FriendValue
  83. #相加
  84. def __add__(self, other):
  85. if isinstance(other,FileSize):
  86. return FileSize(other.size+self.size)
  87. else:
  88. return FileSize(FileSize(other).size+self.size)
  89. def __sub__(self, other):
  90. if isinstance(other,FileSize):
  91. return FileSize(self.size-other.size)
  92. else:
  93. return FileSize(self.size-FileSize(other).size)
  94. def __gt__(self, other):
  95. if isinstance(other,FileSize):
  96. if self.size>other.size:
  97. return True
  98. else:
  99. return False
  100. else:
  101. if self.size>FileSize(other).size:
  102. return True
  103. else:
  104. return False
  105. def __lt__(self, other):
  106. if isinstance(other,FileSize):
  107. if other.size>self.size:
  108. return True
  109. else:
  110. return False
  111. else:
  112. if FileSize(other).size > self.size:
  113. return True
  114. else:
  115. return False
  116. def __ge__(self, other):
  117. if isinstance(other,FileSize):
  118. if self.size>=other.size:
  119. return True
  120. else:
  121. return False
  122. else:
  123. if self.size>=FileSize(other).size:
  124. return True
  125. else:
  126. return False
  127. def __le__(self, other):
  128. if isinstance(other,FileSize):
  129. if other.size>=self.size:
  130. return True
  131. else:
  132. return False
  133. else:
  134. if FileSize(other).size >= self.size:
  135. return True
  136. else:
  137. return False
  138. def MadeUeditorOptions(width=600,height=300,plugins=(),toolbars="normal",filePath="",imagePath="",scrawlPath="",imageManagerPath="",css="",options={}):
  139. import settings as USettings
  140. uOptions={}
  141. uOptions['css']=css
  142. if imagePath=="":imagePath=USettings.UEditorSettings["images_upload"].get("path","")
  143. if filePath=="":filePath=USettings.UEditorSettings["files_upload"].get("path","")
  144. if imageManagerPath=="":imageManagerPath=USettings.UEditorSettings["image_manager"].get("path","")
  145. if scrawlPath=="":scrawlPath=USettings.UEditorSettings["scrawl_upload"].get("path","")
  146. uOptions['imagePath']=FixFilePath(imagePath)
  147. uOptions['filePath']=FixFilePath(filePath)
  148. if imageManagerPath=="":
  149. uOptions['imageManagerPath']=uOptions['imagePath']
  150. else:
  151. uOptions['imageManagerPath']=FixFilePath(imageManagerPath)
  152. if scrawlPath=="":
  153. uOptions['scrawlPath']=uOptions['imagePath']
  154. else:
  155. uOptions['scrawlPath']=FixFilePath(scrawlPath)
  156. uOptions['O_imagePath']=imagePath
  157. uOptions['O_filePath']=filePath
  158. uOptions['O_imageManagerPath']=imageManagerPath
  159. uOptions['O_scrawlPath']=scrawlPath
  160. uOptions['plugins']=plugins
  161. uOptions['toolbars']=toolbars
  162. uOptions['options']=options
  163. uOptions['width']=width
  164. uOptions['height']=height
  165. return uOptions