diff options
author | Camil Staps | 2016-03-02 21:37:50 +0100 |
---|---|---|
committer | Camil Staps | 2016-03-02 21:39:52 +0100 |
commit | 436f26b4eb1b38089396374876908fdb06d3c015 (patch) | |
tree | d599191368ff027e98a704051dfa04a20d6645d1 /project1/proj1_s4498062/webhttp/resource.py | |
parent | Assignment 2 (diff) |
Added framework project 1
Diffstat (limited to 'project1/proj1_s4498062/webhttp/resource.py')
-rw-r--r-- | project1/proj1_s4498062/webhttp/resource.py | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/project1/proj1_s4498062/webhttp/resource.py b/project1/proj1_s4498062/webhttp/resource.py new file mode 100644 index 0000000..dc20067 --- /dev/null +++ b/project1/proj1_s4498062/webhttp/resource.py @@ -0,0 +1,86 @@ +"""Resources
+
+This module contains a handler class for resources.
+"""
+
+import os
+import mimetype
+import urlparse
+
+
+class FileExistError(Exception):
+ """Exception which is raised when file does not exist"""
+ pass
+
+
+class FileAccessError(Exception):
+ """Exception which is raised when file exists, but cannot be accessed"""
+ pass
+
+
+class Resource:
+ """Class for representing a Resource (file)"""
+
+ def __init__(self, uri):
+ """Initialize the resource"
+
+ Raises:
+ FileExistError: if resource does not exist
+ FileAccessError: if resource exists, but cannot be accessed
+
+ Args:
+ uri (str): Uniform Resource Identifier
+ """
+ self.uri = uri
+ out = urlparse.urlparse(uri)
+ self.path = os.path.join("content", out.path.lstrip("/"))
+ if os.path.isdir(self.path):
+ self.path = os.path.join(self.path, "index.html")
+ if not os.path.isfile(self.path):
+ raise FileExistError
+ if not os.access(self.path, os.R_OK):
+ raise FileAccessError
+
+ def generate_etag(self):
+ """Generate the ETag for the resource
+
+ Returns:
+ str: ETag for the resource
+ """
+ stat = os.stat(self.path)
+ etag = ""
+ return etag
+
+ def get_content(self):
+ """Get the contents of the resource
+
+ Returns:
+ str: Contents of the resource
+ """
+ return open(self.path).read()
+
+ def get_content_type(self):
+ """Get the content type, i.e "text/html"
+
+ Returns:
+ str: type of content in the resource
+ """
+ mimetype = mimetypes.guess_type(self.path)
+ return mimetype[0]
+
+ def get_content_encoding(self):
+ """Get the content encoding, i.e "gzip"
+
+ Returns:
+ str: encoding used for the resource
+ """
+ mimetype = mimetypes.guess_type(self.path)
+ return mimetype[1]
+
+ def get_content_length(self):
+ """Get the length of the resource
+
+ Returns:
+ int: length of resource in bytes
+ """
+ return os.path.getsize(self.path)
|