full unix username of a user - python

Wondering if you know if there is a slick way to get full username from shell?
example: if my unix username is froyo then I want to get my full name
as registered in the system in this case [froyo  === Abhishek Pratap]
finger command does it but for all the logged in users at the same time ..so that needs some parsing to get the right value. Anything slicker ?
Anything from inside python would be great too.
Thanks!
-Abhi

One way to achieve this is using pwd.getpwuid and os.getuid:
>>> import os
>>> import pwd
>>> pwd.getpwuid(os.getuid())[4]
This is the traditional Unix way of doing it (based on standard C library calls).

Related

Is There a Way to Get a List of Aliases? (Discord py)

So I've been stuck on this for a while and can't seem to find the answer online...
Is there a way to show a user all of the aliases that a command has?
Example: .sum has the aliases: .add, .plus, .addNums, etc...
Mainly I just need a way to "get" a list of aliases for a given command.
I found bot.get_command("aliasName"), which will return the command that this alias is tied to, however I cannot find a way to get a list of aliases from a given command.
I've checked this, but could not find what I needed.
Any help would be appreciated!
from discord.ext import commands
sum_command = commands.Bot.get_command(bot, "sum")
sum_aliases = sum_command.aliases
Found it!
The default help command does this by default. You would invoke it for the command sum like:
.help sum
and the response would be something like
.[sum|plus|addNums]

Is there a way of deleting specific text for the user in Python?

I am making a program in python and want to clear what the user has enterd, this is because I am using the keyboard function to register input as is is given, but there is still text left over after a keypress is registerd and I don't want this to happen.
I was woundering if there is a module that exists to remove text that is being entered
Any help would be greatly apreciated, and just the name of a module is fine; I can figure out how to use it, just cant find an appropriate module.
EDIT:
Sorry if i did not make my self clear, I dont really want to clear the whole screen, just what the user has typed. So that they don't have to manually back space after their input has been taken.
One way to accomplish this is to use the operating system's clear function.
In windows this is cls and on unix systems this is clear. To call these you would use the os module.
For example:
os.system("clear")
You can use a lambda to make it easier, e.g.:
import os
clear = lambda: os.system('clear') # or 'cls', in case you are on windows.
clear()
You can use os.system('clear') on Mac/Linux or os.system('cls') on Windows
However on some systems you may still be able to scroll up in the terminal. In some cases you can use the up arrow key to see previously entered text.
If it is a password or other sensitive information you can use the getpass module
import getpass
password = getpass.getpass("Enter your password: ")
This will both mask the input and prevent you from up-arrowing into it
'sys.stdout.write' is the moduel I was looking for.

Using Jenkins variables/parameters in Python Script with os.path.join

I'm trying to learn how to use variables from Jenkins in Python scripts. I've already learned that I need to call the variables, but I'm not sure how to implement them in the case of using os.path.join().
I'm not a developer; I'm a technical writer. This code was written by somebody else. I'm just trying to adapt the Jenkins scripts so they are parameterized so we don't have to modify the Python scripts for every release.
I'm using inline Jenkins python scripts inside a Jenkins job. The Jenkins string parameters are "BranchID" and "BranchIDShort". I've looked through many questions that talk about how you have to establish the variables in the Python script, but with the case of os.path.join(),I'm not sure what to do.
Here is the original code. I added the part where we establish the variables from the Jenkins parameters, but I don't know how to use them in the os.path.join() function.
# Delete previous builds.
import os
import shutil
BranchID = os.getenv("BranchID")
BranchIDshort = os.getenv("BranchIDshort")
print "Delete any output from a previous build."
if os.path.exists(os.path.join("C:\\Doc192CS", "Output")):
shutil.rmtree(os.path.join("C:\\Doc192CS", "Output"))
I expect output like: c:\Doc192CS\Output
I am afraid that if I do the following code:
if os.path.exists(os.path.join("C:\\Doc",BranchIDshort,"CS", "Output")):
shutil.rmtree(os.path.join("C:\\Doc",BranchIDshort,"CS", "Output"))
I'll get: c:\Doc\192\CS\Output.
Is there a way to use the BranchIDshort variable in this context to get the output c:\Doc192CS\Output?
User #Adonis gave the correct solution as a comment. Here is what he said:
Indeed you're right. What you would want to do is rather:
os.path.exists(os.path.join("C:\\","Doc{}CS".format(BranchIDshort),"Output"))
(in short use a format string for the 2nd argument)
So the complete corrected code is:
import os
import shutil
BranchID = os.getenv("BranchID")
BranchIDshort = os.getenv("BranchIDshort")
print "Delete any output from a previous build."
if os.path.exists(os.path.join("C:\\Doc{}CS".format(BranchIDshort), "Output")):
shutil.rmtree(os.path.join("C:\\Doc{}CS".format(BranchIDshort), "Output"))
Thank you, #Adonis!

