addons-contrib: more view_layer syntax updates
[blender-addons-contrib.git] / text_editor_hastebin.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": "hastebin",
21     "author": "Dalai Felinto (dfelinto)",
22     "version": (0, 8),
23     "blender": (2, 78, 0),
24     "location": "Text editor > Properties panel",
25     "description": "Send your selection or text to hastebin.com",
26     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/"
27         "Scripts/Text_Editor/hastebin",
28     "tracker_url": "https://developer.blender.org/maniphest/task/edit/form/2/",
29     "category": "Text Editor"}
30
31
32 import bpy
33
34 class TEXT_PT_hastebin(bpy.types.Panel):
35     bl_space_type = 'TEXT_EDITOR'
36     bl_region_type = 'UI'
37     bl_label = "hastebin.com"
38
39     def draw(self, context):
40         layout = self.layout
41         layout.operator("text.hastebin", icon='URL')
42         layout.prop(context.scene, "use_webbrowser")
43
44
45 class TEXT_OT_hastebin(bpy.types.Operator):
46     """"""
47     bl_idname = "text.hastebin"
48     bl_label = "hastebin.com"
49     bl_description = "Send the current text or selection to http://hastebin.com"
50
51     @classmethod
52     def poll(cls, context):
53         if context.area.type != 'TEXT_EDITOR':
54             return False
55         else:
56             return context.space_data.text != None
57
58     def invoke(self, context, event):
59         import webbrowser
60         st = context.space_data
61
62         # get the selected text
63         text = self.get_selected_text(st.text)
64         # if no text is selected send the whole file
65         if text is None: text = st.text.as_string()
66
67         # send the text and receive the returned page
68         page = self.send_text(text)
69
70         if page is None:
71             return {'CANCELLED'}
72
73         # store the link in the clipboard
74         bpy.context.window_manager.clipboard = page
75
76         if context.scene.use_webbrowser:
77             try:
78                 webbrowser.open_new_tab(page)
79             except:
80                 self.report({'WARNING'}, "Error in opening the page %s." % (page))
81
82         return {'FINISHED'}
83
84     def send_text(self, text):
85         """"""
86         import requests
87         base_url = "https://hastebin.com"
88
89         try:
90             response = requests.post(base_url + "/documents", text)
91             final_url = "%s/%s" % (base_url, response.json()['key'])
92
93         except requests.exceptions.SSLError:
94             self.report({'ERROR'}, "Error with SSL authorization, requires a more recent Blender")
95             return None
96
97         except:
98             self.report({'ERROR'}, "Error in sending the text to the server.")
99             return None
100
101         else:
102             return final_url
103
104     def get_selected_text(self, text):
105         """"""
106         current_line = text.current_line
107         select_end_line = text.select_end_line
108
109         current_character = text.current_character
110         select_end_character = text.select_end_character
111
112         # if there is no selected text return None
113         if current_line == select_end_line:
114             if current_character == select_end_character:
115                 return None
116             else:
117                 return current_line.body[min(current_character,select_end_character):max(current_character,select_end_character)]
118
119         text_return = None
120         writing = False
121         normal_order = True # selection from top to bottom
122
123         for line in text.lines:
124             if not writing:
125                 if line == current_line:
126                     text_return = current_line.body[current_character:] + "\n"
127                     writing = True
128                     continue
129                 elif line == select_end_line:
130                     text_return =  select_end_line.body[select_end_character:] + "\n"
131                     writing = True
132                     normal_order = False
133                     continue
134             else:
135                 if normal_order:
136                     if line == select_end_line:
137                         text_return += select_end_line.body[:select_end_character]
138                         break
139                     else:
140                         text_return += line.body + "\n"
141                         continue
142                 else:
143                     if line == current_line:
144                         text_return += current_line.body[:current_character]
145                         break
146                     else:
147                         text_return += line.body + "\n"
148                         continue
149
150         return text_return
151
152
153 def register():
154     bpy.types.Scene.use_webbrowser = bpy.props.BoolProperty(
155         name='Launch Browser',
156         description='Opens the page with the submitted text',
157         default=True)
158
159     bpy.utils.register_module(__name__)
160
161
162 def unregister():
163     del bpy.types.Scene.use_webbrowser
164     bpy.utils.unregister_module(__name__)
165
166 if __name__ == "__main__":
167     register()