85 lines
2.5 KiB
Python
85 lines
2.5 KiB
Python
#! /usr/bin/env python3
|
|
# encoding: utf-8
|
|
|
|
import sys
|
|
import subprocess
|
|
import os
|
|
import pip
|
|
import sys
|
|
|
|
#Install and import pyperclip
|
|
try:
|
|
import pyperclip
|
|
except ImportError as e:
|
|
answer = str(input("Pyperclip module is not installed and is required. Install it now with pip?[y/N]"))
|
|
if answer == "y":
|
|
subprocess.call([sys.executable, "-m", "pip", "install", "pyperclip", "--user"])
|
|
import pyperclip
|
|
|
|
#Reads a file, copies it to clipboard
|
|
|
|
|
|
###############################FONCTS##########################
|
|
|
|
def getArgs():
|
|
'''Gets all the arguments passed to the script and returns them in a parse_args()-type object.
|
|
No args
|
|
Returns:
|
|
-args : an args object containing all the optional arguments passed to the script.
|
|
'''
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("file", action="store", type=str, help="Path of a file or p to read from the pipe") #Required, can be p
|
|
parser.add_argument("-w", "--wiki", help="Formats the text for wiki copying.", action="store_true")
|
|
parser.add_argument("-p", "--python", help="Formats the text for syntaxhighlight lang=python", action='store_true')
|
|
parser.add_argument("-b", "--bash", help="Formats the text for syntaxhighlight bash", action='store_true')
|
|
parser.add_argument("-P", "--pshell", help="Formats the text for syntaxhighlight powershell", action='store_true')
|
|
parser.add_argument("-y", "--yaml", help="Formats the text for syntaxhighlight yaml", action='store_true')
|
|
|
|
#Creating the args object
|
|
args=parser.parse_args()
|
|
|
|
return args
|
|
|
|
################################MAIN#############################
|
|
###GETTING THE FILE
|
|
|
|
args = getArgs()
|
|
|
|
if args.file == "p":
|
|
'''
|
|
p means we ride from the pipe
|
|
pipein, pipeout = os.pipe()
|
|
os.close(pipeout)
|
|
pipein = os.fdopen(pipein)
|
|
content = pipein.read()
|
|
'''
|
|
content = sys.stdin.read()
|
|
content = str(content)
|
|
else:
|
|
myfile = args.file
|
|
with open(myfile, "r") as afile:
|
|
content = afile.read()
|
|
|
|
##formatting
|
|
|
|
if args.wiki:
|
|
tags = [" <nowiki>", "</nowiki>"]
|
|
elif args.python:
|
|
tags = ["<syntaxhighlight lang='python'>", "</syntaxhighlight>"]
|
|
elif args.bash:
|
|
tags = ["<syntaxhighlight lang='bash'>", "</syntaxhighlight>"]
|
|
elif args.pshell:
|
|
tags = ["<syntaxhighlight lang='powershell'>", "</syntaxhighlight>"]
|
|
elif args.yaml:
|
|
tags = ["<syntaxhighlight lang='yaml'>", "</syntaxhighlight>"]
|
|
else:
|
|
tags = None
|
|
|
|
if tags != None:
|
|
content = f"{tags[0]}\n{content}{tags[1]}"
|
|
|
|
pyperclip.copy(content)
|
|
print("File copied to clipboard")
|