How do I read/write files to an unknown user directory?

I'm attempting to read and write files from a User Directory, (C:\Users\USERNAME\Test Source) But I've been unsuccessful in finding any resources on how I can auto detect the name of the user, USERNAME in the above example, or anyway that I can have it read and write to the directory without knowledge off what a users name is.
Could anyone point me towards the right direction or methods for this, if it's even a logical request? I'm not sure how much difference, if any, it makes but this program is being written in Python 2.7.
The simplest way is this:
import os
print os.path.expanduser('~')
Append your folder to the path like so:
userdir = os.path.expanduser('~')
print os.path.join(userdir, 'Test Source')
Besides requiring the least lines of code, this method has the advantage of working under every OS (Linux, Windows XP / 7 / 8 / etc).
You can use in windows command line
echo %username%
or
whoami
for getting the username of the user who is currently logged in .
Store it in a variable and then append it to the path name.
You can also use
‘C:\users\%username%\file‘
directly .To check through whoami do
l=`whoami`
echo $l
Use the %userprofile% variable in your path if you're on Windows:
%userprofile%\Test Source\file.txt
Try:
>>> import getpass
>>> import os.path
>>> usename = getpass.getuser()
>>> mypath = os.path.join("C:\Users", username, "Test Source")

How to modify an ABPerson instance using PyObjC?

I am trying to do some repairs to a very large address book on OS X. I would like to do this programmatically, as it would be a very intense effort to do by hand. On a hunch, I thought I might use PyObjC for this, given my familiarity with Python.
I'm able using PyObjC's module AddressBook to poke around, but I can't seem to make any changes:
>>> import AddressBook
>>> ab = AddressBook.ABAddressBook.addressBook()
>>> p = ab.people()[0]
>>> p.isReadOnly()
True
>>> p.valueForProperty_('First')
u'Foo'
>>> p.setValue_forProperty_('Bar', 'First')
False
>>> p.valueForProperty_('First')
u'Foo'
>>> type(p)
<objective-c class ABPerson at 0x7fff76e01ab8>
My first question is can I edit / modify contacts/persons this way? My second question is, for this kind of work, I can also effect changes to contacts using the ScriptingBridge to talk to the Contacts App, using either AppleScript or even PyObjC (or others). Would this be a better method than using the "low-level" API?
EDIT
I tried a few more methods, plain old AppleScript, and Python using the ScriptingBridge. All of my methods to script modifications to contacts fail, even when I call the save() method of the address book. Do I need to edit some system setting to allow contacts to be scripted?
EDIT
For the record, I am running OS X Mountain Lion 10.8.3
Answering my own question. Looking at errors in the console after running the above script, I saw many errors looking like:
AOSKit ERROR: (-) RAF: Invalid url -- https://[myemail]#webdav.facebook.com/[myfbid]/contacts/
(email and id changed of course). I have a 'Facebook' account set up in OS X preferences. By an educated guess, I deleted this Facebook OS X account, and then re-added it. After that, the above script works correctly.

Categories

Resources