<commit_before>"""XTF viewer (and converter)"""

import os
import csv
import webbrowser
import re
import sys

import numpy
from GUI import Application, ScrollableView, Document, Window, Globals, rgb
from GUI import Image, Frame, Font, Model, Label, Menu, Grid, CheckBox, Button
from GUI import BaseAlert
from GUI.Files import FileType, DirRef
from GUI.FileDialogs import request_old_files, request_new_file
from GUI.Geometry import (pt_in_rect, offset_rect, rects_intersect,
                          rect_sized, rect_height, rect_size)
from GUI.StdColors import black, red, light_grey, white
from GUI.StdFonts import system_font
from GUI.StdMenus import basic_menus, edit_cmds, pref_cmds, print_cmds
from GUI.StdButtons import DefaultButton
from GUI.Numerical import image_from_ndarray
from GUI.BaseAlertFunctions import present_and_destroy
from GUI.Alerts import confirm

import xtf

def log(*args):
    sys.stdout.write(' '.join(args) + '\n')

def app_menu(profiles = None):
    menus = basic_menus(
        exclude = edit_cmds + pref_cmds + print_cmds + 'revert_cmd',
        substitutions = {
            'new_cmd': 'New Project',
            'open_cmd': 'Open Project...',
            'save_cmd': 'Save Project',
            'save_as_cmd': 'Save Project As...'})
    menus.append(Menu('Profile', [('Import XTF files...', 'import_cmd'),
                                  '-',
                                  (profiles or [], 'profiles_cmd')]))
    menus.append(Menu('Tools', [('Export trace headers to CSV (Excel)...',
                                  'export_csv_cmd'),
                                #('Preferences...', 'preferences_cmd')
                               ]))
    return menus

class XTFApp(Application):

    def __init__(self, **kw):
        Application.__init__(self, **kw)
        self.proj_type = FileType(name = "XTF Project", suffix = "project")
        self.file_type = self.proj_type
        self.menus = []

    def open_app(self):
        self.new_cmd()

    def make_document(self, fileref):
        return Project(file_type = self.proj_type)

    def make_window(self, document):
        ProjectWindow(document).show()

    def about_cmd(self):
        present_and_destroy(AboutBox())

def request_old_directory(prompt, default_dir = None):
    # GUI.Win32.BaseFileDialogs._request_old_dir, but with BIF_NEWDIALOGSTYLE

    from win32com.shell import shell as sh
    import win32com.shell.shellcon as sc
    import win32api as api
    import win32gui as gui
    from GUI.BaseFileDialogs import win_fix_prompt

    BIF_NEWDIALOGSTYLE = 0x0040
    win_bif_flags = sc.BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE
    if default_dir:
        def callback(hwnd, msg, lp, data):
            if msg == sc.BFFM_INITIALIZED:
                api.SendMessage(hwnd, sc.BFFM_SETSELECTION, True, default_dir.path)
    else:
        callback = None
    (idl, name, images) = sh.SHBrowseForFolder(None, None,
        win_fix_prompt(prompt), win_bif_flags, callback)
    if idl:
        return DirRef(sh.SHGetPathFromIDList(idl))


XTF_TYPE = FileType(name = 'XTF file', suffix = 'xtf')
SEGY_TYPE = FileType(name = 'SEG-Y file', suffix = 'seg')

