pyDiffTools 0.1.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,39 @@
1
+ def comment_definition(commandname, name, comment_text):
2
+ return r"\newcommand{\%s}{%s}" % (commandname, comment_text) + "\n"
3
+
4
+
5
+ def generate_alphabetnumber(x):
6
+ if x < 26:
7
+ return chr(ord("a") + x)
8
+ else:
9
+ higher_places = x // 26
10
+ return generate_alphabetnumber(
11
+ higher_places - 1
12
+ ) + generate_alphabetnumber(x - higher_places * 26)
13
+
14
+
15
+ def matchingbrackets(content, startpoint, bracket_type):
16
+ if bracket_type == "(":
17
+ opening = "("
18
+ closing = ")"
19
+ elif bracket_type == "[":
20
+ opening = "["
21
+ closing = "]"
22
+ elif bracket_type == "{":
23
+ opening = "{"
24
+ closing = "}"
25
+ else:
26
+ raise ValueError("I didn't understand the type of bracket!")
27
+ first = (
28
+ False # of course, don't want to break until we've found at least one
29
+ )
30
+ level = 0
31
+ for j in range(startpoint, len(content)):
32
+ if content[j] == opening:
33
+ if level == 0:
34
+ first = j
35
+ level += 1
36
+ if content[j] == closing:
37
+ level -= 1
38
+ if level == 0 and first:
39
+ return first, j
@@ -0,0 +1,166 @@
1
+ """
2
+ this requires geckodriver to be installed and available
3
+ """
4
+
5
+ import time
6
+ from selenium import webdriver
7
+ import selenium
8
+ import subprocess, sys, os, psutil, re
9
+ from watchdog.observers import Observer
10
+ from watchdog.events import FileSystemEventHandler
11
+
12
+
13
+ def run_pandoc(filename, html_file):
14
+ if os.path.exists("MathJax-3.1.2"):
15
+ has_local_jax = True
16
+ else:
17
+ has_local_jax = False
18
+ print("you don't have a local copy of mathjax. You could get it with")
19
+ print(
20
+ "wget https://github.com/mathjax/MathJax/archive/refs/tags/3.1.2.zip"
21
+ )
22
+ print("and then unzip")
23
+ current_dir = os.getcwd()
24
+ localfiles = {}
25
+ for k in ["csl", "bib"]:
26
+ localfiles[k] = [
27
+ f for f in os.listdir(current_dir) if f.endswith("." + k)
28
+ ]
29
+ if len(localfiles[k]) == 1:
30
+ localfiles[k] = localfiles[k][0]
31
+ else:
32
+ raise ValueError(
33
+ f"You have more than one (or no) {k} file in this directory!"
34
+ " Get rid of all but one! of "
35
+ + "and".join(localfiles[k])
36
+ )
37
+ command = [
38
+ "pandoc",
39
+ "--bibliography",
40
+ localfiles["bib"],
41
+ f"--csl={localfiles['csl']}",
42
+ "--filter",
43
+ "pandoc-crossref",
44
+ "--citeproc",
45
+ "--mathjax",
46
+ "--number-sections",
47
+ "--toc",
48
+ "-s",
49
+ "-o",
50
+ html_file,
51
+ filename,
52
+ ]
53
+ # command = ['pandoc', '-s', '--mathjax', '-o', html_file, filename]
54
+ print("running:",' '.join(command))
55
+ subprocess.run(
56
+ command,
57
+ )
58
+ print("running:\n", command)
59
+ if has_local_jax:
60
+ # {{{ for slow internet connection, remove remote files
61
+ with open(html_file, encoding="utf-8") as fp:
62
+ text = fp.read()
63
+ patterns = [
64
+ r"<script.{0,20}?cdn\.jsdeli.{0,20}?mathjax.{0,60}?script>",
65
+ r"<script.{0,20}?https...polyfill.{0,60}?script>",
66
+ ]
67
+ for j in patterns:
68
+ text = re.sub(j, "", text, flags=re.DOTALL)
69
+ with open(html_file, "w", encoding="utf-8") as fp:
70
+ fp.write(text)
71
+ # }}}
72
+ return
73
+
74
+
75
+ class Handler(FileSystemEventHandler):
76
+ def __init__(self, filename, observer):
77
+ self.observer = observer
78
+ self.filename = filename
79
+ self.html_file = filename.rsplit(".", 1)[0] + ".html"
80
+ # self.firefox = webbrowser.get('firefox')
81
+ # self.firefox = webdriver.Firefox() # requires geckodriver
82
+ self.init_firefox()
83
+
84
+ def init_firefox(self):
85
+ self.firefox = webdriver.Chrome() # requires chromium
86
+ run_pandoc(self.filename, self.html_file)
87
+ if not os.path.exists(self.html_file):
88
+ print("html doesn't exist")
89
+ self.append_autorefresh()
90
+ # self.firefox.open_new_tab(self.html_file)
91
+ self.firefox.get("file://" + os.path.abspath(self.html_file))
92
+
93
+ def on_modified(self, event):
94
+ # print("modification event")
95
+ if os.path.normpath(
96
+ os.path.abspath(event.src_path)
97
+ ) == os.path.normpath(os.path.abspath(self.filename)):
98
+ # print("about to run pandoc")
99
+ run_pandoc(self.filename, self.html_file)
100
+ self.append_autorefresh()
101
+ try:
102
+ self.firefox.refresh()
103
+ except selenium.common.exceptions.WebDriverException:
104
+ print(
105
+ "I'm quitting!! You probably suspended the computer, which"
106
+ " seems to freak selenium out. Just restart"
107
+ )
108
+ self.firefox.quit()
109
+ self.init_firefox()
110
+ print("and refreshed!")
111
+ else:
112
+ # print("saw a change in",os.path.normpath(os.path.abspath(event.src_path)))
113
+ # print("not",os.path.normpath(os.path.abspath(self.filename)))
114
+ pass
115
+
116
+ def append_autorefresh(self):
117
+ # print("about to add scripts")
118
+ with open(self.html_file, "r", encoding="utf-8") as fp:
119
+ all_data = fp.read()
120
+ all_data = all_data.replace(
121
+ "</head>",
122
+ """
123
+ <script id="MathJax-script" async src="MathJax-3.1.2/es5/tex-mml-chtml.js"></script>
124
+ <script>
125
+ // When the page is about to be unloaded, save the current scroll position
126
+ window.addEventListener('beforeunload', function() {
127
+ sessionStorage.setItem('scrollPosition', window.scrollY);
128
+ });
129
+
130
+ // When the page has loaded, scroll to the previous scroll position
131
+ window.addEventListener('load', function() {
132
+ var scrollPosition = sessionStorage.getItem('scrollPosition');
133
+ if (scrollPosition) {
134
+ window.scrollTo(0, scrollPosition);
135
+ sessionStorage.removeItem('scrollPosition');
136
+ }
137
+ });
138
+ </script>
139
+ </head>
140
+ """,
141
+ )
142
+ with open(self.html_file, "w", encoding="utf-8") as fp:
143
+ fp.write(all_data)
144
+ # print("done adding")
145
+
146
+
147
+ def watch(filename):
148
+ observer = Observer()
149
+ event_handler = Handler(filename, observer)
150
+ observer.schedule(event_handler, path=".", recursive=False)
151
+ observer.start()
152
+
153
+ try:
154
+ while True:
155
+ time.sleep(1)
156
+ except KeyboardInterrupt:
157
+ observer.stop()
158
+
159
+ observer.join()
160
+ # print("returning from watch")
161
+
162
+
163
+ if __name__ == "__main__":
164
+ filename = sys.argv[1]
165
+ watch(filename)
166
+ # Open the HTML file in the default web browser
@@ -0,0 +1,75 @@
1
+ # from https://tex.stackexchange.com/questions/24542/create-list-of-all-external-files-used-by-master-latex-document
2
+ """Copy figures used by document."""
3
+ import os
4
+ import shutil
5
+ from pathlib import Path, PurePosixPath
6
+ import subprocess
7
+ import sys
8
+
9
+
10
+ def copy_image_files(ROOT_TEX, project_name, TARGET_DIR, include_suppinfo):
11
+ all_files = []
12
+ with open(ROOT_TEX + ".tex", "r") as fp:
13
+ alltext = fp.read()
14
+ if r"\RequirePackage{snapshot}" not in alltext:
15
+ raise RuntimeError(
16
+ "You haven't called \\RequirePackage{snapshot} in the root tex file. I can't do my thing without that!"
17
+ )
18
+ with open(ROOT_TEX + ".dep", "r") as f:
19
+ for line in f:
20
+ if "*{file}" in line:
21
+ value = line.split("{")[2].split("}")
22
+ source = value[0]
23
+ _, e = os.path.splitext(source)
24
+ if len(e) == 0 and os.path.exists(source + ".tex"):
25
+ all_files.append(source + ".tex")
26
+ print("found", source + ".tex")
27
+ elif os.path.exists(source):
28
+ all_files.append(source)
29
+ elif "*{package}" in line:
30
+ value = line.split("{")[2].split("}")
31
+ source = value[0]
32
+ _, e = os.path.splitext(source)
33
+ if len(e) == 0 and os.path.exists(source + ".sty"):
34
+ all_files.append(source + ".sty")
35
+ print("found", source + ".sty")
36
+ elif os.path.exists(source):
37
+ all_files.append(source)
38
+ else:
39
+ continue
40
+ os.makedirs(TARGET_DIR, exist_ok=True)
41
+ if include_suppinfo:
42
+ all_files.append("suppinfo.pdf")
43
+ all_files.append("suppinfo.aux")
44
+ for source in all_files:
45
+ d, f = os.path.split(source)
46
+ b, _ = os.path.splitext(source)
47
+ if b == ROOT_TEX:
48
+ f = f.replace(ROOT_TEX, "ms")
49
+ newpath = TARGET_DIR / d / f
50
+ print("copying", source, PurePosixPath(newpath))
51
+ if len(d) > 0:
52
+ print("going to make", newpath.parents[0])
53
+ os.makedirs(newpath.parents[0], exist_ok=True)
54
+ shutil.copy(source, PurePosixPath(newpath))
55
+ shutil.copy(ROOT_TEX + ".tex", os.path.join(TARGET_DIR, "ms.tex"))
56
+
57
+
58
+ if __name__ == "__main__":
59
+ copy_image_files()
60
+ os.chdir(Path.cwd().parent)
61
+ output_filename = f"{project_name}_forarxiv.tgz"
62
+ # create tar process
63
+ tar = subprocess.Popen(
64
+ ["tar", "cf", "-", TARGET_DIR.name], stdout=subprocess.PIPE
65
+ )
66
+ # create gzip process, using tar's stdout as its stdin
67
+ gzip = subprocess.Popen(
68
+ ["gzip", "-9"], stdin=tar.stdout, stdout=subprocess.PIPE
69
+ )
70
+ # close tar's stdout so it doesn't hang around waiting for input
71
+ tar.stdout.close()
72
+ # write gzip's stdout to a file
73
+ with open(output_filename, "wb") as fp:
74
+ shutil.copyfileobj(gzip.stdout, fp)
75
+ gzip.stdout.close()
@@ -0,0 +1,193 @@
1
+ //
2
+ // TortoiseSVN Diff script for Word Doc files
3
+ //
4
+ // Copyright (C) 2004-2008 the TortoiseSVN team
5
+ // This file is distributed under the same license as TortoiseSVN
6
+ //
7
+ // Last commit by:
8
+ // $Author$
9
+ // $Date$
10
+ // $Rev$
11
+ //
12
+ // Authors:
13
+ // Jared Silva, 2008
14
+ // Davide Orlandi and Hans-Emil Skogh, 2005
15
+ //
16
+
17
+ var objArgs,num,sBaseDoc,sNewDoc,sTempDoc,objScript,word,destination;
18
+ // Microsoft Office versions for Microsoft Windows OS
19
+ var vOffice2000 = 9;
20
+ var vOffice2002 = 10;
21
+ var vOffice2003 = 11;
22
+ var vOffice2007 = 12;
23
+ // WdCompareTarget
24
+ var wdCompareTargetSelected = 0;
25
+ var wdCompareTargetCurrent = 1;
26
+ var wdCompareTargetNew = 2;
27
+ // WdViewType
28
+ var wdMasterView = 5;
29
+ var wdNormalView = 1;
30
+ var wdOutlineView = 2;
31
+ // WdSaveOptions
32
+ var wdDoNotSaveChanges = 0;
33
+ var wdPromptToSaveChanges = -2;
34
+ var wdSaveChanges = -1;
35
+
36
+ objArgs = WScript.Arguments;
37
+ num = objArgs.length;
38
+ if (num < 2)
39
+ {
40
+ WScript.Echo("Usage: [CScript | WScript] diff-doc.js base.doc new.doc");
41
+ WScript.Quit(1);
42
+ }
43
+
44
+ sBaseDoc = objArgs(0);
45
+ sNewDoc = objArgs(1);
46
+
47
+ objScript = new ActiveXObject("Scripting.FileSystemObject");
48
+ if ( ! objScript.FileExists(sBaseDoc))
49
+ {
50
+ WScript.Echo("File " + sBaseDoc + " does not exist. Cannot compare the documents.");
51
+ WScript.Quit(1);
52
+ }
53
+ if ( ! objScript.FileExists(sNewDoc))
54
+ {
55
+ WScript.Echo("File " + sNewDoc + " does not exist. Cannot compare the documents.");
56
+ WScript.Quit(1);
57
+ }
58
+
59
+ try
60
+ {
61
+ word = WScript.CreateObject("Word.Application");
62
+ }
63
+ catch(e)
64
+ {
65
+ // before giving up, try with OpenOffice
66
+ try
67
+ {
68
+ var OO;
69
+ OO = WScript.CreateObject("com.sun.star.ServiceManager");
70
+ }
71
+ catch(e)
72
+ {
73
+ WScript.Echo("You must have Microsoft Word or OpenOffice installed to perform this operation.");
74
+ WScript.Quit(1);
75
+ }
76
+ // yes, OO is installed - do the diff with that one instead
77
+ var objFile = objScript.GetFile(sNewDoc);
78
+ if ((objFile.Attributes & 1)==1)
79
+ {
80
+ // reset the readonly attribute
81
+ objFile.Attributes = objFile.Attributes & (~1);
82
+ }
83
+ //Create the DesktopSet
84
+ var objDesktop = OO.createInstance("com.sun.star.frame.Desktop");
85
+ var objUriTranslator = OO.createInstance("com.sun.star.uri.ExternalUriReferenceTranslator");
86
+ //Adjust the paths for OO
87
+ sBaseDoc = sBaseDoc.replace(/\\/g, "/");
88
+ sBaseDoc = sBaseDoc.replace(/:/g, "|");
89
+ sBaseDoc = sBaseDoc.replace(/ /g, "%20");
90
+ sBaseDoc="file:///" + sBaseDoc;
91
+ sBaseDoc=objUriTranslator.translateToInternal(sBaseDoc);
92
+ sNewDoc = sNewDoc.replace(/\\/g, "/");
93
+ sNewDoc = sNewDoc.replace(/:/g, "|");
94
+ sNewDoc = sNewDoc.replace(/ /g, "%20");
95
+ sNewDoc="file:///" + sNewDoc;
96
+ sNewDoc=objUriTranslator.translateToInternal(sNewDoc);
97
+
98
+ //Open the %base document
99
+ var oPropertyValue = new Array();
100
+ oPropertyValue[0] = OO.Bridge_GetStruct("com.sun.star.beans.PropertyValue");
101
+ oPropertyValue[0].Name = "ShowTrackedChanges";
102
+ oPropertyValue[0].Value = true;
103
+ var objDocument=objDesktop.loadComponentFromURL(sNewDoc,"_blank", 0, oPropertyValue);
104
+
105
+ //Set the frame
106
+ var Frame = objDesktop.getCurrentFrame();
107
+
108
+ var dispatcher=OO.CreateInstance("com.sun.star.frame.DispatchHelper");
109
+
110
+ //Execute the comparison
111
+ dispatcher.executeDispatch(Frame, ".uno:ShowTrackedChanges", "", 0, oPropertyValue);
112
+ oPropertyValue[0].Name = "URL";
113
+ oPropertyValue[0].Value = sBaseDoc;
114
+ dispatcher.executeDispatch(Frame, ".uno:CompareDocuments", "", 0, oPropertyValue);
115
+ WScript.Quit(0);
116
+ }
117
+
118
+ if (parseInt(word.Version) >= vOffice2007)
119
+ {
120
+ sTempDoc = sNewDoc;
121
+ sNewDoc = sBaseDoc;
122
+ sBaseDoc = sTempDoc;
123
+ }
124
+
125
+ objScript = null;
126
+
127
+ word.visible = true;
128
+
129
+ // Open the new document
130
+ try
131
+ {
132
+ destination = word.Documents.Open(sNewDoc, true, true);
133
+ }
134
+ catch(e)
135
+ {
136
+ WScript.Echo("Error opening " + sNewDoc);
137
+ // Quit
138
+ WScript.Quit(1);
139
+ }
140
+
141
+ // If the Type property returns either wdOutlineView or wdMasterView and the Count property returns zero, the current document is an outline.
142
+ if (((destination.ActiveWindow.View.Type == wdOutlineView) || (destination.ActiveWindow.View.Type == wdMasterView)) && (destination.Subdocuments.Count == 0))
143
+ {
144
+ // Change the Type property of the current document to normal
145
+ destination.ActiveWindow.View.Type = wdNormalView;
146
+ }
147
+
148
+ // Compare to the base document
149
+ if (parseInt(word.Version) <= vOffice2000)
150
+ {
151
+ // Compare for Office 2000 and earlier
152
+ try
153
+ {
154
+ destination.Compare(sBaseDoc);
155
+ }
156
+ catch(e)
157
+ {
158
+ WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
159
+ // Quit
160
+ WScript.Quit(1);
161
+ }
162
+ }
163
+ else
164
+ {
165
+ // Compare for Office XP (2002) and later
166
+ try
167
+ {
168
+ destination.Compare(sBaseDoc, "Comparison", wdCompareTargetNew, true, true);
169
+ }
170
+ catch(e)
171
+ {
172
+ WScript.Echo("Error comparing " + sBaseDoc + " and " + sNewDoc);
173
+ // Close the first document and quit
174
+ destination.Close(wdDoNotSaveChanges);
175
+ WScript.Quit(1);
176
+ }
177
+ }
178
+
179
+ // Show the comparison result
180
+ if (parseInt(word.Version) < vOffice2007)
181
+ {
182
+ word.ActiveDocument.Windows(1).Visible = 1;
183
+ }
184
+
185
+ // Mark the comparison document as saved to prevent the annoying
186
+ // "Save as" dialog from appearing.
187
+ word.ActiveDocument.Saved = 1;
188
+
189
+ // Close the first document
190
+ if (parseInt(word.Version) >= vOffice2002)
191
+ {
192
+ destination.Close(wdDoNotSaveChanges);
193
+ }
@@ -0,0 +1,107 @@
1
+ from collections import OrderedDict
2
+ from fuzzywuzzy import process
3
+
4
+
5
+ class doc_contents_class(object):
6
+ prefix = {
7
+ "section": "",
8
+ "subsection": "\t",
9
+ "subsubsection": 2 * "\t",
10
+ "paragraph": 3 * "\t",
11
+ }
12
+ inv_prefix = {v: k for k, v in prefix.items()}
13
+
14
+ def __init__(self):
15
+ self.contents = OrderedDict()
16
+ self.contents["header"] = ""
17
+ self.types = {}
18
+ self.types["header"] = "header"
19
+ self._reordering_started = False
20
+ self._aliases = {}
21
+
22
+ def start_sec(self, thistype, thistitle):
23
+ assert thistitle not in self.contents.keys(), (
24
+ "more than one section with the name:\n" + thistitle
25
+ )
26
+ self.contents[thistitle] = ""
27
+ self.types[thistitle] = thistype
28
+ print("added", thistitle)
29
+
30
+ def __setstate__(self, d):
31
+ "set the info from a pickle"
32
+ self.contents = d["contents"]
33
+ self.types = d["types"]
34
+ self._aliases = {} # doesn't exist, but still needed
35
+ self._reordering_started = False
36
+ return
37
+
38
+ def __getstate__(self):
39
+ "return info for a pickle"
40
+ return {
41
+ "contents": self.contents,
42
+ "types": self.types,
43
+ }
44
+
45
+ def __iadd__(self, value):
46
+ self.contents[next(reversed(self.contents))] += value
47
+ return self
48
+
49
+ def __str__(self):
50
+ if len(self._processed_titles) > 0:
51
+ raise ValueError("the following section"
52
+ " titles were not utilized -- this program is"
53
+ " for reordering, not dropping!:\n"+str(self._processed_titles))
54
+ retval = ""
55
+ for j in self.contents.keys():
56
+ if self.types[j] != "header":
57
+ new_name = j
58
+ if j in self._aliases.keys():
59
+ new_name = self._aliases[j]
60
+ retval += f"\\{self.types[j]}{{{new_name}}}"
61
+ retval += f"{self.contents[j]}"
62
+ return retval
63
+
64
+ @property
65
+ def outline(self):
66
+ retval = []
67
+ for j in self.contents.keys():
68
+ if self.types[j] != "header":
69
+ thistitle = (self.prefix[self.types[j]] + "\t").join(j.split("\n"))
70
+ retval.append(self.prefix[self.types[j]] + "*\t" + thistitle)
71
+ self._reordering_started = False
72
+ return "\n".join(retval)
73
+
74
+ def outline_in_order(self, thisline):
75
+ if not self._reordering_started:
76
+ self._processed_titles = [j for j in self.contents.keys()
77
+ if self.types[j] != 'header']
78
+ self._reordering_started = True
79
+ ilevel = 0
80
+ spacelevel = 0
81
+ hitmarker = False
82
+ for j, thischar in enumerate(thisline):
83
+ if not hitmarker:
84
+ if thischar == " ":
85
+ spacelevel += 1
86
+ if spacelevel == 4 or thischar == "\t":
87
+ ilevel += 1
88
+ spacelevel = 0
89
+ elif thischar == "*":
90
+ hitmarker = True
91
+ else:
92
+ assert thischar in [" ", "\t"]
93
+ title = thisline[j + 1 :]
94
+ break
95
+ if not hitmarker:
96
+ raise ValueError("somehow, there wasn't a * marker!")
97
+ if title not in self.contents.keys():
98
+ best_match, match_quality = process.extractOne(title, self.contents.keys())
99
+ yesorno = input(f"didn't find\n\t{title}\nin keys, maybe you want\n\t{best_match}\nsay y or n")
100
+ if yesorno == 'y':
101
+ self._aliases[best_match] = title # will be replaced later
102
+ title = best_match
103
+ else:
104
+ raise ValueError("problem with replacement")
105
+ self.contents.move_to_end(title)
106
+ self._processed_titles.remove(title)
107
+ self.types[title] = self.inv_prefix[ilevel * "\t"]
@@ -0,0 +1,33 @@
1
+ # again rerun
2
+ from lxml import html, etree
3
+ import os
4
+ from pyspecdata.fornotebook import *
5
+ from pyspecdata import *
6
+ import re
7
+
8
+ fp = open(sys.argv[1], "r")
9
+ content = fp.read()
10
+ fp.close()
11
+ doc = html.fromstring(content)
12
+ commentlabel_re = re.compile(r"\[([A-Z]+)([0-9])\]")
13
+ comment_dict = {}
14
+ # for j in doc.xpath('descendant::*[@style="mso-element:comment"]'):
15
+ newlist = []
16
+ thisbody = doc.find("body")
17
+ print("I found the body", lsafen(thisbody))
18
+ commentlist = etree.Element("div", style="mso-element:comment-list")
19
+ for j in doc.xpath('//span[@style="mso-element:comment"]'):
20
+ # for j in doc.xpath('//span[@style="mso-element:comment"]'):
21
+ # print 'found span with style:\n\n',lsafen(html.tostring(j),wrap = 60)
22
+ # if j.attrib['style'] == 'mso-element:comment':
23
+ print("found span with style:\n\n", lsafen(j.attrib, wrap=60))
24
+ newlist.append(j)
25
+ j.drop_tree()
26
+ commentlist.append(j)
27
+ thisbody.append(commentlist)
28
+ # print lsafen(map(html.tostring,newlist),wrap = 60)
29
+ newfile = re.sub(r"(.*)(\.htm.*)", r"\1_htmlcomm\2", sys.argv[1])
30
+ fp = open(newfile, "w")
31
+ content = html.tostring(doc)
32
+ fp.write(content)
33
+ fp.close()