Clean-up: bl_info['tracker_url'] updated to developer.blender.org, some minor other...
[blender-addons-contrib.git] / text_editor_pasteall.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 bl_info = {
20     "name": "PasteAll",
21     "author": "Dalai Felinto (dfelinto)",
22     "version": (0, 7),
23     "blender": (2, 60, 0),
24     "location": "Text editor > Properties panel",
25     "description": "Send your selection or text to www.pasteall.org",
26     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
27         "Scripts/Text_Editor/PasteAll",
28     "tracker_url": "https://developer.blender.org/T23493",
29     "category": "Text Editor"}
30
31 # ########################################################
32 # PasteAll.org Text Sender Script
33 #
34 # Dalai Felinto (dfelinto)
35 # blenderecia.orgfree.com
36 #
37 # Rio de Janeiro - Brasil
38 # Vancouver - Canada
39 #
40 # Original code: 23rd August 2010 (Blender 2.5.3 rev. 31525)
41 #
42 # Important Note:
43 # This script is not official. I did it for fun and for my own usage.
44 # And please do not abuse of their generosity - use it wisely (a.k.a no flood).
45 #
46 # ########################################################
47
48
49 import bpy
50 import urllib
51 import urllib.request
52 import webbrowser
53
54 class TEXT_PT_pasteall(bpy.types.Panel):
55     bl_space_type = 'TEXT_EDITOR'
56     bl_region_type = 'UI'
57     bl_label = "PasteAll.org"
58
59     def draw(self, context):
60         layout = self.layout
61         layout.operator("text.pasteall", icon='URL')
62         layout.prop(context.scene, "use_webbrowser")
63
64 class TEXT_OT_pasteall(bpy.types.Operator):
65     """"""
66     bl_idname = "text.pasteall"
67     bl_label = "PasteAll.org"
68     bl_description = "Send the current text or selection to www.pasteall.org"
69
70     @classmethod
71     def poll(cls, context):
72         if context.area.type != 'TEXT_EDITOR':
73             return False
74         else:
75             return context.space_data.text != None
76
77     def invoke(self, context, event):
78         import webbrowser
79         st = context.space_data
80
81         # get the selected text
82         text = self.get_selected_text(st.text)
83         # if no text is selected send the whole file
84         if text is None: text = st.text.as_string()
85
86         # get the file type based on the extension
87         format = self.get_file_format(st.text)
88
89         # send the text and receive the returned page
90         html = self.send_text(text, format)
91
92         if html is None:
93             self.report({'ERROR'}, "Error in sending the text to the server.")
94             return {'CANCELLED'}
95
96         # get the link of the posted page
97         page = self.get_page(str(html))
98
99         if page is None or page == "":
100             self.report({'ERROR'}, "Error in retrieving the page.")
101             return {'CANCELLED'}
102         else:
103             self.report({'INFO'}, page)
104
105         # store the link in the clipboard
106         bpy.context.window_manager.clipboard = page
107
108         if context.scene.use_webbrowser:
109             try:
110                 webbrowser.open_new_tab(page)
111             except:
112                 self.report({'WARNING'}, "Error in opening the page %s." % (page))
113
114         return {'FINISHED'}
115
116     def send_text(self, text, format):
117         """"""
118         import urllib
119         url = "http://www.pasteall.org/index.php"
120         values = {  'action' : 'savepaste',
121                     'parent_id' : '0',
122                     'language_id': format,
123                     'code' : text }
124
125         try:
126             data = urllib.parse.urlencode(values).encode()
127             req = urllib.request.Request(url, data)
128             response = urllib.request.urlopen(req)
129             page_source = response.read()
130         except:
131             return None
132         else:
133             return page_source
134
135     def get_page(self, html):
136         """"""
137         id = html.find('directlink')
138         id_begin = id + 12 # hardcoded: directlink">
139         id_end = html[id_begin:].find("</a>")
140
141         if id != -1 and id_end != -1:
142             return html[id_begin:id_begin + id_end]
143         else:
144             return None
145
146     def get_selected_text(self, text):
147         """"""
148         current_line = text.current_line
149         select_end_line = text.select_end_line
150
151         current_character = text.current_character
152         select_end_character = text.select_end_character
153
154         # if there is no selected text return None
155         if current_line == select_end_line:
156             if current_character == select_end_character:
157                 return None
158             else:
159                 return current_line.body[min(current_character,select_end_character):max(current_character,select_end_character)]
160
161         text_return = None
162         writing = False
163         normal_order = True # selection from top to bottom
164
165         for line in text.lines:
166             if not writing:
167                 if line == current_line:
168                     text_return = current_line.body[current_character:] + "\n"
169                     writing = True
170                     continue
171                 elif line == select_end_line:
172                     text_return =  select_end_line.body[select_end_character:] + "\n"
173                     writing = True
174                     normal_order = False
175                     continue
176             else:
177                 if normal_order:
178                     if line == select_end_line:
179                         text_return += select_end_line.body[:select_end_character]
180                         break
181                     else:
182                         text_return += line.body + "\n"
183                         continue
184                 else:
185                     if line == current_line:
186                         text_return += current_line.body[:current_character]
187                         break
188                     else:
189                         text_return += line.body + "\n"
190                         continue
191
192         return text_return
193
194     def get_file_format(self, text):
195         """Try to guess what is the format based on the file extension"""
196         extensions =   {'diff':'24',
197                         'patch':'24',
198                         'py':'62',
199                         'c':'12',
200                         'cpp':'18'}
201
202         type = text.name.split(".")[-1]
203         return extensions.get(type, '0')
204
205 def register():
206     bpy.types.Scene.use_webbrowser = bpy.props.BoolProperty(
207         name='Launch Browser',
208         description='Opens the page with the submitted text.',
209         default=True)
210
211     bpy.utils.register_module(__name__)
212
213 def unregister():
214     del bpy.types.Scene.use_webbrowser
215     bpy.utils.unregister_module(__name__)
216
217 if __name__ == "__main__":
218     register()
219
220