class ProjectWindow(Window):
    def __init__(self, document):
        self.current_file = None
        self.xtf_dir = None
        self.segy_dir = None
        Window.__init__(self, size = (500, 400), document = document)
        self.project_changed(document)

    def close_cmd(self):
        Window.close_cmd(self)
        app = Globals._application
        if not app.windows:
            # force close: the remaining console window stops app from quiting
            app._quit()

    def setup_menus(self, m):
        Window.setup_menus(self, m)
        m.about_cmd.enabled = True
        m.import_cmd.enabled = True
        if self.current_file is not None:
            m.export_csv_cmd.enabled = True
        m.profiles_cmd.enabled = True
        m.profiles_cmd.checked = False
        if self.current_file is not None:
            m.profiles_cmd[self.current_file].checked = True

    def import_cmd(self):
        refs = request_old_files('Import XTF files', file_types = [XTF_TYPE])
        if refs is not None:
            self.document.add_files([os.path.join(r.dir.path, r.name)
                                     for r in refs])

    def profiles_cmd(self, i):
        self.current_file = i
        self.project_changed(self.document)

    def export_csv_cmd(self):
        self.xtf_file.export_csv()

    def xtf_cmd(self):
        ref = request_new_file('Save XTF file', file_type = XTF_TYPE)
        if ref is not None:
            numbers = [i for i, cb in enumerate(self.checkboxes) if cb.value]
            filename = os.path.join(ref.dir.path, ref.name)
            xtf.export_XTF(self.xtf_file.filename, filename, numbers)

    def segy_cmd(self):
        ref = request_new_file('Save SEG-Y file', file_type = SEGY_TYPE)
        if ref is not None:
            numbers = [i for i, cb in enumerate(self.checkboxes) if cb.value]
            filename = os.path.join(ref.dir.path, ref.name)
            xtf.export_SEGY(self.xtf_file.filename, filename, numbers)

    def xtf_all_cmd(self):
        ref = request_old_directory('Save XTF files to folder',
                                    self.xtf_dir or self.document.file.dir)
        if self.batch_export(ref, xtf.export_XTF, '.xtf'):
            self.xtf_dir = ref # remember selected dir for next time

    def segy_all_cmd(self):
        ref = request_old_directory('Save SEG-Y files to folder',
                                    self.segy_dir or self.document.file.dir)<commit_msg>don't fail when exporting from untitled project<commit_after>"""XTF viewer (and converter)"""

import os
import csv
import webbrowser
import re
import sys

import numpy
from GUI import Application, ScrollableView, Document, Window, Globals, rgb
from GUI import Image, Frame, Font, Model, Label, Menu, Grid, CheckBox, Button
from GUI import BaseAlert
from GUI.Files import FileType, DirRef
from GUI.FileDialogs import request_old_files, request_new_file
from GUI.Geometry import (pt_in_rect, offset_rect, rects_intersect,
                          rect_sized, rect_height, rect_size)
from GUI.StdColors import black, red, light_grey, white
from GUI.StdFonts import system_font
from GUI.StdMenus import basic_menus, edit_cmds, pref_cmds, print_cmds
from GUI.StdButtons import DefaultButton
from GUI.Numerical import image_from_ndarray
from GUI.BaseAlertFunctions import present_and_destroy
from GUI.Alerts import confirm

import xtf

def log(*args):
    sys.stdout.write(' '.join(args) + '\n')

def app_menu(profiles = None):
    menus = basic_menus(
        exclude = edit_cmds + pref_cmds + print_cmds + 'revert_cmd',
        substitutions = {
            'new_cmd': 'New Project',
            'open_cmd': 'Open Project...',
            'save_cmd': 'Save Project',
            'save_as_cmd': 'Save Project As...'})
    menus.append(Menu('Profile', [('Import XTF files...', 'import_cmd'),
                                  '-',
                                  (profiles or [], 'profiles_cmd')]))
    menus.append(Menu('Tools', [('Export trace headers to CSV (Excel)...',
                                  'export_csv_cmd'),
                                #('Preferences...', 'preferences_cmd')
                               ]))
    return menus

class XTFApp(Application):

    def __init__(self, **kw):
        Application.__init__(self, **kw)
        self.proj_type = FileType(name = "XTF Project", suffix = "project")
        self.file_type = self.proj_type
        self.menus = []

    def open_app(self):
        self.new_cmd()

    def make_document(self, fileref):
        return Project(file_type = self.proj_type)

    def make_window(self, document):
        ProjectWindow(document).show()

    def about_cmd(self):
        present_and_destroy(AboutBox())

