How can I write a Python script to control Gnome window placement? - python

I'm trying to wrangle my Gnome desktop windows and I would like to automate their placement using Python. What is a good approach for this?

You have 2 options:
If you only need it to work in an X session, then this is relatively easy to do using xdotool (short summary in this SO answer). You can also use it from Python, as explained in another question on SO.
The Wayland protocol does not allow this by default for security reasons. If you want to be able to support both X and Wayland, you'll have to get to the inner working of GNOME Shell by writing an extension (in JS). This extension then either does the moving, or exposes an API to move windows, which you can then call from a Python script.

Related

Python Can I Print to Printer using Tkinter

I am making a notepad using Tkinter and I want to be able to print to the printer the notes taken.I am using a Text object in order to allow the user to write his notes.I couldn't find anything on to how to print to printer with Tkinter and I want to avoid using PyQT or win32api that I know have printer support(due to complexity and lack of experience).I am planning on releasing the application only on windows so I donnot need the it to be cross-platform.
Fact to Accept: Cross-Platform Printing is hard.
Depending on the system you use, the commands to send text / files to a printer will be very different.
For Windows, you should probably use the Python win32print module, even if you say you don't want to use it because of complexity etc.
I honestly think that trying to solve it any other way would be a lot more complicated in the end.
For Linux, Mac, Unix, you can send commands much more directly using LPR to the system printer through the built-in os.popen() or the new subprocess module in Python, but for Windows, my bet is you're better off using the win32print module.
Providing cross-platform printing functionality will always be a challenge because of the differences in the underlying sub-systems on different operating systems.
The basic approach
You'll need to separate the logic of your code, so that depending on the underlying OS, your program will choose the right method for executing the printing functionality you need.
I don't know of any way around this.
Using the Python win32 modules needn't be as complex as you might think.
For Windows
This can be done with the module win32print,
Documented nicely here
For Linux, macOS, Unix
Check out the use of LPR commands, and combine this with basic Python os.popen calls or using the new Python subprocess module
I know you probably wanted a more "copy/paste friendly" way, but that would be very hard without having the rest of your code and not knowing the exact requirements / specs for your app.
Bottom line is that you'd probably end up writing custom code for each platform for printing anyway, so might as well jump in head first.

Preferred Method of Opening a PDF in Python 3

Is there a newer way to open a PDF using an external viewer from Python 3 in Linux other than subprocess?
This sounds like a noobish and duplicate question, but I looked at this question and this question, and all of the answers are over 7 years old and recommended discouraged methods like os.system, old methods like manually creating a subprocess.Popen or Windows-only methods like os.startfile.
So in the time since these questions were answered, have preferred methods of launching a PDF reader from within Python emerged, or are these still the best answers?
Python as of 3.6 still doesn't have a cross-platform way to open files using default programs.
Issue 3177 suggested to add one, but it didn't happen yet.
So:
On Windows, there's a system call for this, you can reach it from Python via os.startfile,
On Linux, there's a command-line tool called xdg-open that does this,
On Mac OS, there's a command-line tool simply called open.
This means that unfortunately you still need to check the operating system and pick the right approach. The correct way to call the command-line tools is using the subprocess module.
This answer provides a code snippet:
Open document with default application in Python

Python command line interaction library?

I want to develop a small Python app that interacts with the user via the console/command line. Are there any good libraries I can use to get user input and display the output in a nice-looking way? I tried searching but most of what I found was command-line argument processing, didn't see anything about user interaction in the shell.
It should be crossplatform (Windows and Linux)
A really excellent library is cmd which is part of the python standard library. It is cross platform Windows, Linux, Mac. You just have to implement one class and it offers so many great features:
provides list of supported commands(they end up being implemented as methods)
help command that can be called to explain one of your commands
handles the whole entering a command, checking syntax, and calling your method command loop cycle.
users can save the commands they have run in an interactive session and run them as a script. check out the example below.
If you take the turtle shell example and save it as turtleshell.py and save the below turtle script file as circles.txt
circle 20
circle 50
circle 100
bye
then you could run the turtle script with the following command:
cat circles.txt | ./turtleshell.py
so in the simple example shown in the docs the developer has essentially made a simple mini-language that can be used as an easier interface to to the turtle module making it even easier to introduce programming to kids. The examples above have been taken from the python3 docs because they have included a detailed example in their docs which wasn't there in the 2.7 docs, but cmd is available and fully functional in python 2.3 and later.
You can control the Unix terminal with the curses library. The library essentially lets you build a simple terminal GUI.
If you need more, take a look at Urwid as well. Urwid offers more complex GUI widgets for the discerning terminal GUI developer. :-)
Curses is the most widely used in the Unix environment according to the doc. For Windows you could look at Windows Console Driver, WConio - Windows CONsole I/O for Python or Wcurses. I couldn't find much on cross platform console libraries unfortuntaly.
If your Windows users are CLI users they probably have cygwin which has ncurse support so curses is still the best option if you ask me.

Execution permissions in Python

I need to send code to remote clients to be executed in them but security is a concern for me right now. I don't want unsafe code to be executed there so I would like to control what a program is doing. I mean for example, know if is making connections, where is connecting to, if is reading local files, etc. Is this possible with Python?
EDIT: I'm thinking in something similar to Android permission system. I want to know what a code will do and if it does something different, stop it.
You could use a different Python runtime:
if you run your script using Jython; you can exploit Java's permission system
with Pypy's sandboxed version you can choose what is allowed to run in your controller script
There used to be a module in Python called bastian, but that was deprecated as it wasn't that secure. There's also I believe something called RPython, but I don't know too much about that.
I would in this case use Pyro and write the code on the target server. That way you know clients can only execute written and tested code.
edit - it's probably worth noting that Pyro also supports http://en.wikipedia.org/wiki/Privilege_separation - although I've not had to use it for that.
I think you are looking for a sandboxed python. There used to be an effort to implement this, but it has been abolished a couple of years ago.
Sandboxed python in the python wiki offers a nice overview of possible options for your usecase.
The most rigourous (but probably the slowest) way is to run Python on a bare OS in an emulator.
Depending on the OS you use, there are several ways of running programs with restrictions, but without the overhead of an emulator:
FreeBSD has a nice integrated solution in the form of jails.
These grew out of the chroot system call.
Linux-VServer aims to do more or less the same on Linux.

Is there a way to make a Python console application flash in the taskbar in Windows?

Is there a way to make my Python console application's window flash in the Windows taskbar, to get a user's attention?
My script will be run exclusively in a relatively homogeneous Windows environment, so I don't care about detecting whether a particular API is present, or whether a solution is cross-platform or not (of course cross-platform is better for future reference... but I don't need it for this application).
This is the simplest solution I could come up with:
import ctypes
ctypes.windll.user32.FlashWindow(ctypes.windll.kernel32.GetConsoleWindow(), True )
Flashing the taskbar in Windows is accomplished using the FlashWindowEx API function (Python API help).
I haven't tried this myself, but it should be possible to call this function from Python using PyWin32 (Python for Windows extensions) that can either by installed manually or by installing ActivePython.

Categories

Resources