add a notesystem to http://dm7025/notes accessable via xml
[enigma2-plugins.git] / webinterface / src / WebChilds / Notepad.py
1 import os
2 from twisted.web2 import resource, responsecode, http,http_headers
3
4 class NotepadResource(resource.Resource):
5     DIR = "/etc/enigma2/notes/"
6     
7     def render(self, req):
8         self.args = req.args
9         
10         self.format = "xml"
11         if self.getArg("format"):
12             if self.getArg("format") == "html":
13                 self.format = "html"
14         
15         if self.getArg("show"):
16             return self.do_show(self.getArg("show"))
17         else:
18             return self.do_index()
19             
20     def send(self,content):
21         if self.format == "xml":
22             ctype = http_headers.MimeType('application', 'xhtml+xml', (('charset', 'UTF-8'),))
23         else:
24             ctype = http_headers.MimeType('text', 'html', (('charset', 'UTF-8'),))
25         return http.Response(responsecode.OK,{'content-type': ctype},content)                
26
27     def do_index(self):
28         content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
29         content += "<e2noteslist>\n"
30         for note in self.getNotes():
31             content += "<e2note>\n"
32             content += "<e2notename>%s</e2notename>\n"%note[0]
33             content += "<e2notesize>%s</e2notesize>\n"%note[1]
34             content += "<e2notemtime>%s</e2notemtime>\n"%note[2]
35             content += "<e2notectime>%s</e2notectime>\n"%note[3]
36             content += "</e2note>\n"
37         content += "</e2noteslist>"
38         return self.send(content)
39
40     def do_show(self,filename):
41         content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
42         if os.path.exists(self.DIR+filename) is not True:
43             content += "<e2note>\n"
44             content += "<e2result>False</e2result>\n"
45             content += "<e2resulttext>File '%s' not found</e2resulttext>\n" % (self.DIR+filename)
46             content += "</e2note>\n"
47         else:    
48             details = self.getNotesDetails(filename)
49             fp = open(self.DIR+filename)
50             con = fp.read()
51             fp.close()
52             content += "<e2notedetails>\n"
53             content += "<e2notename>%s</e2notename>\n"%details[0]
54             content += "<e2notesize>%s</e2notesize>\n"%details[1]
55             content += "<e2notemtime>%s</e2notemtime>\n"%details[2]
56             content += "<e2notectime>%s</e2notectime>\n"%details[3]
57             content += "<e2notecontent>%s</e2notecontent>\n"%con
58             content += "</e2notedetails>"
59         return self.send(content)
60     
61     def getNotes(self):
62         list = []
63         if self.check_dir(force_create=True):
64             for i in os.listdir(self.DIR):
65                 list.append(self.getNotesDetails(i))
66         return list
67
68     def getNotesDetails(self,file):
69         fstat = os.stat(self.DIR+file)
70         size = fstat[-4]
71         mtime = fstat[-3]
72         ctime = fstat[-1]
73         return (file,size,mtime,ctime)
74     
75     def check_dir(self,force_create=False):
76         if os.path.isdir(self.DIR):
77             return True
78         elif os.path.isdir(self.DIR) is False and force_create is True:
79             return os.mkdir(self.DIR)
80         else:
81             return False
82     
83     
84     def getArg(self,key):
85         if self.args.has_key(key):
86             return self.args[key][0]
87         else:
88             return False