def request_old_directory(prompt, default_dir = None):
    # GUI.Win32.BaseFileDialogs._request_old_dir, but with BIF_NEWDIALOGSTYLE

    from win32com.shell import shell as sh
    import win32com.shell.shellcon as sc
    import win32api as api
    import win32gui as gui
    from GUI.BaseFileDialogs import win_fix_prompt

    BIF_NEWDIALOGSTYLE = 0x0040
    win_bif_flags = sc.BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE
    if default_dir:
        def callback(hwnd, msg, lp, data):
            if msg == sc.BFFM_INITIALIZED:
                api.SendMessage(hwnd, sc.BFFM_SETSELECTION, True, default_dir.path)
    else:
        callback = None
    (idl, name, images) = sh.SHBrowseForFolder(None, None,
        win_fix_prompt(prompt), win_bif_flags, callback)
    if idl:
        return DirRef(sh.SHGetPathFromIDList(idl))


XTF_TYPE = FileType(name = 'XTF file', suffix = 'xtf')
SEGY_TYPE = FileType(name = 'SEG-Y file', suffix = 'seg')

class ProjectWindow(Window):
    def __init__(self, document):
        self.current_file = None
        self.xtf_dir = None
        self.segy_dir = None
        Window.__init__(self, size = (500, 400), document = document)
        self.project_changed(document)

    def close_cmd(self):
        Window.close_cmd(self)
        app = Globals._application
        if not app.windows:
            # force close: the remaining console window stops app from quiting
            app._quit()

    def setup_menus(self, m):
        Window.setup_menus(self, m)
        m.about_cmd.enabled = True
        m.import_cmd.enabled = True
        if self.current_file is not None:
            m.export_csv_cmd.enabled = True
        m.profiles_cmd.enabled = True
        m.profiles_cmd.checked = False
        if self.current_file is not None:
            m.profiles_cmd[self.current_file].checked = True

    def import_cmd(self):
        refs = request_old_files('Import XTF files', file_types = [XTF_TYPE])
        if refs is not None:
            self.document.add_files([os.path.join(r.dir.path, r.name)
                                     for r in refs])

    def profiles_cmd(self, i):
        self.current_file = i
        self.project_changed(self.document)

    def export_csv_cmd(self):
        self.xtf_file.export_csv()

    def xtf_cmd(self):
        ref = request_new_file('Save XTF file', file_type = XTF_TYPE)
        if ref is not None:
            numbers = [i for i, cb in enumerate(self.checkboxes) if cb.value]
            filename = os.path.join(ref.dir.path, ref.name)
            xtf.export_XTF(self.xtf_file.filename, filename, numbers)

    def segy_cmd(self):
        ref = request_new_file('Save SEG-Y file', file_type = SEGY_TYPE)
        if ref is not None:
            numbers = [i for i, cb in enumerate(self.checkboxes) if cb.value]
            filename = os.path.join(ref.dir.path, ref.name)
            xtf.export_SEGY(self.xtf_file.filename, filename, numbers)

    def xtf_all_cmd(self):
        default_dir = self.document.file.dir if self.document.file else None
        ref = request_old_directory('Save XTF files to folder',
                                    self.xtf_dir or default_dir)
        if self.batch_export(ref, xtf.export_XTF, '.xtf'):
            self.xtf_dir = ref # remember selected dir for next time

    def segy_all_cmd(self):
        default_dir = self.document.file.dir if self.document.file else None
        ref = request_old_directory('Save SEG-Y files to folder',
                                    self.segy_dir or default_dir)
{'fraction_non_alphanumeric': 0.09695218076720967, 'fraction_numerical': 0.0036784025223331584, 'mean_word_length': 2.4739884393063583, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3887325', 'n_tokens_mistral': 3667, 'n_tokens_neox': 3504, 'n_words': 887}