RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
File.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 
4 # Copyright 2016 RAPP
5 
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 
10  #http://www.apache.org/licenses/LICENSE-2.0
11 
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 
18 # Authors: Konstantinos Panayiotou
19 # contact: klpanagi@gmail.com
20 
21 
22 ## @file Objects/File.py
23 #
24 # @copyright Rapp Projecty EU 2016
25 # @author Konstantinos Panayiotou, [klpanagi@gmail.com]
26 #
27 
28 from RappCloud.Utils import RandStrGen
29 from os import path
30 
31 
32 class File(object):
33  """ File object class """
34 
35  def __init__(self, filepath="", postfield="", boundary_size=30):
36  """! Constructor
37 
38  @param path str -The filepath
39  @param postField str - The post field name.
40  @param boundary_size int - Size of random boundary string for the
41  filename to add on the post field.
42  """
43 
44  self.__boundarySize = boundary_size
45  if filepath is not "":
46  self.__path = path.expanduser(path.normpath(filepath))
47  else:
48  self.__path = ""
49 
50  if postfield is not "":
51  self.__postfield = postfield
52  else:
53  # Default data post field name.
54  self.__postfield = "file"
55 
56 
57  def __eq__(self, other):
58  """! Equality method """
59  return self.serialize() == other.serialize()
60 
61 
62  @property
63  def path(self):
64  """! file path getter
65 
66  @return string - The file path.
67  """
68  return self.__path
69 
70 
71  @path.setter
72  def path(self, filepath):
73  """! file path setter
74 
75  @param path (String) - The file path.
76  """
77  absPath = path.expanduser(path.normpath(filepath))
78  self.__path = absPath
79 
80 
81  @property
82  def postfield(self):
83  """! Post field name getter
84 
85  @return string - The post field name.
86  """
87  return self.__postfield
88 
89 
90  @postfield.setter
91  def postfield(self, fieldname):
92  """! Post field name setter
93 
94  @param fieldname (String) - The post field name.
95  """
96  self.__postfield = fieldname
97 
98 
99  def serialize(self):
100  return {'path': self.__path, 'field_name': self.__postfield}
101 
102 
103  def make_tuple(self):
104  # Raise Exception if the file does not exist
105  if not path.isfile(self.__path):
106  raise Exception('File not found: {0}'.format(self.__path))
107  randStr = RandStrGen.create(self.__boundarySize)
108  name, ext = path.splitext(path.basename(self.__path))
109  filename = '.'.join((''.join((name, '-', randStr)), ext))
110  return (self.__postfield, (filename, open(self.__path)))
111 
def __eq__
Equality method.
Definition: File.py:57
def __init__
Constructor.
Definition: File.py:35
def path
file path getter
Definition: File.py:63
def postfield
Post field name getter.
Definition: File.py:82