Skip to content

Commit

Permalink
Upload to a pluginFolder / method to get octoprint version
Browse files Browse the repository at this point in the history
* new method to get octoprint version
* uploads will be now uploaded to a telegramFolder. only when octoPrint
version >= 1.3, else file will be placed in basefodler and gets prefix
"telegram"
* move/copy buttons will only be shown when octoPrint version >= 1.3
* Fix #48
* method to get version is from
https://github.com/Salandora/OctoPrint-FileManager/blob/master/octoprint_filemanager/__init__.py
  • Loading branch information
derpicknicker1 committed Dec 29, 2016
1 parent 8e7c88b commit 06a4014
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
39 changes: 37 additions & 2 deletions octoprint_telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,13 @@ def handleDocumentMessage(self, message, chat_id, from_id):
self.main.send_msg(self.gEmo('warning') + " Sorry, I only accept files with .gcode, .gco or .g extension.", chatID=chat_id)
raise ExitThisLoopException()
# download the file
target_filename = "telegram_" + file_name
if self.main.get_octoprint_base_version() >= 1.3:
target_filename = "TelegramPlugin/"+file_name
from octoprint.server.api.files import _verifyFolderExists
if not _verifyFolderExists(octoprint.filemanager.FileDestinations.LOCAL, "TelegramPlugin"):
self.main._file_manager.add_folder(octoprint.filemanager.FileDestinations.LOCAL,"TelegramPlugin")
else:
target_filename = "telegram_"+file_name
# for parameter no_markup see _send_edit_msg()
self.main.send_msg(self.gEmo('save') + gettext(" Saving file {}...".format(target_filename)), chatID=chat_id)
requests.get(self.main.bot_url + "/sendChatAction", params = {'chat_id': chat_id, 'action': 'upload_document'})
Expand Down Expand Up @@ -402,7 +408,7 @@ def __init__(self):
'play': u'\U000025B6',
'stop': u'\U000025FC'
}
self.emojis.update(telegramEmojiDict)
self.emojis.update(telegramEmojiDict)
# all emojis will be get via this method to disable them globaly by the corrosponding setting
# so if you want to use emojis anywhere use gEmo("...") istead of emojis["..."]
def gEmo(self,key):
Expand Down Expand Up @@ -1075,6 +1081,35 @@ def test_token(self, token=None):
### Helper methods
##########

# gets octoprint base version as float (e.g 1.2)
### From filemanager plugin - https://github.com/Salandora/OctoPrint-FileManager/blob/master/octoprint_filemanager/__init__.py
def get_octoprint_base_version(self):
import pkg_resources
from octoprint.server import VERSION
octoprint_version_string = VERSION

if "-" in octoprint_version_string:
octoprint_version_string = octoprint_version_string[:octoprint_version_string.find("-")]

octoprint_version = pkg_resources.parse_version(octoprint_version_string)
if isinstance(octoprint_version, tuple):
# old setuptools
base_version = []
for part in octoprint_version:
if part.startswith("*"):
break
base_version.append(part)
octoprint_version = ".".join(base_version)
else:
# new setuptools
octoprint_version = pkg_resources.parse_version(octoprint_version.base_version)

oVersion = []
for k in octoprint_version.split("."):
oVersion.append(str(int(k)))
oVersion = float(".".join(oVersion))
return oVersion

def str2bool(self,v):
return v.lower() in ("yes", "true", "t", "1")

Expand Down
13 changes: 8 additions & 5 deletions octoprint_telegram/telegramCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ def __init__(self, main):
'/tune': {'cmd': self.cmdTune, 'param': True},
'/help': {'cmd': self.cmdHelp, 'bind_none': True}
}


############################################################################################
# COMMAND HANDLERS
############################################################################################
Expand Down Expand Up @@ -216,7 +218,7 @@ def cmdFiles(self,chat_id,from_id,cmd,parameter):
self.main.send_msg(self.gEmo('save') + " *Select Storage*",chatID=chat_id,markup="Markdown",responses=keys,msg_id=msg_id)
############################################################################################
def cmdUpload(self,chat_id,from_id,cmd,parameter):
self.main.send_msg(self.gEmo('info') + " To upload a gcode file, just send it to me.",chatID=chat_id)
self.main.send_msg(self.gEmo('info') + " To upload a gcode file, just send it to me.\nThe file will be stored in 'TelegramPlugin' folder.",chatID=chat_id)
############################################################################################
def cmdSys(self,chat_id,from_id,cmd,parameter):
if parameter and parameter != "back":
Expand Down Expand Up @@ -623,8 +625,9 @@ def fileDetails(self,pathHash,page,cmd,fileHash,chat_id,from_id,wait=0):
keys.append(keysRow)
keysRow = []
if self.main.isCommandAllowed(chat_id, from_id, "/files"):
keysRow.append(keyMove)
keysRow.append(keyCopy)
if self.main.get_octoprint_base_version() >= 1.3:
keysRow.append(keyMove)
keysRow.append(keyCopy)
keysRow.append(keyDelete)
keys.append(keysRow)
keysRow = []
Expand Down Expand Up @@ -742,7 +745,7 @@ def fileOption(self,loc,page,cmd,hash,opt,chat_id,from_id):
else:
keys = [[[self.main.emojis['check']+" Yes",cmd+"_" + loc + "|"+str(page)+"|"+ hash+"|d_d"],[self.main.emojis['cross mark']+" No",cmd+"_" + loc + "|"+str(page)+"|"+ hash]]]
self.main.send_msg(self.gEmo('warning')+" Delete "+path+" ?",chatID=chat_id,responses=keys,msg_id=msg_id)
### From filemanager plugin
### From filemanager plugin - https://github.com/Salandora/OctoPrint-FileManager/blob/master/octoprint_filemanager/__init__.py
############################################################################################
def fileCopyMove(self, target, command, source, destination):
from octoprint.server.api.files import _verifyFolderExists, _verifyFileExists
Expand Down Expand Up @@ -777,7 +780,7 @@ def fileCopyMove(self, target, command, source, destination):
elif self.main._file_manager.folder_exists(target, source):
self.main._file_manager.move_folder(target, source, destination)
return "GOOD"
### From filemanager plugin
### From filemanager plugin - https://github.com/Salandora/OctoPrint-FileManager/blob/master/octoprint_filemanager/__init__.py
############################################################################################
def fileDelete(self, target, source):
from octoprint.server.api.files import _verifyFolderExists, _verifyFileExists, _isBusy
Expand Down

0 comments on commit 06a4014

Please sign in to comment.