Skip to content

Commit 1469784

Browse files
committed
Bring back support for comma as a CSV separator
1 parent a40f542 commit 1469784

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ This updated version implements some cool new things:
1212

1313
* Support for macOS 10.14, 10.15, 11 and 12
1414
* Usage of the Munki-included Python3
15-
* 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
15+
* 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.
1616
* The order of the csv columns do not have to be preserved, but **keep the names of the 1st row**.
1717
* Sanity checks for the csv fields
1818
* Option to setup printers using AirPrint provided PPDs, using [airprint-ppd](https://github.com/wycomco/airprint-ppd)
1919
* Option to define a path to Munki repo and an optional subdirectory
2020
* Option to define a separate name for the Munki pkginfo item
21-
* Besides switching to semicolons as csv separator: **This script should preserve backward compatibility!**
21+
* **This script should preserve backward compatibility!**
2222

2323
## Caveats
2424

Template.csv

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Printer Name;Location;Display Name;Address;Driver;Description;Options;Version;Requires;Icon;Catalogs;Subdirectory;Munki Name
2-
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
3-
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
1+
Printer Name,Location,Display Name,Address,Driver,Description,Options,Version,Requires,Icon,Catalogs,Subdirectory,Munki Name
2+
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
3+
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

Template_with_semicolons.csv

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Printer Name;Location;Display Name;Address;Driver;Description;Options;Version;Requires;Icon;Catalogs;Subdirectory;Munki Name
2+
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
3+
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

print_generator.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from plistlib import dump as dump_plist
1515

1616

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

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

119+
# Identify the delimiter of a given CSV file, props to https://stackoverflow.com/questions/69817054/python-detection-of-delimiter-separator-in-a-csv-file
120+
def find_delimiter(filename):
121+
sniffer = csv.Sniffer()
122+
with open(filename) as fp:
123+
delimiter = sniffer.sniff(fp.read(5000)).delimiter
124+
return delimiter
125+
119126
def createPlist(
120127
printer_name: str,
121128
address: str,
@@ -225,7 +232,7 @@ def createPlist(
225232
if args.csv:
226233
# A CSV was found, use that for all data.
227234
with open(args.csv, mode='r') as infile:
228-
reader = csv.DictReader(infile, delimiter=';')
235+
reader = csv.DictReader(infile, delimiter=find_delimiter(args.csv))
229236

230237
for row in reader:
231238

0 commit comments

Comments
 (0)