Skip to content

Commit

Permalink
Bring back support for comma as a CSV separator
Browse files Browse the repository at this point in the history
  • Loading branch information
choules committed Mar 7, 2022
1 parent a40f542 commit 1469784
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ This updated version implements some cool new things:

* Support for macOS 10.14, 10.15, 11 and 12
* Usage of the Munki-included Python3
* Enables usage of Microsoft Excel to edit the CSV file**this required switching the separator from comma to semicolon**, you may need to update your files accordingly
* Enhances usage of Microsoft Excel to edit the CSV file: for regions which use the comma as the decimal separator, Microsoft Excel expects a semicolon as separator in CSV files. The script will distinguish between both variants.
* The order of the csv columns do not have to be preserved, but **keep the names of the 1st row**.
* Sanity checks for the csv fields
* Option to setup printers using AirPrint provided PPDs, using [airprint-ppd](https://github.com/wycomco/airprint-ppd)
* Option to define a path to Munki repo and an optional subdirectory
* Option to define a separate name for the Munki pkginfo item
* Besides switching to semicolons as csv separator: **This script should preserve backward compatibility!**
* **This script should preserve backward compatibility!**

## Caveats

Expand Down
6 changes: 3 additions & 3 deletions Template.csv
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Printer Name;Location;Display Name;Address;Driver;Description;Options;Version;Requires;Icon;Catalogs;Subdirectory;Munki Name
MyPrinterQueue;Tech Office;My Printer Queue;10.0.0.1;HP officejet 5500 series.ppd.gz;Black and white printer in Tech Office;HPOptionDuplexer=True OutputMode=normal;5.0;HPPrinterDriver;HP LaserJet 4250.icns;testing;scripts/printers/hp/;PrinterSetup_Office
BrotherPrinter;Home Office;Printer at home;ipp://brother9022.local;airprint-ppd;A simple AirPrint printer at home;;1.0;airprint_ppd;;testing;scripts/printers/brother/;PrinterSetup_Brother
Printer Name,Location,Display Name,Address,Driver,Description,Options,Version,Requires,Icon,Catalogs,Subdirectory,Munki Name
MyPrinterQueue,Tech Office,My Printer Queue,10.0.0.1,HP officejet 5500 series.ppd.gz,Black and white printer in Tech Office,HPOptionDuplexer=True OutputMode=normal,5.0,HPPrinterDriver,HP LaserJet 4250.icns,testing,scripts/printers/hp/,PrinterSetup_Office
BrotherPrinter,Home Office,Printer at home,ipp://brother9022.local,airprint-ppd,A simple AirPrint printer at home,,1.0,airprint_ppd,,testing,scripts/printers/brother/,PrinterSetup_Brother
3 changes: 3 additions & 0 deletions Template_with_semicolons.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Printer Name;Location;Display Name;Address;Driver;Description;Options;Version;Requires;Icon;Catalogs;Subdirectory;Munki Name
MyPrinterQueue;Tech Office;My Printer Queue;10.0.0.1;HP officejet 5500 series.ppd.gz;Black and white printer in Tech Office;HPOptionDuplexer=True OutputMode=normal;5.0;HPPrinterDriver;HP LaserJet 4250.icns;testing;scripts/printers/hp/;PrinterSetup_Office
BrotherPrinter;Home Office;Printer at home;ipp://brother9022.local;airprint-ppd;A simple AirPrint printer at home;;1.0;airprint_ppd;;testing;scripts/printers/brother/;PrinterSetup_Brother
11 changes: 9 additions & 2 deletions print_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from plistlib import dump as dump_plist


# Preference hanlding copied from Munki:
# Preference handling copied from Munki:
# https://github.com/munki/munki/blob/e8ccc5f53e8f69b59fbc153a783158a34ca6d1ea/code/client/munkilib/cliutils.py#L55

BUNDLE_ID = 'com.googlecode.munki.munkiimport'
Expand Down Expand Up @@ -116,6 +116,13 @@ def throwError(message='Unknown error',exitcode=1,show_usage=True):
templatePlist = load_plist(f)
f.close()

# Identify the delimiter of a given CSV file, props to https://stackoverflow.com/questions/69817054/python-detection-of-delimiter-separator-in-a-csv-file
def find_delimiter(filename):
sniffer = csv.Sniffer()
with open(filename) as fp:
delimiter = sniffer.sniff(fp.read(5000)).delimiter
return delimiter

def createPlist(
printer_name: str,
address: str,
Expand Down Expand Up @@ -225,7 +232,7 @@ def createPlist(
if args.csv:
# A CSV was found, use that for all data.
with open(args.csv, mode='r') as infile:
reader = csv.DictReader(infile, delimiter=';')
reader = csv.DictReader(infile, delimiter=find_delimiter(args.csv))

for row in reader:

Expand Down

0 comments on commit 1469784

Please sign in to comment.