Advertisement
Guest User

Untitled

a guest
May 24th, 2015
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.27 KB | None | 0 0
  1. #!/usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. import csv
  4. import sys
  5. import os
  6. __author__="great"
  7.  
  8. def mime_type_linux(filepath):
  9.     buf = "file -b --mime-type " + filepath
  10.     line = os.popen(buf,"r")
  11.     while True:
  12.         line_str = line.readline()
  13.         if not line_str:
  14.             break
  15.         print(line_str,end="")
  16.  
  17.  
  18. def mime_type(filepath):
  19.     try:
  20.         file = open(filepath,mode="rb")
  21.         analyze_binary_type(file)
  22.         analyze_text_type(file,filepath)
  23.         file.close()
  24.     except IOError:
  25.         print("Отказано в доступе: " + filepath)
  26.  
  27. def analyze_binary_type(file):
  28.     is_png(file)
  29.     is_jpeg(file)
  30.     is_gif(file)
  31.     is_pdf(file)
  32.     is_zip(file)
  33.  
  34.  
  35. def analyze_text_type(file,filepath):
  36.     flag = True
  37.     flag=is_html(file,flag)
  38.     if flag==True:
  39.         flag=is_xml(file,flag)
  40.     if flag==True:
  41.         flag=is_csv(filepath,flag)
  42.     if flag==True:
  43.         is_txt(filepath)
  44.  
  45. def is_png(file):
  46.     str = file.read(8)
  47.     file.seek(0)
  48.     if int.from_bytes(str,byteorder="big") == 0x89504E470D0A1A0A:
  49.         print("image/png")
  50.     else:
  51.         pass
  52.  
  53. def is_jpeg(file):
  54.     str = file.read(4)
  55.     file.seek(0)
  56.     if int.from_bytes(str,byteorder="big") == 0xFFD8FFE0:
  57.         print("image/jpeg")
  58.     else:
  59.         pass
  60.  
  61. def is_gif(file):
  62.     str = file.read(6)
  63.     file.seek(0)
  64.     if int.from_bytes(str,byteorder="big") == 0x474946383761 or int.from_bytes(str,byteorder="big") == 0x474946383961:
  65.         print("image/gif")
  66.     else:
  67.         pass
  68.  
  69. def is_pdf(file):
  70.     str = file.read(4)
  71.     file.seek(0)
  72.     if int.from_bytes(str,byteorder="big") == 0x25504446:
  73.         print("application/pdf")
  74.     else:
  75.         pass
  76.  
  77. def is_zip(file):
  78.     str = file.read(4)
  79.     file.seek(0)
  80.     if int.from_bytes(str,byteorder="big") == 0x504B0304:
  81.         print("application/zip")
  82.     else:
  83.         pass
  84.  
  85. def is_txt(filepath):
  86.     try:
  87.         file = open(filepath,"rb+")
  88.         file.readline()
  89.         file.close()
  90.         file_buf = open(filepath,"r+")
  91.         file_buf.readline()
  92.         file_buf.close()
  93.         print("text/plain")
  94.     except:
  95.         pass
  96.  
  97. def is_html(file,flag):
  98.     file_str = b""
  99.     i=0
  100.     try:
  101.         for x in file.readlines():
  102.             if i == 4: # may be various number
  103.                 break;
  104.             file_str += x
  105.             i+=1
  106.         file.seek(0)
  107.         if file_str.find(b"<!DOCTYPE HTML>") != -1 or file_str.find(b"<html>") != -1:
  108.             print("text/html")
  109.             flag=False
  110.         return flag
  111.     except:
  112.         return flag
  113.  
  114.  
  115. def is_xml(file,flag):
  116.     try:
  117.         file_str=file.read(5)
  118.         file.seek(0)
  119.         if file_str.find(b"<?xml") != -1:
  120.             print("text/xml")
  121.             flag=False
  122.         return flag
  123.     except:
  124.         return flag
  125.  
  126. def is_csv(filepath,flags):
  127.     try:
  128.         file = open(filepath)
  129.         csv_iter = csv.reader(file)
  130.         flag=True
  131.         for line in csv_iter:
  132.             if type(line)==list and len(line)>1:
  133.                 break
  134.             else:
  135.                 flag = False
  136.                 pass
  137.         file.close()
  138.         if flag == True:
  139.             print ("text/csv")
  140.             flags=False
  141.         return flags
  142.     except:
  143.         return flag
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement