Skip to content

Commit b42ee34

Browse files
committed
Add ability to change file encoding, improve file pathing and allow for base directories outside of the repo, use True/False for boolean settings, other minor changes and fixes
1 parent b6c431b commit b42ee34

2 files changed

Lines changed: 153 additions & 95 deletions

File tree

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#! coding: utf-8
2+
#!/usr/bin/env python
3+
24
import subprocess, csv, xlwt, random, time, json, os, sys, platform, datetime, gzip, zipfile, tarfile, shutil, math, traceback
3-
from classes import settings, logger, presets, columns, valuedict, generator, imagegenerator
5+
from ddg.classes import settings, logger, presets, columns, valuedict, generator, imagegenerator
46

57
def clear(platformname = sys.platform): # clear the terminal buffer ~ NOTE: this seems to be quite buggy, need to come back to this
68
if platformname == "win32":
@@ -21,7 +23,7 @@ def view_settings(notification = ""): # displays setting sections in the termina
2123
print("2. File compression settings")
2224
print("3. Data generation settings")
2325
print("4. Image generation settings")
24-
print("5. Logging settings")
26+
print("5. Application settings")
2527

2628
option = input(notification + "\nEnter the section number (1 to 3) to view the settings for that section, or:\nq. Quit\n\nOption:")
2729

@@ -120,19 +122,19 @@ def view_files_list(notification = "", prevstate = "menu", mode = "column"): # d
120122
subnotif = "\n"
121123

122124
if mode == "column":
123-
print("The following column files are located in the '" + settings.columnfolder + "' folder: \n\nCurrent columns file: " + settings.columnfile + "\n")
125+
print("The following column files are located in the '{0}' folder: \n\nCurrent columns file: {1}\n".format(settings.get_columns_path(), settings.columnfile))
124126

125127
if settings.columnfolder == "":
126128
settings.columnfolder = "."
127129

128-
files = os.listdir(settings.columnfolder)
130+
files = os.listdir(settings.get_columns_path())
129131
elif mode == "preset":
130-
print("The following preset files are located in the '" + settings.presetfolder + "' folder: \n" + ("\nCurrent preset file: " + settings.presetfile + "\n" if settings.presetfile != "" else ""))
132+
print("The following preset files are located in the '{0}' folder: \n{1}".format(settings.get_presets_path(), ("\nCurrent preset file: {0}\n".format((settings.presetfile if settings.presetfile != "" else "")))))
131133

132134
if settings.presetfolder == "":
133135
settings.presetfolder = "."
134136

135-
files = os.listdir(settings.presetfolder)
137+
files = os.listdir(settings.get_presets_path())
136138

137139
fileslist = []
138140

@@ -189,9 +191,9 @@ def view_files_list(notification = "", prevstate = "menu", mode = "column"): # d
189191
deletefilename = input("\nEnter the name of the file to be deleted (excluding file extension): ") + ".json"
190192

191193
if mode == "column":
192-
deletepath = settings.columnfolder + "/" + deletefilename
194+
deletepath = "{0}{1}{2}".format(settings.get_columns_path(), os.path.sep, deletefilename)
193195
elif mode == "preset":
194-
deletepath = settings.presetfolder + "/" + deletefilename
196+
deletepath = "{0}{1}{2}".format(settings.get_presets_path(), os.path.sep, deletefilename)
195197

196198
if os.path.exists(deletepath):
197199
os.remove(deletepath)
@@ -236,13 +238,13 @@ def view_files_list(notification = "", prevstate = "menu", mode = "column"): # d
236238
if mode == "column":
237239
duplicatename = input("Enter name for duplicated file (do not include extension): ") + ".json"
238240

239-
originallocation = settings.columnfolder + "/" + settings.columnfile
240-
duplicatelocation = settings.columnfolder + "/" + duplicatename
241+
originallocation = "{0}{1}{2}".format(settings.get_columns_path(), os.path.sep, settings.columnfile)
242+
duplicatelocation = "{0}{1}{2}".format(settings.get_columns_path(), os.path.sep, duplicatename)
241243
elif mode == "preset":
242244
duplicatename = input("Enter name for duplicated file (do not include extension): ") + ".json"
243245

244-
originallocation = settings.presetfolder + "/" + settings.presetfile
245-
duplicatelocation = settings.presetfolder + "/" + duplicatename
246+
originallocation = "{0}{1}{2}".format(settings.get_columns_path(), os.path.sep, settings.presetfile)
247+
duplicatelocation = "{0}{1}{2}".format(settings.get_columns_path(), os.path.sep, duplicatename)
246248

247249
try:
248250
shutil.copy(originallocation, duplicatelocation)
@@ -513,6 +515,9 @@ def create_file(notification = ""):
513515
settings.numberofrows = input(notification + "Enter the number of rows to generate (or enter 'q' or 'b' to go back to the menu): ")
514516
clear()
515517

518+
if settings.filename == "":
519+
settings.filename = input("Enter a name for the file (do not include the format of the file): ")
520+
516521
if settings.numberofrows == "q" or settings.numberofrows == "b":
517522
menu()
518523
elif settings.numberofrows.isdigit() == False and (settings.numberofrows != 'q' or settings.numberofrows != 'b'):
@@ -525,45 +530,51 @@ def create_file(notification = ""):
525530
def menu(notification = ""): # main menu, first thing the user will see
526531
clear()
527532

533+
settings.update_settings_file()
528534
settings.update_values()
529535
presets.get_presets()
530536

537+
columns.get_columns()
538+
valuedict.reset_indexes()
539+
531540
if settings.presetfile != "":
532-
presetmodstring = ": "
541+
presetmodstring = ""
533542

534543
for presetsetting in presets.json:
535544
if presetsetting["value"] != "":
536-
presetmodstring += "\n - " + presetsetting["name"]
545+
presetmodstring += "\n - {0}".format(presetsetting["name"])
537546

538-
if presetmodstring == ": ":
547+
if presetmodstring == "":
539548
presetmodstring += "<None>"
540549

541-
filename = settings.filename
542-
columnfile = settings.columnfile
550+
columnfile = os.path.join(settings.get_columns_path(), settings.columnfile)
551+
presetfile = os.path.join(settings.get_presets_path(), settings.presetfile)
543552

544553
if (settings.foldername != ""):
545554
if settings.filename != "":
546-
filename = settings.foldername + "/" + settings.filename + ("." + settings.fileformat if settings.fileformat != "" else ".csv") + ("." + settings.compresstype.replace("-",".") if settings.compress == "y" else "")
555+
filename = "{0}{1}{2}.{3}{4}".format(
556+
settings.get_file_path(),
557+
os.path.sep,
558+
settings.filename,
559+
(settings.fileformat if settings.fileformat != "" else "csv"),
560+
(".{}".format(settings.compresstype.replace("-",".")) if settings.compress else "")
561+
)
547562
else:
548-
filename = settings.foldername + "/<file name not specified>"
549-
550-
if (settings.columnfolder != ""):
551-
columnfile = settings.columnfolder + "/" + settings.columnfile
552-
553-
columns.get_columns()
554-
valuedict.reset_indexes()
555-
556-
print("Dummy data generator " + version + "\nAndrew H 2020\n" +
557-
("\nCurrent preset file: " + settings.presetfolder + "/" + settings.presetfile + "\nThe selected preset file modifies the following settings" + presetmodstring if settings.presetfile != "" else "") +
558-
"\nCurrent file name: " + filename +
559-
("\nCurrent column file: " + columnfile if settings.fileformat not in settings.imageformats else "") +
560-
("\nImage resolution: " + settings.imagewidth + " x " + settings.imageheight if settings.fileformat in settings.imageformats else "") +
561-
(", grid borders enabled (image resolution may change)" if settings.imagemode == "grid" and settings.gridborders == "y" else "") +
562-
("\nImage generation mode: " + settings.imagemode if settings.fileformat in settings.imageformats else "") +
563-
("\n - Red: (" + settings.rmin + "," + settings.rmax + "), Green: (" + settings.gmin + "," + settings.gmax + "), Blue: (" + settings.bmin + "," + settings.bmax + ")" if settings.fileformat in settings.imageformats and settings.imagemode == "random" else "" ) +
564-
("\n - Red: " + settings.rmax + ", Green: " + settings.gmax + ", Blue: " + settings.bmax + "" if settings.fileformat in settings.imageformats and settings.imagemode == "single" else "" ) +
565-
("\n - Red: (" + settings.rmin + "," + settings.rmax + "), Green: (" + settings.gmin + "," + settings.gmax + "), Blue: (" + settings.bmin + "," + settings.bmax + ")\n - Row height: " + settings.rowheight if settings.fileformat in settings.imageformats and settings.imagemode == "row" else "" ) +
566-
("\nLogging enabled\n" if settings.log == "y" else "\n") + notification)
563+
filename = "{0}{1}<filename not specified>".format(settings.get_file_path(), os.path.sep)
564+
565+
version = "v0.9.0-{0}".format(str(subprocess.check_output(["git", "rev-parse", "HEAD"]).decode('ascii').strip())[:7])
566+
print("Dummy data generator {0}\nAndrew H 2020\n".format(version) +
567+
("\nCurrent preset file: {0}\nThe selected preset file modifies the following settings: {1}".format(presetfile, presetmodstring) if settings.presetfile != "" else "") +
568+
"\nCurrent file name: {0}".format(filename) +
569+
("\nCurrent column file: {0}".format(columnfile) if settings.fileformat not in settings.imageformats else "") +
570+
("\nCSV File encoding: {0}".format(settings.encodingtype) if settings.encoding else "") +
571+
("\nImage resolution: {0} x {1}".format(settings.imagewidth, settings.imageheight) if settings.fileformat in settings.imageformats else "") +
572+
(", grid borders enabled (image resolution may change)" if settings.imagemode == "grid" and settings.gridborders else "") +
573+
("\nImage generation mode: {0}".format(settings.imagemode) if settings.fileformat in settings.imageformats else "") +
574+
("\n - Red: ({0},{1}), Green: ({2},{3}), Blue: ({4},{5})".format(settings.rmin, settings.rmax, settings.gmin, settings.gmax, settings.bmin, settings.bmax) if settings.fileformat in settings.imageformats and settings.imagemode == "random" else "" ) +
575+
("\n - Red: {0}, Green: {1}, Blue: {2}".format(settings.rmax, settings.gmax, settings.bmax) if settings.fileformat in settings.imageformats and settings.imagemode == "single" else "") +
576+
("\n - Red: ({0},{1}), Green: ({2},{3}), Blue: ({4},{5})\n - Row Height: {6}".format(settings.rmin, settings.rmax, settings.gmin, settings.gmax, settings.bmin, settings.bmax, settings.rowheight) if settings.fileformat in settings.imageformats and settings.imagemode == "row" else "" ) +
577+
("\nLogging enabled" if settings.log else "\n") + notification)
567578

568579
print("1. Generate file")
569580
if settings.fileformat not in settings.imageformats:
@@ -599,7 +610,5 @@ def menu(notification = ""): # main menu, first thing the user will see
599610
clear()
600611
print("Loading...")
601612

602-
version = "v0.9.0-" + str(subprocess.check_output(["git", "rev-parse", "HEAD"]).decode('ascii').strip())[:7]
603-
604613
settings.update_settings_file()
605614
menu()

0 commit comments

Comments
 (0)