1- import os
2- import importlib
31import subprocess
2+ from subprocess import Popen , PIPE
43import tempfile
54from pathlib import Path
65from sys import platform
7- from threading import local
8-
6+ from getpass import getpass
97
108from ._bot import BaseBot
119from ..utilities .web_tools import download_file
@@ -21,7 +19,7 @@ class LatexBot(BaseBot):
2119 """LatexBot.
2220 """
2321
24- def __init__ (self , config_file = None , sender = None ) -> None :
22+ def __init__ (self ) -> None :
2523 self .__name__ = "latexbot"
2624
2725 def install_dependencies (self , git , pandoc , miktex ):
@@ -32,7 +30,13 @@ def install_dependencies(self, git, pandoc, miktex):
3230
3331 Parameters
3432 ----------
35- None
33+ git : bool
34+ if True install git
35+ pandoc : bool
36+ if True install pandoc
37+ miktex : bool
38+ if True install miktex
39+
3640
3741 Kwargs
3842 ------
@@ -52,7 +56,16 @@ def install_dependencies(self, git, pandoc, miktex):
5256 try :
5357
5458 if platform == "linux" or platform == "linux2" : # linux
55- raise NotImplementedError ("your os is currently not supported" )
59+ if git :
60+ print ("git is pre-installed in linux" )
61+ if miktex :
62+ print ("NotImplemented" )
63+ settings = {
64+ 'pandoc' :
65+ {'url' : 'https://github.com/jgm/pandoc/releases/download/2.15/pandoc-2.15-1-amd64.deb' ,
66+ 'function' : 'install_deb' ,
67+ 'file_name' : 'pandoc-2.15-1-amd64.deb' },
68+ }
5669 elif platform == "darwin" : # OS X
5770 raise NotImplementedError ("your os is currently not supported" )
5871 elif platform == "win32" : # Windows
@@ -86,6 +99,12 @@ def install_dependencies(self, git, pandoc, miktex):
8699 temp_dir .cleanup ()
87100 raise Exception ("ERROR, something went wrong" )
88101
102+ def add_overleaf_project (self , name , code ):
103+ raise NotImplementedError ()
104+
105+ def add_document (self , name , path ):
106+ raise NotImplementedError ()
107+
89108 def _clone_overleaf_temp (self , document_code ):
90109 """Temporarily clone the overleaf project on the local machine.
91110
@@ -140,14 +159,29 @@ def convert_tex_to_docx(self, input_path, output_path=None, open_docx=False):
140159 open_docx = True
141160 else :
142161 output = Path ().joinpath (output_path , str (file .name ).split ('.tex' )[0 ]+ '.docx' )
143- out = subprocess .run (['pandoc' , '-o' , output , '-t' , 'docx' , file ])
144- print (f"docx saved in { output } " )
162+ if platform == "linux" or platform == "linux2" :
163+ password = getpass ("Please enter your password: " )
164+ # sudo requires the flag '-S' in order to take input from stdin
165+ proc = Popen (f"sudo -S pandoc -o { output } -t docx { file } " .split (), stdin = PIPE , stdout = PIPE , stderr = PIPE )
166+ # Popen only accepts byte-arrays so you must encode the string
167+ stdout , stderr = proc .communicate (password .encode ())
168+ print (stdout .decode ())
169+ print (stderr .decode ())
170+ # out = subprocess.run(['sudo', 'pandoc', '-o', output, '-t', 'docx', file])
171+ else :
172+ out = subprocess .run (['pandoc' , '-o' , output , '-t' , 'docx' , file ])
173+ if out .returncode == 0 :
174+ print (f"docx saved in { output } " )
175+ else :
176+ raise Exception ("could not save docx" )
145177 output_paths .append (output )
146- # print("The exit code was: %d" % out.returncode)
147178 if open_docx :
148179 print ("opening file... don't forget to save it (save as...)!" )
149- p = subprocess .Popen (output , shell = True )
150- p .wait ()
180+ if platform == "linux" or platform == "linux2" : # linux
181+ print ("Not yet supported!" ) # p = subprocess.run(["libreoffice", output])
182+ else :
183+ p = subprocess .Popen (output , shell = True )
184+ p .wait ()
151185 return output_paths
152186
153187 def convert_overleaf_to_docx (self , document_code , output_path = None , open_docx = True , upload = False ):
@@ -166,6 +200,14 @@ def convert_overleaf_to_docx(self, document_code, output_path=None, open_docx=Tr
166200 open_docx : bool, optional
167201 open the docx file after creation, by default True. Use this to
168202 inspect the conversion and save the file in a different location.
203+ upload : bool, optional
204+ uploads the document to your google drive, by default False.
205+
206+ Notes
207+ -----
208+ In order for the upload to succeed, you must configure the `drivebot`
209+ first by running `drivebot configure -type=web -save=True` in the command
210+ line.
169211 """
170212 temp_dir = self ._clone_overleaf_temp (document_code )
171213 try :
0 commit comments