I have a python script I made a couple of months ago that is command line based. I want to make an optional GUI for it. It is a fairly basic program, it fetches cue files for game roms. I coded the GUI separately, and it came to mind that instead of trying to implement it into the code of the program, it'd be 10 times easier to just execute the program with the flags the user specified on the GUI, then print the output on a text field. This is the GUI:
The program parses flags with the argparse library. It has a positional argument which is the directory, and two optional flags being -r and -g (I guess you can identify which is which). How could I do it?
You can use subprocess.check_output() to get the output of a subprocess. Here's sample usage:
var = subprocess.check_output(
['python3', 'CueMaker.py'], shell=True, stderr=subprocess.STDOUT
)
# redirects the standrad error buffer to standrad output to store that in the variable.`
Then I can use var to change the text, assuming there's a StringVar() tied to the widget:
stringVar.set(var)
Related
I am making a program which has a functionality to print things. I have managed to put together this snippet which fetches the name of the default printer (if there is one):
import ctypes
buffer = ctypes.create_unicode_buffer(1024)
ctypes.WinDLL("winspool.drv").GetDefaultPrinterW(buffer, ctypes.byref(ctypes.c_ulong(1024)))
printerName = buffer.value
However I cannot figure out how to actually print the file with this. Using notepad.exe with the -P argument (or running a text file with the print verb) it can print but it opens a notepad window and such, which I want to be silent.
The print command does not allow direct printer names, it requires you to set an LPT port and use that.
If you have any clue how to print the file, now that I have the printer name (It's a txt file if that matters) I would greatly appreciate it!
Please find the following snippet:
import argparse
parser = argparse.ArgumentParser(
description="Create plot from data",
formatter_class=lambda prog: argparse.HelpFormatter(
prog, max_help_position=27))
action = parser.add_mutually_exclusive_group(required=True)
action.add_argument('--foo', help="Create foo plot") # input is <filename>.foo
action.add_argument('--bar', help="Create bar plot") # Input is <filename>.bar
I run this in linux terminal emulator. Is it possible within python that, in the terminal, double tabiing will show only files with extension foo or bar, depending on argument, and not all the files in PWD?
I have found TAB autocomplete python CLI, but that is a decade old. Is there any option now?
Update #Lenormju:
I have updated the code as:
action.add_argument('--foo', help="Create foo plot", choices=('agr'))
action.add_argument(
'--bar', help="Create bar plot").completer = ChoicesCompleter('.agr')
So, now in terminal,
python ~/bin/code.py --foo [TAB][TAB]
I am expecting this to show files with .agr extensions only. Instead it is still shown all the files present in PWD.
Actually, this should not work, because "choices" means, I have to choose between 'a' 'g' or 'r'. May be I was not clear in the main question, show I have elaborated it.
When you press Tab, your shell expects to receive a list of strings (the possible completions).
Either you register a Bash function to do the completion for your Python script (see this answer) so that your Python script is not called until the command line is finished.
Or else you do it with your Python script (see this answer for argcomplete which mixes well with argparse). In this case, Your Python script is indeed executed, but after computing the possible completions it will exit, and thus not run anything else. In other words, it will be called repeatedly to provide completion suggestions, then once at the end with the full command line to do its actual work.
Showing only the files with certain extensions depending on the argument is "just" a matter of customizing the completer.
Your question is really just a duplicate of the other, but you seem to not see it. If the misunderstanding is actually on my side, please provide detailed explanations.
I am new to Blender. I have created a simple project where I have added a text variable to it. The text I added here in Test. See image below.
Now, I want to call this script from the command line by to call this particular project file and pass in parameters like the text variable to display the text james instead of Test.
For example, typing the following command should give me video generated with the text james.
blender proj1.blend variable=james
Note: I am a beginner, and I hope I explained my question clearly.
Use python script, like
blender proj1.blend --python-expr "import bpy; bpy.data.curves['Text'].body = 'james'"
(if your text curve object called "Text")
Argument order is important - you want script to be executed after file is loaded.
You can find the arguments used to start blender listed in sys.argv, the same as if you were running a normal python script. Blender will ignore any arguments after --, your script can then find the -- argument and process any options after that.
blender -b --python maketext.py -- James
Then the contents of maketext.py would start with -
import bpy
import sys
idx = sys.argv.index('--') + 1
string_to_use = sys.argv[idx]
text_data = bpy.data.curves.new('txt', 'FONT')
text_data.body = string_to_use
text_obj = bpy.data.objects.new('text', text_data)
bpy.context.scene.objects.link(text_obj)
# animate and render
For my project work, I am using Inkscape for two tasks:
To resize the page of a drawing (which I have created with another software) to fit exactly the drawing: File --> Document Properties --> Resize page to content...
Save the file as PDF
This tasks are rather simple, however they are time-consuming for larger amount of drawings.
I checked for macros functionality in Inkscape but there is no such thing. However I found out that Inkscape allows one to implement his own extension scripts using Python.
If any of you has similar experience, could you help me implement the steps listed above as an Inkscape extension.
Potentially useful link: http://wiki.inkscape.org/wiki/index.php/PythonEffectTutorial
EDIT: the accepted answer does not solve my request using internal python extension, but it solves the task by using the inkscape command-line options.
I have never scripted stuff from inside inkscape, but I use inkscape from python all the time (via the subprocess module). If you type inkscape --help on the command line, you will see all the options. I believe for your use-case, the following will work:
inkscape -D -A myoutputfile.pdf myinputfile.whatever
The -A says to output to PDF (requires the filename), and the -D tells it to resize to the drawing.
If you've never used the subprocess module, the easiest thing would be to use subprocess.call like this:
subprocess.call(['inkscape', '-D', '-A', outfn, inpfn])
EDIT:
The cheesiest possible script (untested!) to handle input filenames passed on the command line would look something like this:
import sys
import os
# Do all files except the program name
for inpfn in sys.argv[1:]:
# Name result files 'resized_<oldname>.pdf'
# and put them in current directory
shortname = os.path.basename(inpfname).rsplit('.',1)[0]
outfn = 'resized_%s.pdf' % shortname
subprocess.call(['inkscape', '-D', '-A', outfn, inpfn])
I got a python file which is a code that I developed. During his execution I input from the keyboard several characters at different stages of the program itself. Also, during the execution, I need to close a notepad session which comes out when I execute into my program the command subprocess.call(["notepad",filename]). Having said that I would like to run this code several times with inputs which change according to the case and I was wondering if there is an automatic manner to do that. Assuming that my code is called 'mainfile.py' I tried the following command combinations:
import sys
sys.argv=['arg1']
execfile('mainfile.py')
and
import sys
import subprocess
subprocess.call([sys.executable,'mainfile.py','test'])
But it does not seem to work at least for the first argument. Also, as the second argument should be to close a notepad session, do you know how to pass this command?
Maybe have a look at this https://stackoverflow.com/a/20052978/4244387
It's not clear what you are trying to do though, I mean the result you want to accomplish seems to be just opening notepad for the sake of saving a file.
The subprocess.call() you have is the proper way to execute your script and pass it arguments.
As far as launching notepad goes, you could do something like this:
notepad = subprocess.Popen(['notepad', filename])
# do other stuff ...
notepad.terminate() # terminate running session